If you don’t mind, I can give you a full example. I had to figure out most of the stuff a while ago and just ported the whole thing to Gradle 2.3 with the help of Daz. The build.gradle is quite compilicated and I will just give you the native part if you don’t mind, but it should get you going:
apply plugin: 'cpp'
// C++ configuration
model {
components {
libqrblastrs(NativeLibrarySpec) {
// specify target platforms, necessary for
2.3
targetPlatform "x64"
targetPlatform "x86"
sources {
cpp {
def additionalBoostLinkage = System.getProperty("os.name") == "Mac OS X" ? 'shared' : 'api'
lib library: 'oci', linkage: 'shared'
lib library: 'cpprest', linkage: 'shared'
lib library: 'easylogging', linkage: 'api'
lib library: 'boost', linkage: 'api'
lib library: 'boost_chrono', linkage: additionalBoostLinkage
lib library: 'boost_thread', linkage: additionalBoostLinkage
lib library: 'boost_system', linkage: additionalBoostLinkage
}
}
baseName 'qrblastrs'
binaries.withType(SharedLibraryBinarySpec) {
if (toolChain in VisualCpp) {
cppCompiler.define 'LIBQRBLASTRS_EXPORTS'
cppCompiler.define 'WIN32'
cppCompiler.define '_WINDOWS'
cppCompiler.define '_USRDLL'
cppCompiler.define '_WINDLL'
cppCompiler.define '_UNICODE'
cppCompiler.define 'UNICODE'
cppCompiler.define '_EXPORTING'
if (buildType == buildTypes.debug) {
cppCompiler.define '_DEBUG'
cppCompiler.args '/Zi', '/nologo', '/W3', '/WX-', '/sdl', '/Od', '/Gm', '/EHsc', '/RTC1', '/MDd', '/Gs', '/fp:precise', '/Zc:wchar_t', '/Zc:forScope', '/Gd', '/TP'
if (targetPlatform == platforms.x64) {
linker.args '/INCREMENTAL', '/NOLOGO', '/MANIFEST', '/manifest:embed', '/DEBUG', '/SUBSYSTEM:WINDOWS', '/TLBID:1', '/DYNAMICBASE', '/NXCOMPAT', '/MACHINE:X64', '/DLL'
} else {
linker.args
'/INCREMENTAL', '/NOLOGO', '/MANIFEST', '/manifest:embed', '/DEBUG', '/SUBSYSTEM:WINDOWS', '/TLBID:1', '/DYNAMICBASE', '/NXCOMPAT', '/MACHINE:X86', '/DLL'
}
} else {
cppCompiler.define 'NDEBUG'
cppCompiler.args '/Zi', '/nologo', '/W3', '/WX-', '/sdl', '/O2', '/Oi', '/GL', '/Gm-', '/EHsc', '/MD', '/GS', '/Gy', '/fp:precise', '/Zc:wchar_t', '/Zc:forScope', '/Gd', '/TP'
if (targetPlatform == platforms.x64) {
linker.args '/INCREMENTAL:NO', '/NOLOGO', '/MANIFEST', '/manifest:embed', '/DEBUG', '/SUBSYSTEM:WINDOWS', '/OPT:REF', '/OPT:ICF', '/TLBID:1', '/DYNAMICBASE', '/NXCOMPAT', '/MACHINE:X64', '/DLL', '/LTCG'
} else {
linker.args '/INCREMENTAL:NO', '/NOLOGO', '/MANIFEST', '/manifest:embed', '/DEBUG', '/SUBSYSTEM:WINDOWS', '/OPT:REF', '/OPT:ICF', '/TLBID:1', '/DYNAMICBASE', '/NXCOMPAT', '/MACHINE:X86', '/DLL', '/LTCG'
}
}
}
if (toolChain in Gcc) {
buildable = !(targetPlatform == platforms.x86 && targetPlatform.operatingSystem.linux)
if (buildType == buildTypes.debug) {
cppCompiler.args '-g', '-O0', '-std=c++11', '-fPIC'
} else {
cppCompiler.args '-O3', '-march=core2', '-std=c++11', '-fPIC'
}
}
if (toolChain in Clang) {
buildable = !(targetPlatform == platforms.x86 && targetPlatform.operatingSystem.macOsX)
if (buildType == buildTypes.debug) {
cppCompiler.args '-g', '-O0', '-std=c++11', '-fPIC'
} else {
cppCompiler.args '-O3', '-march=core2', '-std=c++11', '-fPIC'
}
}
}
binaries.withType(StaticLibraryBinarySpec) { buildable = false }
}
libqrblastrsjni(NativeLibrarySpec) {
// specify target platforms, necessary for 2.3
targetPlatform "x64"
targetPlatform "x86"
sources {
cpp {
lib library: 'libqrblastrs', linkage: 'shared'
lib library: 'jvm', linkage: 'api'
lib library: 'easylogging', linkage: 'api'
}
}
baseName 'qrblastrsjni'
binaries.withType(SharedLibraryBinarySpec) {
if (toolChain in VisualCpp) {
cppCompiler.define 'LIBQRBLASTRSJNI_EXPORTS'
cppCompiler.define 'WIN32'
cppCompiler.define '_WINDOWS'
cppCompiler.define '_USRDLL'
cppCompiler.define '_WINDLL'
cppCompiler.define '_UNICODE'
cppCompiler.define 'UNICODE'
cppCompiler.define '_EXPORTING'
if (buildType == buildTypes.debug) {
cppCompiler.define '_DEBUG'
cppCompiler.args '/Zi', '/nologo', '/W3', '/WX-', '/sdl', '/Od', '/Gm', '/EHsc', '/RTC1', '/MDd', '/Gs', '/fp:precise', '/Zc:wchar_t', '/Zc:forScope', '/Gd', '/TP'
if (targetPlatform == platforms.x64) {
linker.args '/INCREMENTAL', '/NOLOGO', '/MANIFEST', '/manifest:embed', '/DEBUG', '/SUBSYSTEM:WINDOWS', '/TLBID:1', '/DYNAMICBASE', '/NXCOMPAT', '/MACHINE:X64', '/DLL'
} else {
linker.args '/INCREMENTAL', '/NOLOGO', '/MANIFEST', '/manifest:embed', '/DEBUG', '/SUBSYSTEM:WINDOWS', '/TLBID:1', '/DYNAMICBASE', '/NXCOMPAT', '/MACHINE:X86', '/DLL'
}
} else {
cppCompiler.define 'NDEBUG'
cppCompiler.args '/Zi', '/nologo', '/W3', '/WX-', '/sdl', '/O2', '/Oi', '/GL', '/Gm-', '/EHsc', '/MD', '/GS', '/Gy', '/fp:precise', '/Zc:wchar_t', '/Zc:forScope', '/Gd', '/TP'
if (targetPlatform == platforms.x64) {
linker.args '/INCREMENTAL:NO', '/NOLOGO', '/MANIFEST', '/manifest:embed', '/DEBUG', '/SUBSYSTEM:WINDOWS', '/OPT:REF', '/OPT:ICF', '/TLBID:1', '/DYNAMICBASE', '/NXCOMPAT', '/MACHINE:X64', '/DLL', '/LTCG'
} else {
linker.args '/INCREMENTAL:NO', '/NOLOGO', '/MANIFEST', '/manifest:embed', '/DEBUG', '/SUBSYSTEM:WINDOWS', '/OPT:REF', '/OPT:ICF', '/TLBID:1', '/DYNAMICBASE', '/NXCOMPAT', '/MACHINE:X86', '/DLL', '/LTCG'
}
}
}
if (toolChain in Gcc) {
buildable = !(targetPlatform == platforms.x86 && targetPlatform.operatingSystem.linux)
if (buildType == buildTypes.debug) {
cppCompiler.args '-g', '-O0', '-std=c++11', '-fPIC'
} else {
cppCompiler.args '-O3', '-march=core2', '-std=c++11', '-fPIC'
}
}
if (toolChain in Clang) {
buildable = !(targetPlatform == platforms.x86 && targetPlatform.operatingSystem.macOsX)
if (buildType == buildTypes.debug) {
cppCompiler.args '-g', '-O0', '-std=c++11', '-fPIC'
} else {
cppCompiler.args '-O3', '-march=core2', '-std=c++11', '-fPIC'
}
}
}
binaries.withType(StaticLibraryBinarySpec) { buildable = false }
}
}
buildTypes {
debug
release
}
platforms {
x86 { architecture "x86" }
x64 { architecture "x86_64" }
}
repositories {
libs(PrebuiltLibraries) {
oci {
headers.srcDir "contrib/oracle/include"
binaries.withType(SharedLibraryBinary) {
if (targetPlatform.operatingSystem.windows) {
if (targetPlatform == platforms.x64) {
sharedLibraryFile = file("file://c/program files/quattro research/OracleInstantClient/oci.dll")
sharedLibraryLinkFile = file("contrib/oracle/lib/x64/oci.lib")
} else {
sharedLibraryFile = file("file://c/program files (x86)/quattro research/OracleInstantClient/oci.dll")
sharedLibraryLinkFile = file("contrib/oracle/lib/Win32/oci.lib")
}
} else {
def oracleHome = System.getenv("ORACLE_HOME")
if (file("${oracleHome}/lib/libclntsh.so").exists()) {
sharedLibraryFile = file("${oracleHome}/lib/libclntsh.so")
} else if (file("${oracleHome}/libclntsh.so").exists()) {
sharedLibraryFile = file("${oracleHome}/libclntsh.so")
} else {
sharedLibraryFile = file("${oracleHome}/libclntsh.dylib")
}
}
}
}
cpprest {
headers.srcDir "contrib/cpprest/include"
binaries.withType(SharedLibraryBinary) {
if (targetPlatform.operatingSystem.windows) {
if (targetPlatform == platforms.x64) {
if (buildType == buildTypes.debug) {
sharedLibraryFile = file("packages/cpprestsdk.2.4.0.1/build/native/bin/x64/v120/Debug/Desktop/cpprest120d_2_4.dll")
sharedLibraryLinkFile = file("packages/cpprestsdk.2.4.0.1/build/native/lib/x64/v120/Debug/Desktop/cpprest120d_2_4.lib")
} else {
sharedLibraryFile = file("packages/cpprestsdk.2.4.0.1/build/native/bin/x64/v120/Release/Desktop/cpprest120_2_4.dll")
sharedLibraryLinkFile = file("packages/cpprestsdk.2.4.0.1/build/native/lib/x64/v120/Release/Desktop/cpprest120_2_4.lib")
}
} else {
if (buildType == buildTypes.debug) {
sharedLibraryFile = file("packages/cpprestsdk.2.4.0.1/build/native/bin/Win32/v120/Debug/Desktop/cpprest120d_2_4.dll")
sharedLibraryLinkFile = file("packages/cpprestsdk.2.4.0.1/build/native/lib/Win32/v120/Debug/Desktop/cpprest120d_2_4.lib")
} else {
sharedLibraryFile = file("packages/cpprestsdk.2.4.0.1/build/native/bin/Win32/v120/Release/Desktop/cpprest120_2_4.dll")
sharedLibraryLinkFile = file("packages/cpprestsdk.2.4.0.1/build/native/lib/Win32/v120/Release/Desktop/cpprest120_2_4.lib")
}
}
} else {
if (file("/usr/lib64/libcpprest.so").exists()) {
sharedLibraryFile = file("/usr/lib64/libcpprest.so")
} else if (file("/usr/lib/libcpprest.so").exists()) {
sharedLibraryFile = file("/usr/lib/libcpprest.so")
} else {
sharedLibraryFile = file("/usr/local/lib/libcpprest.dylib")
}
}
}
}
easylogging { headers.srcDir "contrib/easylogging++/include" }
jvm {
def javaHome = System.getenv("JAVA_HOME")
headers.srcDir "${javaHome}/include"
binaries.all {
if (targetPlatform.operatingSystem.windows) {
headers.srcDir "${javaHome}/include/win32"
} else if (targetPlatform.operatingSystem.linux) {
headers.srcDir "${javaHome}/include/linux"
} else {
headers.srcDir "${javaHome}/include/darwin"
}
}
}
// additional libraries on Linux && OS X
boost {
def boostRoot = System.getenv("BOOST_ROOT")
if (boostRoot != "") {
headers.srcDir "${boostRoot}/include"
}
}
boost_chrono {
def boostRoot = System.getenv("BOOST_ROOT")
if (boostRoot != "") {
binaries.withType(SharedLibraryBinary) {
if (targetPlatform.operatingSystem.macOsX) {
sharedLibraryFile = file("${boostRoot}/lib/libboost_chrono.dylib")
}
}
}
}
boost_thread {
def boostRoot = System.getenv("BOOST_ROOT")
if (boostRoot != "") {
binaries.withType(SharedLibraryBinary) {
if (targetPlatform.operatingSystem.macOsX) {
sharedLibraryFile = file("${boostRoot}/lib/libboost_thread-mt.dylib")
}
}
}
}
boost_system {
def boostRoot = System.getenv("BOOST_ROOT")
if (boostRoot != "") {
binaries.withType(SharedLibraryBinary) {
if (targetPlatform.operatingSystem.macOsX) {
sharedLibraryFile = file("${boostRoot}/lib/libboost_system.dylib")
}
}
}
}
}
}
}
The script generates two native libraries where one is a JNI library linking against the first library. Different pre-built libraries have to be linked depending on the Platform the build is running on. Only Windows is generatiing x86 and x86_64 builds while OS X and Linux compile only for 64-bit. Feel free to ask if you find anything fishy. I’m no expert and went through quite a lot of trial and error, but the build works on all 3 target platform.