Add new sourceset via plugin

I would like to add a new sourceset to a binary via a Groovy plugin. It appears that the source property is read only so I am unsure how to go about this. I can add a new srcDir to the existing ‘cpp’ sourceset but that isn’t variant aware so for each new variant the sourceset includes the previous variants srcDir.

So for example this doesn’t work:

(ModelMap<BinarySpec> binaries) { binary ->
  binary.inputs.withType(CppSourceSet) {
    it.source.srcDir(new File('...'))
  }
}

Which ends up producing something like:

'foo:vanilla:sharedLibrary'.cpp
  directory 'fooVanillaSharedLibrary'

'foo:chocolate:sharedLibrary'.cpp
  directory 'fooVanillaSharedLibrary'
  directory 'fooChocolateSharedLibrary'

Taking a step back, what I am trying to do is dynamically add a task that generates some sources and then include those sources when compiling the binary. I have the task working, building the sources, being added as a dependency to the compile task etc, I just can’t find a way of adding the generated sources without running into issues with variants.