Toolchains for multi-project native build

This seems like a silly question, but I couldn’t find a definitive answer anywhere.

I have a multi-project native build, and currently I define the platforms/toolchains in a “subprojects” block in the root build.gradle. However, I’ve heard the general guidance to avoid doing too much in the subprojects block. Is there a good way to declare the toolchains only once?

For clarity, my root build.gradle has something like this:

subprojects {
    apply plugin: 'c'
    apply plugin: 'cpp'

    model {
        platforms {
            // multiple platforms here
        toolchains {
            // multiple toolchains here

We have 4 different platforms/toolchains, and they’re not trivial…there are a handful of method calls and string substitutions in each toolchain to build the proper paths for the tools. Am I causing overhead by doing all of this for each subproject?

I’m not sure I’d agree that this is bad practice. Certainly better than duplicating it. The other option is to refactor that logic into a plugin, perhaps a script plugin (toolchains.gradle) and then apply it to your subprojects.

Thanks for the reply - I figured this was probably the general way to do it. But I know that subprojects inherit properties from their parents, so I wasn’t sure if there was a similar way to inherit toolchains.

No. In this case the toolchains are part of that projects “model”. Every project has it’s own distinct “model space”. We will likely introduce a new construct for sharing model elements across projects.

Makes sense. Thanks!