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 5ef5ff7 commit 97e66d1
Show file tree
Hide file tree
Showing 8 changed files with 154 additions and 98 deletions.
6 changes: 6 additions & 0 deletions src/bindings/jna/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,9 @@ secret-settings.gradle

# ignore gradle cache
/.gradle

# ignore eclipse project files
.project
.classpath
bin
.settings
69 changes: 46 additions & 23 deletions src/bindings/jna/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +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)
Expand All @@ -8,6 +10,13 @@ macro (test_needs_plugin test plugin)
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)
string (APPEND GRADLE_TEST_EXCLUDES " exclude '**/${test}.class")
endif (plugin_index GREATER -1)
endmacro (java_test_needs_plugin test plugin)

if (Java_JAVAC_EXECUTABLE)

if (NOT Java_VERSION)
Expand All @@ -30,22 +39,25 @@ if (Java_JAVAC_EXECUTABLE)
return ()
endif (NOT BUILD_SHARED)

if (MAVEN_EXECUTABLE) # set by find_program
if (GRADLE_EXECUTABLE) # set by find_program
# TODO check gradle version >= 6.8 < 7
add_binding (jna)

file (MAKE_DIRECTORY libelektra)

if (NOT APPLE)
set (USE_SYSTEM_CLASS_LOADER "<useSystemClassLoader>false</useSystemClassLoader>")
endif (NOT APPLE)

set (MAVEN_TEST_EXCLUDES "")
test_needs_plugin (GOptsTest.java gopts)
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)
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
Expand All @@ -54,15 +66,14 @@ if (Java_JAVAC_EXECUTABLE)
OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/libelektra/libelektra-${KDB_VERSION}.pom.xml"
INPUT "${CMAKE_CURRENT_BINARY_DIR}/libelektra/pom.xml")

set (JNA_BINDING_PREFIX libelektra/src)
# The build system calls Maven 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}/main/${JNA_BINDING_NAMESPACE})
set (JNA_BINDING_SOURCE_DIRECTORY ${JNA_BINDING_PREFIX}/src/main/${JNA_BINDING_NAMESPACE})
set (JNA_BINDING_SOURCE_DIRECTORY_PLUGIN ${JNA_BINDING_SOURCE_DIRECTORY}/plugin)
set (JNA_BINDING_SOURCE_DIRECTORY_EXCEPTION ${JNA_BINDING_SOURCE_DIRECTORY}/exception)
set (JNA_BINDING_SOURCE_DIRECTORY_TEST ${JNA_BINDING_PREFIX}/test/${JNA_BINDING_NAMESPACE})
set (JNA_BINDING_SOURCE_DIRECTORY_TEST ${JNA_BINDING_PREFIX}/src/test/${JNA_BINDING_NAMESPACE})

