Gradle Native - Execute task before binaries.all definition

I’m generating cpp sources by using a dedicated task generateSourcesAndLibs.

model {
components {
all {
   sources {
      cpp    {
        source {
          srcDir "./"
          include "**/*.cpp"                        
Foo(NativeLibrarySpec) {
   sources {cpp{source{generatedBy tasks.generateSourcesAndLibs}}}            

This task not only creates cpp files, but also a static library libstat.a only if files with given extension are found (this is the workaround I’m trying to use to solve the problem explained in this topic Gradle native pass to linker precompiled objects).

What I want to do is to link libstat.a only if it exists. How can I accomplish this?

Please note: I tried using a global variable:

project.ext.set("staticLibraryExists", false) //Initialization in build.gradle

setting it to true inside the task generateSourcesAndLibs. Unfortunately this task is called after the binaries compiler definition, so the following code didn’t work:

binaries.all {       
  if(project.staticLibraryExists) {
    linker.args "-lstat"