Skip to content

Commit

Permalink
#3739: Support aarch64 natives
Browse files Browse the repository at this point in the history
  • Loading branch information
Outfluencer authored and md-5 committed Sep 7, 2024
1 parent 8f8c270 commit eca6090
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 2 deletions.
29 changes: 29 additions & 0 deletions native/compile-native-arm.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#!/bin/sh

set -eu

CWD=$(pwd)

echo "Compiling mbedtls"
(cd mbedtls && CFLAGS="-fPIC -I$CWD/src/main/c -DMBEDTLS_USER_CONFIG_FILE='<mbedtls_custom_config.h>'" make CC=aarch64-linux-gnu-gcc AR=aarch64-linux-gnu-ar no_test)

echo "Compiling zlib"
(cd zlib && CFLAGS="-fPIC -DNO_GZIP" CC=aarch64-linux-gnu-gcc CHOST=arm64 ./configure --target="aarch64" --static && make CFLAGS="-fPIC -march=armv8-a+crc" CC=aarch64-linux-gnu-gcc AR=aarch64-linux-gnu-ar)

CC="aarch64-linux-gnu-gcc"
CFLAGS="-c -fPIC -O3 -Wall -Werror -I$JAVA_HOME/include/ -I$JAVA_HOME/include/linux/"
LDFLAGS="-shared"

echo "Compiling bungee"
$CC $CFLAGS -o shared.o src/main/c/shared.c
$CC $CFLAGS -Imbedtls/include -o NativeCipherImpl.o src/main/c/NativeCipherImpl.c
$CC $CFLAGS -Izlib -o NativeCompressImpl.o src/main/c/NativeCompressImpl.c

echo "Linking native-cipher-arm.so"
$CC $LDFLAGS -o src/main/resources/native-cipher-arm.so shared.o NativeCipherImpl.o mbedtls/library/libmbedcrypto.a

echo "Linking native-compress-arm.so"
$CC $LDFLAGS -o src/main/resources/native-compress-arm.so shared.o NativeCompressImpl.o zlib/libz.a

echo "Cleaning up"
rm shared.o NativeCipherImpl.o NativeCompressImpl.o
8 changes: 7 additions & 1 deletion native/src/main/c/NativeCompressImpl.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@

#include <zlib.h>
#include "shared.h"
#if !defined(__aarch64__)
#include "cpuid_helper.h"
#endif
#include "net_md_5_bungee_jni_zlib_NativeCompressImpl.h"

typedef unsigned char byte;
Expand All @@ -28,7 +30,11 @@ jint throwException(JNIEnv *env, const char* message, int err) {
}

JNIEXPORT jboolean JNICALL Java_net_md_15_bungee_jni_zlib_NativeCompressImpl_checkSupported(JNIEnv* env, jobject obj) {
return (jboolean) checkCompressionNativesSupport();
#if !defined(__aarch64__)
return (jboolean) checkCompressionNativesSupport();
#else
return JNI_TRUE;
#endif
}

void JNICALL Java_net_md_15_bungee_jni_zlib_NativeCompressImpl_reset(JNIEnv* env, jobject obj, jlong ctx, jboolean compress) {
Expand Down
13 changes: 12 additions & 1 deletion native/src/main/java/net/md_5/bungee/jni/NativeCode.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ public boolean load()
{
if ( enableNativeFlag && !loaded && isSupported() )
{
String name = this.name + ( isAarch64() ? "-arm" : "" );
String fullName = "bungeecord-" + name;

try
Expand Down Expand Up @@ -94,6 +95,16 @@ public boolean load()

public static boolean isSupported()
{
return "Linux".equals( System.getProperty( "os.name" ) ) && "amd64".equals( System.getProperty( "os.arch" ) );
return "Linux".equals( System.getProperty( "os.name" ) ) && ( isAmd64() || isAarch64() );
}

private static boolean isAmd64()
{
return "amd64".equals( System.getProperty( "os.arch" ) );
}

private static boolean isAarch64()
{
return "aarch64".equals( System.getProperty( "os.arch" ) );
}
}
Binary file added native/src/main/resources/native-cipher-arm.so
Binary file not shown.
Binary file added native/src/main/resources/native-compress-arm.so
Binary file not shown.

0 comments on commit eca6090

Please sign in to comment.