diff --git a/.gitignore b/.gitignore index c3b5630408..b9d48d3e6a 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,6 @@ build/ +# the maven convention for built files +target/ # Prerequisites *.d @@ -13,11 +15,6 @@ build/ *.gch *.pch -# Compiled Dynamic libraries -*.so -*.dylib -*.dll - # Fortran module files *.mod *.smod @@ -32,3 +29,10 @@ build/ *.exe *.out *.app + +# IDE files: IntellIJ +.idea/ +*.iml + +# IDE files: eclipse +.settings/ diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000000..03fc536c2e --- /dev/null +++ b/.travis.yml @@ -0,0 +1,75 @@ +language: java + +cache: + directories: + - $HOME/.m2 + - $HOME/.jabba/jdk + +before_cache: + - rm -rf $HOME/.m2/repository/com/github/java-native + +matrix: + include: + - os: linux + addons: + apt: + packages: + - cmake3 + - g++ + - os: linux + env: PROFILE=m32 + addons: + apt: + packages: + - cmake3 + - g++-multilib + - os: linux + env: PROFILE=mingw32 + addons: + apt: + packages: + - cmake3 + - g++-mingw-w64-i686 + - os: linux + env: PROFILE=mingw64 + addons: + apt: + packages: + - cmake3 + - g++-mingw-w64-x86-64 + - os: linux + env: PROFILE=armsf,armtrusty + addons: + apt: + packages: + - cmake3 + - g++-arm-linux-gnueabi + - os: linux + env: PROFILE=aarch64 + addons: + apt: + packages: + - cmake3 + - g++-aarch64-linux-gnu + - os: linux + env: PROFILE=ppc64 + addons: + apt: + packages: + - cmake3 + - g++-powerpc64le-linux-gnu + - os: osx + addons: + homebrew: + packages: + - cmake + +install: + - mvn dependency:resolve + +script: if [ -z "$PROFILE" ]; then mvn --batch-mode; else mvn -P "$PROFILE" --batch-mode; fi + +after_success: + - bash <(curl -s https://codecov.io/bash) + + diff --git a/CMakeLists.txt b/CMakeLists.txt index b13043e56c..75dd26733c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,5 +1,8 @@ -cmake_minimum_required(VERSION 3.9) -project(jssc VERSION 2.8.0 DESCRIPTION "Java Simple Serial Connector") +cmake_minimum_required(VERSION 3.0) +cmake_policy(SET CMP0048 NEW) +cmake_policy(SET CMP0042 NEW) + +project(jssc VERSION 2.7.1 LANGUAGES CXX) find_package(Java) find_package(JNI) @@ -56,15 +59,60 @@ else() set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -O2") endif() -add_library(jssc SHARED - src/cpp/${JSSC_PLATFORM}/jssc.cpp -) +# Detect platform if -DNATIVE_LIB_DIR is not provided +# TODO: Handle arm, hardfloat, etc +if(NOT NATIVE_LIB_DIR) + # windows, linux, darwin, etc + string(TOLOWER "${CMAKE_SYSTEM_NAME}" OS_NAME) + if(OS_NAME MATCHES "darwin") + set(OS_NAME "osx") + endif() + + # 32-bit or 64-bit + #FIXME: Might fail on cross-compile + if("${CMAKE_SIZEOF_VOID_P}" EQUAL "8") + set(OS_BITS 64) + else() + set(OS_BITS 32) + endif() + SET(NATIVE_LIB_DIR ${OS_NAME}_${OS_BITS}) +endif() +set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/natives/${NATIVE_LIB_DIR}) + +# version.h using #cmakedefine for version from pom.xml. +set(JSSC_VERSION "0.0.0-UNKNOWN") +file(STRINGS ${CMAKE_CURRENT_SOURCE_DIR}/pom.xml POM_FILE) +foreach(POM_LINE ${POM_FILE}) + # Assume first "" is the project version + if(POM_LINE MATCHES "") + string(REGEX REPLACE "^[ \t]+|<[^>]*>" "" DETECTED_VERSION "${POM_LINE}") + string(STRIP "${DETECTED_VERSION}" DETECTED_VERSION) + if(DETECTED_VERSION STREQUAL "") + MESSAGE(WARNING "Could not parse version from pom.xml, defaulting to \"${JSSC_VERSION}\"") + else() + SET(JSSC_VERSION "${DETECTED_VERSION}") + MESSAGE(STATUS "Found version \"${JSSC_VERSION}\" in pom.xml") + endif() + break() + endif() +endforeach() +configure_file(src/cpp/version.h.in ${CMAKE_CURRENT_BINARY_DIR}/version.h @ONLY) + +add_library(jssc SHARED src/cpp/${JSSC_PLATFORM}/jssc.cpp) -target_include_directories(jssc PRIVATE ${JNI_INCLUDE_DIRS} ${JSSC_ADDITIONAL_INCLUDES}) +target_include_directories(jssc PRIVATE ${JNI_INCLUDE_DIRS} ${CMAKE_CURRENT_BINARY_DIR} ${JSSC_ADDITIONAL_INCLUDES}) -set_target_properties(jssc PROPERTIES VERSION ${PROJECT_VERSION}) -set_target_properties(jssc PROPERTIES PUBLIC_HEADER src/jssc_SerialNativeInterface.h) +set_target_properties(jssc PROPERTIES PUBLIC_HEADER ${CMAKE_CURRENT_BINARY_DIR}/jssc_SerialNativeInterface.h) set_target_properties(jssc PROPERTIES POSITION_INDEPENDENT_CODE ON) +if(WIN32) + # Fix paths for MSVC (Debug/Release) and MINGW + set_target_properties(jssc PROPERTIES RUNTIME_OUTPUT_DIRECTORY_DEBUG "${CMAKE_LIBRARY_OUTPUT_DIRECTORY}") + set_target_properties(jssc PROPERTIES RUNTIME_OUTPUT_DIRECTORY_RELEASE "${CMAKE_LIBRARY_OUTPUT_DIRECTORY}") +endif() +if(FORCE_M32) + # Build 32-bit binary on Linux + set_target_properties(jssc PROPERTIES COMPILE_FLAGS -m32 LINK_FLAGS -m32) +endif() # Call strip on non-debug builds if(CMAKE_STRIP AND NOT CMAKE_BUILD_TYPE MATCHES "Deb") @@ -73,3 +121,12 @@ if(CMAKE_STRIP AND NOT CMAKE_BUILD_TYPE MATCHES "Deb") endif() add_custom_command(TARGET jssc POST_BUILD COMMAND "${CMAKE_STRIP}" ${STRIP_ARGS} $) endif() + +# Handle compiler warnings +if(MSVC) + #TODO Treat warnings as errors /WX + target_compile_options(jssc PRIVATE /W4) +else() + #TODO Treat warnings as errors -Werror + target_compile_options(jssc PRIVATE -Wall -Wextra -pedantic) +endif() diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000000..4497e4e65c --- /dev/null +++ b/pom.xml @@ -0,0 +1,421 @@ + + + 4.0.0 + + io.github.java-native.jssc + jssc + 2.8.1-SNAPSHOT + + + 1.6 + 1.6 + 6 + UTF-8 + + + ${project.build.directory}/cmake + + + false + + + + false + false + ${os.detected.name}-${os.detected.arch}-${os.detected.bitness} + + ${sun.arch.data.model} + + + 4.12 + 1.2.3 + 2.3.3 + + + 1.17 + 1.8 + 3.1.1 + 3.1.1 + 3.6.0 + 1.6.2 + 1.1 + 3.0.0-M3 + + + + Java Native + https://github.com/java-native/ + + + + + GNU LGPL 3 + http://www.gnu.org/licenses/lgpl.txt + repo + + + + + + org.scijava + native-lib-loader + ${dependency.nativelibloader.version} + + + junit + junit + ${dependency.junit.version} + test + + + ch.qos.logback + logback-classic + ${dependency.logback.version} + test + + + + + install + + src/java + + + ${project.basedir}/src/main/resources-precompiled + + + + + + kr.motd.maven + os-maven-plugin + ${plugin.osmaven.version} + + + + + + + com.github.maven-nar + nar-maven-plugin + ${plugin.nar.version} + true + + + default-nar-javah + + nar-javah + + compile + + ${javah.skip} + + + + + + + + org.apache.maven.plugins + maven-antrun-plugin + ${plugin.antrun.version} + + + + cmake-generate + run + generate-sources + + ${cmake.generate.skip} + + + + + + + + + + + + + cmake-compile + run + compile + + ${cmake.compile.skip} + + + + + + + + + + + + + + + + + + + org.apache.maven.plugins + maven-surefire-plugin + ${plugin.surfire.version} + + + + org.apache.maven.plugins + maven-jar-plugin + ${plugin.jar.version} + + + + + org.apache.maven.plugins + maven-assembly-plugin + ${plugin.assembly.version} + + + + make-assembly + package + + single + + + ${cmake.generate.skip} + + ${project.basedir}/src/assembly/one-off-jar.xml + + + + + + + + + org.codehaus.mojo + animal-sniffer-maven-plugin + ${plugin.animalsniffer.version} + + + org.codehaus.mojo.signature + java16 + ${plugin.signature.version} + + + + + ensure-java-1.6-class-library + verify + + check + + + + + + + + + + + + java-9 + + [9,) + + + true + + + + + + maven-compiler-plugin + + + -h + ${cmake.generated.directory} + + + + + + + + + + + package + + true + true + true + true + + + + + + m32 + + x86_32 + 32 + + true + -DNATIVE_LIB_DIR=${os.detected.name}_${os.detected.bitness} + -DFORCE_M32=True + + + + + + win64 + + + Windows + amd64 + + + + -DCMAKE_GENERATOR_PLATFORM=x64 -DNATIVE_LIB_DIR=${os.detected.name}_${os.detected.bitness} + --config Release + + + + + + win32 + + + Windows + x86 + + + + -DNATIVE_LIB_DIR=${os.detected.name}_${os.detected.bitness} + --config Release + + + + + + mingw32 + + windows + x86_32 + 32 + + true + -DCMAKE_TOOLCHAIN_FILE=toolchain/Mingw${os.detected.bitness}.cmake -DNATIVE_LIB_DIR=${os.detected.name}_${os.detected.bitness} + + + + + + mingw64 + + windows + x86_64 + 64 + + true + -DCMAKE_TOOLCHAIN_FILE=toolchain/Mingw${os.detected.bitness}.cmake -DNATIVE_LIB_DIR=${os.detected.name}_${os.detected.bitness} + + + + + + armsf + + linux + arm_32 + 32 + + true + -DCMAKE_TOOLCHAIN_FILE=toolchain/Armsf.cmake -DNATIVE_LIB_DIR=${os.detected.name}_arm + + + + + + armhf + + linux + arm_32 + 32 + + true + + -DCMAKE_TOOLCHAIN_FILE=toolchain/Armhf.cmake -DNATIVE_LIB_DIR=${os.detected.name}_arm + + + + + + aarch64 + + linux + aarch_64 + 64 + + true + -DCMAKE_TOOLCHAIN_FILE=toolchain/Aarch64.cmake -DNATIVE_LIB_DIR=${os.detected.name}_arm64 + + + + + + ppc64 + + linux + ppc_64 + 64 + + true + -DCMAKE_TOOLCHAIN_FILE=toolchain/Ppc64.cmake -DNATIVE_LIB_DIR=${os.detected.name}_ppc + + + + + + armtrusty + + + -DSKIP_COMPILER_VERSION=True + + + + + + + arm-fix32 + + + arm + + + + -DNATIVE_LIB_DIR=${os.detected.name}_arm + + + + arm-fix64 + + + aarch64 + + + + -DNATIVE_LIB_DIR=${os.detected.name}_arm64 + + + + diff --git a/src/assembly/one-off-jar.xml b/src/assembly/one-off-jar.xml new file mode 100644 index 0000000000..ccd915b038 --- /dev/null +++ b/src/assembly/one-off-jar.xml @@ -0,0 +1,37 @@ + + + ${maven.assembly.id} + + + jar + + + false + + + + / + true + true + false + true + compile + + ${project.groupId}:${project.artifactId} + + + + + + + ${cmake.generated.directory}/natives + natives + + + + diff --git a/src/cpp/_nix_based/jssc.cpp b/src/cpp/_nix_based/jssc.cpp index 634b620187..2a96c9f4e6 100644 --- a/src/cpp/_nix_based/jssc.cpp +++ b/src/cpp/_nix_based/jssc.cpp @@ -44,15 +44,16 @@ #endif #include -#include "../jssc_SerialNativeInterface.h" +#include +#include "version.h" //#include //-lCstd use for Solaris linker /* * Get native library version */ -JNIEXPORT jstring JNICALL Java_jssc_SerialNativeInterface_getNativeLibraryVersion(JNIEnv *env, jobject object) { - return env->NewStringUTF(jSSC_NATIVE_LIB_VERSION); +JNIEXPORT jstring JNICALL Java_jssc_SerialNativeInterface_getNativeLibraryVersion(JNIEnv *env, jclass clazz) { + return env->NewStringUTF(JSSC_VERSION); } /* OK */ diff --git a/src/cpp/jssc_SerialNativeInterface.h b/src/cpp/jssc_SerialNativeInterface.h deleted file mode 100644 index 7029b1bbee..0000000000 --- a/src/cpp/jssc_SerialNativeInterface.h +++ /dev/null @@ -1,201 +0,0 @@ -/* jSSC (Java Simple Serial Connector) - serial port communication library. - * © Alexey Sokolov (scream3r), 2010-2014. - * - * This file is part of jSSC. - * - * jSSC is free software: you can redistribute it and/or modify - * it under the terms of the GNU Lesser General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * jSSC is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with jSSC. If not, see . - * - * If you use jSSC in public project you can inform me about this by e-mail, - * of course if you want it. - * - * e-mail: scream3r.org@gmail.com - * web-site: http://scream3r.org | http://code.google.com/p/java-simple-serial-connector/ - */ -/* DO NOT EDIT THIS FILE - it is machine generated */ -#include -/* Header for class jssc_SerialNativeInterface */ - -#ifndef _Included_jssc_SerialNativeInterface -#define _Included_jssc_SerialNativeInterface -#ifdef __cplusplus -extern "C" { -#endif - -#undef jSSC_NATIVE_LIB_VERSION -#define jSSC_NATIVE_LIB_VERSION "2.8" - -#undef jssc_SerialNativeInterface_OS_LINUX -#define jssc_SerialNativeInterface_OS_LINUX 0L -#undef jssc_SerialNativeInterface_OS_WINDOWS -#define jssc_SerialNativeInterface_OS_WINDOWS 1L -#undef jssc_SerialNativeInterface_OS_SOLARIS -#define jssc_SerialNativeInterface_OS_SOLARIS 2L -#undef jssc_SerialNativeInterface_OS_MAC_OS_X -#define jssc_SerialNativeInterface_OS_MAC_OS_X 3L -#undef jssc_SerialNativeInterface_ERR_PORT_BUSY -#define jssc_SerialNativeInterface_ERR_PORT_BUSY -1LL -#undef jssc_SerialNativeInterface_ERR_PORT_NOT_FOUND -#define jssc_SerialNativeInterface_ERR_PORT_NOT_FOUND -2LL -#undef jssc_SerialNativeInterface_ERR_PERMISSION_DENIED -#define jssc_SerialNativeInterface_ERR_PERMISSION_DENIED -3LL -#undef jssc_SerialNativeInterface_ERR_INCORRECT_SERIAL_PORT -#define jssc_SerialNativeInterface_ERR_INCORRECT_SERIAL_PORT -4LL -/* - * Class: jssc_SerialNativeInterface - * Method: getNativeLibraryVersion - * Signature: ()Ljava/lang/String; - */ -JNIEXPORT jstring JNICALL Java_jssc_SerialNativeInterface_getNativeLibraryVersion - (JNIEnv *, jobject); - -/* - * Class: jssc_SerialNativeInterface - * Method: openPort - * Signature: (Ljava/lang/String;Z)J - */ -JNIEXPORT jlong JNICALL Java_jssc_SerialNativeInterface_openPort - (JNIEnv *, jobject, jstring, jboolean); - -/* - * Class: jssc_SerialNativeInterface - * Method: setParams - * Signature: (JIIIIZZI)Z - */ -JNIEXPORT jboolean JNICALL Java_jssc_SerialNativeInterface_setParams - (JNIEnv *, jobject, jlong, jint, jint, jint, jint, jboolean, jboolean, jint); - -/* - * Class: jssc_SerialNativeInterface - * Method: purgePort - * Signature: (JI)Z - */ -JNIEXPORT jboolean JNICALL Java_jssc_SerialNativeInterface_purgePort - (JNIEnv *, jobject, jlong, jint); - -/* - * Class: jssc_SerialNativeInterface - * Method: closePort - * Signature: (J)Z - */ -JNIEXPORT jboolean JNICALL Java_jssc_SerialNativeInterface_closePort - (JNIEnv *, jobject, jlong); - -/* - * Class: jssc_SerialNativeInterface - * Method: setEventsMask - * Signature: (JI)Z - */ -JNIEXPORT jboolean JNICALL Java_jssc_SerialNativeInterface_setEventsMask - (JNIEnv *, jobject, jlong, jint); - -/* - * Class: jssc_SerialNativeInterface - * Method: getEventsMask - * Signature: (J)I - */ -JNIEXPORT jint JNICALL Java_jssc_SerialNativeInterface_getEventsMask - (JNIEnv *, jobject, jlong); - -/* - * Class: jssc_SerialNativeInterface - * Method: waitEvents - * Signature: (J)[[I - */ -JNIEXPORT jobjectArray JNICALL Java_jssc_SerialNativeInterface_waitEvents - (JNIEnv *, jobject, jlong); - -/* - * Class: jssc_SerialNativeInterface - * Method: setRTS - * Signature: (JZ)Z - */ -JNIEXPORT jboolean JNICALL Java_jssc_SerialNativeInterface_setRTS - (JNIEnv *, jobject, jlong, jboolean); - -/* - * Class: jssc_SerialNativeInterface - * Method: setDTR - * Signature: (JZ)Z - */ -JNIEXPORT jboolean JNICALL Java_jssc_SerialNativeInterface_setDTR - (JNIEnv *, jobject, jlong, jboolean); - -/* - * Class: jssc_SerialNativeInterface - * Method: readBytes - * Signature: (JI)[B - */ -JNIEXPORT jbyteArray JNICALL Java_jssc_SerialNativeInterface_readBytes - (JNIEnv *, jobject, jlong, jint); - -/* - * Class: jssc_SerialNativeInterface - * Method: writeBytes - * Signature: (J[B)Z - */ -JNIEXPORT jboolean JNICALL Java_jssc_SerialNativeInterface_writeBytes - (JNIEnv *, jobject, jlong, jbyteArray); - -/* - * Class: jssc_SerialNativeInterface - * Method: getBuffersBytesCount - * Signature: (J)[I - */ -JNIEXPORT jintArray JNICALL Java_jssc_SerialNativeInterface_getBuffersBytesCount - (JNIEnv *, jobject, jlong); - -/* - * Class: jssc_SerialNativeInterface - * Method: setFlowControlMode - * Signature: (JI)Z - */ -JNIEXPORT jboolean JNICALL Java_jssc_SerialNativeInterface_setFlowControlMode - (JNIEnv *, jobject, jlong, jint); - -/* - * Class: jssc_SerialNativeInterface - * Method: getFlowControlMode - * Signature: (J)I - */ -JNIEXPORT jint JNICALL Java_jssc_SerialNativeInterface_getFlowControlMode - (JNIEnv *, jobject, jlong); - -/* - * Class: jssc_SerialNativeInterface - * Method: getSerialPortNames - * Signature: ()[Ljava/lang/String; - */ -JNIEXPORT jobjectArray JNICALL Java_jssc_SerialNativeInterface_getSerialPortNames - (JNIEnv *, jobject); - -/* - * Class: jssc_SerialNativeInterface - * Method: getLinesStatus - * Signature: (J)[I - */ -JNIEXPORT jintArray JNICALL Java_jssc_SerialNativeInterface_getLinesStatus - (JNIEnv *, jobject, jlong); - -/* - * Class: jssc_SerialNativeInterface - * Method: sendBreak - * Signature: (JI)Z - */ -JNIEXPORT jboolean JNICALL Java_jssc_SerialNativeInterface_sendBreak - (JNIEnv *, jobject, jlong, jint); - -#ifdef __cplusplus -} -#endif -#endif diff --git a/src/cpp/version.h.in b/src/cpp/version.h.in new file mode 100644 index 0000000000..07985fe1d6 --- /dev/null +++ b/src/cpp/version.h.in @@ -0,0 +1 @@ +#cmakedefine JSSC_VERSION "@JSSC_VERSION@" diff --git a/src/cpp/windows/jssc.cpp b/src/cpp/windows/jssc.cpp index b247187461..808be39f96 100644 --- a/src/cpp/windows/jssc.cpp +++ b/src/cpp/windows/jssc.cpp @@ -25,7 +25,8 @@ #include #include #include -#include "../jssc_SerialNativeInterface.h" +#include +#include "version.h" //#include @@ -34,8 +35,8 @@ /* * Get native library version */ -JNIEXPORT jstring JNICALL Java_jssc_SerialNativeInterface_getNativeLibraryVersion(JNIEnv *env, jobject object) { - return env->NewStringUTF(jSSC_NATIVE_LIB_VERSION); +JNIEXPORT jstring JNICALL Java_jssc_SerialNativeInterface_getNativeLibraryVersion(JNIEnv *env, jclass clazz) { + return env->NewStringUTF(JSSC_VERSION); } /* diff --git a/src/java/jssc/SerialNativeInterface.java b/src/java/jssc/SerialNativeInterface.java index c5264f5e8a..fd96e8babc 100644 --- a/src/java/jssc/SerialNativeInterface.java +++ b/src/java/jssc/SerialNativeInterface.java @@ -24,11 +24,9 @@ */ package jssc; -import java.io.BufferedReader; -import java.io.File; -import java.io.FileOutputStream; -import java.io.InputStream; -import java.io.InputStreamReader; +import org.scijava.nativelib.NativeLoader; + +import java.io.IOException; /** * @@ -77,185 +75,11 @@ public class SerialNativeInterface { public static final String PROPERTY_JSSC_PARMRK = "JSSC_PARMRK"; static { - String libFolderPath; - String libName; - - String osName = System.getProperty("os.name"); - String architecture = System.getProperty("os.arch"); - String userHome = System.getProperty("user.home"); - String fileSeparator = System.getProperty("file.separator"); - String tmpFolder = System.getProperty("java.io.tmpdir"); - - //since 2.3.0 -> - String libRootFolder = new File(userHome).canWrite() ? userHome : tmpFolder; - //<- since 2.3.0 - - String javaLibPath = System.getProperty("java.library.path");//since 2.1.0 - - if(osName.equals("Linux")){ - osName = "linux"; - osType = OS_LINUX; - } - else if(osName.startsWith("Win")){ - osName = "windows"; - osType = OS_WINDOWS; - }//since 0.9.0 -> - else if(osName.equals("SunOS")){ - osName = "solaris"; - osType = OS_SOLARIS; - } - else if(osName.equals("Mac OS X") || osName.equals("Darwin")){//os.name "Darwin" since 2.6.0 - osName = "mac_os_x"; - osType = OS_MAC_OS_X; - }//<- since 0.9.0 - - if(architecture.equals("i386") || architecture.equals("i686")){ - architecture = "x86"; - } - else if(architecture.equals("amd64") || architecture.equals("universal")){//os.arch "universal" since 2.6.0 - architecture = "x86_64"; - } - else if(architecture.equals("arm")) {//since 2.1.0 - String floatStr = "sf"; - if(javaLibPath.toLowerCase().contains("gnueabihf") || javaLibPath.toLowerCase().contains("armhf")){ - floatStr = "hf"; - } - else { - try { - Process readelfProcess = Runtime.getRuntime().exec("readelf -A /proc/self/exe"); - BufferedReader reader = new BufferedReader(new InputStreamReader(readelfProcess.getInputStream())); - String buffer = ""; - while((buffer = reader.readLine()) != null && !buffer.isEmpty()){ - if(buffer.toLowerCase().contains("Tag_ABI_VFP_args".toLowerCase())){ - floatStr = "hf"; - break; - } - } - reader.close(); - } - catch (Exception ex) { - //Do nothing - } - } - architecture = "arm" + floatStr; - } - - libFolderPath = libRootFolder + fileSeparator + ".jssc" + fileSeparator + osName; - libName = "jSSC-" + libVersion + "_" + architecture; - libName = System.mapLibraryName(libName); - - if(libName.endsWith(".dylib")){//Since 2.1.0 MacOSX 10.8 fix - libName = libName.replace(".dylib", ".jnilib"); - } - - boolean loadLib = false; - - if(isLibFolderExist(libFolderPath)){ - if(isLibFileExist(libFolderPath + fileSeparator + libName)){ - loadLib = true; - } - else { - if(extractLib((libFolderPath + fileSeparator + libName), osName, libName)){ - loadLib = true; - } - } - } - else { - if(new File(libFolderPath).mkdirs()){ - if(extractLib((libFolderPath + fileSeparator + libName), osName, libName)){ - loadLib = true; - } - } - } - - if (loadLib) { - System.load(libFolderPath + fileSeparator + libName); - String versionBase = getLibraryBaseVersion(); - String versionNative = getNativeLibraryVersion(); - if (!versionBase.equals(versionNative)) { - System.err.println("Warning! jSSC Java and Native versions mismatch (Java: " + versionBase + ", Native: " + versionNative + ")"); - } - } - } - - /** - * Is library folder exists - * - * @param libFolderPath - * - * @since 0.8 - */ - private static boolean isLibFolderExist(String libFolderPath) { - boolean returnValue = false; - File folder = new File(libFolderPath); - if(folder.exists() && folder.isDirectory()){ - returnValue = true; - } - return returnValue; - } - - /** - * Is library file exists - * - * @param libFilePath - * - * @since 0.8 - */ - private static boolean isLibFileExist(String libFilePath) { - boolean returnValue = false; - File folder = new File(libFilePath); - if(folder.exists() && folder.isFile()){ - returnValue = true; - } - return returnValue; - } - - /** - * Extract lib to lib folder - * - * @param libFilePath - * @param osName - * @param libName - * - * @since 0.8 - */ - private static boolean extractLib(String libFilePath, String osName, String libName) { - boolean returnValue = false; - File libFile = new File(libFilePath); - InputStream input = null; - FileOutputStream output = null; - input = SerialNativeInterface.class.getResourceAsStream("/libs/" + osName + "/" + libName); - if(input != null){ - int read; - byte[] buffer = new byte[4096]; - try { - output = new FileOutputStream(libFilePath); - while((read = input.read(buffer)) != -1){ - output.write(buffer, 0, read); - } - output.close(); - input.close(); - returnValue = true; - } - catch (Exception ex) { - try { - output.close(); - if(libFile.exists()){ - libFile.delete(); - } - } - catch (Exception ex_out) { - //Do nothing - } - try { - input.close(); - } - catch (Exception ex_in) { - //Do nothing - } - } + try { + NativeLoader.loadLibrary("jssc"); + } catch (IOException ioException) { + throw new UnsatisfiedLinkError("Could not load the jssc library: " + ioException.getMessage()); } - return returnValue; } /** diff --git a/src/java/libs/linux/libjSSC-2.8_armhf.so b/src/java/libs/linux/libjSSC-2.8_armhf.so deleted file mode 100644 index 3742fbe08f..0000000000 Binary files a/src/java/libs/linux/libjSSC-2.8_armhf.so and /dev/null differ diff --git a/src/java/libs/linux/libjSSC-2.8_armsf.so b/src/java/libs/linux/libjSSC-2.8_armsf.so deleted file mode 100644 index 894ec80876..0000000000 Binary files a/src/java/libs/linux/libjSSC-2.8_armsf.so and /dev/null differ diff --git a/src/java/libs/linux/libjSSC-2.8_x86.so b/src/java/libs/linux/libjSSC-2.8_x86.so deleted file mode 100644 index 8ea5d6e21e..0000000000 Binary files a/src/java/libs/linux/libjSSC-2.8_x86.so and /dev/null differ diff --git a/src/java/libs/linux/libjSSC-2.8_x86_64.so b/src/java/libs/linux/libjSSC-2.8_x86_64.so deleted file mode 100644 index 1108cb5f28..0000000000 Binary files a/src/java/libs/linux/libjSSC-2.8_x86_64.so and /dev/null differ diff --git a/src/java/libs/mac_os_x/libjSSC-2.8_ppc.jnilib b/src/java/libs/mac_os_x/libjSSC-2.8_ppc.jnilib deleted file mode 100644 index bdba1fca85..0000000000 Binary files a/src/java/libs/mac_os_x/libjSSC-2.8_ppc.jnilib and /dev/null differ diff --git a/src/java/libs/mac_os_x/libjSSC-2.8_ppc64.jnilib b/src/java/libs/mac_os_x/libjSSC-2.8_ppc64.jnilib deleted file mode 100644 index f32ae9be7d..0000000000 Binary files a/src/java/libs/mac_os_x/libjSSC-2.8_ppc64.jnilib and /dev/null differ diff --git a/src/java/libs/mac_os_x/libjSSC-2.8_x86.jnilib b/src/java/libs/mac_os_x/libjSSC-2.8_x86.jnilib deleted file mode 100644 index 103909220c..0000000000 Binary files a/src/java/libs/mac_os_x/libjSSC-2.8_x86.jnilib and /dev/null differ diff --git a/src/java/libs/mac_os_x/libjSSC-2.8_x86_64.jnilib b/src/java/libs/mac_os_x/libjSSC-2.8_x86_64.jnilib deleted file mode 100644 index 10dc27e43e..0000000000 Binary files a/src/java/libs/mac_os_x/libjSSC-2.8_x86_64.jnilib and /dev/null differ diff --git a/src/java/libs/solaris/libjSSC-2.8_x86.so b/src/java/libs/solaris/libjSSC-2.8_x86.so deleted file mode 100644 index 3e6a3a3d53..0000000000 Binary files a/src/java/libs/solaris/libjSSC-2.8_x86.so and /dev/null differ diff --git a/src/java/libs/solaris/libjSSC-2.8_x86_64.so b/src/java/libs/solaris/libjSSC-2.8_x86_64.so deleted file mode 100644 index b67dccbaeb..0000000000 Binary files a/src/java/libs/solaris/libjSSC-2.8_x86_64.so and /dev/null differ diff --git a/src/java/libs/windows/jSSC-2.8_x86.dll b/src/java/libs/windows/jSSC-2.8_x86.dll deleted file mode 100644 index 8ff7541a02..0000000000 Binary files a/src/java/libs/windows/jSSC-2.8_x86.dll and /dev/null differ diff --git a/src/java/libs/windows/jSSC-2.8_x86_64.dll b/src/java/libs/windows/jSSC-2.8_x86_64.dll deleted file mode 100644 index d4e19e069f..0000000000 Binary files a/src/java/libs/windows/jSSC-2.8_x86_64.dll and /dev/null differ diff --git a/src/main/resources-precompiled/README.md b/src/main/resources-precompiled/README.md new file mode 100644 index 0000000000..fb437d4f99 --- /dev/null +++ b/src/main/resources-precompiled/README.md @@ -0,0 +1,51 @@ +# Precompiled libraries + +The `native` folder contains precompiled libraries for distribution. +If a library is missing, please consider a pull request or open an issue. + +Whenever the interface of `SerialNativeInterface.java` changes, developers +are to recompile binaries for each platform. + +## Valid paths + +The old path structure is ambigious, but supported for compatiblity. +This structure must be used before `native-lib-loader` version 2.4.0. + + * `linux_32` + * `linux_64` + * `linux_arm` + * `linux_arm64` + * `mac_32` + * `mac_64` + * `windows_32` + * `windows_64` + + +## Next version + +Note: Valid starting from `native-lib-loader` version 2.4.0 or later. +The path names were aligned to the [os-maven-plugin](https://github.com/trustin/os-maven-plugin/). +It follows the same conventions as the [osdetector-gradle-plugin](https://github.com/google/osdetector-gradle-plugin). + + * `linux-arm_32-32` + * `linux-x86_32-32` (also alias of `linux-i386-64`) + * `windows-x86_64-32` + * `aix-ppc_64-64` + * `linux-x86_64-64` + * `linux-aarch_64-64` (instead of `linux-arm64-64`) + * `linux-ppcle_64-64` + * `mac-x86_64-64` + * `mac-ppc_64-64` + * `windows-x86_32-64` + * `windows-x86_64-64` + +If an architecture, bitness or cpu feature is missing, please consider a pull request against `native-lib-loader` + +## Activation + +No activation needed, a jar with these libraries is always built. +Optionally, to skip native compilation and only build this artifact: + +```bash +mvn clean install -Ppackage +``` diff --git a/src/main/resources-precompiled/natives/linux_32/libjssc.so b/src/main/resources-precompiled/natives/linux_32/libjssc.so new file mode 100755 index 0000000000..5129d6a67e Binary files /dev/null and b/src/main/resources-precompiled/natives/linux_32/libjssc.so differ diff --git a/src/main/resources-precompiled/natives/linux_64/libjssc.so b/src/main/resources-precompiled/natives/linux_64/libjssc.so new file mode 100755 index 0000000000..3d683e3374 Binary files /dev/null and b/src/main/resources-precompiled/natives/linux_64/libjssc.so differ diff --git a/src/main/resources-precompiled/natives/linux_arm/libjssc.so b/src/main/resources-precompiled/natives/linux_arm/libjssc.so new file mode 100755 index 0000000000..64c9d6db17 Binary files /dev/null and b/src/main/resources-precompiled/natives/linux_arm/libjssc.so differ diff --git a/src/main/resources-precompiled/natives/linux_arm64/libjssc.so b/src/main/resources-precompiled/natives/linux_arm64/libjssc.so new file mode 100755 index 0000000000..d0f5832894 Binary files /dev/null and b/src/main/resources-precompiled/natives/linux_arm64/libjssc.so differ diff --git a/src/main/resources-precompiled/natives/linux_ppc/libjssc.so b/src/main/resources-precompiled/natives/linux_ppc/libjssc.so new file mode 100755 index 0000000000..bca530f4f6 Binary files /dev/null and b/src/main/resources-precompiled/natives/linux_ppc/libjssc.so differ diff --git a/src/main/resources-precompiled/natives/osx_64/libjssc.dylib b/src/main/resources-precompiled/natives/osx_64/libjssc.dylib new file mode 100755 index 0000000000..193787fc79 Binary files /dev/null and b/src/main/resources-precompiled/natives/osx_64/libjssc.dylib differ diff --git a/src/main/resources-precompiled/natives/windows_32/jssc.dll b/src/main/resources-precompiled/natives/windows_32/jssc.dll new file mode 100755 index 0000000000..f5888b9f70 Binary files /dev/null and b/src/main/resources-precompiled/natives/windows_32/jssc.dll differ diff --git a/src/main/resources-precompiled/natives/windows_64/jssc.dll b/src/main/resources-precompiled/natives/windows_64/jssc.dll new file mode 100755 index 0000000000..da2d8cf8eb Binary files /dev/null and b/src/main/resources-precompiled/natives/windows_64/jssc.dll differ diff --git a/src/test/java/jssc/SerialNativeInterfaceTest.java b/src/test/java/jssc/SerialNativeInterfaceTest.java new file mode 100644 index 0000000000..d0a2d6f2c6 --- /dev/null +++ b/src/test/java/jssc/SerialNativeInterfaceTest.java @@ -0,0 +1,42 @@ +package jssc; + +import org.junit.Test; + +import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.CoreMatchers.not; +import static org.hamcrest.CoreMatchers.nullValue; +import static org.junit.Assert.assertThat; +import static org.junit.Assert.fail; + +public class SerialNativeInterfaceTest { + + + @Test + public void testInitNativeInterface() { + SerialNativeInterface serial = new SerialNativeInterface(); + + long handle = -1; + try { + handle = serial.openPort("ttyS0",false); + assertThat(handle, is(not(-1L))); + } finally { + if (handle != -1) { + serial.closePort(handle); + } + } + } + + @Test + public void testPrintVersion() { + try { + final String nativeLibraryVersion = SerialNativeInterface.getNativeLibraryVersion(); + assertThat(nativeLibraryVersion, is(not(nullValue()))); + assertThat(nativeLibraryVersion, is(not(""))); + } catch (UnsatisfiedLinkError linkError) { + linkError.printStackTrace(); + fail("Should be able to call method!"); + } + + } + +} diff --git a/toolchain/Aarch64.cmake b/toolchain/Aarch64.cmake new file mode 100644 index 0000000000..ff91bb610e --- /dev/null +++ b/toolchain/Aarch64.cmake @@ -0,0 +1,10 @@ +SET(CMAKE_SYSTEM_NAME Linux) +SET(TOOLCHAIN_PREFIX aarch64-linux-gnu) +SET(CMAKE_C_COMPILER ${TOOLCHAIN_PREFIX}-gcc) +SET(CMAKE_CXX_COMPILER ${TOOLCHAIN_PREFIX}-g++) +SET(CMAKE_STRIP ${MINGW_PREFIX}-strip CACHE FILEPATH "" FORCE) +SET(CMAKE_FIND_ROOT_PATH /usr/${TOOLCHAIN_PREFIX}/) +set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER) +set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY BOTH) +set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE BOTH) + diff --git a/toolchain/Armhf.cmake b/toolchain/Armhf.cmake index 444d57069e..994a62d683 100644 --- a/toolchain/Armhf.cmake +++ b/toolchain/Armhf.cmake @@ -1,9 +1,14 @@ SET(CMAKE_SYSTEM_NAME Linux) SET(TOOLCHAIN_PREFIX arm-linux-gnueabi) -SET(TOOLCHAIN_VERSION 5) +if(NOT SKIP_COMPILER_VERSION) + SET(COMPILER_VERSION 5) + if(NOT COMPILER_VERSION MATCHES "-.*") + SET(COMPILER_VERSION "-${COMPILER_VERSION}") + endif() +endif() SET(TOOLCHAIN_SUFFIX hf) -SET(CMAKE_C_COMPILER ${TOOLCHAIN_PREFIX}-gcc-${TOOLCHAIN_VERSION}) -SET(CMAKE_CXX_COMPILER ${TOOLCHAIN_PREFIX}-g++-${TOOLCHAIN_VERSION}) +SET(CMAKE_C_COMPILER ${TOOLCHAIN_PREFIX}-gcc${COMPILER_VERSION}) +SET(CMAKE_CXX_COMPILER ${TOOLCHAIN_PREFIX}-g++${COMPILER_VERSION}) SET(CMAKE_STRIP ${MINGW_PREFIX}-strip CACHE FILEPATH "" FORCE) SET(CMAKE_FIND_ROOT_PATH /usr/${TOOLCHAIN_PREFIX}${TOOLCHAIN_SUFFIX}/) set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER) diff --git a/toolchain/Armsf.cmake b/toolchain/Armsf.cmake index 4d515924f8..66e18d72d3 100644 --- a/toolchain/Armsf.cmake +++ b/toolchain/Armsf.cmake @@ -1,9 +1,14 @@ SET(CMAKE_SYSTEM_NAME Linux) SET(TOOLCHAIN_PREFIX arm-linux-gnueabi) -SET(TOOLCHAIN_VERSION 5) +if(NOT SKIP_COMPILER_VERSION) + SET(COMPILER_VERSION 5) + if(NOT COMPILER_VERSION MATCHES "-.*") + SET(COMPILER_VERSION "-${COMPILER_VERSION}") + endif() +endif() SET(TOOLCHAIN_SUFFIX "") -SET(CMAKE_C_COMPILER ${TOOLCHAIN_PREFIX}-gcc-${TOOLCHAIN_VERSION}) -SET(CMAKE_CXX_COMPILER ${TOOLCHAIN_PREFIX}-g++-${TOOLCHAIN_VERSION}) +SET(CMAKE_C_COMPILER ${TOOLCHAIN_PREFIX}-gcc${COMPILER_VERSION}) +SET(CMAKE_CXX_COMPILER ${TOOLCHAIN_PREFIX}-g++${COMPILER_VERSION}) SET(CMAKE_STRIP ${MINGW_PREFIX}-strip CACHE FILEPATH "" FORCE) SET(CMAKE_FIND_ROOT_PATH /usr/${TOOLCHAIN_PREFIX}${TOOLCHAIN_SUFFIX}/) set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER) diff --git a/toolchain/Ppc64.cmake b/toolchain/Ppc64.cmake new file mode 100644 index 0000000000..4193f0ba93 --- /dev/null +++ b/toolchain/Ppc64.cmake @@ -0,0 +1,10 @@ +SET(CMAKE_SYSTEM_NAME Linux) +SET(TOOLCHAIN_PREFIX powerpc64le-linux-gnu) +SET(CMAKE_C_COMPILER ${TOOLCHAIN_PREFIX}-gcc) +SET(CMAKE_CXX_COMPILER ${TOOLCHAIN_PREFIX}-g++) +SET(CMAKE_STRIP ${MINGW_PREFIX}-strip CACHE FILEPATH "" FORCE) +SET(CMAKE_FIND_ROOT_PATH /usr/${TOOLCHAIN_PREFIX}/) +set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER) +set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY BOTH) +set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE BOTH) +