Skip to content

Latest commit

 

History

History
102 lines (85 loc) · 2.7 KB

README.md

File metadata and controls

102 lines (85 loc) · 2.7 KB

gradle-cmakebuild

Release common build

Gradle plugin helps to build CMake project

Usage

  1. Add to root build.gradle
buildscript {
    repositories {
        maven(url = uri("https://jitpack.io"))
    }
    dependencies {
        classpath("com.edwardstock:gradle-cmakebuild:0.2.2")
    }
}
  1. Apply plugin
plugins {
    id("com.edwardstock.cmakebuild")
}
  1. 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}"
        )
    }
}