- Add to root build.gradle
buildscript {
repositories {
maven(url = uri("https://jitpack.io"))
}
dependencies {
classpath("com.edwardstock:gradle-cmakebuild:0.2.2")
}
}
- Apply plugin
plugins {
id("com.edwardstock.cmakebuild")
}
- Configure
plugins {
`java-library`
kotlin("jvm")
id("maven-publish")
id("com.edwardstock.cmakebuild")
}
// Simple configuration requires only path to cmake project directory
// Plugin adds a task named buildCMake
cmakeBuild {
// you can switch off native build by pass boolean
enable = project.property("enable_native_build") == "1"
path = rootProject.file("my-cmake-project-dir")
/* set specific cmake build directory, by default it uses ${project}/build/cmake
stagingPath = project.buildDir
*/
// cmake's CMAKE_BUILD_TYPE
buildType = "Debug"
// common cmake arguments
arguments += listOf(
"-DMY_OPTION=1"
)
// specify cmake configure time definitions by special variable
definitions["MY_OPTION"] = "1"
definitions["CMAKE_BUILD_TYPE"] = "Debug"
// set configuration for all OS:
allOS {
// configure:
// cFlags, cppFlags, arguments or definitions for all OS
}
// set configuration for specific OS:
windows {}
macos {}
linux {}
}
// Set Jar-generation tasks depends on cmake project to make ability attach libs to jar
tasks.withType<Jar> {
// exclude adding generated libs to javadoc and sources jar files
if (archiveClassifier.get() != "sources" && archiveClassifier.get() != "javadoc") {
dependsOn("buildCMake")
// ${project.buildDir}/.cxx is a default location for cmake artifacts (archive, library and runtime)
from(file("${project.buildDir}/.cxx"))
}
}
// Also, to run local test with JNI-bindings, use Test task
tasks.withType<Test> {
val arch = when (System.getProperty("os.arch")) {
"x86_64",
"amd64" -> "x86_64"
else -> System.getProperty("os.arch")
}
allJvmArgs = if(cmakeBuild.isWindows) {
allJvmArgs + listOf(
// windows MSVC puts artifacts to configuration-specific dir
"-Djava.library.path=${project.buildDir}/.cxx/${arch}/${cmakeBuild.buildType}"
)
} else {
allJvmArgs + listOf(
"-Djava.library.path=${project.buildDir}/.cxx/${arch}"
)
}
}