Skip to content

Commit 5d7d97f

Browse files
authored
Merge pull request scala#11099 from judovana/jdk11AndUpNatives
Making natives test working on newer jdk then 8
2 parents a32508f + 26134ec commit 5d7d97f

File tree

2 files changed

+46
-27
lines changed

2 files changed

+46
-27
lines changed

test/files/jvm/mkLibNatives.sh

Lines changed: 31 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
#!/bin/sh -e
1+
#!/bin/sh
2+
set -eu
23

34
##############################################################################
45
# Author : Stephane Micheloud
@@ -26,14 +27,14 @@ CLASS_DIR=natives-jvm.obj
2627

2728
if [ ! -f "${CLASS_DIR}/${CLASS_NAME}.class" ]; then
2829
echo "first you need to run this within sbt:"
29-
echo "partest --debug test/files/jvm/natives.scala"
30+
echo "sbt \"partest --debug test/files/jvm/natives.scala\""
3031
exit
3132
fi
3233

3334
OBJ_NAME=natives
3435
LIB_NAME=libnatives
3536

36-
if [ -z "${JAVA_HOME}" ]; then
37+
if [ -z "${JAVA_HOME:-}" ]; then
3738
echo "environment variable JAVA_HOME is undefined."
3839
exit
3940
elif $cygwin; then
@@ -42,16 +43,7 @@ elif $cygwin; then
4243
fi
4344

4445
JAVAH=${JAVA_HOME}/bin/javah
45-
JAVAH_OPTIONS="-jni -force -classpath ${CLASS_DIR} -o ${OBJ_NAME}.h"
46-
47-
if [ ! -f "${JAVAH}" ]; then
48-
# Oracle removed `javah`. The replacement is `javac -h`, but
49-
# requiring 8 seems fine for now, especially since we commit
50-
# the generated files to version control, so this script hardly
51-
# ever needs to be run at all
52-
echo "this script only works on Java 8"
53-
exit
54-
fi
46+
JAVA=${JAVA_HOME}/bin/java
5547

5648
CC=gcc
5749

@@ -76,11 +68,35 @@ else
7668
FULL_LIB_NAME=${LIB_NAME}.so
7769
fi
7870

71+
ljavah() {
72+
local lclass_dir="${1}"
73+
local lobj_name="${2}"
74+
local lclass_name="${3}"
75+
if [ -f "${JAVAH}" ]; then
76+
echo "javah exists in JAVA_HOME, will be used."
77+
${JAVAH} -jni -force -classpath ${lclass_dir} -o ${lobj_name}.h ${lclass_name}
78+
else
79+
echo "javah does not exist in JAVA_HOME. Wrapper for .h generation from .class filess will be downloaded and used."
80+
local gjavah_version=0.3.1
81+
local gjava=gjavah-${gjavah_version}.jar
82+
local asm=asm-9.1.jar
83+
local url="https://github.com/Glavo/gjavah/releases/download/${gjavah_version}"
84+
if [ ! -f "${gjava}" ]; then
85+
curl -k -f -L -O "${url}/${gjava}"
86+
fi
87+
if [ ! -f "${asm}" ]; then
88+
curl -k -f -L -O "${url}/${asm}"
89+
fi
90+
${JAVA} -jar ${gjava} -classpath ${lclass_dir} ${lclass_name}
91+
mv Test__.h ${lobj_name}.h
92+
fi
93+
}
94+
7995
##############################################################################
8096
# commands
8197

82-
[ $debug ] && echo ${JAVAH} ${JAVAH_OPTIONS} ${CLASS_NAME}
83-
${JAVAH} ${JAVAH_OPTIONS} ${CLASS_NAME}
98+
[ $debug ] && echo ljavah ${CLASS_DIR} ${OBJ_NAME} ${CLASS_NAME}
99+
ljavah ${CLASS_DIR} ${OBJ_NAME} ${CLASS_NAME}
84100

85101
[ $debug ] && echo ${CC} ${CC_OPTIONS} ${CC_INCLUDES} -o ${OBJ_NAME}.o natives.c
86102
${CC} ${CC_OPTIONS} ${CC_INCLUDES} -o ${OBJ_NAME}.o natives.c

test/files/jvm/natives.scala

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,21 @@ object Test {
44

55
//println("java.library.path=" + System.getProperty("java.library.path"))
66

7-
val os = System.getProperty("os.name")
8-
val arch = System.getProperty("os.arch")
9-
10-
val libName = (os, arch) match {
11-
case ("Mac OS X", "aarch64") =>
12-
"natives-arm"
13-
case ("Mac OS X", "x86_64") =>
14-
"natives-x86"
15-
case _ =>
16-
val wordSize = System.getProperty("sun.arch.data.model", "32")
17-
"natives-" + wordSize
18-
}
7+
val libName =
8+
// set to 'natives' for freshly built binary on linux. See mkLibNatives.sh
9+
sys.env.getOrElse("scala_test_nativelib", {
10+
val os = sys.props("os.name")
11+
val arch = sys.props("os.arch")
12+
(os, arch) match {
13+
case ("Mac OS X", "aarch64") =>
14+
"natives-arm"
15+
case ("Mac OS X", "x86_64") =>
16+
"natives-x86"
17+
case _ =>
18+
val wordSize = sys.props.getOrElse("sun.arch.data.model", "32")
19+
s"natives-$wordSize"
20+
}
21+
})
1922

2023
System.loadLibrary(libName)
2124

0 commit comments

Comments
 (0)