From 1bfd08da2c37b3ffb9e06ce9135c049da335f1fa Mon Sep 17 00:00:00 2001 From: Janrupf Date: Sat, 11 Apr 2020 18:00:43 +0200 Subject: [PATCH 1/7] Enable runtime AVX check --- opus-jni-native/3rdparty/CMakeLists.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/opus-jni-native/3rdparty/CMakeLists.txt b/opus-jni-native/3rdparty/CMakeLists.txt index c2cd8a7..b1581a3 100644 --- a/opus-jni-native/3rdparty/CMakeLists.txt +++ b/opus-jni-native/3rdparty/CMakeLists.txt @@ -6,7 +6,7 @@ set(OPUS_X86_MAY_HAVE_SSE OFF CACHE BOOL "" FORCE) set(OPUS_X86_MAY_HAVE_SSE2 OFF CACHE BOOL "" FORCE) set(OPUS_X86_MAY_HAVE_SSE4_1 OFF CACHE BOOL "" FORCE) -set(OPUS_X86_MAY_HAVE_AVX OFF CACHE BOOL "" FORCE) +set(OPUS_X86_MAY_HAVE_AVX ON CACHE BOOL "" FORCE) set(OPUS_X86_PRESUME_SSE OFF CACHE BOOL "" FORCE) set(OPUS_X86_PRESUME_SSE2 OFF CACHE BOOL "" FORCE) set(OPUS_ENABLE_FLOAT_API ON CACHE BOOL "" FORCE) From 16f1a27b6bb226b1b8b700c087bc4c7ea092dd47 Mon Sep 17 00:00:00 2001 From: Janrupf Date: Sat, 11 Apr 2020 18:12:39 +0200 Subject: [PATCH 2/7] Try force AVX off --- opus-jni-native/3rdparty/CMakeLists.txt | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/opus-jni-native/3rdparty/CMakeLists.txt b/opus-jni-native/3rdparty/CMakeLists.txt index b1581a3..6a78a1e 100644 --- a/opus-jni-native/3rdparty/CMakeLists.txt +++ b/opus-jni-native/3rdparty/CMakeLists.txt @@ -3,12 +3,15 @@ ############ # Thanks to someone having an old CPU :) +set(AVX_SUPPORTED OFF CACHE BOOL "" FORCE) set(OPUS_X86_MAY_HAVE_SSE OFF CACHE BOOL "" FORCE) set(OPUS_X86_MAY_HAVE_SSE2 OFF CACHE BOOL "" FORCE) set(OPUS_X86_MAY_HAVE_SSE4_1 OFF CACHE BOOL "" FORCE) -set(OPUS_X86_MAY_HAVE_AVX ON CACHE BOOL "" FORCE) +set(OPUS_X86_MAY_HAVE_AVX OFF CACHE BOOL "" FORCE) set(OPUS_X86_PRESUME_SSE OFF CACHE BOOL "" FORCE) set(OPUS_X86_PRESUME_SSE2 OFF CACHE BOOL "" FORCE) +set(OPUS_X86_MAY_HAVE_SSE4_1 OFF CACHE BOOL "" FORCE) +set(OPUS_X86_MAY_HAVE_AVX OFF CACHE BOOL "" FORCE) set(OPUS_ENABLE_FLOAT_API ON CACHE BOOL "" FORCE) set(OPUS_FIXED_POINT ON CACHE BOOL "" FORCE) add_subdirectory(opus) From 78cf6db94056278ab6dfe7fdc9347f595a4147d6 Mon Sep 17 00:00:00 2001 From: Janrupf Date: Sun, 12 Apr 2020 14:22:01 +0200 Subject: [PATCH 3/7] Support both 64 and 32 bit --- build.gradle | 43 ++++++++++++++++++- .../main/java/net/labymod/opus/OpusCodec.java | 40 +++++++++++++++-- opus-jni-native/build.gradle | 17 ++++++++ 3 files changed, 95 insertions(+), 5 deletions(-) diff --git a/build.gradle b/build.gradle index 3081460..9150ac3 100644 --- a/build.gradle +++ b/build.gradle @@ -19,7 +19,7 @@ def ciBuild = project.hasProperty("CI") && Boolean.parseBoolean(project.property if(!project.hasProperty("nativeBinaryExternalDir")) { if (Os.isFamily(Os.FAMILY_WINDOWS)) { // WARNING, this assumes CMake uses visual studio - // For GCC or Clang the output should be $nativeBinariesDir/libopus-jni-natibe.dll + // For GCC or Clang the output should be $nativeBinariesDir/libopus-jni-native.dll ext.nativeBinaries = Arrays.asList(new File(nativeBinariesDir, "Debug/opus-jni-native.dll")) } else if (Os.isFamily(Os.FAMILY_MAC)) { ext.nativeBinaries = Arrays.asList(new File(nativeBinariesDir, "libopus-jni-native.dylib")) @@ -49,6 +49,12 @@ if(!project.hasProperty("nativeBinaryExternalDir")) { isExternalBuild = true } +if(project.hasProperty("32bit")) { + ext.is32bit = true +} else { + ext.is32bit = false +} + project(":opus-jni-java").afterEvaluate { javaProject -> project("opus-jni-native").afterEvaluate { nativeProject -> nativeProject.tasks["build"].dependsOn(javaProject.tasks["generateJniHeaders"]) @@ -59,6 +65,26 @@ project(":opus-jni-java").afterEvaluate { javaProject -> } } +def getExtension(File file) { + String fileName = file.getName() + + if(fileName.lastIndexOf(".") != -1 && fileName.lastIndexOf(".") != 0) { + return fileName.substring(fileName.lastIndexOf(".") + 1) + } else { + return "" + } +} + +def getPathWithoutExtension(File file) { + String fileName = file.getAbsolutePath() + + if(fileName.lastIndexOf(".") != -1 && fileName.lastIndexOf(".") != 0) { + return fileName.substring(0, fileName.lastIndexOf(".")) + } else { + return "" + } +} + if(ciBuild) { def ciDir = file("ci") if(!ciDir.exists() && !ciDir.mkdirs()) { @@ -67,6 +93,21 @@ if(ciBuild) { new File(ciDir, "binaries").withWriter { for(File binary in ext.nativeBinaries) { + String extension = getExtension(binary) + String path = getPathWithoutExtension(binary) + + File newPath = null + if(ext.is32bit) { + newPath = new File(path + "-32" + extension) + binary.renameTo(newPath) + } else { + newPath = new File(path + "-64" + extension) + } + + println "Moving ${binary.absolutePath} tp ${newPath.absolutePath}" + binary.renameTo(newPath) + binary = newPath + it.write(binary.absolutePath) } } diff --git a/opus-jni-java/src/main/java/net/labymod/opus/OpusCodec.java b/opus-jni-java/src/main/java/net/labymod/opus/OpusCodec.java index e548fb2..2cef078 100644 --- a/opus-jni-java/src/main/java/net/labymod/opus/OpusCodec.java +++ b/opus-jni-java/src/main/java/net/labymod/opus/OpusCodec.java @@ -213,22 +213,54 @@ public Builder withMaxPacketSize(int maxPacketSize) { public OpusCodec build() { return new OpusCodec(OpusCodecOptions.of(frameSize, sampleRate, channels, bitrate, maxFrameSize, maxPacketSize)); } - } private static String getNativeLibraryName() { + String bitnessArch = System.getProperty("os.arch").toLowerCase(); + String bitnessDataModel = System.getProperty("sun.arch.data.model", null); + if(bitnessDataModel != null) { + bitnessArch = bitnessDataModel.toLowerCase(); + } + + boolean is64bit = bitnessArch.contains("64"); + if(is64bit) { + String library64 = processLibraryName("opus-jni-native-64"); + if(hasResource("/native-binaries/" + library64)) { + return library64; + } + } else { + String library32 = processLibraryName("opus-jni-native-32"); + if(hasResource("/native-binaries/" + library32)) { + return library32; + } + } + + String library = processLibraryName("opus-jni-native"); + if(!hasResource("/native-binaries/" + library)) { + throw new NoSuchElementException("No binary for the current system found, even after trying bit neutral names"); + } else { + return library; + } + } + + private static String processLibraryName(String library) { String systemName = System.getProperty("os.name", "bare-metal?").toLowerCase(); + if (systemName.contains("nux") || systemName.contains("nix")) { - return "libopus-jni-native.so"; + return "lib" + library + ".so"; } else if (systemName.contains("mac")) { - return "libopus-jni-native.dylib"; + return "lib" + library + ".dylib"; } else if (systemName.contains("windows")) { - return "opus-jni-native.dll"; + return library + ".dll"; } else { throw new NoSuchElementException("No native library for system " + systemName); } } + private static boolean hasResource(String resource) { + return OpusCodec.class.getResource(resource) != null; + } + public static void extractNatives(File directory) throws IOException { String nativeLibraryName = getNativeLibraryName(); Files.copy(OpusCodec.class.getResourceAsStream("/native-binaries/" + nativeLibraryName), diff --git a/opus-jni-native/build.gradle b/opus-jni-native/build.gradle index 637d50e..def46f1 100644 --- a/opus-jni-native/build.gradle +++ b/opus-jni-native/build.gradle @@ -1,4 +1,5 @@ import net.jan.gradle.cmake.tasks.CMakeBuildTask +import org.apache.tools.ant.taskdefs.condition.Os buildscript { dependencies { @@ -13,6 +14,22 @@ task build(type: CMakeBuildTask) { generationDirectory new File(buildDir, "cmake-gen") cmake { + if(is32bit) { + if(!Os.isFamily(Os.FAMILY_WINDOWS)) { + throw new GradleException("Only windows supports 32bit currently") + } else { + arguments = [ + "-A", "Win32", + "-T", "host=x64" + ] + } + } else if(Os.isFamily(Os.FAMILY_WINDOWS)) { + arguments = [ + "-A", "Win64", + "-T", "host=x64" + ] + } + variables = [ "CMAKE_BUILD_TYPE": java.util.Optional.of("Release"), "CMAKE_RUNTIME_OUTPUT_DIRECTORY": java.util.Optional.of(nativeBinariesDir.absolutePath), From ae191c878529d5e2ef92f34f759bf0e40c489e4c Mon Sep 17 00:00:00 2001 From: Janrupf Date: Sun, 12 Apr 2020 14:27:03 +0200 Subject: [PATCH 4/7] Update github workflow --- .github/workflows/ci.yml | 47 ++++++++++++++++++++++++++++++++-------- 1 file changed, 38 insertions(+), 9 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 9d8b4cb..bdfeb3c 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -2,11 +2,11 @@ name: CI on: [push] jobs: - build-binaries: + build-binaries-unix: strategy: fail-fast: false matrix: - os: [windows-latest, macos-latest, ubuntu-latest] + os: [macos-latest, ubuntu-latest] name: ${{ matrix.os }} build runs-on: ${{ matrix.os }} @@ -20,16 +20,45 @@ jobs: uses: actions/setup-java@v1 with: java-version: 8 - - name: Make gradlew executable (Unix) + - name: Make gradlew executable run: "chmod +x gradlew" - if: runner.os != 'Windows' - - name: Build native with gradle (Unix) + - name: Build native with gradle run: "./gradlew -PCI=true opus-jni-native:build" - if: runner.os != 'Windows' + - name: Prepare artifacts + shell: bash + run: | + mkdir artifacts + for file in $(cat ci/binaries); do # File generated by gradle + echo "Found binary artifact ${file}" + cp "${file}" artifacts + done + - name: Upload artifacts + uses: actions/upload-artifact@v1 + with: + name: native-binaries + path: artifacts + + build-binaries-windows: + strategy: + fail-fast: false + matrix: + arch: [32bit, 64bit] + + name: windows-latest build + runs-on: windows-latest + + steps: + - name: Checkout repository + uses: actions/checkout@v2 + with: + submodules: recursive + - name: Install java + uses: actions/setup-java@v1 + with: + java-version: 8 - name: Build native with gradle (Windows) - run: "gradlew -PCI=true opus-jni-native:build" + run: gradlew -PCI=true opus-jni-native:build -P${{ matrix.arch }} shell: cmd - if: runner.os == 'Windows' - name: Prepare artifacts shell: bash run: | @@ -45,7 +74,7 @@ jobs: path: artifacts build-jar: - needs: build-binaries + needs: [build-binaries-unix, build-binaries-windows] runs-on: ubuntu-latest # Doesn't matter, but linux tends to be the fastest name: Build final jar From 6aeff0298c12c8ad2b18fc1642e96eda5a788b10 Mon Sep 17 00:00:00 2001 From: Janrupf Date: Sun, 12 Apr 2020 14:34:02 +0200 Subject: [PATCH 5/7] Really move binaries --- build.gradle | 46 ++++++++++++++++++++++++++++++---------------- 1 file changed, 30 insertions(+), 16 deletions(-) diff --git a/build.gradle b/build.gradle index 9150ac3..a106995 100644 --- a/build.gradle +++ b/build.gradle @@ -85,6 +85,25 @@ def getPathWithoutExtension(File file) { } } +def processBinary(File binary, boolean doMove) { + String extension = getExtension(binary) + String path = getPathWithoutExtension(binary) + + File newPath = null + if(ext.is32bit) { + newPath = new File(path + "-32." + extension) + } else { + newPath = new File(path + "-64." + extension) + } + + if(doMove) { + println "Moving ${binary.absolutePath} to ${newPath.absolutePath}" + binary.renameTo(newPath) + } + + return newPath +} + if(ciBuild) { def ciDir = file("ci") if(!ciDir.exists() && !ciDir.mkdirs()) { @@ -93,22 +112,17 @@ if(ciBuild) { new File(ciDir, "binaries").withWriter { for(File binary in ext.nativeBinaries) { - String extension = getExtension(binary) - String path = getPathWithoutExtension(binary) - - File newPath = null - if(ext.is32bit) { - newPath = new File(path + "-32" + extension) - binary.renameTo(newPath) - } else { - newPath = new File(path + "-64" + extension) - } - - println "Moving ${binary.absolutePath} tp ${newPath.absolutePath}" - binary.renameTo(newPath) - binary = newPath - - it.write(binary.absolutePath) + it.write(processBinary(binary, false).absolutePath) } } + + task moveNativeBinaries { + for(File binary in ext.nativeBinaries) { + processBinary(binary, true) + } + } + + project("opus-jni-native").afterEvaluate { nativeProject -> + nativeProject.task["build"].finalizedBy(moveNativeBinaries) + } } From f41b0e45b355467bd2014f527d96538e48bd89bf Mon Sep 17 00:00:00 2001 From: Janrupf Date: Sun, 12 Apr 2020 14:38:42 +0200 Subject: [PATCH 6/7] Extract variable --- build.gradle | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/build.gradle b/build.gradle index a106995..ba9b227 100644 --- a/build.gradle +++ b/build.gradle @@ -116,13 +116,17 @@ if(ciBuild) { } } + def binaries = ext.nativeBinaries + task moveNativeBinaries { - for(File binary in ext.nativeBinaries) { - processBinary(binary, true) + doLast { + for (File binary in binaries) { + processBinary(binary, true) + } } } project("opus-jni-native").afterEvaluate { nativeProject -> - nativeProject.task["build"].finalizedBy(moveNativeBinaries) + nativeProject.tasks["build"].finalizedBy(moveNativeBinaries) } } From c9a2def335bd249b582c5c0a2f1c21d85c3b8954 Mon Sep 17 00:00:00 2001 From: Janrupf Date: Sun, 12 Apr 2020 14:45:43 +0200 Subject: [PATCH 7/7] Change Win64 to x64 --- opus-jni-native/build.gradle | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/opus-jni-native/build.gradle b/opus-jni-native/build.gradle index def46f1..8934978 100644 --- a/opus-jni-native/build.gradle +++ b/opus-jni-native/build.gradle @@ -25,7 +25,7 @@ task build(type: CMakeBuildTask) { } } else if(Os.isFamily(Os.FAMILY_WINDOWS)) { arguments = [ - "-A", "Win64", + "-A", "x64", "-T", "host=x64" ] }