Native Support for Fortran and C/C++ Mixed


(Michael Raupp) #1

I need to be able to compile Fortran and C/C++ code and link the object files together. I also need to be able to compile on both Windows and Linux and detect which compilers are installed: VisualFortran, msbuild.exe, gcc, gfortran and ifort (if available). In Gradle it seems like I have a couple of options:

  1. Write two tasks that detect what toolchains are installed and then execute the compilers from command line. With this approach I am going to miss a lot of the builtin features Gradle offers, so I figure this is the last resort.

  2. Write a ‘FortranCMixedPlugin’ plugin using the base class CppPlugin found in the Gradle source. With this method I would define a Fortran ‘fcompiler’, ‘flinker’, and ‘fsources’ that are separate from the already defined ‘c’ and ‘cpp’ toolchains. With this approach, I am not sure where to begin, I realize I might have to define a new ‘FortranToolChain’ so I was hoping I could get some feedback from one of the experienced Native developers to the feasibility of this approach.

  3. I have been able to build a small section of code on Linux with the following method. I have been replacing the cpp compiler with the gfortran compiler, but it doesn’t feel very clean. Also, I need to be able to detect what Fortran toolchains are installed on the system, are there any builtins for doing that?

    apply plugin: "cpp"
    apply plugin: “c”

    model {

    toolChains {
    gcc(Gcc) {
    eachPlatform { tools ->
    tools.cppCompiler.executable = "gfortran"
    tools.cCompiler.executable = "gcc"
    tools.linker.executable = “gfortran”

    buildTypes {

    platforms {
    x86 {
    architecture “x86”
    x64 {
    architecture “x86_64”

    components {
    main(NativeExecutableSpec) {
    binaries.all {
    if (toolChain in Gcc) {
    cppCompiler.define "GFORTRAN"
    cppCompiler.args “-x”, “none”, “-cpp”, “-ffixed-line-length-132”, “-fno-backslash”, “-falign-commons”, “-fno-automatic”, “-funderscoring”, "-fno-second-underscore"
    linker.args “-Xlinker”, “-S”
    sources {
    cpp {
    source {
    srcDirs "src/formatter"
    include “/*.f"
    exportedHeaders {
    srcDir "src/formatter/incls"
    include "
    c {
    source {
    srcDir "src/formatter"
    include "**/

Thanks for any help you can give, I really appreciate you taking the time to look at my post.
Cheers, Mike