⚙️Customization

Variables

The variables below will be linked to their Kotlin Documentation for more information.

This will set the headersportions of the definition file.

Let's say your file tree looks like this:

include/
--- header1.h
--- header2.h
--- header3.h
--- private.h
--- inner/
------- header4.h
------- header5.h

src/
--- impl.c
--- cpp_impl.cpp
--- main.c
file(GLOB_RECURSE KN_DEFINITION_HEADERS RELATIVE "${CMAKE_CURRENT_SOURCE_DIR}/include" "include/*.h")

Will output:

headers = header1.h header2.h header3.h private.h inner/header4.h inner/header5.h

You can import Objective C modules using this variable. This is especially useful when working with Apple modules written in Objective C.

set(KN_DEFINITION_MODULES "UIKit IOKit CoreGraphics")

Will output:

modules = UIKit IOKit CoreGraphics

You can filter out KN_DEFINITION_HEADERSby a glob in case you want to ignore private headers (such as private.h declared earlier).

set(KN_DEFINITION_HEADER_FILTER "!private.h")

Will output:

headerFilter = !private.h

KN_DEFINITION_PACKAGE

This is the Kotlin package to store all of the C functions, structs, unions, and members.

set(KN_DEFINITION_PACKAGE "my.custom.package")

Will output:

package = my.custom.package

cinteropuses the C compiler to properly analyze headers. By default, the plugin will automatically pass CMAKE_C_FLAGS or CMAKE_OBJC_FLAGS to these properties. It will also try to include your include directory to the compiler and link any output libraries to the linker.

Specify directories to look for library files. By default, the plugin will specify ${CMAKE_CURRENT_BINARY_DIR} and ${RUNTIME_OUTPUT_DIRECTORY} if they exist.

set(KN_DEFINITION_LIBRARY_PATHS "${CMAKE_CURRENT_SOURCE_DIR} ${CMAKE_CURRENT_BINARY_DIR}")

Will output:

libraryPaths = /path/to/project /path/to/project/binary-dir

Specify library files that should be included in the klib generated file. These libraries must be located in the KN_DEFINITION_LIBRARY_PATHS variable. By default, the plugin will automatically look for the following libraries:

  • *.a

  • *.so

  • *.dll

  • *.dylib

  • *.lib

file(GLOB KN_DEFINITION_LIBRARIES RELATIVE "${CMAKE_CURRENT_BINARY_DIR}"
        "${CMAKE_CURRENT_BINARY_DIR}/*.a" 
        "${CMAKE_CURRENT_BINARY_DIR}/*.dylib" 
        "${CMAKE_CURRENT_BINARY_DIR}/*.so" 
        "${CMAKE_CURRENT_BINARY_DIR}/*.dll"
        "${CMAKE_CURRENT_BINARY_DIR}/*.lib"
)

Will output:

staticLibraries = libfoo.so libbar.so

You can configure custom declarations in the definition file using this variable. The plugin will prepend --- to this variable and then create a custom declaration.

extra.c
static inline int getMagicNumber() {
    return 42;
}
CMakeLists.txt
file(READ extra.c KN_DEFINITION_EXTRA)

Will output:

---
static inline int getMagicNumber() {
    return 42;
}

KN_CINTEROP_EXTRA_OPTS

Optionally pass extra options or flags to the cinterop command.

set(KN_CINTEROP_EXTRA_OPTS "-verbose")

KN_CINTEROP_FOLDER

Specify the folder where the definition and klib binaries will be generated.

The default directory is ${CMAKE_CURRENT_SOURCE_DIR}/build/cinterop.

set(KN_CINTEROP_FOLDER "${CMAKE_CURRENT_SOURCE_DIR}/build")

KN_DEFINITION_FILE_NAME

Specify the file name to where the .def file should be generated.

The default name is ${PROJECT_NAME}-${PROJECT_VERSION}.def.

set(KN_DEFINITION_FILE_NAME "${PROJECT_NAME}.def")

KN_CINTEROP_FILE_NAME

Specify the file namewhere the .klib file should be generated.

The default name is ${PROJECT_NAME}-${PROJECT_VERSION}.klib.

set(KN_CINTEROP_FILE_NAME "${PROJECT_NAME}.klib")

Installing to Maven Local

CMakeKt will calculate the directory to install the klibbinary for your maven local repository.

KN_INSTALL_GROUPID specifies the Group ID in your pom.xml file. By default, this is the same as ${KN_DEFINITION_PACKAGE} . KN_INSTALL_ARTIFACTIDspecifies the Artifact ID in your pom.xml file. By default, this is the same as ${PROJECT_NAME}.

CMakeLists.txt
set(KN_INSTALL_GROUPID "dev.superdev.app.native")
set(KN_INSTALL_ARTIFACTID "superapp")

Deploying to Maven

CMakeKt specifies a target using the mvn deploy:deploy-file task. You need to specify MAVEN_REMOTE_URL and MAVEN_REMOTE_ID into the maven deployment function.

CMakeLists.txt
set(MAVEN_REMOTE_URL "https://repo.example.com/repository/maven-releases")
set(MAVEN_REMOTE_ID "my-repository")

add_maven_deployment(my_target "${MAVEN_REMOTE_URL}" "${MAVEN_REMOTE_ID")

You can use your settings.xmlcredentials if necessary.

settings.xml
<servers>
    <server>
        <id>my-repository</id> <!-- Ensure same name as MAVEN_REMOTE_ID -->
        <username>demo-user</username>
        <password>dummy-password</password>
        <privateKey>${user.home}/.ssh/dummy_key</privateKey>
        <passphrase>dummy-passphrase</passphrase>
    </server>
</servers>

Last updated