⚙️Customization
Variables
The variables below will be linked to their Kotlin Documentation for more information.
All variables are optional. The plugin will do its best to estimate where your header and source files are, and how to link it all together. Projects that have a specified binary output or RUNTIME_OUTPUT_DIRECTORY
and an include
directory at the root of their project don't need to do anything.
This will set the headers
portions of the definition file.
This , the plugin will do a recursive blob check in ${CMAKE_CURRENT_SOURCE_DIR}/include
directory. If the list is blank, then the headers
section is not written.
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
The variable is parsed as a space-separated string. To declare a custom header list, use the file()
command. You need to specify the RELATIVE
parameter because cinterop
looks relatively for headers
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_HEADERS
by 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
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
cinterop
uses 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
The library paths need be a relative path. cinterop
will not accept absolute paths.
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.
static inline int getMagicNumber() {
return 42;
}
file(READ extra.c KN_DEFINITION_EXTRA)
Will output:
---
static inline int getMagicNumber() {
return 42;
}
KN_CINTEROP_EXTRA_OPTS
KN_CINTEROP_EXTRA_OPTS
Optionally pass extra options or flags to the cinterop
command.
set(KN_CINTEROP_EXTRA_OPTS "-verbose")
KN_CINTEROP_FOLDER
KN_CINTEROP_FOLDER
Specify the folder where the definition and klib binaries will be generated.
set(KN_CINTEROP_FOLDER "${CMAKE_CURRENT_SOURCE_DIR}/build")
KN_DEFINITION_FILE_NAME
KN_DEFINITION_FILE_NAME
Specify the file name to where the .def
file should be generated.
set(KN_DEFINITION_FILE_NAME "${PROJECT_NAME}.def")
KN_CINTEROP_FILE_NAME
KN_CINTEROP_FILE_NAME
Specify the file namewhere the .klib
file should be generated.
set(KN_CINTEROP_FILE_NAME "${PROJECT_NAME}.klib")
Installing to Maven Local
CMakeKt will calculate the directory to install the klib
binary 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_ARTIFACTID
specifies the Artifact ID in your pom.xml
file. By default, this is the same as ${PROJECT_NAME}
.
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.
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.xml
credentials if necessary.
<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