-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild-toolkit.sh
1396 lines (1280 loc) · 36.9 KB
/
build-toolkit.sh
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
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env bash
if [[ -z "$BASH_VERSION" ]]; then
echo "[error] This script must be run with Bash (BASH_VERSION is not set)" >&2
exit 1
fi
if [[ "$(uname -s)" == "Darwin" ]]; then
if (( BASH_VERSINFO[0] < 3 && BASH_VERSINFO[1] < 2 )); then
echo "[error] Bash 3.2 or newer is required (found $BASH_VERSION)" >&2
exit 1
fi
else
if (( BASH_VERSINFO[0] < 4 )); then
echo "[error] Bash 4.0 or newer is required (found $BASH_VERSION)" >&2
exit 1
fi
fi
append_pkg_config_path() {
local new_path="$1"
if [[ ":$PKG_CONFIG_PATH:" != *":$new_path:"* ]]; then
export PKG_CONFIG_PATH="$PKG_CONFIG_PATH:$new_path"
fi
}
append_pkg_config_path /usr/local/lib/pkgconfig
append_pkg_config_path /usr/local/share/pkgconfig
append_pkg_config_path /usr/lib/pkgconfig
export ACLOCAL_PATH=/usr/share/aclocal
RUN_UPDATES=false
RUN_NO_CACHE=false
export OS_NAME
OS_NAME="$(uname -s | tr '[:upper:]' '[:lower:]')"
: "${MAIN_SCRIPT:="$0"}"
for arg in "$@"; do
if [[ $arg == --update ]]; then
RUN_UPDATES=true
elif [[ $arg == --no-cache ]]; then
RUN_NO_CACHE=true
elif [[ $arg == --platform=* ]]; then
export OS_NAME="${arg#--platform=}"
fi
done
export BUILD_KIT_DIR
BUILD_KIT_DIR="$(pwd)/.buildkit"
export DEFAULT_BUILD_FOLDER="$BUILD_KIT_DIR/build"
export DEFAULT_TOOLS_FOLDER="$BUILD_KIT_DIR/tools"
export CACHE_FILE
if $RUN_NO_CACHE; then
CACHE_FILE="$(mktemp)"
trap '[[ -f "$CACHE_FILE" ]] && rm -f "$CACHE_FILE"' EXIT
else
CACHE_FILE="$BUILD_KIT_DIR/fox.cache"
fi
export VS_BASE_PATH="/c/Program Files/Microsoft Visual Studio"
export WINDOWS_KITS_BASE_PATH="/c/Program Files (x86)/Windows Kits/10"
mkdir -p "$BUILD_KIT_DIR"
os_lib_format() {
local is_static=false
case "$1" in
static)
is_static=true
;;
dynamic)
is_static=false
;;
*)
echo "[error] Unknown library type: $1" >&2
exit 1
;;
esac
lib_name="${2#lib}"
lib_name="${lib_name%.a}"
lib_name="${lib_name%.dylib}"
lib_name="${lib_name%.dll}"
lib_name="${lib_name%.so}"
if is_windows; then
if $is_static; then
echo "$lib_name.lib"
else
echo "$lib_name.dll"
fi
elif is_linux || is_android; then
if $is_static; then
echo "lib$lib_name.a"
else
echo "lib$lib_name.so"
fi
elif is_macos; then
if $is_static; then
echo "lib$lib_name.a"
else
echo "lib$lib_name.dylib"
fi
else
echo "[error] Unknown OS: $(uname -s)" >&2
exit 1
fi
}
import() {
local is_import=true
local file_name=$1
local found_from=false
local remote_source=''
local internal_source=''
local content=''
for arg in "$@"; do
if $is_import; then
is_import=false
elif [[ "$arg" == from ]]; then
found_from=true
elif $found_from; then
remote_source="$arg"
break
else
echo "[error] Invalid argument: $arg" >&2
return 1
fi
done
if [[ -z "$remote_source" ]] && $found_from; then
echo "No remote source provided for import $file_name" >&2
return 1
fi
if [[ -z "$file_name" ]]; then
echo "No file name provided for import" >&2
return 1
fi
if [[ ! -f "$file_name" ]] && ! $found_from; then
echo "Failed to import $file_name, file not found" >&2
return 1
fi
local allow_file_detection=true
if $found_from; then
case "$remote_source" in
python*)
if ! "$remote_source" -m pip show "$file_name" &>/dev/null; then
"$remote_source" -m pip install "$file_name" -U
fi
allow_file_detection=false
;;
rust)
if ! cargo install --list | grep -q "^$file_name v"; then
cargo install "$file_name"
fi
allow_file_detection=false
;;
*)
internal_source="$remote_source"
internal_source="${internal_source%/}"
internal_source="$internal_source/$file_name"
normalized_source="${remote_source#http://}"
normalized_source="${normalized_source#https://}"
if [[ "$normalized_source" == github.com/* ]]; then
temp_remote="${normalized_source/github.com\//}"
temp_remote="${temp_remote%.git}"
repo_owner="${temp_remote%%/*}"
rest="${temp_remote#*/}"
repo_name="${rest%%/*}"
remote_source="github.com/$repo_owner/$repo_name"
internal_source="https://raw.githubusercontent.com/$repo_owner/$repo_name/master/$file_name"
fi
response=$(curl -L -s -w "%{http_code}" "$internal_source")
http_code="${response: -3}"
content="${response:0:${#response}-3}"
if [[ "$http_code" -ge 400 ]]; then
echo "[error] Failed to import: $file_name" >&2
echo " source returned HTTP $http_code ($remote_source)" >&2
exit 1
elif [[ -z "$content" ]]; then
echo "[error] Failed to import: $file_name" >&2
echo " empty content received from $remote_source" >&2
exit 1
fi
;;
esac
else
content=$(<"$file_name")
fi
if $allow_file_detection; then
if [[ "$file_name" == *.properties ]]; then
while IFS= read -r line; do
if [[ "$line" =~ ^[[:space:]]*([^=[:space:]]+)[[:space:]]*=[[:space:]]*(.*)$ ]]; then
raw_key="${BASH_REMATCH[1]}"
value="${BASH_REMATCH[2]}"
value="${value//$'\r'/}"
if [[ "$raw_key" =~ \.git$ ]]; then
raw_key="${raw_key%.git}"
echo "[warn] Trailing '.git' suffix detected in $raw_key" >&2
echo " This suffix is not required for the import" >&2
fi
if [[ ! "$raw_key" =~ ^[a-zA-Z0-9._/-]+$ ]]; then
echo "[error] Invalid import: $raw_key" >&2
echo " invalid characters in import declaration" >&2
exit 1
fi
local is_updatable=true
if [[ "$value" =~ ^\$ ]]; then
var_ref="$(echo "$value" | tr '[:lower:]' '[:upper:]')"
var_ref="${var_ref//-/_}"
var_ref="LIB_${var_ref#\$}_VERSION"
var_ref="${!var_ref}"
if [[ -n "$var_ref" ]]; then
value="$var_ref"
is_updatable=false
else
echo "[error] Invalid import: $raw_key" >&2
echo " invalid import reference in import declaration" >&2
exit 1
fi
fi
if $is_updatable && is_git_commit "$raw_key" "$value"; then
is_updatable=false
fi
key=$(basename "$raw_key")
key="$(echo "$key" | tr '[:lower:]' '[:upper:]')"
key="${key//-/_}"
version_var="LIB_${key}_VERSION"
source_var="LIB_${key}_SOURCE"
if [[ -n "${!version_var}" ]]; then
echo "[warn] version for $raw_key already set" >&2
echo " previous value : ${!version_var}" >&2
echo " declared at : ${!source_var}" >&2
echo " overriding with : $value" >&2
echo " imported from : $remote_source/$file_name" >&2
fi
if ! is_valid_repo "$raw_key"; then
echo "[error] Invalid import: $raw_key" >&2
echo " invalid git repository in import declaration" >&2
exit 1
fi
prefix=$(find_prefix_tag "$raw_key" "$value")
if [[ $? -ne 0 ]]; then
exit 1
fi
pkg_config_path="$(read_cache "lib" "$raw_key")"
if [[ -n "$pkg_config_path" ]]; then
append_pkg_config_path "$pkg_config_path/lib/pkgconfig"
fi
export "$version_var=$value"
export "$source_var=$remote_source/$file_name"
export "LIB_${key}_GIT=$raw_key"
export "LIB_${key}_PREFIX=$prefix"
if $is_updatable; then
export "LIB_${key}_UPDATABLE=true"
fi
fi
done <<< "$content"
elif [[ "$file_name" == *.sh || "$file_name" == *.env ]]; then
source /dev/stdin <<< "$content"
else
echo "[error] Unknown import file type: $file_name" >&2
exit 1
fi
fi
local caller_file="${BASH_SOURCE[1]}"
local caller_line="${BASH_LINENO[0]}"
local last_line
if [[ "$caller_file" != "$MAIN_SCRIPT" ]]; then
return
fi
last_line=$(
grep -n -E '^[[:space:]]*import\b' "$caller_file" \
| cut -d: -f1 \
| sort -n \
| tail -n1
)
if [[ "$caller_line" -eq "$last_line" ]] && $RUN_UPDATES; then
update_dependencies
exit 0
fi
}
update_dependencies() {
for var in $(compgen -v | grep 'LIB_.*_UPDATABLE$'); do
local source_var="${var/_UPDATABLE/_SOURCE}"
local version_var="${var/_UPDATABLE/_VERSION}"
local rgx
version_var="${!version_var}"
local git_var="${var/_UPDATABLE/_GIT}"
git_var="${!git_var}"
if [[ -z "${!source_var}" ]] || [[ ! "${!source_var}" =~ ^/ ]]; then
continue
fi
echo "[info] Checking for updates in $git_var" >&2
latest_version="$(find_latest_version "$git_var" "$version_var")"
if [[ -z "$latest_version" ]]; then
echo "[error] Failed to find latest version for $git_var" >&2
exit 1
fi
if [[ "$latest_version" == "$version_var" ]]; then
echo "[info] $git_var is up to date" >&2
continue
fi
echo "[info] $git_var is outdated" >&2
echo " Current version: $version_var" >&2
echo " Updating to $latest_version" >&2
source_var="${!source_var/\//}"
rgx="s|^$git_var=.*|$git_var=$latest_version|"
if is_macos; then
sed -i "" "$rgx" "$source_var"
else
sed -i "$rgx" "$source_var"
fi
echo "[info] Updated $git_var to $latest_version" >&2
done
}
is_valid_repo() {
local repo="https://$1.git"
read_cache "is_valid" "$1" &>/dev/null
if [[ $? -eq 0 ]]; then
return 0
fi
if git ls-remote "$repo" &>/dev/null; then
write_cache "is_valid" "$1" "true"
return 0
fi
return 1
}
git_list_tags() {
local repo="https://$1.git"
git ls-remote --tags "$repo" 2>/dev/null | \
awk -F'refs/tags/' '/refs\/tags\// {print $2}' | \
sed 's/\^{}//' | sort -u
}
is_git_commit() {
read_cache "is_commit" "$1:$2" &>/dev/null
if [[ $? -eq 0 ]]; then
return 0
fi
echo "$2" | grep -Eq '^[0-9a-fA-F]{7,40}$' || return 1
if git ls-remote "https://$1.git" | awk '{print $1}' | grep -qx "$2"; then
write_cache "is_commit" "$1:$2" "true"
return 0
else
return 1
fi
}
write_cache() {
local index_name="$1"
local repo="$2"
local value="$3"
local rgx=""
if [[ -f "$CACHE_FILE" ]] && grep -q "^$index_name:$repo=" "$CACHE_FILE"; then
rgx="s|^$index_name:$repo=.*|$index_name:$repo=$value|"
if is_macos; then
sed -i "" "$rgx" "$CACHE_FILE"
else
sed -i "$rgx" "$CACHE_FILE"
fi
else
echo "$index_name:$repo=$value" >> "$CACHE_FILE"
fi
}
read_cache() {
local index_name="$1"
local repo="$2"
rgx="^$index_name:$repo="
if [[ -f "$CACHE_FILE" ]]; then
if grep -q "$rgx" "$CACHE_FILE"; then
grep "$rgx" "$CACHE_FILE" | cut -d= -f2-
return 0
fi
fi
return 1
}
retrieve_prefix() {
local tags="$1"
local base_version="$2"
matching_tags=$(echo "$tags" | grep "$base_version" | head -n 1)
if [[ -z "$matching_tags" ]]; then
return 1
fi
echo "$matching_tags" | sed -E 's/[0-9]+(\.|_)[0-9]+.*//'
}
identify_separator() {
local tags="$1"
local base_version="$2"
matching_tags=$(echo "$tags" | grep "$base_version" | head -n 1)
if [[ -z "$matching_tags" ]]; then
return 1
fi
if [[ "$matching_tags" =~ [0-9]+(\.|_)[0-9]+ ]]; then
echo "${BASH_REMATCH[1]}"
else
return 1
fi
}
apply_separator() {
local version="$1"
local separator="$2"
echo "$version" | sed -E "s/([0-9]+)(\.|_)/\1$separator/g"
}
find_prefix_tag() {
local repo="$1"
local base_version="$2"
local prefix=""
local separator=""
local tags=""
index_cache=$(read_cache "prefix" "$repo:$base_version")
if [[ $? -eq 0 ]]; then
echo "${index_cache%%;*}"
return 0
fi
echo "[info] Importing $repo" >&2
if ! is_git_commit "$repo" "$base_version"; then
tags=$(git_list_tags "$repo")
prefix=$(retrieve_prefix "$tags" "$base_version")
if [[ $? -ne 0 ]]; then
echo "[error] Failed to find prefix for $repo" >&2
return 1
fi
separator=$(identify_separator "$tags" "$base_version")
fi
write_cache "prefix" "$repo:$base_version" "$prefix;$separator"
echo "$prefix"
}
find_latest_version() {
local repo="$1"
local base_version="$2"
local void_prefix=""
local suffix=""
tags=$(git_list_tags "$repo")
index_cache=$(read_cache "prefix" "$repo:$base_version")
if [[ -z "$index_cache" ]]; then
prefix=$(retrieve_prefix "$tags" "$base_version")
separator=$(identify_separator "$tags" "$base_version")
write_cache "prefix" "$repo:$base_version" "$prefix;$separator"
fi
prefix="${index_cache%%;*}"
separator="${index_cache#*;}"
if [[ "$base_version" =~ ^[0-9] ]]; then
void_prefix="[0-9]"
fi
if [[ "$base_version" =~ [0-9]$ ]]; then
suffix=".*[0-9]$"
else
suffix=".*-$(echo "$base_version" | sed -E 's/.*[0-9]+-//')$"
fi
base_version="$(apply_separator "$base_version" "$separator")"
pattern_version="^$(echo "$base_version" | perl -pe 's/\d+/\\d+/g; s/\./\\./g; s/\//\\\//g;')$"
apply_separator "$(echo "$tags" \
| grep "^$prefix$void_prefix$suffix" \
| while read -r tag; do
ver="${tag/$prefix/}"
if [[ "$ver" == "$base_version" || "$ver" > "$base_version" ]] && \
echo "$ver" | perl -ne 'exit 1 unless /'"$pattern_version"'/'; then
echo "$ver"
fi
done \
| sort -V \
| awk 'END { print $1 }')" "."
}
url_encode() {
local string="$1"
local strlen=${#string}
local encoded=""
for (( pos=0 ; pos<strlen ; pos++ )); do
c=${string:$pos:1}
case "$c" in
[a-zA-Z0-9.~_-]) encoded+="$c" ;;
*) printf -v hex '%%%02X' "'$c"
encoded+="$hex"
esac
done
echo "$encoded"
}
dependency_version() {
local key="$1"
key="$(echo "$key" | tr '[:lower:]' '[:upper:]')"
key="${key//-/_}"
local tag_prefix="LIB_${key}_PREFIX"
local version_var="LIB_${key}_VERSION"
echo "${!tag_prefix}${!version_var}"
}
is_windows() {
case "$OS_NAME" in
cygwin*|mingw*|msys*|windows)
return 0
;;
esac
return 1
}
is_linux() {
case "$OS_NAME" in
linux)
return 0
;;
esac
return 1
}
is_macos() {
case "$OS_NAME" in
darwin)
return 0
;;
esac
return 1
}
is_android() {
case "$OS_NAME" in
android)
return 0
;;
esac
return 1
}
get_vs_edition() {
local VS_YEAR VS_EDITION year dir
local VS_BASE_PATH="$1"
for dir in "$VS_BASE_PATH"/*; do
[[ -d "$dir" ]] || continue
year="${dir##*/}"
if [[ "$year" =~ ^[0-9]{4}$ ]]; then
if [[ -z "$VS_YEAR" || "$year" -gt "$VS_YEAR" ]]; then
VS_YEAR="$year"
fi
fi
done
local VS_BASE_PATH="$VS_BASE_PATH/$VS_YEAR"
for edition in Enterprise Professional Community; do
if [[ -d "$VS_BASE_PATH/$edition" ]]; then
VS_EDITION="$edition"
break
fi
done
echo "$VS_YEAR/$VS_EDITION"
}
get_msvc_version() {
local VS_BASE_PATH="$1"
local VS_EDITION="$2"
local msvc_dir version MSVC_VERSION
for msvc_dir in "$VS_BASE_PATH/$VS_EDITION/VC/Tools/MSVC/"*; do
[[ -d "$msvc_dir" ]] || continue
version="${msvc_dir##*/}"
if [[ "$version" =~ [0-9.]+ ]]; then
if [[ -z "$MSVC_VERSION" || "$version" > "$MSVC_VERSION" ]]; then
MSVC_VERSION="$version"
fi
fi
done
echo "$MSVC_VERSION"
}
get_windows_kits_version() {
local WINDOWS_KITS_BASE_PATH="$1"
local WINDOWS_KITS_VERSION=""
local version dir
for dir in "$WINDOWS_KITS_BASE_PATH/Include/"*; do
[[ -d "$dir" ]] || continue
version="${dir##*/}"
if [[ "$version" =~ ^[0-9.]+$ ]]; then
if [[ -z "$WINDOWS_KITS_VERSION" || "$version" > "$WINDOWS_KITS_VERSION" ]]; then
WINDOWS_KITS_VERSION="$version"
fi
fi
done
echo "$WINDOWS_KITS_VERSION"
}
cpu_count() {
if is_macos; then
sysctl -n hw.logicalcpu
else
nproc
fi
}
run() {
local new_args=()
local IGNORE_ERRORS=0
local EXIT_CODE
for arg in "$@"; do
if [[ "$arg" == --ignore-errors* ]]; then
IGNORE_ERRORS+=("${arg#--ignore-errors=}")
else
new_args+=("$arg")
fi
done
echo ">>> $(quote_args "${new_args[@]}")" >&2
"${new_args[@]}"
EXIT_CODE=$?
if [[ ! ${IGNORE_ERRORS[*]} =~ $EXIT_CODE ]]; then
echo "[error] Error while executing $(quote_args "${new_args[@]}")" >&2
exit $EXIT_CODE
fi
}
require() {
case "$1" in
rust)
if [ ! -d "$HOME/.cargo" ]; then
run curl https://sh.rustup.rs -sSf | sh -s -- -y
fi
source "$HOME"/.cargo/env
;;
venv)
if [ ! -d "venv" ]; then
run python3 -m venv venv
fi
source venv/bin/activate
;;
xcode)
if is_macos; then
export MACOSX_DEPLOYMENT_TARGET=12.0
fi
;;
msvc)
if (is_windows); then
VS_EDITION="$(get_vs_edition "$VS_BASE_PATH")"
MSVC_VERSION="$(get_msvc_version "$VS_BASE_PATH" "$VS_EDITION")"
WINDOWS_KITS_VERSION="$(get_windows_kits_version "$WINDOWS_KITS_BASE_PATH")"
export PATH="$VS_BASE_PATH/$VS_EDITION/VC/Tools/MSVC/$MSVC_VERSION/bin/Hostx64/x64:$PATH"
export LIB="$VS_BASE_PATH/$VS_EDITION/VC/Tools/MSVC/$MSVC_VERSION/lib/x64:$WINDOWS_KITS_BASE_PATH/Lib/$WINDOWS_KITS_VERSION/um/x64:$WINDOWS_KITS_BASE_PATH/Lib/$WINDOWS_KITS_VERSION/ucrt/x64"
export INCLUDE="$VS_BASE_PATH/$VS_EDITION/VC/Tools/MSVC/$MSVC_VERSION/include:$WINDOWS_KITS_BASE_PATH/Include/$WINDOWS_KITS_VERSION/ucrt:$WINDOWS_KITS_BASE_PATH/Include/$WINDOWS_KITS_VERSION/um:$WINDOWS_KITS_BASE_PATH/Include/$WINDOWS_KITS_VERSION/shared"
echo "[info] Correctly set env vars for Visual Studio $VS_EDITION, MSVC $MSVC_VERSION and Windows Kits $WINDOWS_KITS_VERSION" >&2
fi
;;
ndk|ndk-local)
if is_android; then
if [[ -z "$ANDROID_NDK_ROOT" || "$1" == "ndk" ]]; then
export ANDROID_API=21
platform_name="$(uname -s | tr '[:upper:]' '[:lower:]')"
case "$platform_name" in
cygwin*|mingw*|msys*)
platform_name="windows"
;;
esac
mkdir -p "$DEFAULT_TOOLS_FOLDER"
ndk_name="android-ndk-r26b"
export ANDROID_NDK_ROOT="$DEFAULT_TOOLS_FOLDER/$ndk_name"
if [ ! -d "$ANDROID_NDK_ROOT" ]; then
run curl -L "https://dl.google.com/android/repository/$ndk_name-$platform_name.zip" -o "$DEFAULT_TOOLS_FOLDER/android-ndk.zip"
run unzip -q "$DEFAULT_TOOLS_FOLDER/android-ndk.zip" -d "$DEFAULT_TOOLS_FOLDER"
rm "$DEFAULT_TOOLS_FOLDER/android-ndk.zip"
fi
elif [[ ! -d "$ANDROID_NDK_ROOT" ]]; then
echo "[error] ANDROID_NDK_ROOT is set but the directory does not exist" >&2
exit 1
fi
export ANDROID_PREBUILT;
ANDROID_PREBUILT="${ANDROID_NDK_ROOT}/toolchains/llvm/prebuilt/$(uname -s | tr '[:upper:]' '[:lower:]')-$(uname -m)"
fi
;;
*)
echo "[error] Unknown requirement: $1" >&2
exit 1
;;
esac
}
configure_autogen() {
if [ -f configure.ac ]; then
if [ ! -f autogen.sh ]; then
run autoreconf -ivf
else
run ./autogen.sh
fi
fi
}
build_and_install() {
local repo_url=$1
local sub_path=""
if [[ "$repo_url" =~ ^http(s)* ]]; then
local branch=$2
local build_type=$3
shift 3
else
lib_name="${1%%/*}"
lib_name="$(echo "$lib_name" | tr '[:lower:]' '[:upper:]')"
lib_name="${lib_name//-/_}"
[[ "$1" == */* ]] && sub_path="${1#*/}"
local git_var="LIB_${lib_name}_GIT"
git_var="${!git_var}"
local tag_prefix="LIB_${lib_name}_PREFIX"
local version_var="LIB_${lib_name}_VERSION"
version_var="${!version_var}"
separator=$(read_cache "prefix" "$git_var")
separator="${separator#*;}"
version_var=$(apply_separator "$version_var" "$separator")
if [[ $? -eq 1 ]]; then
echo "[error] Failed to find prefix for $git_var" >&2
exit 1
fi
repo_url="https://$git_var.git"
if [[ -z "$git_var" ]]; then
echo "[error] No dependency found for ${1%%/*}" >&2
exit 1
fi
local branch="${!tag_prefix}$version_var"
local build_type=$2
shift 2
fi
local repo_name
repo_name=$(basename "$repo_url" .git)
local update_submodules=false
local skip_build=false
local setup_commands=""
local cleanup_commands=""
local build_dir=""
local build_tool=""
local dir_after_build=""
local new_args=()
local executable_command=()
local last_branch=""
local pl_args=""
current_dir="$(pwd)"
for arg in "$@"; do
if [[ "$arg" == "--update-submodules" ]]; then
update_submodules=true
elif [[ "$arg" == "--skip-build" ]]; then
skip_build=true
elif [[ "$arg" == --setup-commands* ]]; then
setup_commands="${arg#--setup-commands=}"
elif [[ "$arg" == --cleanup-commands* ]]; then
cleanup_commands="${arg#--cleanup-commands=}"
elif [[ "$arg" == --prefix=* || "$arg" == -DCMAKE_INSTALL_PREFIX=* ]];then
build_dir="${arg#*=}"
elif [[ "$arg" =~ --(windows|linux|macos|android).*=.* ]]; then
platforms="${arg%%=*}"
platforms="${platforms#--}"
IFS='-' read -r -a platform_list <<< "$platforms"
platform_supported=false
for platform in "${platform_list[@]}"; do
case "$platform" in
windows) is_windows && platform_supported=true ;;
linux) is_linux && platform_supported=true ;;
macos) is_macos && platform_supported=true ;;
android) is_android && platform_supported=true ;;
esac
done
if $platform_supported; then
pl_args="${arg#--"$platforms"=}"
pl_args="${pl_args//\/ }"
eval "new_args+=($pl_args)"
fi
else
new_args+=("$arg")
fi
done
mkdir -p "$DEFAULT_BUILD_FOLDER"
cd "$DEFAULT_BUILD_FOLDER" || exit 1
if [[ ! -d "$repo_name" ]]; then
git init "$repo_name" &>/dev/null
cd "$repo_name" || exit 1
run git remote add origin "$repo_url"
if is_git_commit "$git_var" "$branch"; then
run git fetch origin "$branch" --depth=1
else
run git fetch origin tag "$branch" --depth=1
fi
run git checkout FETCH_HEAD
write_cache "last_branch" "$git_var" "$branch"
cd .. || exit 1
fi
cd "$repo_name" || exit 1
last_branch="$(read_cache "last_branch" "$git_var")"
if [[ "$branch" != "$last_branch" ]]; then
if is_git_commit "$git_var" "$branch"; then
run git fetch origin "$branch" --depth=1
else
run git fetch origin tag "$branch" --depth=1
fi
run git checkout FETCH_HEAD
run git reset --hard
run git clean -fdx
write_cache "last_branch" "$git_var" "$branch"
fi
if [ -n "$sub_path" ]; then
cd "$sub_path" || exit 1
fi
if [[ "$update_submodules" == "true" ]]; then
run git submodule update --init --recursive
fi
if [ -n "$setup_commands" ]; then
local setup_commands_array
eval "setup_commands_array=($setup_commands)"
run "${setup_commands_array[@]}"
fi
if [[ $build_type =~ -static$ ]]; then
is_static=true
else
is_static=false
fi
if [ -z "$build_dir" ]; then
if $is_static; then
build_dir="$DEFAULT_BUILD_FOLDER/$repo_name/build"
else
build_dir="/usr"
fi
fi
if $is_static; then
write_cache "lib_kind" "$git_var" "static"
else
write_cache "lib_kind" "$git_var" "dynamic"
fi
write_cache "lib" "$git_var" "$build_dir"
case "$build_type" in
autogen|autogen-static)
executable_command=("./autogen.sh")
if $is_static; then
new_args+=("--enable-static" "--disable-shared" "--enable-pic")
fi
new_args+=("--prefix=$build_dir")
;;
configure|configure-static)
executable_command=("./configure")
if $is_static; then
new_args+=("--enable-static" "--disable-shared" "--enable-pic")
fi
new_args+=("--prefix=$build_dir")
;;
meson|meson-static)
executable_command=(python -m mesonbuild.mesonmain setup --reconfigure build)
if $is_static; then
new_args+=("--default-library=static")
fi
new_args+=("--prefix=$build_dir")
new_args+=("--libdir=lib")
new_args+=("--buildtype=release")
;;
cmake|cmake-static)
executable_command=(cmake)
build_tool=$(process_args get "-G" "${new_args[@]}")
dir_after_build=$(process_args get "-B" "${new_args[@]}")
tmp_args=()
while IFS= read -r line; do
tmp_args+=("$line")
done < <(process_args filter "-G" "${new_args[@]}")
new_args=("${tmp_args[@]}")
tmp_args=()
while IFS= read -r line; do
tmp_args+=("$line")
done < <(process_args filter "-B" "${new_args[@]}")
new_args=("${tmp_args[@]}")
tmp_args=()
while IFS= read -r line; do
tmp_args+=("$line")
done < <(process_args filter "-DCMAKE_INSTALL_PREFIX" "${new_args[@]}")
new_args=("${tmp_args[@]}")
tmp_args=()
while IFS= read -r line; do
tmp_args+=("$line")
done < <(process_args filter "-DBUILD_SHARED_LIBS" "${new_args[@]}")
new_args=("${tmp_args[@]}")
if [[ -z "$build_tool" ]]; then
build_tool="Unix Makefiles"
fi
if [[ -z "$dir_after_build" ]]; then
dir_after_build="build"
fi
new_args+=("-G" "$build_tool")
new_args+=("-B" "$dir_after_build")
if $is_static; then
new_args+=("-DBUILD_SHARED_LIBS=OFF")
fi
new_args+=("-DCMAKE_INSTALL_PREFIX=$build_dir")
;;
make)
;;
*)
echo "[error] Unknown build type: $build_type" >&2
exit 1
;;
esac
merged_commands=("${executable_command[@]}" "${new_args[@]}")
echo "[info] Running $build_type with options: $(quote_args "${new_args[@]}")" >&2
case "$build_type" in
configure|configure-static)
configure_autogen
;;
cmake|cmake-static)
rm -rf "$dir_after_build"
;;
esac
write_cache "lib_include" "$git_var" ";"
if [[ -n "${merged_commands[*]}" ]]; then
save_headers run "${merged_commands[@]}"
fi
if ! $skip_build; then
if [[ -n "$dir_after_build" ]]; then
cd "$dir_after_build" || exit 1
fi
if [[ "$build_type" == autogen* || "$build_type" == configure* || "$build_type" == "make" || "$build_tool" == "Unix Makefiles" ]]; then
run make clean --ignore-errors=2
save_headers run make -j"$(cpu_count)" --ignore-errors=2
save_headers run make install
elif [[ "$build_type" == meson* || "$build_tool" == "Ninja" ]]; then
run python -m ninja -C build -t clean
save_headers run python -m ninja -C build -j"$(cpu_count)"
save_headers run python -m ninja -C build install
elif [[ -n "$build_tool" ]]; then
run cmake --build . --target clean --config Release
save_headers run cmake --build . --config Release -j"$(cpu_count)"
save_headers run cmake --install . --config Release
else
echo "[error] Unknown build tool: $build_tool" >&2
exit 1
fi
fi
if [ -n "$cleanup_commands" ]; then
local cleanup_commands_array
eval "cleanup_commands_array=($cleanup_commands)"
run "${cleanup_commands_array[@]}"
fi
cd "$current_dir" || exit 1
append_pkg_config_path "$build_dir/lib/pkgconfig"
}
save_headers() {
tmp_before=$(mktemp)
tmp_after=$(mktemp)
touch "$tmp_before"
"$@"
touch "$tmp_after"
if [[ -d "$build_dir/include" ]]; then
local new_headers=()
while IFS= read -r line; do