forked from pschichtel/JavaCAN
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcompile-native.sh
More file actions
executable file
·77 lines (68 loc) · 1.84 KB
/
compile-native.sh
File metadata and controls
executable file
·77 lines (68 loc) · 1.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#!/usr/bin/env bash
java_home="${1?no java home given}"
libname="${2?no lib name given}"
output_dir="${3?no output directory given}"
cc_opts="-shared -std=c99 -O2 -flto -fPIC"
base="target"
jni="$base/jni"
jni_headers="$base/java-jni-headers"
jni_libs="$base/java-jni-libs"
src="src/main/c"
rm -R "$jni_headers" "$jni_libs" 2>/dev/null
cp -r "${java_home}/include" "$jni_headers"
cp -r "${java_home}/lib" "$jni_libs"
mkdir -p "$output_dir" 2>/dev/null
translate_arch() {
case "$1" in
x86)
echo "x86_32"
;;
x64)
echo "x86_64"
;;
arm64)
echo "aarch64"
;;
*)
echo "$1"
;;
esac
}
if [[ -z "${BUILD_ARCH}" ]]
then
archs=(x86 x64 armv7 arm64)
else
archs=(${BUILD_ARCH})
fi
compiler_dir="$base/cross-compile"
for arch in "${archs[@]}"
do
translated_arch=$(translate_arch "$arch")
echo "Compiling for: $translated_arch"
image="dockcross/linux-$arch"
dir="$compiler_dir/$arch"
mkdir -p "$dir" 2>/dev/null
proxy="${dir}/proxy"
docker run --rm "$image" > "$proxy"
chmod +x "$proxy"
lib_output="$base/lib$libname-${translated_arch}.so"
includes=(
-I"$src"
-I"$jni"
-I"src/include"
-I"$jni_headers"
-I"$jni_headers/linux"
)
out_files=()
CC="$("$proxy" bash -c 'echo "$CC"')"
for c_file in "$src"/*.c "$jni"/*.c
do
name="$(basename "$c_file" .c)"
out_file="$dir/$(dirname "$c_file")/$name.o"
mkdir -p "$(dirname "$out_file")"
"$proxy" "$CC" "${includes[@]}" -Werror -o"$out_file" -c "$c_file" $cc_opts || exit 1
out_files+=("$out_file")
done
"$proxy" "$CC" -I "$jni_libs" -o"$lib_output" "${out_files[@]}" -z noexecstack $cc_opts -fvisibility=hidden || exit 1
mv "$lib_output" "$output_dir"
done