Different set sources for flavors

I have few flavors defined and would like to take additional source for one of it:

sources {
  c {
    source {
      srcDir "src"
      include "foo.c"
      if (flavor == flavors.my ) { 
         include "foo2.c" 
      }
    }  
  }
}

This is no allowed since I get complaining “> No such property: flavor for class: org.gradle.api.internal.file.DefaultSourceDirectorySet” Where should I put my flavor’s condition?

I’ve changed it by adding binaries.all, so I don’t have mentioned error. However with that approach foo2.c is taken to all flavors, which is not expected.

Of course I can use targetFlavors, but that needs do define new components per condition. Is there a simpler way to have different sources per flavor.

I found it, but unfortunately there is a bug in the implementation. It perfectly works with the following approach:

binaries.all {
  if (condition) {
    sources {
      C(CSourceSet) {
         source.srcDir "foo"
      }
   }
 }
}

but as soon as “C(CSourceSet)” is changed to “c”, than it stops to work.
I don’t really know how does it work in the gradle’s community. Should I raise a bug?

When you apply the ‘c’ plugin, the plugin automatically creates a CSourceSet with the name “c”. You’re creating a source set with the name “C”.

There’s a sample included with Gradle that does what you’re looking for, I think:

This creates a different source set for each platform that all depend on the plugin created source set.

Thanks, your code works. In your example different CSourceSet are taken for different conditions platformWindows(CSourceSet) and platformLinux(CSourceSet). It also works where sourceSet names are not unique as in my example (if condition is true or false C(CSourceSet) is taken).
So I still don’t understand why default sources.c doesn’t work. Behavior when using sources.name(CSourceSet) or sources.c should be the same, but it isn’t.