I have a bunch of C++ libraries that I want to build using gradle. They’re all plugins for Gazebo and all have the same exact link/compile setup. Previous, in CMake, I had a list that I iterated over to build each plugin. What is the gradle-way to do this?
I’ve tried this, but the syntax is wrong. I have no clue how groovy works, and even less about how Gradle works. I’m just doing this based on examples and intution.
Anyone know a clean solution to this?
I think you’re almost there. Try this:
"$it"(NativeLibrarySpec)
1 Like
Ok, it works. But what’s going on here?
I’m only guessing here, but I think model
and components
are closures. is `“gz_$it”(NativeLibrarySpec){} also some kind of closure, or is it more like a normal java function? How come I can give it a string as it’s name? Is NativeLibrarySpec a gradle defined constant, or what?
I think questions like these are why I don’t get gradle.
//iterate over each plugin
model {
components {
plugins.each {
"gz_$it"(NativeLibrarySpec){
sources {
Essentially what is happening here is when you do something like lib1(NativeLibrarySpec)
you are creating a new NativeLibrarySpec
with the name ‘lib1’. Syntactically this is actually a method call. What you really want is for the name to be the value of the current item you are iterating on, rather than the literal it
. What was happening before is you were trying to create multiple components with the name ‘it’.
So if I have a list of plugins [‘clock’, ‘motor’, ‘encoder’], then I am calling a methods:
gz_clock(NativeLibrarySpec)
gz_motor(NativeLibrarySpec)
gz_encoder(NativeLibrarySpec)
But where are these functions defined?
When you say I’m "creating a new NativeLibrarySpec " you don’t mean like an object of type NativeLibrarySpec? Isn’t NativeLibrarySpec a parameter to the methods I am creating?
They aren’t. We interpret this at runtime as “create a new thing of type NativeLibrarySpec
called 'gz_clock`”.
If you’re curious about the implementation details, we use Groovy’s method missing feature to make this work.
I mean, I could ask why you don’t just do new NativeLibrarySpec
and pass it the gz_clock
and sources
closure or whatever, but I have no idea what I’m talking about.
Thanks for answering my questions! I will definitely be posting more if I stick with this.
Because it’s important that Gradle controls instantiation of these objects. In fact, NativeLibrarySpec
is an interface, whose implementation is internal, or possibly, generated at runtime.
1 Like