0

我有一个 gradle 项目,用于 gradle(2.10 版)中的本机 c 应用程序,由多个组件组成:

components {

   component_1(NativeLibrarySpec)
   component_2(NativeLibrarySpec)
   ...
   component_n(NativeLibrarySpec)

   main_component(NativeExecutableSpec){
       sources {
            c.lib library: "component_1", linkage: "static"
            c.lib library: "component_2", linkage: "static"
            ...
            c.lib library: "component_n", linkage: "static"
        }

   }
}

以这种形式拥有应用程序的主要思想是更容易测试单个组件。但是,我有两个大问题:

main_component是一个应用程序,因此具有主要功能,这会产生此错误:multiple definition of 'main'... gradle_cunit_main.c:(.text+0x0): first defined here。这是可以预料的,并且在文档中有所提及,但我想知道是否有办法避免这个问题,例如,阻止主要组件包含在测试中。这与下一个问题有关

当我尝试按如下方式定义要测试的组件时,CUnit 插件(从 2.10 版开始)抱怨:

testSuites {
    component_1Test(CUnitTestSuiteSpec){
            testing $.components.component_1
        }
   }
}

毕业抱怨:

无法使用创建规则 'component_1Test(org.gradle.nativeplatform.test.cunit.CUnitTestSuiteSpec) { ... } @ build.gradle line 68, column 9' 作为规则 'CUnitPlugin.Rules#createCUnitTestSuitePerComponent > 创建 'testSuites.component_1Test' create(component_1Test)' 已注册以创建此模型元素

综上所述,我想指示 cunit 插件只测试一些组件,并防止编译主组件(带有 main 函数)进行测试。同样,请注意我使用的是 gradle 2.10 并且无法升级到更新的版本,因为这会破坏我们的 CI 工具链。

提前感谢您的评论。

4

1 回答 1

0

以下是如何构建项目以允许对组件进行单元测试,但阻止对主程序进行单元测试:将组件拆分为子项目并将cunit插件应用到子项目,如下所示:

project(':libs'){
apply plugin: 'c'
apply plugin: 'cunit'
model{

    repositories {
        libs(PrebuiltLibraries) {
            cunit {
                headers.srcDir "/usr/include/"
                    binaries.withType(StaticLibraryBinary) {
                        staticLibraryFile = file("/usr/lib/x86_64-linux-gnu/libcunit.a")
                    }
            }
        }
    }

    components {
        component_1(NativeLibrarySpec)
        component_2(NativeLibrarySpec)
        ...
        component_n(NativeLibrarySpec)
    }

    binaries {
        withType(CUnitTestSuiteBinarySpec) {
            lib library: "cunit", linkage: "static"
        }
    }
}
}
apply plugin: 'c'
model {
    components{
        myprogram(NativeExecutableSpec) {
            sources.c {
                lib project: ':libs', library: 'component_1', linkage: 'static'
                lib project: ':libs', library: 'component_2', linkage: 'static'
                lib project: ':libs', library: "component_n", linkage: 'static'
                source {
                     srcDir "src/myprogram/c"
                          include "**/*.c"
                }
            }
        }
    }
}
于 2017-04-23T21:37:51.550 回答