From 06c84fd58822a899eaaf6a9d19ae62e5c7c72c77 Mon Sep 17 00:00:00 2001 From: Mathieu Baudier Date: Tue, 1 Nov 2022 08:19:45 +0100 Subject: [PATCH] Move JNI from Argeo Commons to Argeo SLC --- Makefile | 16 ++-- Makefile-rcp.mk | 9 +- jni/.cproject | 68 ++++++++++++++ jni/.project | 26 ++++++ jni/.settings/language.settings.xml | 11 +++ jni/.settings/org.eclipse.cdt.core.prefs | 6 ++ jni/Makefile | 14 +++ jni/jni.mk | 58 ++++++++++++ jni/org_argeo_api_uuid_libuuid/.gitignore | 1 + jni/org_argeo_api_uuid_libuuid/Makefile | 8 ++ ...eo_api_uuid_libuuid_DirectLibuuidFactory.c | 8 ++ ...eo_api_uuid_libuuid_DirectLibuuidFactory.h | 21 +++++ ...rg_argeo_api_uuid_libuuid_LibuuidFactory.c | 91 +++++++++++++++++++ ...rg_argeo_api_uuid_libuuid_LibuuidFactory.h | 45 +++++++++ 14 files changed, 370 insertions(+), 12 deletions(-) create mode 100644 jni/.cproject create mode 100644 jni/.project create mode 100644 jni/.settings/language.settings.xml create mode 100644 jni/.settings/org.eclipse.cdt.core.prefs create mode 100644 jni/Makefile create mode 100644 jni/jni.mk create mode 100644 jni/org_argeo_api_uuid_libuuid/.gitignore create mode 100644 jni/org_argeo_api_uuid_libuuid/Makefile create mode 100644 jni/org_argeo_api_uuid_libuuid/org_argeo_api_uuid_libuuid_DirectLibuuidFactory.c create mode 100644 jni/org_argeo_api_uuid_libuuid/org_argeo_api_uuid_libuuid_DirectLibuuidFactory.h create mode 100644 jni/org_argeo_api_uuid_libuuid/org_argeo_api_uuid_libuuid_LibuuidFactory.c create mode 100644 jni/org_argeo_api_uuid_libuuid/org_argeo_api_uuid_libuuid_LibuuidFactory.h diff --git a/Makefile b/Makefile index 1aea6cc2a..08c9add49 100644 --- a/Makefile +++ b/Makefile @@ -1,9 +1,12 @@ include sdk.mk -.PHONY: clean all osgi +.PHONY: clean all osgi jni -all: osgi +all: osgi jni $(MAKE) -f Makefile-rcp.mk all +jni: + $(MAKE) -C jni + A2_CATEGORY = org.argeo.slc BUNDLES = \ @@ -17,10 +20,6 @@ org.argeo.slc.jcr \ swt/rap/org.argeo.tool.rap.cli \ swt/rap/org.argeo.tool.server \ -clean: - rm -rf $(BUILD_BASE) - $(MAKE) -f Makefile-rcp.mk clean - DEP_CATEGORIES = \ org.argeo.tp \ org.argeo.tp.sdk \ @@ -38,6 +37,11 @@ org.argeo.cms.jcr \ swt/org.argeo.cms \ swt/rap/org.argeo.cms \ +clean: + rm -rf $(BUILD_BASE) + $(MAKE) -C jni clean + $(MAKE) -f Makefile-rcp.mk clean + GRAALVM_HOME = /opt/graalvm-ce A2_BUNDLES_CLASSPATH = $(subst $(space),$(pathsep),$(strip $(A2_BUNDLES))) diff --git a/Makefile-rcp.mk b/Makefile-rcp.mk index 24203cc64..c88eacb8f 100644 --- a/Makefile-rcp.mk +++ b/Makefile-rcp.mk @@ -9,12 +9,6 @@ BUNDLES = \ swt/rcp/org.argeo.tool.rcp.cli \ swt/rcp/org.argeo.tool.desktop \ -clean: - rm -rf $(BUILD_BASE) - -A2_OUTPUT = $(SDK_BUILD_BASE)/a2 -A2_BASE = $(A2_OUTPUT) - DEP_CATEGORIES = \ org.argeo.tp \ org.argeo.tp.sdk \ @@ -29,6 +23,9 @@ org.argeo.cms \ swt/org.argeo.cms \ swt/rcp/org.argeo.cms \ +clean: + rm -rf $(BUILD_BASE) + GRAALVM_HOME = /opt/graalvm-ce A2_BUNDLES_CLASSPATH = $(subst $(space),$(pathsep),$(strip $(A2_BUNDLES))) diff --git a/jni/.cproject b/jni/.cproject new file mode 100644 index 000000000..259cf27db --- /dev/null +++ b/jni/.cproject @@ -0,0 +1,68 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + make + + ide + true + true + true + + + + \ No newline at end of file diff --git a/jni/.project b/jni/.project new file mode 100644 index 000000000..492a807be --- /dev/null +++ b/jni/.project @@ -0,0 +1,26 @@ + + + jni-commons + + + + + + org.eclipse.cdt.managedbuilder.core.genmakebuilder + + + + + org.eclipse.cdt.managedbuilder.core.ScannerConfigBuilder + full,incremental, + + + + + + org.eclipse.cdt.core.cnature + org.eclipse.cdt.core.ccnature + org.eclipse.cdt.managedbuilder.core.managedBuildNature + org.eclipse.cdt.managedbuilder.core.ScannerConfigNature + + diff --git a/jni/.settings/language.settings.xml b/jni/.settings/language.settings.xml new file mode 100644 index 000000000..e30ee1672 --- /dev/null +++ b/jni/.settings/language.settings.xml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/jni/.settings/org.eclipse.cdt.core.prefs b/jni/.settings/org.eclipse.cdt.core.prefs new file mode 100644 index 000000000..c8ec5df2d --- /dev/null +++ b/jni/.settings/org.eclipse.cdt.core.prefs @@ -0,0 +1,6 @@ +doxygen/doxygen_new_line_after_brief=true +doxygen/doxygen_use_brief_tag=false +doxygen/doxygen_use_javadoc_tags=true +doxygen/doxygen_use_pre_tag=false +doxygen/doxygen_use_structural_commands=false +eclipse.preferences.version=1 diff --git a/jni/Makefile b/jni/Makefile new file mode 100644 index 000000000..de2b84c19 --- /dev/null +++ b/jni/Makefile @@ -0,0 +1,14 @@ +include ../sdk.mk + +JNIDIRS = org_argeo_api_uuid_libuuid + +.PHONY: clean all + +all: + $(foreach dir, $(JNIDIRS), $(MAKE) -C $(dir);) + +clean: + rm -rf $(BUILD_DIR) $(SDK_BUILD_BASE)/jni + + + diff --git a/jni/jni.mk b/jni/jni.mk new file mode 100644 index 000000000..40dde4469 --- /dev/null +++ b/jni/jni.mk @@ -0,0 +1,58 @@ +TARGET_EXEC := libJava_$(NATIVE_PACKAGE).so + +LDFLAGS = -shared -fPIC -Wl,-soname,$(TARGET_EXEC).$(MAJOR).$(MINOR) $(ADDITIONAL_LIBS) +CFLAGS = -O3 -fPIC + +SRC_DIRS := . + +# +# Generic Argeo +# +BUILD_DIR := $(SDK_BUILD_BASE)/jni/$(NATIVE_PACKAGE) + +# Every folder in ./src will need to be passed to GCC so that it can find header files +INC_DIRS := $(shell find $(SRC_DIRS) -type d) $(JAVA_HOME)/include $(JAVA_HOME)/include/linux $(ADDITIONAL_INCLUDES) + + +.PHONY: clean all ide +all: $(SDK_BUILD_BASE)/jni/$(TARGET_EXEC) + +# Find all the C and C++ files we want to compile +# Note the single quotes around the * expressions. Make will incorrectly expand these otherwise. +SRCS := $(shell find $(SRC_DIRS) -name '*.cpp' -or -name '*.c' -or -name '*.s') + +# String substitution for every C/C++ file. +# As an example, hello.cpp turns into ./build/hello.cpp.o +OBJS := $(SRCS:%=$(BUILD_DIR)/%.o) + +# String substitution (suffix version without %). +# As an example, ./build/hello.cpp.o turns into ./build/hello.cpp.d +DEPS := $(OBJS:.o=.d) + +# Add a prefix to INC_DIRS. So moduleA would become -ImoduleA. GCC understands this -I flag +INC_FLAGS := $(addprefix -I,$(INC_DIRS)) + +# The -MMD and -MP flags together generate Makefiles for us! +# These files will have .d instead of .o as the output. +CPPFLAGS := $(INC_FLAGS) -MMD -MP + +# The final build step. +$(SDK_BUILD_BASE)/jni/$(TARGET_EXEC): $(OBJS) + $(CC) $(OBJS) -o $@ $(LDFLAGS) + +# Build step for C source +$(BUILD_DIR)/%.c.o: %.c + mkdir -p $(dir $@) + $(CC) $(CPPFLAGS) $(CFLAGS) -c $< -o $@ + +# Build step for C++ source +$(BUILD_DIR)/%.cpp.o: %.cpp + mkdir -p $(dir $@) + $(CXX) $(CPPFLAGS) $(CXXFLAGS) -c $< -o $@ + +# Include the .d makefiles. The - at the front suppresses the errors of missing +# Makefiles. Initially, all the .d files will be missing, and we don't want those +# errors to show up. +-include $(DEPS) + +# MAKEFILE_DIR := $(dir $(firstword $(MAKEFILE_LIST))) diff --git a/jni/org_argeo_api_uuid_libuuid/.gitignore b/jni/org_argeo_api_uuid_libuuid/.gitignore new file mode 100644 index 000000000..84c048a73 --- /dev/null +++ b/jni/org_argeo_api_uuid_libuuid/.gitignore @@ -0,0 +1 @@ +/build/ diff --git a/jni/org_argeo_api_uuid_libuuid/Makefile b/jni/org_argeo_api_uuid_libuuid/Makefile new file mode 100644 index 000000000..cfeb1db55 --- /dev/null +++ b/jni/org_argeo_api_uuid_libuuid/Makefile @@ -0,0 +1,8 @@ +NATIVE_PACKAGE := org_argeo_api_uuid_libuuid + +ADDITIONAL_INCLUDES = /usr/include/uuid +ADDITIONAL_LIBS = -luuid + +include ../../sdk.mk +include ../jni.mk + diff --git a/jni/org_argeo_api_uuid_libuuid/org_argeo_api_uuid_libuuid_DirectLibuuidFactory.c b/jni/org_argeo_api_uuid_libuuid/org_argeo_api_uuid_libuuid_DirectLibuuidFactory.c new file mode 100644 index 000000000..a5aeed009 --- /dev/null +++ b/jni/org_argeo_api_uuid_libuuid/org_argeo_api_uuid_libuuid_DirectLibuuidFactory.c @@ -0,0 +1,8 @@ +#include +#include +#include "org_argeo_api_uuid_libuuid_DirectLibuuidFactory.h" + +JNIEXPORT void JNICALL Java_org_argeo_api_uuid_libuuid_DirectLibuuidFactory_timeUUID( + JNIEnv *env, jobject uuidFactory, jobject uuidBuf) { + uuid_generate_time((*env)->GetDirectBufferAddress(env, uuidBuf)); +} diff --git a/jni/org_argeo_api_uuid_libuuid/org_argeo_api_uuid_libuuid_DirectLibuuidFactory.h b/jni/org_argeo_api_uuid_libuuid/org_argeo_api_uuid_libuuid_DirectLibuuidFactory.h new file mode 100644 index 000000000..5f18bf7f1 --- /dev/null +++ b/jni/org_argeo_api_uuid_libuuid/org_argeo_api_uuid_libuuid_DirectLibuuidFactory.h @@ -0,0 +1,21 @@ +/* DO NOT EDIT THIS FILE - it is machine generated */ +#include +/* Header for class org_argeo_api_uuid_libuuid_DirectLibuuidFactory */ + +#ifndef _Included_org_argeo_api_uuid_libuuid_DirectLibuuidFactory +#define _Included_org_argeo_api_uuid_libuuid_DirectLibuuidFactory +#ifdef __cplusplus +extern "C" { +#endif +/* + * Class: org_argeo_api_uuid_libuuid_DirectLibuuidFactory + * Method: timeUUID + * Signature: (Ljava/nio/ByteBuffer;)V + */ +JNIEXPORT void JNICALL Java_org_argeo_api_uuid_libuuid_DirectLibuuidFactory_timeUUID + (JNIEnv *, jobject, jobject); + +#ifdef __cplusplus +} +#endif +#endif diff --git a/jni/org_argeo_api_uuid_libuuid/org_argeo_api_uuid_libuuid_LibuuidFactory.c b/jni/org_argeo_api_uuid_libuuid/org_argeo_api_uuid_libuuid_LibuuidFactory.c new file mode 100644 index 000000000..f97b1f452 --- /dev/null +++ b/jni/org_argeo_api_uuid_libuuid/org_argeo_api_uuid_libuuid_LibuuidFactory.c @@ -0,0 +1,91 @@ +#include +#include +#include "org_argeo_api_uuid_libuuid_LibuuidFactory.h" + +/* + * UTILITIES + */ + +static inline jobject fromBytes(JNIEnv *env, uuid_t out) { + jlong msb = 0; + jlong lsb = 0; + + for (int i = 0; i < 8; i++) + msb = (msb << 8) | (out[i] & 0xff); + for (int i = 8; i < 16; i++) + lsb = (lsb << 8) | (out[i] & 0xff); + + jclass uuidClass = (*env)->FindClass(env, "java/util/UUID"); + jmethodID uuidConstructor = (*env)->GetMethodID(env, uuidClass, "", + "(JJ)V"); + + jobject jUUID = (*env)->AllocObject(env, uuidClass); + (*env)->CallVoidMethod(env, jUUID, uuidConstructor, msb, lsb); + + return jUUID; +} + +static inline void toBytes(JNIEnv *env, jobject jUUID, uuid_t result) { + + jclass uuidClass = (*env)->FindClass(env, "java/util/UUID"); + jmethodID getMostSignificantBits = (*env)->GetMethodID(env, uuidClass, + "getMostSignificantBits", "()J"); + jmethodID getLeastSignificantBits = (*env)->GetMethodID(env, uuidClass, + "getLeastSignificantBits", "()J"); + + jlong msb = (*env)->CallLongMethod(env, jUUID, getMostSignificantBits); + jlong lsb = (*env)->CallLongMethod(env, jUUID, getLeastSignificantBits); + + for (int i = 0; i < 8; i++) + result[i] = (unsigned char) ((msb >> ((7 - i) * 8)) & 0xff); + for (int i = 8; i < 16; i++) + result[i] = (unsigned char) ((lsb >> ((15 - i) * 8)) & 0xff); +} + +/* + * JNI IMPLEMENTATION + */ + +JNIEXPORT jobject JNICALL Java_org_argeo_api_uuid_libuuid_LibuuidFactory_timeUUID( + JNIEnv *env, jobject uuidFactory) { + uuid_t out; + + uuid_generate_time(out); + return fromBytes(env, out); +} + +JNIEXPORT jobject JNICALL Java_org_argeo_api_uuid_libuuid_LibuuidFactory_nameUUIDv5( + JNIEnv *env, jobject uuidFactory, jobject namespaceUuid, + jbyteArray name) { + uuid_t ns; + uuid_t out; + + toBytes(env, namespaceUuid, ns); + jsize length = (*env)->GetArrayLength(env, name); + jbyte *bytes = (*env)->GetByteArrayElements(env, name, 0); + + uuid_generate_sha1(out, ns, bytes, length); + return fromBytes(env, out); +} + +JNIEXPORT jobject JNICALL Java_org_argeo_api_uuid_libuuid_LibuuidFactory_nameUUIDv3( + JNIEnv *env, jobject uuidFactory, jobject namespaceUuid, + jbyteArray name) { + uuid_t ns; + uuid_t out; + + toBytes(env, namespaceUuid, ns); + jsize length = (*env)->GetArrayLength(env, name); + jbyte *bytes = (*env)->GetByteArrayElements(env, name, 0); + + uuid_generate_md5(out, ns, bytes, length); + return fromBytes(env, out); +} + +JNIEXPORT jobject JNICALL Java_org_argeo_api_uuid_libuuid_LibuuidFactory_randomUUIDStrong( + JNIEnv *env, jobject uuidFactory) { + uuid_t out; + + uuid_generate_random(out); + return fromBytes(env, out); +} diff --git a/jni/org_argeo_api_uuid_libuuid/org_argeo_api_uuid_libuuid_LibuuidFactory.h b/jni/org_argeo_api_uuid_libuuid/org_argeo_api_uuid_libuuid_LibuuidFactory.h new file mode 100644 index 000000000..ad0ac5e76 --- /dev/null +++ b/jni/org_argeo_api_uuid_libuuid/org_argeo_api_uuid_libuuid_LibuuidFactory.h @@ -0,0 +1,45 @@ +/* DO NOT EDIT THIS FILE - it is machine generated */ +#include +/* Header for class org_argeo_api_uuid_libuuid_LibuuidFactory */ + +#ifndef _Included_org_argeo_api_uuid_libuuid_LibuuidFactory +#define _Included_org_argeo_api_uuid_libuuid_LibuuidFactory +#ifdef __cplusplus +extern "C" { +#endif +/* + * Class: org_argeo_api_uuid_libuuid_LibuuidFactory + * Method: timeUUID + * Signature: ()Ljava/util/UUID; + */ +JNIEXPORT jobject JNICALL Java_org_argeo_api_uuid_libuuid_LibuuidFactory_timeUUID + (JNIEnv *, jobject); + +/* + * Class: org_argeo_api_uuid_libuuid_LibuuidFactory + * Method: nameUUIDv5 + * Signature: (Ljava/util/UUID;[B)Ljava/util/UUID; + */ +JNIEXPORT jobject JNICALL Java_org_argeo_api_uuid_libuuid_LibuuidFactory_nameUUIDv5 + (JNIEnv *, jobject, jobject, jbyteArray); + +/* + * Class: org_argeo_api_uuid_libuuid_LibuuidFactory + * Method: nameUUIDv3 + * Signature: (Ljava/util/UUID;[B)Ljava/util/UUID; + */ +JNIEXPORT jobject JNICALL Java_org_argeo_api_uuid_libuuid_LibuuidFactory_nameUUIDv3 + (JNIEnv *, jobject, jobject, jbyteArray); + +/* + * Class: org_argeo_api_uuid_libuuid_LibuuidFactory + * Method: randomUUIDStrong + * Signature: ()Ljava/util/UUID; + */ +JNIEXPORT jobject JNICALL Java_org_argeo_api_uuid_libuuid_LibuuidFactory_randomUUIDStrong + (JNIEnv *, jobject); + +#ifdef __cplusplus +} +#endif +#endif -- 2.30.2