Help on using the new " cpp-library " plugin

Hi, we have a project that uses the native software model to compile cpp components and now I want to use the new ” cpp-library ” plugin .
Can someone help me with that?. I have tried to change few things but the userguide seems to be missing few things I need.
Thanks in advance :slightly_smiling_face:

The file is as below ::

description = 'Loader'
apply plugin: 'google-test-test-suite'

// compile cpp
model {
    binaries {
        all {
            if (toolChain in Gcc)
            {
                // setting for debug and code coverage
                if ( project.hasProperty("debug") && project.properties['debug']){
                    cppCompiler.args "-O0", "-g3", "-fexceptions", "-fpermissive", "-Wno-write-strings", "-coverage"
                    cppCompiler.args "-fPIC", "-m64"
                    linker.args "-coverage"
                }

                // setting for optimized build
                cppCompiler.args "-O3", "-fexceptions", "-fpermissive", "-Wno-write-strings"
                cppCompiler.args "-fPIC", "-m64"
            }
        }
        withType(GoogleTestTestSuiteBinarySpec) {
           cppCompiler.define "QGC_UNIT_TEST"

           lib library: "gtest", linkage: "static"
           lib project: ":qgi", library: "tdqgi", linkage: "static"

           cppCompiler.args '-pthread', '-lrt', '-coverage'
           linker.args '-pthread', '-ldl', '-lrt', '-coverage'
        }
    }

    components {
        tdqgc(NativeLibrarySpec) {
            version computeVersion(connectorsVersion)
            sources {
                cpp {
                    source {
                        srcDirs "src/main/cpp/"
                        include "*.cpp", "*.cc"
                    }
                    exportedHeaders {
                        srcDirs "src/main/cpp",
                        "${project(':qgi').projectDir}/src/main/cpp/include/"
                        include "*.h", "*.hpp"
                    }
                    lib library: 'jsoncpp', linkage: 'static'
                    lib project: ':qgi', library: 'uds', linkage: 'static'
                    lib project: ':qgi', library: 'shm', linkage: 'static'

                }
            }
        }
    }

    testSuites {
        qgcTest(GoogleTestTestSuiteSpec) {
            sources{
                cpp {
                    source {
                        srcDirs 'src/test/cpp'
                        include "*.cpp"
                    }
                    lib project: ':qgi', library: 'uds', linkage: 'static'
                    lib project: ':qgi', library: 'shm', linkage: 'static'
                }
            }
            testing $.components.tdqgc
        }
    }

    tasks {
        copyDependentDebugObjects(Copy) {
            from "${rootDir}/qgi/toolchain/jsoncpp/libjsoncpp.a"
            from "${project(':qgi').buildDir}/objs/shm/static/shmCpp/"
            from "${project(':qgi').buildDir}/objs/uds/static/udsCpp/"
            into "${project.buildDir}/objs/main/shared/mainCpp"
        }
        tdqgcStaticLibrary.dependsOn copyDependentDebugObjects

        makeLibjsoncpp(Exec) {
            workingDir "${project(':qgi').projectDir}/toolchain/jsoncpp"
            commandLine 'make'
        }
        linkTdqgcSharedLibrary.dependsOn makeLibjsoncpp
    }

    repositories {
        libs(PrebuiltLibraries) {
            tdqgi {
                headers.srcDir "${project(':qgi').projectDir}/src/main/cpp/include"
                binaries.withType(StaticLibraryBinary) {
                    staticLibraryFile = file("${project(':qgi').projectDir}/build/libs/tdqgi/static/libtdqgi.a")
                }
            }
            jsoncpp {
                headers.srcDir "${rootDir}/qgi/toolchain/jsoncpp/include/"
                binaries.withType(StaticLibraryBinary) {
                    staticLibraryFile = file("${rootDir}/qgi/toolchain/jsoncpp/libjsoncpp.a")
                }
            }
            gtest {
                headers.srcDir "${rootDir}/qgi/toolchain/googletest/include"
                binaries.withType(StaticLibraryBinary) {
                     staticLibraryFile = file ("${rootDir}/qgi/toolchain/googletest/make/gtest_main.a")
                }
            }
            gmock {
                headers.srcDir "${rootDir}/../toolchain/googlemock/include"
                binaries.withType(StaticLibraryBinary) {
                     staticLibraryFile = file ("${rootDir}/../toolchain/googlemock/make/gmock_main.a")
                }
            }
        }
    }
}

