我正在使用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提供不同预构建库的正确方法是什么?
答案似乎是像这样使用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 {
...
}
}
}
}
...
}