Dependencies and the software model

I am trying to use gradle to both build my c-source and to do some pre and post build tasks (run a few scripts).

How can I use dependOn with the NativeExecutableSpec tasks of my software model?

More conrete, let’s assume I have the gradle.build file as in Example 72.32 in Native Software with an additional task “preBuild” defined outside the model closure. If I write mainExecutable.dependsOn preBuild, I get an error:
Could not find property ‘mainExecutable’ on root project

Is it possible to use dependencies on these tasks or is there some other way to achieve the same thing?

I think you want to use a task dependency something like this

mainExecutable {
  dependsOn 'preBuild'
}

In this case you are configuring the mainExecutable task to be dependent on the preBuild task.

If I do this I seem to hide the task mainExecutable that is created by the model-dsl (runing gradle mainExecutable does not build my source code anymore)

I could reproduce your issue. I set up a minimal example and got it to work like this.

plugins {
  id 'cpp'
}

model {
    components {
        main(NativeExecutableSpec) {
        }
    }
}

task preBuild (type: DefaultTask) {
  doLast { println "Do this first" }
}

model {
  tasks.mainExecutable {
  dependsOn 'preBuild'
  }
}

Aha, will try this.
I ended up having a compile.gradle file containing the sw-model and another build.gradle file. In the build.gradle I have a task of BuildType that runs the mainExecutable task in the compile.gradle file. It works, but I prefer your way.

Is there a specific reason you split this into two model blocks?

No, but I do find I have a tendency to do my model blocks in smaller chunks. I find it more clear than scrolling up through a long model block to see the structure. Just a preference. Is there a reason not to? Would there be any significant performance penalty during parsing?

For example I do things like

model {
  components {
     mylib {}
  }
  testSuites {
    test_mylib {}
  }
}

model {
  components {
     mylib2 {}
  }
  testSuites {
     test_mylib2 {}
  }
}

Thanks

I just did not want @magnus to get the impression that he has to split them. There is no downside to splitting your logic if you prefer that :slight_smile: