Skip to content

ISNing/CMakePlugin

Repository files navigation

CMakePlugin

This plugin allows to configure and build using CMake.

This plugin should work as documented, but is in an early development phase. If you have feature requests or find bugs, please create an issue.

Prerequisites

  • CMake installed on the system. Available here.

To apply the plugin:

plugins {
  id("io.github.isning.gradle.plugins.cmake") version "0.1.5"
}

and configure within cmake { ... } block:

cmake {
    projects {
        val hello by creating {
            val buildPath = project.layout.buildDirectory.dir("cmake").get().asFile.absolutePath +
                    "/{projectName}/{targetName}"
            configParams {
                sourceDir = file("src/cpp").absolutePath
                buildDir = buildPath
            }
            buildParams {
                buildDir = buildPath
            }

            listOf(
              androidX64.ndk(),
              androidX86.ndk(),
              androidArm32.ndk(),
              androidArm64.ndk(),
            ).forEach {
                it.configParams {
                    entries {
                        ndk = "C:/Users/ISNing/AppData/Local/Android/Sdk/ndk/25.2.9519653"
                    }
                }
            }
          mingwX64.zig()
          msvcX64.zig()
            host()
        }
    }
}

Note: Here's project refers to CMakeProject as well as a library or something, not gradle project.

You can define properties in different scopes such as cmake block, CMakeProject block orCMakeTarget block, and finally when it's going to build, the properties will be merged together.

The preset functions like androidX64.ndk() will return a target with some default properties for crosscompiling using Android NDK toolchains, and there's also different toolchain specified presets for different target.

And for different presets, there are still some properties needs to be manually filled For example, androidX64() will require ndk and/or other related properties.

And for all the properties, there are several stub strings that will be replaced by the plugin:

Stub String To
{gradleProjectName} The name of gradle project
{projectName} The name of the project
{targetName} The name of the target

For some properties, instead of define them in the build script, you can also define them in a file like local.properties file in the root of the project, and read them into project extra properties.

Property Name Used for
ndk.dir Path to Android NDK (Only used for android targets)
{targetName}.sysRoot Will be taken as the value of CMAKE_SYSROOT
{targetName}.gccInstallDir Will be taken as a flag for clang indicating the path to gcc installation
{targetName}.gccToolchain Will be taken as a flag for clang indicating the path to gcc toolchain
{targetName}.extraCompilerFlags Extra flags for the compiler

Auto-created tasks

  • cmakeConfigure${projectName}${targetName}: Calls CMake to generate build scripts for the specified target.

  • cmakeBuild${projectName}${targetName}: Calls CMake to build the specified target.

  • cmakeClean: Cleans the workingFolder.

  • cmakeListGenerators: Trys to list the generators available on the current platform by parsing cmake --help's output.

  • cmakeVersion: CMake's version info. cmake --version's output.

Examples

clean, configure and build:

./gradlew cmakeClean cmakeConfigureHelloAndroidArm64 cmakeBuildHelloAndroidArm64

and just call

./gradlew clean assemble

If you want to get the output of cmake, add -i to your gradle call, for example:

./gradlew cmakeConfigure -i

Custom tasks

You can create custom tasks the following way:

val configureFoo = task("configureFoo", CMakeConfigureTask::class) {
    parameters = ModifiableCMakeGeneralParamsImpl {
        generator = "Ninja"
        buildDir = "build/cmake"
        sourceDir = "src/cpp"
        // ...
    } + ModifiableBasicCMakeEntriesImpl().apply {
        /// ...
    }.asCMakeParams
}

val buildFoo = task("configureFoo", type = CMakeBuildTask::class) {
    parameters = ModifiableCMakeBuildParamsImpl {
        buildDir = "build/cmake"
    }
}

buildFoo.dependsOn(configureFoo) // Optional: Just make sure its configured when you run the build task

Learn more about configuring Cross-Compiling

Go to CMake official docs: Cross-Compiling

Go to LLVM official docs: Cross-compilation using Clang

License

All these plugins are licensed under the Apache License, Version 2.0 with no warranty (expressed or implied) for any purpose.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages