RuleSource -dding native PrebuiltStaticLibrary/PrebuiltSharedLibrary


I have a RuleSource based plugin that extends the C++ plugin.
I want to add PrebuiltStaticLibrariy and PrebuiltSharedLibrary to the set of prebuilt libraries in the model.

model {
repositories {
libs { // The libs is also added through the plugin
// Add prebuilt libs using the plugin here

I found how to add a PrebuiltLibraries to the repositories.
However I can’t find a way to create and add a prebuilt library to libs.


Here is how I use the RuleSource plugin to do exactly that, this is a portion of a much bigger collection of library definitions.

class PreBuiltLibsLinuxRules extends RuleSource
    void populatePreBuiltLibsForLinux(Repositories repositories) {
        PrebuiltLibraries linuxLibs = repositories.create("preBuiltLibsLinux", PrebuiltLibraries.class);

        linuxLibs.create("zlib") {
            headers.srcDir "/usr/include"
            binaries.withType(StaticLibraryBinary) {
                staticLibraryFile = new File("/usr/lib/x86_64-linux-gnu/libz.a")
            binaries.withType(SharedLibraryBinary) {
                sharedLibraryFile = new File("/usr/lib/x86_64-linux-gnu/")

I did this in a groovy source file to take advantage of the Groovy configuration closure syntax but this could just as easily have been pure Java.

All you would have to change to attach to the predefined “libs” repository instead of creating one called “preBuiltLibsLinux” is the first line which would have to use maybeCreate() instead to be most flexible.

I’ll try this tomorrow.

Do you know if a same/similar approach can be used to create build blocks for other types of rule/model containers?

It works great.


Yes, every aspect of the model configuration is accessible and modifiable by Rules as needed. Care should be taken to pick the appropriate rule each time depending on what you are trying to do. This is all explained in the Gradle documentation.