Is there a way to extend a source set for a specific binary variant only?

I’m working with a C code base that supports multiple platforms by combining generic and platform specific source files. Consider the following directory structure,

thread thread/src/thread.c thread/src/mac_x86/thread_plt.c thread/src/mac_x86_64/thread_plt.c thread/src/win_x86/thread_plt.c

For the library “thread” I wish to: - always include thread.c - only include mac_x86/thread_plt.c for the Mac x86 binary variant - only include mac_x86_64/thread_plt.c for the Mac x86-64 binary variant - only include win_x86/thread_plt.c for the Windows x86 binary variant

I’ve tried extending the source set for a specific binary variant (using the binary’s target os and platform) from within the “binaries.all” closure, but the source gets added for all binary variants.

I’m working around that by limiting the platforms and architectures defined in the model to one, based on the host (or provided target if cross-compiling); but when you only define a single platform/architecture Gradle smartly writes the objects/binaries to a non-platform-specific directory, so if you then decide to cross-compile for another architecture new objects are not created because they already exist and linking fails due to object format errors.

I’ve gotten around that by defining a “dummy” architecture that never get’s built. While this works the dummy architecture clutters the model, clutters the tasks list, will confuse anyone looking at this build script and such hacks will probably not work with future versions of Gradle (nor should it).

So, is there a way to add a specific source file to a specific binary variant only? Or, is there a way to get Gradle to write the objects/binaries to a platform specific location even if only one platform/architecture is defined in the model?


It’s possible to do this, as demonstrated in the ‘native-binaries/assembler’ sample. The problem is that you need to define a separate sourceSet for each binary in order include binary-specific sources. Simply modifying the existing source set for a binary is modifying a shared source set, so the changes affect all binaries.

Thanks Daz!

I actually figured this out yesterday. I rethought my build system and realized I was trying to force Gradle to do things I’m used to with make. So I went back to basics, simplified to let Gradle do what Gradle does, and indeed have platform specific source sets that I add to the binaries as needed. Works like a charm. I’m just putting finishing touches on it now.

Thanks for replying, much appreciated.