External dependencies in cpp projects

Hi everybody,

I am new to Gradle and Groovy and I have a question regarding the cpp plugin.

I have a C++ artifact in a Nexus repository which is a zip file including a lib + some header files (lets call this libA). I have now a Gradle native project defined which has now a dependency to libA. I have read through the documentation and it seems that dependency resolution is not supported yet in Gradle 2.1 for native projects. I have found this post here: http://forums.gradle.org/gradle/topics/right_way_to_copy_contents_from_dependency_archives which talks about downloading a zip package and extracting it.

Unfortunatetly I am struggling with getting this to work with a cpp project. Is there somewhere a full working example that downloads a zip file, unpacks it and is used by a cpp project by defining a dependency (probably via PrebuiltLibraries).

Thanks a lot for your help.

Cheers, Martin

This response to the question you mentioned contains an example of how to download and unzip a package.

Yes, but I am missing there the part how the zip is obtained from a artifacts repo.

Oh, I see. You need to specify the repository: repositories {

maven { url “http://your.repo.com” } }

Hi Marcin,

I have this project:

project(':CfgMgr') {
    repositories {
      maven { url "http://my.repo/nexus/content/repositories/releases" }
    }
      configurations {
        tinyXml
    }
      dependencies {
        tinyXml group: "com.tomtom", name: "tinyXml", version: "1.0", ext: "zip"
    }
      task extractTinyXml(type: Copy) {
        from { // use of closure defers evaluation until execution time
            configurations.tinyXml.collect { zipTree(it) }
        }
        into "$buildDir/tinyXml/"
    }
      sources {
        main {
            cpp {
                lib project: ':SomeLib', library: 'main'
            }
        }
    }
   }

How do I tell the build script that the tinyXml dependency should be triggered?

How is the zip file downloaded and how is the task that extracts the zip file called?

How can I than in the end add a source dependency to the extracted project?

If you can point me to some documentation than I can figure this out by myself, but unfortunately I haven’t found anything for C++.

Thanks a lot. Martin

I have added a dependsOn dependency to extractTinyXml in the afterEvaluate section and added some logs into the extractTinyXml task. I see the logs added in the task but it does not unzip the package.

afterEvaluate {
        mainStaticLibrary.dependsOn extractTinyXml
    }

Any idea?

Thank you. Works now.

Here’s an end-to-end example of doing this with a C executable and a prebuilt library. This should be directly portable to a C++ build (i.e. CppCompile instead of CCompile and cpp source set instead of c). I used a file-based dependency, but this could just as easily be a dependency in a repository somewhere.

apply plugin: 'c'
  // Define a configuration to represent the zipped libraries
configurations {
    cunit
}
  dependencies {
    cunit files("libs/cunit.zip")
}
  // Task to explode the zipped library into a temporary dir
task extractCunitZip(type: Copy) {
    ext.libsdir = "${buildDir}/libs"
    from { configurations.cunit.collect { zipTree(it) } }
    into libsdir
}
  model {
    repositories {
        // Set up a prebuilt library to represent the unzipped archive
        lib(PrebuiltLibraries) {
            cunit {
                headers.srcDir "${extractCunitZip.libsdir}/cunit/2.1-2/include"
                binaries.withType(StaticLibraryBinary) {
                    staticLibraryFile = file("${extractCunitZip.libsdir}/cunit/2.1-2/lib/osx/libcunit.a")
                }
            }
        }
    }
}
  executables {
    cuniter {
        binaries.all {
            // Inject the compile task dependency on the extraction task
            tasks.withType(CCompile) {
                dependsOn extractCunitZip
            }
        }
    }
}
  sources {
    cuniter {
        c {
            // Link to the prebuilt library
            lib library: 'cunit', linkage: 'static'
        }
    }
}
2 Likes