How do you add a custom compiler with cpp-library and cpp-application plugins?

How do you add a custom compiler with cpp-library and cpp-application plugins?

With the old native plugins a build script could define gcc(Gcc) in the toolChains; however, we’re using the new cpp-library and cpp-application plugins and yet we are really struggling to switch between g++, clang and GNU style cross-compilers.

What is the recommended flow to change the name of the compiler e.g. from g++ to something like g++-aarch64-linux-gnu?

Also, I’ve prepended my PATH with a path to clang and yet Gradle doesn’t seem to pick that up, so how can we specify the location (directory) of the custom compiler?

Totally not an answer to your actual question which I cannot answer as I’m not doing native development.
But I heard very good things about the Nokee plugin suite for building native software with Gradle.
Maybe you want to have a look at https://nokee.dev/ :slight_smile:

Thanks @Vampire. I see that @Daniel_L is the author of this plugin as well as an avid contributor to these forums.

@Daniel_L do you know by chance if what I’m trying to do here is supported by the new Gradle plugins?

Hey Nik, I may be answering this question out of order given our other discussion on Slack/email but I don’t recall answering that specific question. The toolchain selection is a bit tricky in Gradle. It still uses the old native plugins to do so with a twist for the new cpp-library/cpp-application plugins. The twist is mostly gear toward removing support for allowing an escape hatch for cross-compilers configuration. Nokee unofficially patched the issue but is met with the “source compatibility” feature issue baked in by the introduction of Swift compilation. The goal is to roll out a better toolchain selection and part way with the old native plugins. Until then you will have to software model to the best of what it offers to achieve this configuration.

Gradle selects the first tool it finds on the PATH for the compiler executable. Prepending the PATH environment should work but there is a chance it may be picking up GCC instead of Clang by default. You can quickly see which one is configured using gradlew model. As for g++ vs g++-aarch64-linux-gnu, you will need to customize the tool executable to include the suffix.

This documentation will help you but there may be some gotchas: Building native software Don’t hesitate to ping me on Slack if you have specific question or just want to validate the build logic itself.

Hey @Daniel_L, Yeah thank you for checking in on this, and you are correct - this is a different question than we’ve touched base on, regardless I did make this work and the trick was to use:

model {
    toolChains {
        clang(Clang) {
            path '/custom/path/to/clang/bin/'
        }
    }
}

As you correctly identified here, Gradle was still finding a g++ compiler in my PATH and even though clang came first (it was prepended to PATH) it did not seem sufficient to get Gradle to select clang over g++.

The use of this ./gradlew model command will be useful as we move forward :wink:

Great to hear! A little note on ./gradlew model, it only works for configuration inside the software model land which is pretty much only the toolchain configuration if you are using the newer plugins. I’m hoping to introduce a similar functionality for all Nokee configurations as it’s so useful for tracking down issues like this.