Skip to content

Commit

Permalink
ElektraInitiative#3713 WIP jna binding gradle build cmake integration
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael Tucek committed Mar 29, 2021
1 parent 97e66d1 commit b25380a
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 69 deletions.
4 changes: 2 additions & 2 deletions doc/news/2019-11-26_0.9.1.md
Original file line number Diff line number Diff line change
Expand Up @@ -248,8 +248,8 @@ you up to date with the multi-language support provided by Elektra.
### Java

- Completely overhauled the Java binding to be able to use Elektra plugins directly. A new PluginLoader can load Elektra plugins or a native implemented Java plugin.
All Plugins now implement the new [Plugin](../../src/bindings/jna/libelektra5j/src/main/java/org/libelektra/Plugin.java) interface.
For an example see the [test case](../../src/bindings/jna/libelektra5j/src/test/java/org/libelektra/PluginLoaderIT.java). _(Michael Zronek)_
All Plugins now implement the new [Plugin](../../src/bindings/jna/libelektra/src/main/java/org/libelektra/Plugin.java) interface.
For an example see the [test case](../../src/bindings/jna/libelektra/src/test/java/org/libelektra/PluginLoaderIT.java). _(Michael Zronek)_
- Upgraded maven dependencies for Java binding _(Michael Zronek)_
- The Java binding now supports the [error codes](../decisions/error_codes.md) in a native way. All exceptions contain the necessary information. _(Michael Zronek)_
- Further improved the Java binding such as wording and documentation. _(Michael Zronek)_
Expand Down
16 changes: 8 additions & 8 deletions doc/tutorials/java-kdb.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ After that you can start loading a `KDB` object as follows:
```java
Key key = Key.create("user:/kdbsession/javabinding");
try (KDB kdb = KDB.open(key)) {
//Your code to manipulate keys
// Your code to manipulate keys
} catch (KDB.KDBException e) {
e.printStackTrace();
}
Expand Down Expand Up @@ -54,7 +54,7 @@ The method call would return `it_works!`.

```java
String str = set.lookup("user:/my/test").getString()
System.out.println(str) //prints "it works!"
System.out.println(str) // prints "it works!"
```

## Saving Keys
Expand All @@ -64,7 +64,7 @@ Next I will show you how to save a new key into the database. First we need need
```java
KeySet set = KeySet.create();
Key namespace = Key.create("user:/");
kdb.get(set, namespace); //Fetch all keys for the namespace
kdb.get(set, namespace); // Fetch all keys for the namespace
set.append(Key.create("user:/somekey", "myValue"));
kdb.set(set, key);
```
Expand All @@ -83,12 +83,12 @@ environments though as it is not intended for productive systems.
Key key = Key.create("user:/errors");
try (KDB kdb = KDB.open(key)) {
KeySet set = KeySet.create();
Key namespace = Key.create("user:/"); //Select a namespace from which all keys should be fetched
kdb.get(set, namespace); //Fetch all keys into the set object
for (int i = 0; i < set.length(); i++) { //Traverse the set
Key namespace = Key.create("user:/"); // Select a namespace from which all keys should be fetched
kdb.get(set, namespace); // Fetch all keys into the set object
for (int i = 0; i < set.length(); i++) { // Traverse the set
String keyAndValue = String.format("%s: %s",
set.at(i).getName(), //Fetch the key's name
set.at(i).getString()); //Fetch the key's value
set.at(i).getName(), // Fetch the key's name
set.at(i).getString()); // Fetch the key's value
System.out.println(keyAndValue);
}
} catch (KDB.KDBException e) {
Expand Down
107 changes: 48 additions & 59 deletions src/bindings/jna/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,15 +1,6 @@
find_program (MAVEN_EXECUTABLE mvn)
find_program (GRADLE_EXECUTABLE gradle)
#find_program(GRADLE_EXECUTABLE NAMES gradle PATHS $ENV{GRADLE_HOME}/bin NO_CMAKE_FIND_ROOT_PATH)
find_package (Java QUIET)

macro (test_needs_plugin test plugin)
list (FIND REMOVED_PLUGINS ${plugin} plugin_index)
if (plugin_index GREATER -1)
string (APPEND MAVEN_TEST_EXCLUDES "<exclude>**/${test}</exclude>")
endif (plugin_index GREATER -1)
endmacro (test_needs_plugin test plugin)

macro (java_test_needs_plugin test plugin)
list (FIND REMOVED_PLUGINS ${plugin} plugin_index)
if (plugin_index GREATER -1)
Expand Down Expand Up @@ -45,28 +36,17 @@ if (Java_JAVAC_EXECUTABLE)

file (MAKE_DIRECTORY libelektra)

set (MAVEN_TEST_EXCLUDES "")
test_needs_plugin (GOptsTest.java gopts)
# generate test exclusions based on specific plugins
set (GRADLE_TEST_EXCLUDES "")
java_test_needs_plugin (GOptsTest gopts)

# first fill the elektra version in the pom file @ONLY to avoid replacing pom.xml placeholders which also use the
# format ${}
configure_file ("${CMAKE_CURRENT_SOURCE_DIR}/libelektra/pom.xml.in"
"${CMAKE_CURRENT_BINARY_DIR}/libelektra/pom.xml" @ONLY)
# first fill the elektra version in the pom file @ONLY to avoid replacing pom.xml placeholders which also use the format ${}
configure_file ("${CMAKE_CURRENT_SOURCE_DIR}/version-settings.gradle.in"
"${CMAKE_CURRENT_BINARY_DIR}/version-settings.gradle" @ONLY)
configure_file ("${CMAKE_CURRENT_SOURCE_DIR}/libelektra/test-exclusion.gradle.in"
"${CMAKE_CURRENT_BINARY_DIR}/libelektra/test-exclusion.gradle" @ONLY)

# required to have a recognizable name for the pom upon installation in case other software installs a pom to the
# target directory and calls it pom.xml
file (
GENERATE
OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/libelektra/libelektra-${KDB_VERSION}.pom.xml"
INPUT "${CMAKE_CURRENT_BINARY_DIR}/libelektra/pom.xml")

# The build system calls Maven to recompile the binding, if we change any of the following source files.
# the build system calls Gradle to recompile the binding, if we change any of the following source files.
set (JNA_BINDING_PREFIX libelektra)
set (JNA_BINDING_NAMESPACE java/org/libelektra)
set (JNA_BINDING_SOURCE_DIRECTORY ${JNA_BINDING_PREFIX}/src/main/${JNA_BINDING_NAMESPACE})
Expand Down Expand Up @@ -114,24 +94,33 @@ if (Java_JAVAC_EXECUTABLE)
${JNA_BINDING_SOURCE_DIRECTORY_TEST}/KeyTest.java
${JNA_BINDING_SOURCE_DIRECTORY_TEST}/PluginLoaderIT.java
${JNA_BINDING_PREFIX}/build.gradle
${JNA_BINDING_PREFIX}/gradle.properties
${JNA_BINDING_PREFIX}/java-library.gradle
${JNA_BINDING_PREFIX}/java-shared.gradle
${JNA_BINDING_PREFIX}/maven-publish.gradle
${JNA_BINDING_PREFIX}/settings.gradle)

# Compile the source files and build the jar. We copy the source files into the build folder as that is the easiest
# way to handle the integration between CMake and Maven.
${JNA_BINDING_PREFIX}/dependencies.gradle
build.gradle
gradle.properties
java-library.gradle
java-shared.gradle
maven-publish.gradle
settings.gradle)

# compile the source files and build the jar. We copy the source files into the build folder as that is the easiest
# way to handle the integration between CMake and Gradle.
add_custom_command (
OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/libelektra/build/libs/libelektra-${KDB_VERSION}.jar"
COMMAND ${CMAKE_COMMAND} ARGS -E copy_directory "${CMAKE_CURRENT_SOURCE_DIR}/libelektra"
"${CMAKE_CURRENT_BINARY_DIR}/libelektra"
COMMAND ${MAVEN_EXECUTABLE} -q -B -DskipTests=true package
WORKING_DIRECTORY libelektra
DEPENDS "${CMAKE_CURRENT_BINARY_DIR}/libelektra/pom.xml" ${JNA_BINDING_SOURCE_FILES})
# TODO add changes in variable replaced build files to dependencies
"${CMAKE_CURRENT_BINARY_DIR}/libelektra/build/libs/libelektra-${KDB_VERSION}-javadoc.jar"
"${CMAKE_CURRENT_BINARY_DIR}/libelektra/build/libs/libelektra-${KDB_VERSION}-sources.jar"
"${CMAKE_CURRENT_BINARY_DIR}/libelektra/build/publications/libelektra/libelektra-${KDB_VERSION}.pom.xml"
COMMAND ${CMAKE_COMMAND} ARGS -E copy_directory
"${CMAKE_CURRENT_SOURCE_DIR}"
"${CMAKE_CURRENT_BINARY_DIR}"
COMMAND ${GRADLE_EXECUTABLE} -q assemble
#WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
DEPENDS "${CMAKE_CURRENT_BINARY_DIR}/libelektra/test-exclusion.gradle"
"${CMAKE_CURRENT_BINARY_DIR}/version-settings.gradle"
${JNA_BINDING_SOURCE_FILES})

create_lib_symlink (libelektra-${KDB_VERSION}.jar libelektra.jar java-elektra JAVA)
create_lib_symlink (libelektra-${KDB_VERSION}-javadoc.jar libelektra-javadoc.jar java-elektra JAVA)
create_lib_symlink (libelektra-${KDB_VERSION}-sources.jar libelektra-sources.jar java-elektra JAVA)
create_lib_symlink (libelektra-${KDB_VERSION}.pom.xml libelektra.pom.xml java-elektra JAVA)

add_custom_target (jna ALL DEPENDS "${CMAKE_CURRENT_BINARY_DIR}/libelektra/build/libs/libelektra-${KDB_VERSION}.jar")
Expand All @@ -141,31 +130,32 @@ if (Java_JAVAC_EXECUTABLE)
"${CMAKE_CURRENT_BINARY_DIR}/libelektra/build/libs/libelektra-${KDB_VERSION}-javadoc.jar"
"${CMAKE_CURRENT_BINARY_DIR}/libelektra/build/libs/libelektra-${KDB_VERSION}-sources.jar"
"${CMAKE_CURRENT_BINARY_DIR}/libelektra/build/publications/libelektra/libelektra-${KDB_VERSION}.pom.xml"
"${CMAKE_CURRENT_BINARY_DIR}/libelektra/build/publications/libelektra/libelektra-${KDB_VERSION}.module.json"
DESTINATION "share/java"
COMPONENT java-elektra)

# and add maven test to execute with ctest in the testing phase
if ((CMAKE_SYSTEM_NAME STREQUAL "Darwin") AND NOT (CMAKE_SYSTEM_VERSION VERSION_LESS 15))

# we cannot set DYLD_LIBRARY_PATH on new macOS versions, making the kdb tests fail if its not installed in
# the system
set (MAVEN_TEST_ENV "env-test-without-kdb")
else ()
set (MAVEN_TEST_ENV "env-test")
endif ()

if (NOT ENABLE_ASAN)
add_test (
NAME testjna_maven
COMMAND
${MAVEN_EXECUTABLE} -q -B -e -P ${MAVEN_TEST_ENV} test
WORKING_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/libelektra")

set_property (TEST testjna_maven PROPERTY ENVIRONMENT "LD_LIBRARY_PATH=${CMAKE_BINARY_DIR}/lib")

set_property (TEST testjna_maven PROPERTY LABELS kdbtests bindings memleak)
endif (NOT ENABLE_ASAN)
##if ((CMAKE_SYSTEM_NAME STREQUAL "Darwin") AND NOT (CMAKE_SYSTEM_VERSION VERSION_LESS 15))
##
## # we cannot set DYLD_LIBRARY_PATH on new macOS versions, making the kdb tests fail if its not installed in
## # the system
## set (MAVEN_TEST_ENV "env-test-without-kdb")
##else ()
## set (MAVEN_TEST_ENV "env-test")
##endif ()
##
##if (NOT ENABLE_ASAN)
## add_test (
## NAME testjna_maven
## COMMAND
## ${MAVEN_EXECUTABLE} -q -B -e -P ${MAVEN_TEST_ENV} test
## WORKING_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/libelektra")
##
## set_property (TEST testjna_maven PROPERTY ENVIRONMENT "LD_LIBRARY_PATH=${CMAKE_BINARY_DIR}/lib")
##
## set_property (TEST testjna_maven PROPERTY LABELS kdbtests bindings memleak)
##endif (NOT ENABLE_ASAN)
# TODO test execution

else (GRADLE_EXECUTABLE)
exclude_binding (jna "Gradle not found, but required to build the jna bindings")
endif (GRADLE_EXECUTABLE)
Expand All @@ -176,5 +166,4 @@ else (Java_JAVAC_EXECUTABLE)
exclude_binding (jna "javac (java compiler) not found, which is only included in a jdk")
endif (Java_JAVAC_EXECUTABLE)

mark_as_advanced (MAVEN_EXECUTABLE)
mark_as_advanced (GRADLE_EXECUTABLE)
3 changes: 3 additions & 0 deletions src/bindings/jna/libelektra/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,6 @@ publishing {
signing {
sign publishing.publications.libelektra
}

assemble.dependsOn generateMetadataFileForLibelektraPublication
assemble.dependsOn generatePomFileForLibelektraPublication
6 changes: 6 additions & 0 deletions src/bindings/jna/maven-publish.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@ publishing {
}
}

tasks.withType(GenerateMavenPom).all {
def matcher = name =~ /generatePomFileFor(\w+)Publication/
def publicationName = matcher[0][1].toLowerCase()
destination = "$buildDir/publications/${publicationName}/${publicationName}-${version}.pom.xml"
}

// configure signing
signing {
required { project.isReleaseVersion && gradle.taskGraph.hasTask("publish") }
Expand Down

0 comments on commit b25380a

Please sign in to comment.