Passing compiler arguments to GCC

How can I pass arguments to GCC when it is compiling. My old automake is passing the compiler argument -fPIC.

I have searched everywhere and none of the suggestions have worked.

toolChains {
        gcc(Gcc) {
            linker.withArguments { List<String> args ->
                args << "-fPIC"
            }
        }
    }

Exception thrown while executing model rule: model.toolChains

Could not find property ‘linker’ on Tool chain ‘gcc’ (GNU GCC).

libraries {
    myLib {}
    all {
        binaries.all {
            cppCompiler.args "-fPIC"
            linker.args "-fPIC"
        }
    }
}

Build works fine, but I cannot see any gcc command being fired with the arguments.

toolChains {
        gcc(Gcc) {}
        target("x64") {
            cppCompiler.withArguments { args ->
                args << "-fPIC"
            }
        }
    }

Exception thrown while executing model rule: model.toolChains

Could not find method target() for arguments [x64, build_1rpm5hj0c7lgodlywh65wn8if$_run_closure1_closure4_closure7@38ed139b] on NativeToolChain container.

toolChains {
        gcc(Gcc) {
            cppCompiler.withArguments { args ->
                args << "-fPIC"
            }
        }
    }

Exception thrown while executing model rule: model.toolChains

Could not find property ‘cppCompiler’ on Tool chain ‘gcc’ (GNU GCC).

toolChains {
        gcc(Gcc) {
            cppCompiler.args '-fPIC'
        }
    }

Edit: In an obscure corner of the Internet I found something that works. At least gradle build is successful.

model {
    platforms {
        x64 {
            architecture "x86_64"
            operatingSystem "linux"
        }
    }
    toolChains {
        gcc(Gcc) {
            cppCompiler.withArguments { args ->
                args.add("-fPIC")
            }
        }
    }
}

On a second thougth: It seems args.add("-ARG") works just as args << “-ARG”, but only on Gradle 2.1. Using Gradle 2.2.1 and the latest 2.4-nightly I get the following error: > Exception thrown while executing model rule: model.toolChains

Could not find property ‘cppCompiler’ on Tool chain ‘gcc’ (GNU GCC).

The simplest way is to specify args for your binary:
’’'
binaries.all { 
   cppCompiler.args “-fPIC” 
   linker.args “-fPIC” 

‘’'

The arguments are all passed to GCC via ‘@file’: you can see the arguments file in the ‘build/temp’ directory.

There are two methods mentioned in the Gradle documentation.

Define compiler arguments within binaries.all. Example 55.11. Settings that apply to all binaries

Define compiler arguments within a toolChain. Example 55.28. Reconfigure tool arguments

In what circumstances would one define arguments in the toolChain instead of the binaries? Though it does say: “The arguments hook should be seen as a ‘last-resort’ mechanism”

If you set the compiler arguments on the binaries, it affects all binaries produced by that “scope”. So if you’re at the top-level of the build script, that’s everything. If you’re inside a component, it’s only binaries for that component. You can also select certain binaries by type (exe vs lib) or some other attribute (flavor, buildType, etc). It also works across all tool chains, so if you add an argument unconditionally, it’ll get added to all tool chains (even if that doesn’t make sense). You also can only add arguments to the end (I believe, but that’s a bit of an implementation detail).

If you set the compiler arguments on the tool chain, it affects all binaries produced by that project with that tool chain. It’s “last resort” because we just give out a list of arguments that can be modified in any way (add/remove/change) and trust that you don’t change something that would break our assumptions (up-to-date checks, incremental builds, etc).

In general, using the binaries approach is the more structured way of doing it. The tool chain approach wins when you need to pass something to GCC and you don’t care about the binary being produced (e.g., maybe to select a particular version of GCC, logging level, etc).