I may be mistaken, but from my current understanding of how Gradle works with source sets, you canât change a source set for different targets (platforms, build types, flavors, etc.). AFAIK, this is because a source set is just a collection of files that is then shared between the different output binaries. Binaries are generated for all the different platform, build type and flavor combinations.
There are some ways to work around this. The easiest is probably to have compile-time defines to indicate the target platform. The build.gradle file then contains something like:
components {
Foundation(NativeLibrarySpec) {
sources {
cpp {
source {
srcDir 'src'
include '**/*.cpp'
}
}
}
binaries.all {
if (Os.isFamily(Os.FAMILY_WINDOWS))
cppCompiler.define '__OS_WINDOWS__' // Or any other custom definition
}
}
}
This is what weâre using now (see here), but IMHO there should be another more elegant way.
Components do have target variations, so an option would be to create platform specific components. This should work normally, but thereâs the burden that youâre manually taking care of the component variants, which might not be an option (especially for native libraries).
Maybe thereâs a simpler way, but this is what Iâve found so far.
This way you can have a sourceset that contains your common code, and sourcesets that contain variant specific code depending on target platforms, toolchains, build types, flavors etcâŚ
This is very interesting. So binaries have their own source sets? This makes things easier
Might be interesting to add this information to the user guide (maybe just a sentence?).
While your proposal is quite interesting, it does not solve my issue because I cannot change the organization of the Cpp src directory. All sources, be for Linux or Windows or WindowsCE are all together in this unique src directory.
So from my perspective, a solution with a conditional include/exclude is more adapted.
@zosrothko I just tried your example with a conditional and it works for me with simple cpp sources. What exactly is the error you get?
With
project(â:Foundationâ) {
model {
components {
PocoFoundation(NativeLibrarySpec) {
sources {
mc {
source {
srcDir âsrcâ
include â/*.mcâ
}
}
rc {
source {
srcDir âsrcâ
include '/.rcâ
}
}
c {
source {
srcDir âsrcâ
include '**/.câ
}
exportedHeaders {
srcDir âincludeâ
}
}
cpp {
source {
srcDir âsrcâ
include â**/.cppâ
exclude 'Environment_.cppâ
exclude âFPEnvironment_.cppâ
exclude 'Timezone_.cppâ
exclude âDirectoryIterator_.cppâ
exclude 'File_.cppâ
exclude âFileStream_.cppâ
exclude 'Path_.cppâ
exclude âLogFile_.cppâ
exclude 'NamedEvent_.cppâ
exclude âNamedMutex_.cppâ
exclude 'PipeImpl_.cppâ
exclude âProcess_.cppâ
exclude 'SharedMemory_.cppâ
exclude âSharedLibrary_.cppâ
exclude 'Event_.cppâ
exclude âMutex_.cppâ
exclude 'RWLock_.cppâ
exclude âSemaphore_.cppâ
exclude 'Thread_.cppâ
if (toolChain in VisualCpp) {
exclude âSyslogChannel.cppâ
exclude âOpcomChannel.cppâ
exclude âAndroidLogChannel.cppâ
}
}
exportedHeaders {
srcDir âincludeâ
}
}
}
}
}
binaries {
all {
if (toolChain in VisualCpp) {
linker.args âws2_32.libâ
linker.args âiphlpapi.libâ
}
}
withType(SharedLibraryBinarySpec) {
if (toolChain in VisualCpp) {
cCompiler.define âFoundation_EXPORTSâ
cppCompiler.define âFoundation_EXPORTSâ
}
}
}
}
I got
What went wrong:
A problem occurred configuring project â:Foundationâ.
Exception thrown while executing model rule: model.components > create(PocoFoundation) > named(cpp)
No such property: toolChain for class: org.gradle.api.internal.file.DefaultSourceDirectorySet