# The build system calls Maven to recompile the binding, if we change any of the following source files.
set (
JNA_BINDING_SOURCE_FILES
${JNA_BINDING_SOURCE_DIRECTORY}/Elektra.java
Expand All @@ -73,10 +84,12 @@ if (Java_JAVAC_EXECUTABLE)
${JNA_BINDING_SOURCE_DIRECTORY}/KeySetIterator.java
${JNA_BINDING_SOURCE_DIRECTORY}/Plugin.java
${JNA_BINDING_SOURCE_DIRECTORY}/PluginLoader.java
${JNA_BINDING_SOURCE_DIRECTORY_PLUGIN}/NativePlugin.java
${JNA_BINDING_SOURCE_DIRECTORY_PLUGIN}/Echo.java
${JNA_BINDING_SOURCE_DIRECTORY_PLUGIN}/NativePlugin.java
${JNA_BINDING_SOURCE_DIRECTORY_PLUGIN}/PropertiesStorage.java
${JNA_BINDING_SOURCE_DIRECTORY_PLUGIN}/Return.java
${JNA_BINDING_SOURCE_DIRECTORY_EXCEPTION}/mapper/ExceptionMapperService.java
${JNA_BINDING_SOURCE_DIRECTORY_EXCEPTION}/model/WarningEntry.java
${JNA_BINDING_SOURCE_DIRECTORY_EXCEPTION}/ConflictingStateException.java
${JNA_BINDING_SOURCE_DIRECTORY_EXCEPTION}/InstallationException.java
${JNA_BINDING_SOURCE_DIRECTORY_EXCEPTION}/InterfaceException.java
Expand All @@ -90,36 +103,45 @@ if (Java_JAVAC_EXECUTABLE)
${JNA_BINDING_SOURCE_DIRECTORY_EXCEPTION}/SemanticValidationException.java
${JNA_BINDING_SOURCE_DIRECTORY_EXCEPTION}/SyntacticValidationException.java
${JNA_BINDING_SOURCE_DIRECTORY_EXCEPTION}/ValidationException.java
${JNA_BINDING_SOURCE_DIRECTORY_EXCEPTION}/model/WarningEntry.java
${JNA_BINDING_SOURCE_DIRECTORY_EXCEPTION}/mapper/ExceptionMapperService.java
${JNA_BINDING_SOURCE_DIRECTORY_TEST}/exception/model/WarningEntryTest.java
${JNA_BINDING_SOURCE_DIRECTORY_TEST}/AllTests.java
${JNA_BINDING_SOURCE_DIRECTORY_TEST}/ExceptionMapperIT.java
${JNA_BINDING_SOURCE_DIRECTORY_TEST}/PluginLoaderIT.java
${JNA_BINDING_SOURCE_DIRECTORY_TEST}/exception/model/WarningEntryTest.java
${JNA_BINDING_SOURCE_DIRECTORY_TEST}/ExceptionMapperTest.java
${JNA_BINDING_SOURCE_DIRECTORY_TEST}/GOptsTest.java
${JNA_BINDING_SOURCE_DIRECTORY_TEST}/KDBExceptionTest.java
${JNA_BINDING_SOURCE_DIRECTORY_TEST}/KDBTest.java
${JNA_BINDING_SOURCE_DIRECTORY_TEST}/KeySetTest.java
${JNA_BINDING_SOURCE_DIRECTORY_TEST}/KeyTest.java)
${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.
add_custom_command (
OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/libelektra/target/libelektra-${KDB_VERSION}.jar"
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

create_lib_symlink (libelektra-${KDB_VERSION}.jar libelektra.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/target/libelektra-${KDB_VERSION}.jar")
add_custom_target (jna ALL DEPENDS "${CMAKE_CURRENT_BINARY_DIR}/libelektra/build/libs/libelektra-${KDB_VERSION}.jar")

install (
FILES "${CMAKE_CURRENT_BINARY_DIR}/libelektra/target/libelektra-${KDB_VERSION}.jar"
"${CMAKE_CURRENT_BINARY_DIR}/libelektra/libelektra-${KDB_VERSION}.pom.xml"
FILES "${CMAKE_CURRENT_BINARY_DIR}/libelektra/build/libs/libelektra-${KDB_VERSION}.jar"
"${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)

Expand All @@ -144,9 +166,9 @@ if (Java_JAVAC_EXECUTABLE)

set_property (TEST testjna_maven PROPERTY LABELS kdbtests bindings memleak)
endif (NOT ENABLE_ASAN)
else (MAVEN_EXECUTABLE)
exclude_binding (jna "Maven Executable not found, but required to build the jna bindings")
endif (MAVEN_EXECUTABLE)
else (GRADLE_EXECUTABLE)
exclude_binding (jna "Gradle not found, but required to build the jna bindings")
endif (GRADLE_EXECUTABLE)
else ((${Java_VERSION} VERSION_GREATER "1.8.0") OR (${Java_VERSION} VERSION_EQUAL "1.8.0"))
exclude_binding (jna "At least Java 8 is required, but ${Java_VERSION_STRING} was found")
endif ((${Java_VERSION} VERSION_GREATER "1.8.0") OR (${Java_VERSION} VERSION_EQUAL "1.8.0"))
Expand All @@ -155,3 +177,4 @@ else (Java_JAVAC_EXECUTABLE)
endif (Java_JAVAC_EXECUTABLE)

mark_as_advanced (MAVEN_EXECUTABLE)
mark_as_advanced (GRADLE_EXECUTABLE)
23 changes: 21 additions & 2 deletions src/bindings/jna/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,31 @@ plugins {
id 'maven-publish'
}

ext {
version = 'DEV'
allprojects {
version = 'SNAPSHOT'
}

// apply optional version information override
File versionSettings = new File(rootProject.projectDir, 'version-settings.gradle')
if (versionSettings.exists()) {
apply from: versionSettings
}

allprojects {
ext {
groupId = 'org.libelektra'
isReleaseVersion = !project.version.contains("SNAPSHOT")
}
}

// log task graph
gradle.taskGraph.whenReady {taskGraph ->
logger.lifecycle("Found task graph: $taskGraph")
logger.lifecycle("Found ${taskGraph.allTasks.size()} tasks.")
taskGraph.allTasks.forEach { task ->
logger.lifecycle("$task")
task.dependsOn.forEach { dep ->
logger.lifecycle(" - $dep")
}
}
}
51 changes: 45 additions & 6 deletions src/bindings/jna/libelektra/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,49 @@ apply from: "$rootProject.projectDir/java-library.gradle"
apply from: 'dependencies.gradle'

// apply maven publish configuration
ext {
artifactId = 'libelektra'
pomName = "$project.name - $parent.name"
pomDescription = 'Low-level Elektra java binding to elekrify Java applications and write Elektra plugins using Java.'
pomUrl = 'https://www.libelektra.org'
}
apply from: "$rootProject.projectDir/maven-publish.gradle"

// confugure maven-publish plugin
publishing {
publications {
libelektra(MavenPublication) {
groupId rootProject.groupId
artifactId 'libelektra'
version project.version

from components.java

pom {
name = "$project.name - $parent.name"
description = 'Low-level Elektra java binding to elekrify Java applications and write Elektra plugins using Java.'
url = 'https://www.libelektra.org'

licenses {
license {
name = 'BSD 3-Clause License'
url = 'https://github.com/ElektraInitiative/libelektra/blob/master/LICENSE.md'
}
}

//developers {
// developer {
// id = 'johnd'
// name = 'John Doe'
// email = 'john.doe@example.com'
// }
//}

scm {
connection = 'scm:git:git://github.com/ElektraInitiative/libelektra.git'
developerConnection = 'scm:git:ssh://github.com:ElektraInitiative/libelektra.git'
url = 'https://github.com/ElektraInitiative/libelektra'
}
}
}
}
}

// configure signing
signing {
sign publishing.publications.libelektra
}
3 changes: 3 additions & 0 deletions src/bindings/jna/libelektra/test-exclusion.gradle.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
test {
@GRADLE_TEST_EXCLUDES@
}
84 changes: 23 additions & 61 deletions src/bindings/jna/maven-publish.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,10 @@ apply plugin: 'signing'
// export project variables for maven publishing
// if publishing is used wiht this java library, values should be overriden via 'secret-settings.gradle'
ext {
sonartypeUsername='SONATYPE_USER_NAME_NOT_CONFIGURED'
sonartypePassword='SONATYPE_USER_PASSWORD_NOT_CONFIGURED'
signingKeyId='SIGNING_KEY_ID_NOT_CONFIGURED'
sonatypeUsername='SONATYPE_USER_NAME_NOT_CONFIGURED'
sonatypePassword='SONATYPE_USER_PASSWORD_NOT_CONFIGURED'
signingPassword='KEY_PASSWORD_NOT_CONFIGURED'
signingSecretKeyRingFile='/PATH/TO/SECRET/RING/FILE/NOT/CONFIGURED'
signingSecretKey='ASCII_ARMORED_PGP_SECRET_KEY_NOT_CONFIGURED'
}

// apply optional secret settings (used for maven central plublishing)
Expand All @@ -17,63 +16,26 @@ if (secretSettings.exists()) {
apply from: secretSettings
}

// confugure maven-publish plugin (deferred)
subprojects {
publishing {
publications {
mavenJava(MavenPublication) {
groupId "$groupId"
artifactId project.artifactId
version rootProject.version

from components.java

pom {
name = project.pomName
description = project.pomDescription
url = project.pomUrl

licenses {
license {
name = 'BSD 3-Clause License'
url = 'https://github.com/ElektraInitiative/libelektra/blob/master/LICENSE.md'
}
}
// developers {
// developer {
// id = 'johnd'
// name = 'John Doe'
// email = 'john.doe@example.com'
// }
// }
scm {
connection = 'scm:git:git://github.com/ElektraInitiative/libelektra.git'
developerConnection = 'scm:git:ssh://github.com:ElektraInitiative/libelektra.git'
url = 'https://github.com/ElektraInitiative/libelektra'
}
}
}
}
repositories {
maven {
def releasesRepoUrl = 'https://s01.oss.sonatype.org/service/local/staging/deploy/maven2/'
def snapshotsRepoUrl = 'https://s01.oss.sonatype.org/content/repositories/snapshots/'
url = version.endsWith('SNAPSHOT') ? snapshotsRepoUrl : releasesRepoUrl

credentials {
username project.sonatypeUsername
password project.sonatypePassword
}
}
}
}

// configure signing
signing {
sign publishing.publications.mavenJava
// confugure maven-publish plugin
publishing {
repositories {
maven {
def releasesRepoUrl = 'https://s01.oss.sonatype.org/service/local/staging/deploy/maven2/'
def snapshotsRepoUrl = 'https://s01.oss.sonatype.org/content/repositories/snapshots/'
url = version.endsWith('SNAPSHOT') ? snapshotsRepoUrl : releasesRepoUrl

credentials {
username project.sonatypeUsername
password project.sonatypePassword
}
}
}
}

keyId = project.signingKeyId
password = signingPassword
secretKeyRingFile = signingSecretKeyRingFile
// configure signing
signing {
required { project.isReleaseVersion && gradle.taskGraph.hasTask("publish") }
if ( project.isReleaseVersion ) {
useInMemoryPgpKeys(project.signingSecretKey, project.signingPassword)
}
}
12 changes: 7 additions & 5 deletions src/bindings/jna/secret-settings.gradle.in
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// set secrets used for maven central publishing
project.sonartypeUsername='YOUR_SONATYPE_USER_NAME'
project.sonartypePassword='YOUR_SONATYPE_USER_PASSWORD'
project.signingKeyId='KEY_ID'
// set secrets used for maven central publishing (sonatype account credentials)
project.sonatypeUsername='YOUR_SONATYPE_USER_NAME'
project.sonatypePassword='YOUR_SONATYPE_USER_PASSWORD'

// set passphrase protected signing key
// you can use the following command for expoprting the secret key: gpg --armor --export-secret-keys C1E5DC2707235980 | awk '{print}' ORS='\\n' >> secret-settings.gradle
project.signingPassword='KEY_PASSWORD'
project.signingSecretKeyRingFile='/PATH/TO/SECRET/RING/FILE'
project.signingSecretKey='ASCII_ARMORED_PGP_SECRET_KEY'
4 changes: 3 additions & 1 deletion src/bindings/jna/version-settings.gradle.in
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
// override artifact version
rootProject.artifactVersion = '@KDB_VERSION@'
allprojects {
version = '@KDB_VERSION@'
}

0 comments on commit 97e66d1

Please sign in to comment.