From a9754777d0bb89e47f0b99dcdb3e838380551aaa Mon Sep 17 00:00:00 2001 From: Joseph Henry Date: Fri, 24 Aug 2018 15:45:05 -0700 Subject: [PATCH] Improvements to packaging scripts for Windows, macOS and Linux --- CMakeLists.txt | 24 +- Makefile | 32 +-- include/Xcode-Bridging-Header.h | 71 +++++ packages/README.md | 34 ++- packages/clean.bat | 4 + packages/clean.sh | 7 + packages/dist.bat | 48 +++- packages/dist.sh | 268 ++++++++---------- packages/java/com/zerotier/libzt/ZTFDSet.java | 54 ++++ .../com/zerotier/libzt/ZTSocketAddress.java | 92 ++++++ .../java/com/zerotier/libzt/ZeroTier.java | 82 ++++++ packages/module.modulemap | 6 + packages/package.sh | 37 +++ 13 files changed, 561 insertions(+), 198 deletions(-) create mode 100644 include/Xcode-Bridging-Header.h create mode 100644 packages/clean.bat create mode 100755 packages/clean.sh create mode 100644 packages/java/com/zerotier/libzt/ZTFDSet.java create mode 100644 packages/java/com/zerotier/libzt/ZTSocketAddress.java create mode 100644 packages/java/com/zerotier/libzt/ZeroTier.java create mode 100644 packages/module.modulemap create mode 100755 packages/package.sh diff --git a/CMakeLists.txt b/CMakeLists.txt index 4c2b9fa..a01dda7 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -89,13 +89,17 @@ endif () # ----------------------------------------------------------------------------- MESSAGE (STATUS "Looking for JNI") -# We are only interested in finding jni.h: we do not care about extended JVM -# functionality or the AWT library. -set(JAVA_AWT_LIBRARY NotNeeded) -set(JAVA_JVM_LIBRARY NotNeeded) -set(JAVA_INCLUDE_PATH2 NotNeeded) -set(JAVA_AWT_INCLUDE_PATH NotNeeded) -set(JAVA_INCLUDE_PATH "C:\\Program Files\\Java\\jdk-10.0.2\\include") + +if (BUILDING_WIN) + # We are only interested in finding jni.h: we do not care about extended JVM + # functionality or the AWT library. + set(JAVA_AWT_LIBRARY NotNeeded) + set(JAVA_JVM_LIBRARY NotNeeded) + set(JAVA_INCLUDE_PATH2 NotNeeded) + set(JAVA_AWT_INCLUDE_PATH NotNeeded) + set(JAVA_INCLUDE_PATH "C:\\Program Files\\Java\\jdk-10.0.2\\include") +endif () + find_package (JNI) if (JNI_FOUND) message (STATUS "JNI_INCLUDE_DIRS=${JNI_INCLUDE_DIRS}") @@ -103,6 +107,7 @@ if (JNI_FOUND) list (GET JNI_INCLUDE_DIRS 0 JNI_INCLUDE_DIR) message (STATUS "jni path=${JNI_INCLUDE_DIR}") include_directories ("${JNI_INCLUDE_DIR}") + include_directories ("${JNI_INCLUDE_DIRS}") if (BUILDING_WIN) include_directories ("${JNI_INCLUDE_DIR}\\win32") endif () @@ -330,13 +335,10 @@ if (IN_XCODE) FRAMEWORK_VERSION A DEFINES_MODULE TRUE MACOSX_FRAMEWORK_IDENTIFIER com.cmake.${XCODE_FRAMEWORK_NAME} - MODULE_MAP "/Users/joseph/op/zt/libzt/module.modulemap" + MODULE_MAP "~/op/zt/libzt/packages/module.modulemap" PUBLIC_HEADER "${frameworkHeaderGlob}" XCODE_ATTRIBUTE_CODE_SIGN_IDENTITY "iPhone Developer" ) - - #PUBLIC_HEADER "${INCLUDE_PATH}/Xcode-Bridging-Header.h" - #PRIVATE_HEADER "${frameworkPrivateHeaderGlob}" endif () # ----------------------------------------------------------------------------- diff --git a/Makefile b/Makefile index 4b4ef16..5a893a2 100644 --- a/Makefile +++ b/Makefile @@ -1,25 +1,32 @@ # NOTE: This file only exists as a convenience for cleaning and building # products for release. To build, use CMake. Instructions in README.md +ifeq ($(OS),Windows_NT) +DIST_BUILD_SCRIPT := packages\dist.bat +CLEAN_SCRIPT := packages\clean.bat +else +DIST_BUILD_SCRIPT := ./packages/dist.sh +CLEAN_SCRIPT := ./packages/clean.sh +PACKAGE_SCRIPT := ./packages/package.sh +endif + .PHONY: clean clean: - rm -rf bin build products tmp - rm -f *.o *.s *.exp *.lib .depend* *.core core - rm -rf .depend - find . -type f \( -name '*.a' -o -name '*.o' -o -name '*.so' -o -name \ - '*.o.d' -o -name '*.out' -o -name '*.log' -o -name '*.dSYM' -o -name '*.dylib' -o -name '*.class' \) -delete + $(CLEAN_SCRIPT) # Build and package everything # This command shall be run twice: # (1) Generates projects # # (2) Build products and package everything + .PHONY: dist dist: patch - ./packages/dist.sh -.PHONY: dist_win -dist_win: patch - packages\dist.bat + $(DIST_BUILD_SCRIPT) + +.PHONY: package +package: + $(PACKAGE_SCRIPT) # Initialize submodules and apply patches .PHONY: all @@ -27,13 +34,6 @@ all: update patch cmake -H. -Bbuild -DCMAKE_BUILD_TYPE=Release cmake --build build -# Clean build paths -clean_win: - -"rd /S /Q bin" - -"rd /S /Q build" - -"rd /S /Q WinBuild32" - -"rd /S /Q WinBuild64" - # Remove any CMake-generated library-building projects clean_packages: rm -rf packages/xcode_ios diff --git a/include/Xcode-Bridging-Header.h b/include/Xcode-Bridging-Header.h new file mode 100644 index 0000000..ab043be --- /dev/null +++ b/include/Xcode-Bridging-Header.h @@ -0,0 +1,71 @@ +/* + * ZeroTier SDK - Network Virtualization Everywhere + * Copyright (C) 2011-2017 ZeroTier, Inc. https://www.zerotier.com/ + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + * -- + * + * You can be released from the requirements of the license by purchasing + * a commercial license. Buying such a license is mandatory as soon as you + * develop commercial closed-source software that incorporates or links + * directly against ZeroTier software without disclosing the source code + * of your own application. + */ + +#ifndef LIBZT_BRIDGING_HEADER_H +#define LIBZT_BRIDGING_HEADER_H + +#include +#include "libzt.h" + +// ZT SERVICE CONTROLS +int zts_start(const char *path, int blocking); +int zts_startjoin(const char *path, const uint64_t nwid); +void zts_stop(); +int zts_core_running(); +int zts_stack_running(); +int zts_ready(); +int zts_join(uint64_t nwid); +int zts_leave(uint64_t nwid); +uint64_t zts_get_node_id(); +// SOCKET API +int zts_connect(int fd, const struct sockaddr *addr, socklen_t addrlen); +int zts_bind(int fd, const struct sockaddr *addr, socklen_t addrlen); +int zts_accept(int fd, struct sockaddr *addr, socklen_t *addrlen); +int zts_listen(int fd, int backlog); +int zts_socket(int socket_family, int socket_type, int protocol); +int zts_setsockopt(int fd, int level, int optname, const void *optval, socklen_t optlen); +int zts_getsockopt(int fd, int level, int optname, void *optval, socklen_t *optlen); +int zts_close(int fd); +int zts_getsockname(int fd, struct sockaddr *addr, socklen_t *addrlen); +int zts_getpeername(int fd, struct sockaddr *addr, socklen_t *addrlen); +ssize_t zts_send(int fd, const void *buf, size_t len, int flags); +ssize_t zts_sendto(int fd, const void *buf, size_t len, int flags, const struct sockaddr *addr, socklen_t addrlen); +ssize_t zts_sendmsg(int fd, const struct msghdr *msg, int flags); +ssize_t zts_recv(int fd, void *buf, size_t len, int flags); +ssize_t zts_recvfrom(int fd, void *buf, size_t len, int flags, struct sockaddr *addr, socklen_t *addrlen); +ssize_t zts_recvmsg(int fd, struct msghdr *msg,int flags); +int zts_read(int fd, void *buf, size_t len); +int zts_write(int fd, const void *buf, size_t len); +int zts_shutdown(int fd, int how); +int zts_select(int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, struct timeval *timeout); +int zts_fcntl(int fd, int cmd, int flags); +int zts_ioctl(int fd, unsigned long request, void *argp); + +#endif /* LIBZT_BRIDGING_HEADER_H */ + + + + diff --git a/packages/README.md b/packages/README.md index c1289e3..8a2d93b 100644 --- a/packages/README.md +++ b/packages/README.md @@ -1,9 +1,25 @@ -### Projects and scripts for building various packages - -### Android - -Pre-built binaries can be found [here]() - -This android project builds from the root `CMakeLists.txt` and generates a `.aar` Android Archive which can be imported into an Android Studio project as a library. An example of this library's usage can be found in [examples/android](examples/android). Further examples of the libzt JNI API can be seen in [examples/java](examples/java). - -### PyPI \ No newline at end of file +### Projects and scripts for building various packages + +Pre-built binaries [here](https://download.zerotier.com/RELEASES/) + +*** + +### To build entire distribution package: + +Step 1: On a Windows host, run `make dist`. Outputs will be copied to `prebuilt/(debug|release)/(win32/win64)` +Step 2: On a Linux host, run `make dist`. Outputs will be copied to `prebuilt/linux-$ARCH` +Step 3: On a macOS host, run `make dist`. Outputs will be copied to `prebuilt/macos-$ARCH` +Step 4: Perform any necessary modifications to the generated projects (such as Xcode projects) +Step 5: Re-run `make dist` +Step 6: On a Unix-like host, run `make package`. This will zip everything up into a pair of `tar.gz` files in `products` + +*** + +### iOS + + - In `packages/xcode_ios`, change SDK from `macOS` to `iOS` + - Build + +### Android + +This project is not currently generated by CMake, but does utilize the `CMakeLists.txt` and generates a `.aar` Android Archive which can be imported into an Android Studio project as a library. An example of this library's usage can be found in [examples/android](examples/android). Further examples of the libzt JNI API can be seen in [examples/java](examples/java). diff --git a/packages/clean.bat b/packages/clean.bat new file mode 100644 index 0000000..6e337a8 --- /dev/null +++ b/packages/clean.bat @@ -0,0 +1,4 @@ +rd /S /Q bin +rd /S /Q build +rd /S /Q WinBuild32 +rd /S /Q WinBuild64 \ No newline at end of file diff --git a/packages/clean.sh b/packages/clean.sh new file mode 100755 index 0000000..33bceb9 --- /dev/null +++ b/packages/clean.sh @@ -0,0 +1,7 @@ +#!/bin/bash + +rm -rf bin build products tmp +rm -f *.o *.s *.exp *.lib .depend* *.core core +rm -rf .depend +find . -type f \( -name '*.o' -o -name '*.so' -o -name \ + '*.o.d' -o -name '*.out' -o -name '*.log' -o -name '*.dSYM' -o -name '*.class' \) -delete diff --git a/packages/dist.bat b/packages/dist.bat index a10c00d..b82f883 100644 --- a/packages/dist.bat +++ b/packages/dist.bat @@ -1,9 +1,9 @@ REM Build all target configurations and copy results into "prebuilt" -set PrebuiltDebugWin32Dir=prebuilt\debug\win32 -set PrebuiltDebugWin64Dir=prebuilt\debug\win64 -set PrebuiltReleaseWin32Dir=prebuilt\release\win32 -set PrebuiltReleaseWin64Dir=prebuilt\release\win64 +set PrebuiltDebugWin32Dir=staging\debug\win32 +set PrebuiltDebugWin64Dir=staging\debug\win64 +set PrebuiltReleaseWin32Dir=staging\release\win32 +set PrebuiltReleaseWin64Dir=staging\release\win64 mkdir %PrebuiltDebugWin32Dir% mkdir %PrebuiltDebugWin64Dir% @@ -50,15 +50,39 @@ popd cmake --build WinBuild32 --config Release cmake --build WinBuild32 --config Debug -copy %DebugWinBuildDir%\zt-static.lib %PrebuiltDebugWin32Dir%\zt-jni.lib -copy %DebugWinBuildDir%\zt-shared.dll %PrebuiltDebugWin32Dir%\zt-jni.dll -copy %ReleaseWinBuildDir%\zt-static.lib %PrebuiltReleaseWin32Dir%\zt-jni.lib -copy %ReleaseWinBuildDir%\zt-shared.dll %PrebuiltReleaseWin32Dir%\zt-jni.dll +REM Build JAR file +REM release variant +cd packages\java +del com/zerotier/libzt/*.class +move ..\..\%ReleaseWinBuildDir%\zt-shared.dll zt.dll +javac com/zerotier/libzt/*.java +jar cf zt.jar zt.dll com/zerotier/libzt/*.class +move zt.jar ..\..\%PrebuiltReleaseWin32Dir% +REM debug variant +del com/zerotier/libzt/*.class +move ..\..\%DebugWinBuildDir%\zt-shared.dll zt.dll +javac com/zerotier/libzt/*.java +jar cf zt.jar zt.dll com/zerotier/libzt/*.class +move zt.jar ..\..\%PrebuiltDebugWin32Dir% +popd +popd cmake --build WinBuild64 --config Release cmake --build WinBuild64 --config Debug -copy %DebugWinBuildDir%\zt-static.lib %PrebuiltDebugWin64Dir%\zt-jni.lib -copy %DebugWinBuildDir%\zt-shared.dll %PrebuiltDebugWin64Dir%\zt-jni.dll -copy %ReleaseWinBuildDir%\zt-static.lib %PrebuiltReleaseWin64Dir%\zt-jni.lib -copy %ReleaseWinBuildDir%\zt-shared.dll %PrebuiltReleaseWin64Dir%\zt-jni.dll +REM Build JAR file +REM release variant +cd packages\java +del com/zerotier/libzt/*.class +move ..\..\%ReleaseWinBuildDir%\zt-shared.dll zt.dll +javac com/zerotier/libzt/*.java +jar cf zt.jar zt.dll com/zerotier/libzt/*.class +move zt.jar ..\..\%PrebuiltReleaseWin64Dir% +REM debug variant +del com/zerotier/libzt/*.class +move ..\..\%DebugWinBuildDir%\zt-shared.dll zt.dll +javac com/zerotier/libzt/*.java +jar cf zt.jar zt.dll com/zerotier/libzt/*.class +move zt.jar ..\..\%PrebuiltDebugWin64Dir% +popd +popd \ No newline at end of file diff --git a/packages/dist.sh b/packages/dist.sh index 8dcf2fc..e0535a3 100755 --- a/packages/dist.sh +++ b/packages/dist.sh @@ -7,37 +7,38 @@ # - once projects have been generated, this script will use their tooling to build the libraries/packages # - when all products have been built and moved to `tmp`, they will be compressed and moved to `products` -PROJNAME="zt" -LIBNAME="lib"$PROJNAME OSNAME=$(uname | tr '[A-Z]' '[a-z]') -LIBZT_VERSION="1.2.0" -LIBZT_REVISION="1" -ZT_CORE_VERSION="1.2.12" -FILENAME_PREFIX=${LIBNAME}"-"${LIBZT_VERSION}"r"${LIBZT_REVISION} - +BUILD_CONCURRENCY=4 PROJROOT=$(pwd) BUILD_PRODUCTS_DIR=$(pwd)/bin -LIB_PRODUCTS_DIR=${BUILD_PRODUCTS_DIR}/lib +LIB_PRODUCTS_DIR=$BUILD_PRODUCTS_DIR/lib FINISHED_PRODUCTS_DIR=$(pwd)/products -TMP_PRODUCTS_DIR=${BUILD_PRODUCTS_DIR}/tmp - -# previously built, will include in package -WIN_PREBUILT_DIR=${PROJROOT}/prebuilt -WIN_RELEASE_PRODUCTS_DIR=${WIN_PREBUILT_DIR}/release -WIN_DEBUG_PRODUCTS_DIR=${WIN_PREBUILT_DIR}/debug -WIN32_RELEASE_PRODUCTS_DIR=${WIN_RELEASE_PRODUCTS_DIR}/win32 -WIN64_RELEASE_PRODUCTS_DIR=${WIN_RELEASE_PRODUCTS_DIR}/win64 -WIN32_DEBUG_PRODUCTS_DIR=${WIN_DEBUG_PRODUCTS_DIR}/win32 -WIN64_DEBUG_PRODUCTS_DIR=${WIN_DEBUG_PRODUCTS_DIR}/win64 - +STAGING_DIR=$(pwd)/staging +# Windows (previously built) +WIN_PREBUILT_DIR=$PROJROOT/staging/win +WIN_RELEASE_PRODUCTS_DIR=$WIN_PREBUILT_DIR/release +WIN_DEBUG_PRODUCTS_DIR=$WIN_PREBUILT_DIR/debug +WIN32_RELEASE_PRODUCTS_DIR=$WIN_RELEASE_PRODUCTS_DIR/win32 +WIN64_RELEASE_PRODUCTS_DIR=$WIN_RELEASE_PRODUCTS_DIR/win64 +WIN32_DEBUG_PRODUCTS_DIR=$WIN_DEBUG_PRODUCTS_DIR/win32 +WIN64_DEBUG_PRODUCTS_DIR=$WIN_DEBUG_PRODUCTS_DIR/win64 +# Linux +LINUX_PROD_DIR=$PROJROOT/staging/linux +# macOS +MACOS_PROD_DIR=$PROJROOT/staging/macos +MACOS_RELEASE_PROD_DIR=$MACOS_PROD_DIR/release +MACOS_DEBUG_PROD_DIR=$MACOS_PROD_DIR/debug +# iOS +IOS_PROD_DIR=$PROJROOT/staging/ios +# Android +ANDROID_PROJ_DIR=$(pwd)/"packages/android" +ANDROID_ARCHIVE_FILENAME="zt.aar" +# Xcode XCODE_IOS_PROJ_DIR=$(pwd)/"packages/xcode_ios" XCODE_MACOS_PROJ_DIR=$(pwd)/"packages/xcode_macos" -ANDROID_PROJ_DIR=$(pwd)/"packages/android" -ANDROID_ARCHIVE_FILENAME="zt.aar" - -mkdir ${FINISHED_PRODUCTS_DIR} -mkdir ${TMP_PRODUCTS_DIR} +mkdir $FINISHED_PRODUCTS_DIR +mkdir $TMP_PRODUCTS_DIR # Check that projects exist, generate them and exit if they don't exist generate_projects_if_necessary() @@ -65,145 +66,112 @@ generate_projects_if_necessary() # android? if [[ $should_exit = 1 ]]; then echo "Generated projects. Perform necessary modifications and then re-run this script" - echo "Please place previously built windows binaries in ${WIN_PREBUILT_DIR} before running again." + echo "Please place previously built windows binaries in $WIN_PREBUILT_DIR before running again." exit 0 else echo "Projects detected, going to build stage next" fi } -# Xcode Frameworks -build_xcode_targets() -{ - CMAKE_CONFIG=${1} - UPPERCASE_CONFIG="$(tr '[:lower:]' '[:upper:]' <<< ${1:0:1})${1:1}" - if [[ ${2} = *"jni"* ]]; then - CMAKE_FLAGS=${CMAKE_FLAGS}" -DJNI=1" - fi - if [[ $OSNAME = *"darwin"* && ${2} != *"JNI"* ]]; then - CURR_BUILD_PRODUCTS_DIR=${LIB_PRODUCTS_DIR}/${UPPERCASE_CONFIG} - # (iOS) - echo "BUILDING: iOS" - cd $XCODE_IOS_PROJ_DIR - xcodebuild -target zt -configuration "${UPPERCASE_CONFIG}" -sdk "iphoneos" - xcodebuild -target zt-static -configuration "${UPPERCASE_CONFIG}" -sdk "iphoneos" - cd - - CURR_ARCH="arm64" # anything older should be built custom - CURR_TMP_PRODUCT_DIR=${TMP_PRODUCTS_DIR}/ios-${CURR_ARCH} - mkdir -p ${CURR_TMP_PRODUCT_DIR} - mv ${CURR_BUILD_PRODUCTS_DIR}/*.framework ${CURR_TMP_PRODUCT_DIR} - mv ${CURR_BUILD_PRODUCTS_DIR}/libzt.* ${CURR_TMP_PRODUCT_DIR} - - # (macOS) - echo "BUILDING: macOS" - cd $XCODE_MACOS_PROJ_DIR - xcodebuild -target zt -configuration "${UPPERCASE_CONFIG}" -sdk "macosx" - xcodebuild -target zt-static -configuration "${UPPERCASE_CONFIG}" -sdk "macosx" - xcodebuild -target zt-shared -configuration "${UPPERCASE_CONFIG}" -sdk "macosx" - cd - - CURR_ARCH=${HOSTTYPE} - CURR_TMP_PRODUCT_DIR=${TMP_PRODUCTS_DIR}/macos-${CURR_ARCH} - mkdir -p ${CURR_TMP_PRODUCT_DIR} - mv ${CURR_BUILD_PRODUCTS_DIR}/*.framework ${CURR_TMP_PRODUCT_DIR} - mv ${CURR_BUILD_PRODUCTS_DIR}/libzt.* ${CURR_TMP_PRODUCT_DIR} - fi -} - -# Android archive (AAR) -build_aar() -{ - CMAKE_CONFIG=${1} - UPPERCASE_CONFIG="$(tr '[:lower:]' '[:upper:]' <<< ${1:0:1})${1:1}" - if [[ ${2} = *"jni"* ]]; then - CMAKE_FLAGS=${CMAKE_FLAGS}" -DJNI=1" - fi - CURR_ARCH="armeabi-v7a" - CURR_TMP_PRODUCT_DIR=${TMP_PRODUCTS_DIR}/android-${CURR_ARCH} - mkdir -p ${CURR_TMP_PRODUCT_DIR} - echo "BUILDING: AAR" - cd ${ANDROID_PROJ_DIR} - ./gradlew assemble${UPPERCASE_CONFIG} # e.g. assembleRelease - mv ${ANDROID_PROJ_DIR}/app/build/outputs/aar/app-${CONFIG}.aar ${CURR_TMP_PRODUCT_DIR}/${ANDROID_ARCHIVE_FILENAME} - cd - -} - -# Java archive (JAR) -#Call ordinary CMake build script with JNI flag set, use product in JAR file -build_jar() -{ - CMAKE_CONFIG=${1} - UPPERCASE_CONFIG="$(tr '[:lower:]' '[:upper:]' <<< ${1:0:1})${1:1}" - if [[ ${2} = *"jni"* ]]; then - CMAKE_FLAGS=${CMAKE_FLAGS}" -DJNI=1" - fi - CURR_ARCH=${HOSTTYPE} - CURR_TMP_PRODUCT_DIR=${TMP_PRODUCTS_DIR}/macos-${CURR_ARCH} - mkdir -p ${CURR_TMP_PRODUCT_DIR} - echo "BUILDING: JAR" - rm -rf ${LIB_PRODUCTS_DIR} # clean-lite - cmake -H. -Bbuild -DCMAKE_BUILD_TYPE=${CMAKE_CONFIG} "-DJNI=1 -DBUILD_TESTS=0" - cmake --build build - cd ${PROJROOT}/examples/java - cp ../../bin/lib/libzt.dylib . - mv ${LIB_PRODUCTS_DIR}/libzt.a ${CURR_TMP_PRODUCT_DIR}/libzt-jni.a - mv ${LIB_PRODUCTS_DIR}/libzt.dylib ${CURR_TMP_PRODUCT_DIR}/libzt-jni.dylib - javac com/zerotier/libzt/*.java - jar cf libzt.jar libzt.dylib com/zerotier/libzt/*.class - mv libzt.jar ${CURR_TMP_PRODUCT_DIR} - cd - -} - -# Build everything (to a specific configuration) -build() -{ - if [[ $OSNAME == *"darwin"* ]]; then - build_xcode_targets ${1} ${2} - build_aar ${1} ${2} - fi - build_jar ${1} ${2} -} - -# Package everything together -package_products() -{ - CONFIG=${1} - PRODUCT_FILENAME=${FILENAME_PREFIX}-${CONFIG}.tar.gz - echo "Making: " ${FINISHED_PRODUCTS_DIR}/${PRODUCT_FILENAME} - cd ${TMP_PRODUCTS_DIR} - tar -zcvf ${PRODUCT_FILENAME} . - mv *.tar.gz ${FINISHED_PRODUCTS_DIR} - cd - -} - -copy_windows_targets() -{ - echo "Copying prebuilt windows binaries into temporary staging directory" - if [ ! -d "$XCODE_MACOS_PROJ_DIR" ]; then - echo "WARNING: windows products directory appears to be empty. Exiting" - exit 0 - fi - CONFIG=${1} - cp -r ${WIN_PREBUILT_DIR}/${CONFIG}/win32 ${TMP_PRODUCTS_DIR} - cp -r ${WIN_PREBUILT_DIR}/${CONFIG}/win64 ${TMP_PRODUCTS_DIR} -} - build_all_products() { - CONFIG=${1} - build ${CONFIG} - copy_windows_targets ${CONFIG} - package_products ${CONFIG} + CONFIG=$1 + UPPERCASE_CONFIG="$(tr '[:lower:]' '[:upper:]' <<< $1:0:1)$1:1" + + # Targets to build on and for darwin + if [[ $OSNAME = *"darwin"* ]]; then + # Xcode Frameworks --- Builds targets from a CMake-generated Xcode project + if true; then + if [[ $2 != *"JNI"* ]]; then + CURR_BUILD_PRODUCTS_DIR=$LIB_PRODUCTS_DIR/$UPPERCASE_CONFIG + # (iOS) + echo "BUILDING: iOS" + cd $XCODE_IOS_PROJ_DIR + xcodebuild -target zt -configuration "$UPPERCASE_CONFIG" -sdk "iphoneos" + xcodebuild -target zt-static -configuration "$UPPERCASE_CONFIG" -sdk "iphoneos" + cd - + CURR_ARCH="arm64" # anything older should be built custom + CURR_TMP_PRODUCT_DIR=$STAGING_DIR/$CONFIG/ios-$CURR_ARCH + mkdir -p $CURR_TMP_PRODUCT_DIR + mv $CURR_BUILD_PRODUCTS_DIR/*.framework $CURR_TMP_PRODUCT_DIR + mv $CURR_BUILD_PRODUCTS_DIR/libzt.* $CURR_TMP_PRODUCT_DIR + + # (macOS) + echo "BUILDING: macOS" + cd $XCODE_MACOS_PROJ_DIR + xcodebuild -target zt -configuration "$UPPERCASE_CONFIG" -sdk "macosx" + xcodebuild -target zt-static -configuration "$UPPERCASE_CONFIG" -sdk "macosx" + xcodebuild -target zt-shared -configuration "$UPPERCASE_CONFIG" -sdk "macosx" + cd - + CURR_ARCH=$HOSTTYPE + CURR_TMP_PRODUCT_DIR=$STAGING_DIR/$CONFIG/macos-$CURR_ARCH + mkdir -p $CURR_TMP_PRODUCT_DIR + mv $CURR_BUILD_PRODUCTS_DIR/*.framework $CURR_TMP_PRODUCT_DIR + mv $CURR_BUILD_PRODUCTS_DIR/libzt.* $CURR_TMP_PRODUCT_DIR + fi + fi + # Android Archive (AAR) --- Executes a Gradle task + if true; then + CMAKE_FLAGS=$CMAKE_FLAGS" -DJNI=1" + CURR_ARCH="armeabi-v7a" + CURR_TMP_PRODUCT_DIR=$STAGING_DIR/$CONFIG/android-$CURR_ARCH + mkdir -p $CURR_TMP_PRODUCT_DIR + echo "BUILDING: AAR" + cd $ANDROID_PROJ_DIR + ./gradlew assemble$UPPERCASE_CONFIG # e.g. assembleRelease + mv $ANDROID_PROJ_DIR/app/build/outputs/aar/app-$CONFIG.aar $CURR_TMP_PRODUCT_DIR/$ANDROID_ARCHIVE_FILENAME + cd - + fi + # Java Archive (JAR) + if true; then + CMAKE_FLAGS=$CMAKE_FLAGS" -DJNI=1" + CURR_ARCH=$HOSTTYPE + CURR_TMP_PRODUCT_DIR=$STAGING_DIR/$CONFIG/macos-$CURR_ARCH + mkdir -p $CURR_TMP_PRODUCT_DIR + echo "BUILDING: JAR" + rm -rf $LIB_PRODUCTS_DIR # clean-lite + cmake -H. -Bbuild -DCMAKE_BUILD_TYPE=$CONFIG "-DJNI=1 -DBUILD_TESTS=0" + cmake --build build + cd $PROJROOT/packages/java + #cp $CURR_BUILD_PRODUCTS_DIR/libzt.dylib . + javac com/zerotier/libzt/*.java + jar cf zt.jar $CURR_BUILD_PRODUCTS_DIR/libzt.dylib com/zerotier/libzt/*.class + mv zt.jar $CURR_TMP_PRODUCT_DIR + cd - + fi + fi + # Linux targets + if [[ $OSNAME = *"linux"* ]]; then + # Ordinary libraries + if false; then + rm -rf $LIB_PRODUCTS_DIR + cmake -H. -Bbuild -DCMAKE_BUILD_TYPE=$CONFIG "-DBUILD_TESTS=0" + cmake --build build -j $BUILD_CONCURRENCY + CURR_ARCH=$HOSTTYPE + CURR_TMP_PRODUCT_DIR=$STAGING_DIR/$CONFIG/linux-$CURR_ARCH + mv $CURR_BUILD_PRODUCTS_DIR/libzt.* $CURR_TMP_PRODUCT_DIR + fi + # Java JAR file + if true; then + rm -rf $LIB_PRODUCTS_DIR + cmake -H. -Bbuild -DCMAKE_BUILD_TYPE=$CONFIG "-DJNI=1 -DBUILD_TESTS=0" + cmake --build build -j $BUILD_CONCURRENCY + CURR_ARCH=$HOSTTYPE + CURR_TMP_PRODUCT_DIR=$STAGING_DIR/$CONFIG/linux-$CURR_ARCH + mkdir -p $CURR_TMP_PRODUCT_DIR + cd $PROJROOT/packages/java + #cp $CURR_BUILD_PRODUCTS_DIR/libzt.so . + javac com/zerotier/libzt/*.java + jar cf zt.jar $CURR_BUILD_PRODUCTS_DIR/libzt.so com/zerotier/libzt/*.class + mv zt.jar $CURR_TMP_PRODUCT_DIR + cd - + fi + fi } main() { - # prepare environment generate_projects_if_necessary - mkdir -p ${WIN32_RELEASE_PRODUCTS_DIR} - mkdir -p ${WIN64_RELEASE_PRODUCTS_DIR} - mkdir -p ${WIN32_DEBUG_PRODUCTS_DIR} - mkdir -p ${WIN64_DEBUG_PRODUCTS_DIR} - # build build_all_products "release" build_all_products "debug" } diff --git a/packages/java/com/zerotier/libzt/ZTFDSet.java b/packages/java/com/zerotier/libzt/ZTFDSet.java new file mode 100644 index 0000000..cf5bd7b --- /dev/null +++ b/packages/java/com/zerotier/libzt/ZTFDSet.java @@ -0,0 +1,54 @@ +/* + * ZeroTier SDK - Network Virtualization Everywhere + * Copyright (C) 2011-2018 ZeroTier, Inc. https://www.zerotier.com/ + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + * -- + * + * You can be released from the requirements of the license by purchasing + * a commercial license. Buying such a license is mandatory as soon as you + * develop commercial closed-source software that incorporates or links + * directly against ZeroTier software without disclosing the source code + * of your own application. + */ + +package com.zerotier.libzt; + +public class ZTFDSet +{ + byte[] fds_bits = new byte[1024]; + + public void CLR(int fd) + { + fds_bits[fd] = 0x00; + } + + public boolean ISSET(int fd) + { + return fds_bits[fd] == 0x01; + } + + public void SET(int fd) + { + fds_bits[fd] = 0x01; + } + + public void ZERO() + { + for (int i=0; i<1024; i++) { + fds_bits[i] = 0x00; + } + } +} \ No newline at end of file diff --git a/packages/java/com/zerotier/libzt/ZTSocketAddress.java b/packages/java/com/zerotier/libzt/ZTSocketAddress.java new file mode 100644 index 0000000..c922251 --- /dev/null +++ b/packages/java/com/zerotier/libzt/ZTSocketAddress.java @@ -0,0 +1,92 @@ +/* + * ZeroTier SDK - Network Virtualization Everywhere + * Copyright (C) 2011-2018 ZeroTier, Inc. https://www.zerotier.com/ + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + * -- + * + * You can be released from the requirements of the license by purchasing + * a commercial license. Buying such a license is mandatory as soon as you + * develop commercial closed-source software that incorporates or links + * directly against ZeroTier software without disclosing the source code + * of your own application. + */ + +package com.zerotier.libzt; + +import com.zerotier.libzt.ZeroTier; + +import java.net.InetAddress; + +// Designed to transport address information across the JNI boundary +public class ZTSocketAddress +{ + public byte[] _ip6 = new byte[16]; + public byte[] _ip4 = new byte[4]; + + public int _family; + public int _port; // Also reused for netmask or prefix + + public ZTSocketAddress() {} + + public ZTSocketAddress(String ipStr, int port) + { + if(ipStr.contains(":")) { + _family = ZeroTier.AF_INET6; + try { + InetAddress ip = InetAddress.getByName(ipStr); + _ip6 = ip.getAddress(); + } + catch (Exception e) { } + } + else if(ipStr.contains(".")) { + _family = ZeroTier.AF_INET; + try { + InetAddress ip = InetAddress.getByName(ipStr); + _ip4 = ip.getAddress(); + } + catch (Exception e) { } + } + _port = port; + } + + public int getPort() { return _port; } + public int getNetmask() { return _port; } + public int getPrefix() { return _port; } + + private String ipString() + { + if (_family == ZeroTier.AF_INET) { + try { + InetAddress inet = InetAddress.getByAddress(_ip4); + return "" + inet.getHostAddress(); + } catch (Exception e) { + System.out.println(e); + } + } + if (_family == ZeroTier.AF_INET6) { + try { + InetAddress inet = InetAddress.getByAddress(_ip6); + return "" + inet.getHostAddress(); + } catch (Exception e) { + System.out.println(e); + } + } + return ""; + } + + public String toString() { return ipString() + ":" + _port; } + public String toCIDR() { return ipString() + "/" + _port; } +} diff --git a/packages/java/com/zerotier/libzt/ZeroTier.java b/packages/java/com/zerotier/libzt/ZeroTier.java new file mode 100644 index 0000000..26e337b --- /dev/null +++ b/packages/java/com/zerotier/libzt/ZeroTier.java @@ -0,0 +1,82 @@ +/* + * ZeroTier SDK - Network Virtualization Everywhere + * Copyright (C) 2011-2018 ZeroTier, Inc. https://www.zerotier.com/ + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + * -- + * + * You can be released from the requirements of the license by purchasing + * a commercial license. Buying such a license is mandatory as soon as you + * develop commercial closed-source software that incorporates or links + * directly against ZeroTier software without disclosing the source code + * of your own application. + */ + +package com.zerotier.libzt; + +import java.net.*; + +public class ZeroTier { + + public static int AF_INET = 2; + public static int AF_INET6 = 30; + public static int SOCK_STREAM = 1; + public static int SOCK_DGRAM = 2; + public static int O_APPEND = 1024; + public static int O_NONBLOCK = 2048; + public static int O_ASYNC = 8192; + public static int O_DIRECT = 65536; + public static int O_NOATIME = 262144; + public static int F_GETFL = 3; + public static int F_SETFL = 4; + + public native void start(String homePath, boolean blocking); + public native void startjoin(String homePath, long nwid); + public native void stop(); + public native boolean core_running(); + public native boolean stack_running(); + public native boolean ready(); + public native int join(long nwid); + public native int leave(long nwid); + public native String get_path(); + public native long get_node_id(); + public native int get_num_assigned_addresses(long nwid); + public native boolean get_address_at_index(long nwid, int index, ZTSocketAddress addr); + public native boolean has_address(long nwid); + public native boolean get_address(long nwid, int address_family, ZTSocketAddress addr); + public native void get_6plane_addr(long nwid, long nodeId, ZTSocketAddress addr); + public native void get_rfc4193_addr(long nwid, long nodeId, ZTSocketAddress addr); + + public native int socket(int family, int type, int protocol); + public native int connect(int fd, ZTSocketAddress addr); + public native int bind(int fd, ZTSocketAddress addr); + public native int listen(int fd, int backlog); + public native int accept(int fd, ZTSocketAddress addr); + public native int accept4(int fd, String addr, int port); + public native int close(int fd); + public native int setsockopt(int fd, int level, int optname, int optval, int optlen); + public native int getsockopt(int fd, int level, int optname, int optval, int optlen); + public native int sendto(int fd, byte[] buf, int flags, ZTSocketAddress addr); + public native int send(int fd, byte[] buf, int flags); + public native int recv(int fd, byte[] buf, int flags); + public native int recvfrom(int fd, byte[] buf, int flags, ZTSocketAddress addr); + public native int read(int fd, byte[] buf); + public native int write(int fd, byte[] buf); + public native int shutdown(int fd, int how); + public native boolean getsockname(int fd, ZTSocketAddress addr); + public native int getpeername(int fd, ZTSocketAddress addr); + public native int fcntl(int sock, int cmd, int flag); + public native int select(int nfds, ZTFDSet readfds, ZTFDSet writefds, ZTFDSet exceptfds, int timeout_sec, int timeout_usec); +} \ No newline at end of file diff --git a/packages/module.modulemap b/packages/module.modulemap new file mode 100644 index 0000000..2fd95d7 --- /dev/null +++ b/packages/module.modulemap @@ -0,0 +1,6 @@ +framework module zt { + umbrella header "Xcode-Bridging-Header.h" + + export * + module * { export * } +} diff --git a/packages/package.sh b/packages/package.sh new file mode 100755 index 0000000..f172623 --- /dev/null +++ b/packages/package.sh @@ -0,0 +1,37 @@ +#!/bin/bash + +PROJNAME="zt" +LIBNAME="lib"$PROJNAME +LIBZT_VERSION="1.2.0" +LIBZT_REVISION="1" +ZT_CORE_VERSION="1.2.12" +FILENAME_PREFIX=${LIBNAME}"-"${LIBZT_VERSION}"r"${LIBZT_REVISION} + +STAGING_DIR=$(pwd)/staging +STAGING_DEBUG_DIR=$(pwd)/staging/debug +STAGING_RELEASE_DIR=$(pwd)/staging/release +FINISHED_PRODUCTS_DIR=$(pwd)/products + +# Clean before zipping +find . -type f \( -name '*.DS_Store' -o -name 'thumbs.db' \) -delete + +# Emit a README file +echo $'ZeroTier Manual: https://www.zerotier.com/manual.shtml\n +Other Downloads: https://www.zerotier.com/download.shtml +\nlibzt Repo: https://github.com/zerotier/libzt' > ${STAGING_DIR}/README.md + +# Package everything together +# (debug) +PRODUCT_FILENAME=${FILENAME_PREFIX}-debug.tar.gz +echo "Making: " ${FINISHED_PRODUCTS_DIR}/${PRODUCT_FILENAME} +cd ${STAGING_DEBUG_DIR} +tar --exclude=${PRODUCT_FILENAME} -zcvf ${PRODUCT_FILENAME} . +mv *.tar.gz ${FINISHED_PRODUCTS_DIR} +cd - +# (release) +PRODUCT_FILENAME=${FILENAME_PREFIX}-release.tar.gz +echo "Making: " ${FINISHED_PRODUCTS_DIR}/${PRODUCT_FILENAME} +cd ${STAGING_RELEASE_DIR} +tar --exclude=${PRODUCT_FILENAME} -zcvf ${PRODUCT_FILENAME} . +mv *.tar.gz ${FINISHED_PRODUCTS_DIR} +cd - \ No newline at end of file