// build distribution package
def distributionConfiguration = {
    dependsOn assemble
    into("${version}") {
        into ('lib') {
            def tdqgcSharedFile = project(':TeradataConnector:Loader').buildDir.toString() + '/' + project(':TeradataConnector:Loader').libsDirName + '/tdqgc/shared/libtdqgc.so'
            from(tdqgcSharedFile) {
                fileMode 0755
            }
        }
        into ('bin') {
            from ( rootProject.projectDir.toString()  + '/TeradataConnector/TDInstall' ) {
                eachFile { file ->
                     if(file.getName().endsWith(".sh") ) {
                          file.setMode(0750)
                     }
                }
            }
            include "*"
        }
    }
}

tasks.withType(RunTestExecutable) {
    args "--gtest_output=xml:qgc_utest.xml"
}

//=============================================
// LCOV and unit tests
//=============================================
task lcovgen(type: Exec) {
    commandLine '../../../toolchain/lcov-1.11/bin/lcov', '-d', '.', '-c', '-o', 'qgc_lcov'
}

task lcovclean(dependsOn: ["lcovgen"], type: Exec) {
    def lcovremove = ["/usr/include*", "toolchain*", "Commons*", "QGC/TeradataConnector/Loader/src/test/*", "cpp/src/proto*"]
    def cmdl = ['../../../toolchain/lcov-1.11/bin/lcov', '--remove', 'qgc_lcov'] + lcovremove + [ '-o', 'qgc_lcov']
    commandLine cmdl
}

//lcovgen.finalizeBy lcovclean
lcovclean.mustRunAfter lcovgen

task genhtml(dependsOn: ["lcovclean"], type: Exec) {
    commandLine '../../../toolchain/lcov-1.11/bin/genhtml', 'qgc_lcov', 'qgc_lcov', '-o', 'coverage'
}

task lcov(dependsOn: ["genhtml"]){
}
//=============================================
//=============================================

project(':TeradataConnector').distributionTar {
    configure distributionConfiguration
}

If this is your only project that builds native software, I would instead suggest what’s the issue you are trying to solve with the migration? If you just want to use the new cpp-library plugin, then there are some things that will be hard to migrate (or may require some custom code on your part given some missing feature). Your current Gradle code is already pretty good, I can suggest some minor changes to align with the latest Gradle.

Nokee project is also lacking some features to make the transition from the software model, most notably the prebuilt binaries. I did some work around that but it’s not ready. I think your best course of action, at the moment, is to prepare for the migration but not go through the migration just yet.

Hey Daniel,
Thanks for the prompt response. This is one of the subprojects out of many ( say 20 other ). So this has some dependency on the pre-compiled libraries. The reason we want to move away from this approach is to follow the recommended way ( with the new cpp-library plugin ) , and not have any limitation or surprises in the future. So as said can you suggest those changes you want us to make on this one?. And also how likely is the native software model is going to get deprecated one day?.

I understand the confusion and we should make a better effort to clear this confusion. I can speak for Nokee’s native support but I can’t speak for Gradle. In terms of features, the software model still has the most features. That doesn’t mean that Nokee and the newer cpp-library plugin don’t have awesome features as well, the focus was put into different areas. The newer cpp-library plugins focused on C++ and Swift as well as using Gradle dependency engine (allowing transitive dependencies and publishing). Nokee focuses on replacing both the software model and the newer cpp-library plugins while focusing on the extensibility of the model. Given that we (Nokee) is currently on a technical hup that we are working hard on finishing so we can proceed with the feature parity with the software model. The current work at Nokee is focusing on strategic development for the future.

As for the software model, I’m pushing hard so the software model is only phased out when Nokee can fully replace it. At the current pace, I wouldn’t bother too much for the next 12 months.

1 Like