每个工具链的Gradle本机库

时间:2019-04-25 00:34:01

标签: gradle gradle-native

我正在使用Gradle的本机代码项目中工作。该项目在使用gcc的Linux上构建良好,包括使用预构建的库,特别是openssl。现在,我需要使用clang在MacOS上构建它。

在我的模型开始时,我有:

model { 
    repositories {
        libs(PrebuiltLibraries) {                     
            openssl {
                headers.srcDir "/usr/local/include/openssl"      
                headers.include "**/*.h"          
                binaries.withType(StaticLibraryBinary) {
                    staticLibraryFile = file("/usr/local/lib/libssl.a") 
                }
                binaries.withType(StaticLibraryBinary) {
                    staticLibraryFile = file("/usr/local/lib/libcrypto.a")
                }
                binaries.withType(SharedLibraryBinary) {
                    sharedLibraryFile = file("/usr/local/lib/libssl.so")
                }
                binaries.withType(SharedLibraryBinary) {
                    sharedLibraryFile = file("/usr/local/lib/libcrypto.so")
                }
            }
        }
    }
}
components {
    ...
}

基本上,我只需要在Mac编译时更改文件路径。我习惯从组件生成二进制文件时查看toolChain属性,因此我尝试在if (toolChain in <Gcc | Clang>) { ... }声明周围添加libs()块,但出现错误:

Cannot create a ArtifactRepository named 'toolChain'

我还尝试创建一个单独的openssl-mac存储库对象,然后在组件声明中使用相同的if (toolChain ...) {}逻辑,如下所示:

components {                
        myLib(NativeLibrarySpec) {
            sources {
                cpp { 
                    source {
                        srcDir "mylib/src"
                        include "*.cpp"
                    }
                    exportedHeaders {
                        srcDir "mylib/src"
                        include "*.h"
                    }
                    if (toolChain in Gcc) { lib library: 'openssl', linkage: 'static' }
                    if (toolChain in Clang) { lib library: 'openssl-mac', linkage: 'static' }
                }
            }
        }
    }
}

但我收到类似的错误:

No such property: toolChain for class: org.gradle.language.cpp.CppSourceSet

很明显,我要解决这个错误。根据目标工具链/平台/ os / etc提供不同的预构建库的正确方法是什么?

1 个答案:

答案 0 :(得分:0)

答案似乎是像这样使用org.gradle.internal.os.OperatingSystem

import org.gradle.internal.os.OperatingSystem;

model { 
    repositories {
        if (OperatingSystem.current().isLinux()) {
            libs(PrebuiltLibraries) {                     
                openssl {
                    ...
                }
            }
        }
        if (OperatingSystem.current().isMacOsX()) {
            libs(PrebuiltLibraries) {                     
                openssl {
                    ...
                }
            }
        }
    }
    ...
}