-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.bash
More file actions
executable file
·385 lines (296 loc) · 7.55 KB
/
Copy pathbuild.bash
File metadata and controls
executable file
·385 lines (296 loc) · 7.55 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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
# SPDX-License-Identifier: MIT
shopt -s globstar
set -euo pipefail
#
print_usage() {
echo "Usage: $0 [flags]"
echo "Build options:"
echo " -d, --debug Enable debug build (default)"
echo " -r, --release Enable release build"
echo " --full-build Build/rebuild everything (default)"
echo " -c, --only-c Build only the C code"
echo " -j, --only-java Build only the Java code"
echo " --all-arch Build for all architecture (default)"
echo " --32-bit Build only for armeabi-v7a architecture"
echo " --64-bit Build only for arm64-v8a architecture"
echo " --proc=[proc] Enable parallelism for [proc] process (default is 4)"
echo " --conf=[file] Use [file] as compile config file"
echo
echo "Other options:"
echo " --clean Clean build artifacts"
echo " -h, --help Show help message"
}
#
build_dir="./.build"
cache_dir="./.cache"
res_dir="./res"
src_dir="./src"
java_src="$cache_dir/java_src.txt"
java_sdl="$src_dir/java/third_party/SDL3/SDL3.jar"
java_classes="$cache_dir/**/*.class"
c_dir="$src_dir/jni"
c_third_party="$c_dir/third_party"
incl="$c_third_party/include"
sdl_32="$c_third_party/lib/SDL3/armeabi-v7a"
sdl_64="$c_third_party/lib/SDL3/arm64-v8a"
lib_dir_32="$cache_dir/lib/armeabi-v7a"
lib_dir_64="$cache_dir/lib/arm64-v8a"
lib_32="$lib_dir_32/libmain.so"
lib_64="$lib_dir_64/libmain.so"
dir_32="$cache_dir/32"
dir_64="$cache_dir/64"
apk_unsigned="$build_dir/app-unsigned.apk"
apk_aligned="$build_dir/app-aligned.apk"
apk_signed="$build_dir/app-signed.apk"
manifest="./AndroidManifest.xml"
flat="$cache_dir/*.flat"
build_type="debug"
build_which="all"
build_arch="all"
proc="4"
clean="false"
#
if [[ $# -eq 0 ]]; then
print_usage
exit 1
fi
while [[ $# -gt 0 ]]; do
case "$1" in
-h|--help)
print_usage
exit 0
;;
-d|--debug)
build_type="debug"
shift
;;
-r|--release)
build_type="release"
shift
;;
--full-build)
build_which="all"
shift
;;
-j|--only-java)
build_which="java"
shift
;;
-c|--only-c)
build_which="c"
shift
;;
--all-arch)
build_arch="all"
shift
;;
--32-bit)
build_arch="32-bit"
shift
;;
--64-bit)
build_arch="64-bit"
shift
;;
--proc=*)
proc="${1#*=}"
shift
;;
--conf=*)
conf_file="${1#*=}"
shift
;;
--clean)
clean="true"
shift
;;
*)
echo "Unknown option: $1"
exit 1
;;
esac
done
#
if [[ "$clean" == "true" ]]; then
echo "Cleaning build artifacts"
rm -rf $build_dir $cache_dir
echo "Done"
exit 0
fi
#
source $conf_file
android_home="$HOME/.local/share/android"
sdk="$android_home/platforms/android-$target_sdk_ver/android.jar"
ndk="$android_home/ndk/$ndk_ver"
ndk_bin="$ndk/toolchains/llvm/prebuilt/linux-x86_64/bin"
#
aapt2_compile_flag="compile -o $cache_dir --dir $res_dir"
aapt2_link_flag="link -I $sdk --manifest $manifest -o $apk_unsigned $flat"
javac_flag="--release 17 -cp "$sdk:$java_sdl" -d $cache_dir @$java_src"
d8_flag="--min-api $min_sdk_ver --lib $sdk --output $cache_dir $java_classes $java_sdl"
cc_32="$ndk_bin/armv7a-linux-androideabi$target_sdk_ver-clang"
cc_64="$ndk_bin/aarch64-linux-android$target_sdk_ver-clang"
cc_flag_c_d="-std=c99 -Wall -Wextra -Wpedantic -O0 -g3 -fno-omit-frame-pointer -isystem ../.$incl -fcolor-diagnostics -c -fPIC"
cc_flag_c_r="-std=c99 -Wall -Wextra -Wpedantic -O2 -DNDEBUG -isystem ../.$incl -fcolor-diagnostics -c -fPIC"
cc_flag_l="-shared"
ld_flag_32="-L$sdl_32 -lSDL3 -lGLESv3"
ld_flag_64="-L$sdl_64 -lSDL3 -lGLESv3"
cc_strip="$ndk_bin/llvm-strip"
#
print_config() {
echo "Build type: $build_type"
echo "Build architecture: $build_arch"
echo
}
setup_libs() {
if [[ "$build_which" == "all" ]]; then
echo "Extracting libraries"
7z -y x $c_third_party/lib/SDL3/libSDL3.7z -o$c_third_party/lib/SDL3/
echo "Copying libraries"
case "$build_arch" in
"32-bit")
mkdir -p $lib_dir_32
cp $sdl_32/libSDL3.so $lib_dir_32
;;
"64-bit")
mkdir -p $lib_dir_64
cp $sdl_64/libSDL3.so $lib_dir_64
;;
*)
mkdir -p $lib_dir_32 $lib_dir_64
cp $sdl_32/libSDL3.so $lib_dir_32
cp $sdl_64/libSDL3.so $lib_dir_64
;;
esac
fi
}
compile_link_res() {
echo "Compiling resources"
aapt2 $aapt2_compile_flag
echo "Linking resources"
aapt2 $aapt2_link_flag
}
compile_java() {
if [[ "$build_which" == "java" || "$build_which" == "all" ]]; then
echo "Compiling Java sources"
find $src_dir -name "*.java" > $java_src
javac $javac_flag
echo "Dexing Java classes"
d8 $d8_flag
fi
}
_compile_c_32_d() {
cd $dir_32
printf "../.$c_dir/%s\n" "${c_src[@]}" | parallel -j $proc $cc_32 $cc_flag_c_d {}
cd $OLDPWD
}
_compile_c_32_r() {
cd $dir_32
printf "../.$c_dir/%s\n" "${c_src[@]}" | parallel -j $proc $cc_32 $cc_flag_c_r {}
cd $OLDPWD
}
_compile_c_64_d() {
cd $dir_64
printf "../.$c_dir/%s\n" "${c_src[@]}" | parallel -j $proc $cc_64 $cc_flag_c_d {}
cd $OLDPWD
}
_compile_c_64_r() {
cd $dir_64
printf "../.$c_dir/%s\n" "${c_src[@]}" | parallel -j $proc $cc_64 $cc_flag_c_r {}
cd $OLDPWD
}
_link_c_32() {
$cc_32 $cc_flag_l -o $lib_32 $dir_32/*.o $ld_flag_32
}
_link_c_64() {
$cc_64 $cc_flag_l -o $lib_64 $dir_64/*.o $ld_flag_64
}
cc_build_variant="${build_type}_${build_arch}"
compile_c() {
if [[ "$build_which" == "c" || "$build_which" == "all" ]]; then
case "$cc_build_variant" in
"release_32-bit")
mkdir -p $lib_dir_32 $dir_32
echo "Compiling C sources"
_compile_c_32_r
echo "Linking C objects"
_link_c_32
;;
"release_64-bit")
mkdir -p $lib_dir_64 $dir_64
echo "Compiling C sources"
_compile_c_64_r
echo "Linking C objects"
_link_c_64
;;
"release_all")
mkdir -p $lib_dir_32 $dir_32 $lib_dir_64 $dir_64
echo "Compiling C sources"
_compile_c_32_r
_compile_c_64_r
echo "Linking C objects"
_link_c_32
_link_c_64
;;
"debug_32-bit")
mkdir -p $lib_dir_32 $dir_32
echo "Compiling C sources"
_compile_c_32_d
echo "Linking C objects"
_link_c_32
;;
"debug_64-bit")
mkdir -p $lib_dir_64 $dir_64
echo "Compiling C sources"
_compile_c_64_d
echo "Linking C objects"
_link_c_64
;;
*)
mkdir -p $lib_dir_32 $dir_32 $lib_dir_64 $dir_64
echo "Compiling C sources"
_compile_c_32_d
_compile_c_64_d
echo "Linking C objects"
_link_c_32
_link_c_64
;;
esac
fi
}
c_strip() {
if [[ "$build_type" == "release" ]]; then
echo "Stripping shared libraries"
$cc_strip --strip-unneeded $cache_dir/**/*.so
fi
}
zip_libs() {
echo "Zipping APK"
cd $cache_dir
zip -0 -ur .$apk_unsigned ./lib ./*.dex
cd $OLDPWD
}
apk_align() {
echo "Aligning APK"
zipalign -v 4 $apk_unsigned $apk_aligned
}
apk_sign() {
echo "Signing APK"
apksigner sign --ks $ks_file --ks-key-alias $ks_alias --ks-pass pass:$ks_pass --out $apk_signed $apk_aligned
}
#
main() {
rm -rf $build_dir
mkdir -p $build_dir $cache_dir
print_config
setup_libs
compile_link_res
compile_java
compile_c
c_strip
zip_libs
apk_align
apk_sign
echo "Done"
}
main