forked from topjohnwu/ondk
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbuild.sh
More file actions
executable file
·141 lines (115 loc) · 3.22 KB
/
build.sh
File metadata and controls
executable file
·141 lines (115 loc) · 3.22 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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
#!/usr/bin/env bash
# Copyright 2022 Google LLC.
# SPDX-License-Identifier: Apache-2.0
set -e
if [ -z $1 ]; then
echo "Usage: $0 <arch>"
echo "<arch> is either x86_64 or aarch64"
echo
exit 1
fi
. common.sh
OS=$(uname | tr '[:upper:]' '[:lower:]')
ARCH="$1"
if [ $OS = "darwin" ]; then
NDK_DIRNAME='darwin-x86_64'
TRIPLE="${ARCH}-apple-darwin"
DYN_EXT='dylib'
if [ $ARCH = "aarch64" ]; then
# Configure jemalloc to use 16k pages for Apple Silicon
export JEMALLOC_SYS_WITH_LG_PAGE=14
fi
command -v ninja >/dev/null || brew install ninja
else
NDK_DIRNAME='linux-x86_64'
TRIPLE="${ARCH}-unknown-linux-gnu"
DYN_EXT='so'
command -v ninja >/dev/null || sudo apt-get install ninja-build
fi
build() {
cd rust
python3 ./x.py --config "../config-${OS}.toml" --host $TRIPLE install
cd ../
cd out
cp -af ../rust/build/$TRIPLE/llvm/bin llvm-bin
cp -af ../rust/build/$TRIPLE/llvm/lib/clang/$RUST_CLANG/include clang-include
ln -s ../lib/rustlib/$TRIPLE/bin/rust-lld llvm-bin/lld
ln -s lld llvm-bin/ld
find ../rust/build/$TRIPLE/llvm/lib -name "*.${DYN_EXT}*" -exec cp -an {} lib \;
cd ..
}
ndk() {
local NDK_ZIP="android-ndk-${NDK_VERSION}-${OS}.zip"
# Download and extract
[ -f $NDK_ZIP ] || curl -O -L "https://dl.google.com/android/repository/$NDK_ZIP"
unzip -q $NDK_ZIP
mv "android-ndk-${NDK_VERSION}" ndk
# Copy the whole output folder into ndk
cp -af out ndk/toolchains/rust
# Replace headers
cd ndk/toolchains
mv llvm/prebuilt/$NDK_DIRNAME llvm.dir
ln -s ../../llvm.dir llvm/prebuilt/$NDK_DIRNAME
rm -rf llvm.dir/lib64/clang/$NDK_CLANG/include
mv rust/clang-include llvm.dir/lib64/clang/$NDK_CLANG/include
# Redirect library
cd rust/lib
mkdir clang
ln -s ../../../llvm.dir/lib64/clang/$NDK_CLANG clang/$RUST_CLANG
cd ../../
# Replace files with those from the rust toolchain
cd llvm.dir/bin
ln -sf ../../rust/llvm-bin/* .
rm clang-14
cd ../lib64
ln -sf ../../rust/lib/*.$DYN_EXT* .
rm -f libclang.so.13 libLLVM-14git.so libLTO.so.14git libRemarks.so.14git
# Redirect library
cd ../lib
mkdir clang
ln -s ../../lib64/clang/$NDK_CLANG clang/$RUST_CLANG
cd ../../../../
}
universal() {
cp -af out.x86 out
cp -an out.arm/. out/. || true
# Replace lld link with universal binary
rm out/llvm-bin/lld
lipo -create -output out/llvm-bin/lld \
out/lib/rustlib/x86_64-apple-darwin/bin/rust-lld \
out/lib/rustlib/aarch64-apple-darwin/bin/rust-lld
# Merge all Mach-O files as universal binary and adhoc codesign
find out -type f -exec sh -c "file {} | grep -q Mach-O" \; -print0 | \
while IFS= read -r -d '' o; do
local a="${o/out/out.x86}"
local b="${o/out/out.arm}"
if [ -f "$a" -a -f "$b" ]; then
lipo -create -output "$o" "$a" "$b"
fi
codesign -s - "$o"
done
}
clone
if [ $OS = "darwin" -a $ARCH = "aarch64" ]; then
if [ ! -f tmp/stage-1.tar.gz ]; then
echo '! Missing stage 1 artifacts'
exit 1
fi
tar zxf tmp/stage-1.tar.gz
mv out out.x86
fi
build
if [ $OS = "darwin" ]; then
if [ $ARCH = "x86_64" ]; then
# Pack up first stage artifacts
mkdir tmp
tar zcf tmp/stage-1.tar.gz rust/build/$TRIPLE/ll* out
# Exit early
exit 0
else
mv out out.arm
universal
fi
fi
ndk
dist