Correct way to use headers from googletest artifact with cpp-unit-test?


(Mike "Traff" Traffanstead) #1

I have a native composite project building using a cpp-application and multiple cpp-library subprojects. Everything builds and runs fine but when I try to add unit tests with cpp-unit-test, it breaks in various ways.

Environment: RHEL 6.5 (x86-64), Gradle 4.9, Java 1.8.0b121, RH Dev Toolset 7 (GCC 7.2.1)

I’m trying to add tests to one library.

If I add:

dependencies {
    implementation "org.gradle.cpp-samples:googletest_linux_x86-64_4.5:1.9.0-SNAPSHOT"
}

It fails to download the headers and the compile fails looking for <gtest/gtest.h>. I’ve tried with “org.gradle.cpp-samples:googletest:1.9.0-SNAPSHOT” and pulling the metadataFormat from ModuleMetadataParser.

If I change it to:

dependencies {
    unitTestImplementation "org.gradle.cpp-samples:googletest_linux_x86-64_4.5:1.9.0-SNAPSHOT"
}

It fails with:

Could not find method unitTestImplementation() for arguments [org.gradle.cpp-samples:googletest:latest.integration] on object of type org.gradle.language.internal.DefaultComponentDependencies.

I’ve tried putting the unitTestImplementation dependency at the project level, at the unitTest level and in unitTest under binaries.whenElementFinalized. All are failing with variations of could not find unitTestImplementation.

Any suggestions? Thanks in advance.

Current build.gradle:

plugins {
    id 'cpp-library'
    id 'cpp-unit-test'
}

library {
    linkage = [Linkage.STATIC, Linkage.SHARED]

    binaries.configureEach { binary ->
        def compileTask = binary.compileTask.get()

        compileTask.source.from fileTree(dir: 'src/main/c', include: '*.c')

        compileTask.compilerArgs.addAll project.provider {
            [
                '-x', 'c', 
                '-std=c11'
            ]
        }
    }
}

unitTest {
    binaries.configureEach { binary ->
        def compileTask = binary.compileTask.get()

        compileTask.macros _GLIBCXX_USE_CXX11_ABI: '0'

        compileTask.compilerArgs.addAll project.provider {
            [
                '-std=c++03'
            ]
        }
    }

    binaries.whenElementFinalized {
        def metadataFormat = org.gradle.api.internal.artifacts.ivyservice.ivyresolve.parser.ModuleMetadataParser.FORMAT_VERSION

        dependencies {
            implementation "org.gradle.cpp-samples:googletest_${metadataFormat}:latest.integration"
        }
    }
}