forked from DeFiCh/ain
-
Notifications
You must be signed in to change notification settings - Fork 0
/
make.sh
executable file
·1344 lines (1110 loc) · 34.4 KB
/
make.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
# Copyright (c) DeFi Blockchain Developers
# Maker script
# Notes:
# - Ideal to use POSIX C.UTF-8, however Apple systems don't have
# this locale and throws a fit, so en-US.UTF-8 is reasonable middle ground.
# - Note that this has cascaded effects such as affecting glibc behavior:
# https://wiki.musl-libc.org/functional-differences-from-glibc.html#Default-locale
# - We also do not support muslc at this point due to issues like:
# https://wiki.musl-libc.org/functional-differences-from-glibc.html#Floating-point-and-mathematical-library
set -Eeuo pipefail
export LC_ALL=en_US.UTF-8
setup_vars() {
IMAGE_PREFIX=${IMAGE_PREFIX:-"defichain"}
GIT_VERSION=${GIT_VERSION:-0}
if [[ "$GIT_VERSION" == 1 ]]; then
IMAGE_VERSION=${IMAGE_VERSION:-"$(git_version 0)"}
else
IMAGE_VERSION=${IMAGE_VERSION:-"latest"}
fi
DOCKER_ROOT_CONTEXT=${DOCKER_ROOT_CONTEXT:-"."}
DOCKER_RELATIVE_BUILD_DIR=${DOCKER_RELATIVE_BUILD_DIR:-"./build"}
DOCKERFILES_DIR=${DOCKERFILES_DIR:-"./contrib/dockerfiles"}
DEFI_DOCKERFILE=${DEFI_DOCKERFILE:-"${DOCKERFILES_DIR}/defi.dockerfile"}
ROOT_DIR="$(_canonicalize "${_SCRIPT_DIR}")"
TARGET=${TARGET:-"$(get_default_target)"}
DOCKERFILE=${DOCKERFILE:-"$(get_default_docker_file)"}
BUILD_DIR=${BUILD_DIR:-"./build"}
BUILD_DIR="$(_canonicalize "$BUILD_DIR")"
# Was previously ${BUILD_DIR}/$TARGET for host specific
# But simplifying this since autotools conf ends up in reconf and
# rebuilds anyway, might as well just point manually if needed
BUILD_TARGET_DIR="${BUILD_DIR}"
BUILD_DEPENDS_DIR=${BUILD_DEPENDS_DIR:-"${BUILD_DIR}/depends"}
BUILD_DEPENDS_DIR="$(_canonicalize "$BUILD_DEPENDS_DIR")"
PYTHON_VENV_DIR=${PYTHON_VENV_DIR:-"${BUILD_DIR}/pyenv"}
CLANG_DEFAULT_VERSION=${CLANG_DEFAULT_VERSION:-"15"}
RUST_DEFAULT_VERSION=${RUST_DEFAULT_VERSION:-"1.76"}
MAKE_DEBUG=${MAKE_DEBUG:-"1"}
MAKE_USE_CLANG=${MAKE_USE_CLANG:-"$(get_default_use_clang)"}
if [[ "${MAKE_USE_CLANG}" == "1" ]]; then
local clang_ver="${CLANG_DEFAULT_VERSION}"
export CC=clang-${clang_ver}
export CXX=clang++-${clang_ver}
fi
MAKE_JOBS=${MAKE_JOBS:-"$(get_default_jobs)"}
MAKE_CONF_ARGS="$(get_default_conf_args) ${MAKE_CONF_ARGS:-}"
if [[ "${MAKE_DEBUG}" == "1" ]]; then
MAKE_CONF_ARGS="${MAKE_CONF_ARGS} --enable-debug"
fi
MAKE_CONF_ARGS_OVERRIDE="${MAKE_CONF_ARGS_OVERRIDE:-}"
if [[ "${MAKE_CONF_ARGS_OVERRIDE}" ]]; then
MAKE_CONF_ARGS="${MAKE_CONF_ARGS_OVERRIDE}"
fi
MAKE_ARGS=${MAKE_ARGS:-}
MAKE_DEPS_ARGS=${MAKE_DEPS_ARGS:-}
TESTS_FAILFAST=${TESTS_FAILFAST:-"0"}
TESTS_COMBINED_LOGS=${TESTS_COMBINED_LOGS:-"0"}
CI_GROUP_LOGS=${CI_GROUP_LOGS:-"$(get_default_ci_group_logs)"}
}
main() {
_bash_version_check
_setup_dir_env
trap _cleanup 0 1 2 3 6 15 ERR
cd "$_SCRIPT_DIR"
_platform_init
setup_vars
git_add_hooks
# Get all functions declared in this file except ones starting with
# '_' or the ones in the list
# shellcheck disable=SC2207
COMMANDS=($(declare -F | cut -d" " -f3 | grep -v -E "^_.*$|main|setup_vars")) || true
# Commands use `-` instead of `_` for getopts consistency. Flip this.
local cmd=${1:-} && cmd="${cmd//-/_}"
local x
for x in "${COMMANDS[@]}"; do
if [[ "$x" == "$cmd" ]]; then
shift
${cmd} "$@"
return 0
fi
done
help
return 1
}
_setup_dir_env() {
_WORKING_DIR="$(pwd)"
local dir
dir="$(dirname "${BASH_SOURCE[0]}")"
_SCRIPT_DIR="$(cd "${dir}/" && pwd)"
}
_cleanup() {
cd "$_WORKING_DIR"
}
help() {
echo "Usage: $0 <commands>"
printf "\n\`%s build\` or \`%s docker-build\` are your friends :) \n" "$0" "$0"
printf "\nCommands:\n"
printf "\t%s\n" "${COMMANDS[@]//_/-}"
printf "\nNote: All commands without docker-* prefix assume that it's run in\n"
printf "an environment with correct arch and pre-requisites configured. \n"
printf "(most pre-requisites can be installed with pkg-* commands). \n"
}
# ----------- Direct builds ---------------
build_deps() {
local target=${1:-${TARGET}}
local make_deps_args=${MAKE_DEPS_ARGS:-}
local make_jobs=${MAKE_JOBS}
local src_depends_dir=${ROOT_DIR}/depends
local build_depends_dir=${BUILD_DEPENDS_DIR}
echo "> build-deps: target: ${target} / deps_args: ${make_deps_args} / jobs: ${make_jobs}"
_ensure_enter_dir "$build_depends_dir"
if [[ "$target" =~ .*darwin.* ]]; then
pkg_local_ensure_osx_sysroot
fi
_fold_start "build-deps"
# shellcheck disable=SC2086
make -C "${src_depends_dir}" DESTDIR="${build_depends_dir}" \
HOST="${target}" -j${make_jobs} ${make_deps_args}
_fold_end
_exit_dir
}
build_conf() {
local target=${1:-${TARGET}}
local make_conf_opts=${MAKE_CONF_ARGS:-}
local make_jobs=${MAKE_JOBS}
local root_dir=${ROOT_DIR}
local build_target_dir=${BUILD_TARGET_DIR}
local build_depends_dir=${BUILD_DEPENDS_DIR}
echo "> build-conf: target: ${target} / conf_args: ${make_conf_opts} / jobs: ${make_jobs}"
_ensure_enter_dir "${build_target_dir}"
_fold_start "build-conf::autogen"
"$root_dir/autogen.sh"
_fold_end
_fold_start "build-conf::configure"
# shellcheck disable=SC2086
CONFIG_SITE="$build_depends_dir/${target}/share/config.site" \
$root_dir/configure --prefix="$build_depends_dir/${target}" \
${make_conf_opts}
_fold_end
_exit_dir
}
build_make() {
local target=${1:-${TARGET}}
local make_args=${MAKE_ARGS:-}
local make_jobs=${MAKE_JOBS}
local build_target_dir=${BUILD_TARGET_DIR}
echo "> build: target: ${target} / args: ${make_args} / jobs: ${make_jobs}"
_ensure_enter_dir "${build_target_dir}"
_fold_start "build_make"
# shellcheck disable=SC2086
make DESTDIR="${build_target_dir}" -j${make_jobs} JOBS=${make_jobs} ${make_args}
_fold_end
_exit_dir
}
build() {
build_deps "$@"
build_conf "$@"
build_make "$@"
}
deploy() {
local target=${1:-${TARGET}}
local img_prefix="${IMAGE_PREFIX}"
local img_version="${IMAGE_VERSION}"
local build_dir="${BUILD_DIR}"
local build_target_dir="${BUILD_TARGET_DIR}"
local versioned_name="${img_prefix}-${img_version}"
local versioned_build_path
versioned_build_path="$(_canonicalize "${build_dir}/${versioned_name}")"
echo "> deploy into: ${build_dir} from ${versioned_build_path}"
_safe_rm_rf "${versioned_build_path}" && mkdir -p "${versioned_build_path}"
make -C "${build_target_dir}" prefix=/ DESTDIR="${versioned_build_path}" \
install && cp README.md "${versioned_build_path}/"
echo "> deployed: ${versioned_build_path}"
}
package() {
local target=${1:-${TARGET}}
local img_prefix="${IMAGE_PREFIX}"
local img_version="${IMAGE_VERSION}"
local build_dir="${BUILD_DIR}"
local pkg_name="${img_prefix}-${img_version}-${target}"
local pkg_path
if [[ "$target" == "x86_64-w64-mingw32" ]]; then
pkg_path="$(_canonicalize "${build_dir}/${pkg_name}.zip")"
else
pkg_path="$(_canonicalize "${build_dir}/${pkg_name}.tar.gz")"
fi
local versioned_name="${img_prefix}-${img_version}"
local versioned_build_dir="${build_dir}/${versioned_name}"
if [[ ! -d "$versioned_build_dir" ]]; then
echo "> error: deployment required to package"
echo "> tip: try \`$0 deploy\` or \`$0 docker-deploy\` first"
exit 1
fi
echo "> packaging: ${pkg_name} from ${versioned_build_dir}"
if [[ "$target" == "x86_64-w64-mingw32" ]]; then
_ensure_enter_dir "${build_dir}"
zip -r "${pkg_path}" "$(basename "${versioned_build_dir}")"
else
_ensure_enter_dir "${versioned_build_dir}"
_tar --transform "s,^./,${versioned_name}/," -czf "${pkg_path}" ./*
fi
sha256sum "${pkg_path}" >"${pkg_path}.SHA256"
_exit_dir
echo "> package: ${pkg_path}"
}
release() {
local target=${1:-${TARGET}}
build "${target}"
deploy "${target}"
package "${target}"
_sign "${target}"
}
# -------------- Docker ---------------
docker_build() {
local target=${1:-${TARGET}}
local img_prefix="${IMAGE_PREFIX}"
local img_version="${IMAGE_VERSION}"
local docker_context="${DOCKER_ROOT_CONTEXT}"
local docker_file="${DOCKERFILE}"
echo "> docker-build"
local img="${img_prefix}-${target}:${img_version}"
echo "> building: ${img}"
echo "> docker build: ${img}"
docker build -f "${docker_file}" \
--build-arg TARGET="${target}" \
--build-arg MAKE_DEBUG="${MAKE_DEBUG}" \
-t "${img}" "${docker_context}"
}
docker_deploy() {
local target=${1:-${TARGET}}
local img_prefix="${IMAGE_PREFIX}"
local img_version="${IMAGE_VERSION}"
local build_dir="${BUILD_DIR}"
echo "> docker-deploy"
local img="${img_prefix}-${target}:${img_version}"
echo "> deploy from: ${img}"
local versioned_name="${img_prefix}-${img_version}"
local versioned_build_dir="${build_dir}/${versioned_name}"
_safe_rm_rf "${versioned_build_dir}" && mkdir -p "${versioned_build_dir}"
local cid
cid=$(docker create "${img}")
local e=0
{ docker cp "${cid}:/app/." "${versioned_build_dir}" 2>/dev/null && e=1; } || true
docker rm "${cid}"
if [[ "$e" == "1" ]]; then
echo "> deployed into: ${versioned_build_dir}"
else
echo "> failed: please ensure package is built first"
fi
}
docker_release() {
local target=${1:-${TARGET}}
docker_build "$target"
docker_deploy "$target"
package "$target"
_sign "$target"
}
docker_build_from_binaries() {
local target=${1:-${TARGET}}
local img_prefix="${IMAGE_PREFIX}"
local img_version="${IMAGE_VERSION}"
local build_dir="${DOCKER_RELATIVE_BUILD_DIR}"
local docker_context="${DOCKER_ROOT_CONTEXT}"
local docker_file="${DEFI_DOCKERFILE}"
echo "> docker-build-from-binaries"
local img="${img_prefix}-${target}:${img_version}"
echo "> building: ${img}"
echo "> docker defi build: ${img}"
local versioned_name="${img_prefix}-${img_version}"
local versioned_build_dir="${build_dir}/${versioned_name}"
docker build -f "${docker_file}" \
--build-arg BINARY_DIR="${versioned_build_dir}" \
-t "${img}" "${docker_context}"
}
docker_clean_builds() {
echo "> clean: defichain build images"
_docker_clean "org.defichain.name=defichain"
}
docker_clean_all() {
echo "> clean: defichain* images"
_docker_clean "org.defichain.name"
}
_docker_clean() {
local labels_to_clean=${1:?labels required}
# shellcheck disable=SC2046
docker rmi $(docker images -f label="${labels_to_clean}" -q) \
--force 2>/dev/null || true
}
# Python helpers
# ---
py_ensure_env_active() {
pkg_local_ensure_py_deps
py_env_activate
}
py_env_activate() {
local python_venv="${PYTHON_VENV_DIR}"
python3 -m venv "${python_venv}"
# shellcheck disable=SC1091
source "${python_venv}/bin/activate"
}
py_env_deactivate() {
deactivate
}
# -------------- Misc -----------------
# check
# ---
check() {
check_git_dirty
check_sh
check_py
check_rs
# check_lints
check_cpp
}
check_git_dirty() {
if [[ $(git status -s) ]]; then
echo "error: Git tree dirty. Please commit or stash first"
exit 1
fi
}
check_py() {
py_ensure_env_active
_exec_black 1
# TODO Add flake as well
py_env_deactivate
}
check_rs() {
# lib clippy 1
lib fmt-check 1
}
check_lints() {
py_ensure_env_active
_fold_start "check-doc"
test/lint/check-doc.py
_fold_end
# _fold_start "check-rpc-mappings"
# test/lint/check-rpc-mappings.py .
# _fold_end
test/lint/lint-all.sh
py_env_deactivate
}
check_sh() {
py_ensure_env_active
# TODO: Remove most of the specific ignores after resolving them
# shellcheck disable=SC2086
find . -not \( -path ./build -prune \
-or -path ./autogen.sh \
-or -path ./test/lint/lint-python.sh \
-or -path ./test/lint/lint-rpc-help.sh \
-or -path ./test/lint/lint-shell.sh \
-or -path ./test/lint/disabled-lint-spelling.sh \
-or -path ./test/lint/commit-script-check.sh \
-or -path ./test/lint/lint-includes.sh \
-or -path ./test/lint/lint-python-dead-code.sh \
-or -path ./src/univalue -prune \
-or -path ./src/secp256k1 -prune \
-or -path ./build\* \) -name '*.sh' -print0 | xargs -0L1 shellcheck
py_env_deactivate
}
check_cpp() {
_run_clang_format 1
}
check_enter_build_rs_dir() {
local build_dir="${BUILD_DIR}"
_ensure_enter_dir "$build_dir/lib" || {
echo "Please configure first"
exit 1
}
}
# fmt
# ---
fmt() {
fmt_py
fmt_lib
fmt_cpp
}
fmt_py() {
echo "> fmt: py"
py_ensure_env_active
_exec_black
py_env_deactivate
}
fmt_rs() {
fmt_lib
}
fmt_cpp() {
echo "> fmt: cpp"
_run_clang_format 0
}
_run_clang_format() {
local check_only=${1:-0}
local clang_ver=${CLANG_DEFAULT_VERSION}
local clang_formatters=("clang-format-${clang_ver}" "clang-format")
local index=-1
local fmt_args=""
for ((idx = 0; idx < ${#clang_formatters[@]}; ++idx)); do
if "${clang_formatters[$idx]}" --version &>/dev/null; then
index="$idx"
break
fi
done
if [[ "$index" == -1 ]]; then
echo "clang-format(-${clang_ver}) required"
exit 1
fi
if [[ "$check_only" == 1 ]]; then
fmt_args="--dry-run --Werror"
fi
# shellcheck disable=SC2086
find src/dfi src/ffi \( -iname "*.cpp" -o -iname "*.h" \) -print0 |
xargs -0 -I{} "${clang_formatters[$index]}" $fmt_args -i -style=file {}
local whitelist_files=(src/miner.{cpp,h} src/txmempool.{cpp,h} src/validation.{cpp,h})
# shellcheck disable=SC2086
"${clang_formatters[$index]}" $fmt_args -i -style=file "${whitelist_files[@]}"
}
fmt_lib() {
echo "> fmt: rs"
check_enter_build_rs_dir
lib fmt
_exit_dir
}
# tests
# ---
test() {
_fold_start "unit-tests"
# shellcheck disable=SC2086
test_unit "$@"
_fold_end
_fold_start "functional-tests"
# shellcheck disable=SC2119
test_py
_fold_end
}
test_unit() {
test_cpp "$@"
test_rs "$@"
}
test_cpp() {
local make_jobs=${MAKE_JOBS}
local make_args=${MAKE_ARGS:-}
local build_target_dir=${BUILD_TARGET_DIR}
_ensure_enter_dir "${build_target_dir}"
# shellcheck disable=SC2086
make -j$make_jobs check "$@"
_exit_dir
}
test_rs() {
lib test "$@"
}
# shellcheck disable=SC2120
test_py() {
local build_target_dir=${BUILD_TARGET_DIR}
local src_dir=${_SCRIPT_DIR}
local tests_fail_fast=${TESTS_FAILFAST}
local tests_combined_logs=${TESTS_COMBINED_LOGS}
local make_jobs=${MAKE_JOBS}
local extra_args=""
local first_arg="${1:-}"
# If an argument is given as an existing file, we switch that
# out to the last arg
if [[ -f "${first_arg}" ]]; then
shift
elif [[ -f "${src_dir}/test/functional/${first_arg}" ]]; then
first_arg="${src_dir}/test/functional/${first_arg}"
shift
else
# We don't shift, this just ends up in the $@ args
first_arg=""
fi
if [[ "$tests_fail_fast" == "1" ]]; then
extra_args="--failfast"
fi
_ensure_enter_dir "${build_target_dir}"
py_ensure_env_active
# shellcheck disable=SC2086
(
set -x
python3 ${build_target_dir}/test/functional/test_runner.py \
--tmpdirprefix="./test_runner/" \
--ansi \
--configfile="${build_target_dir}/test/config.ini" \
--jobs=${make_jobs} \
--combinedlogslen=${tests_combined_logs} \
${extra_args} ${first_arg} "$@"
)
py_env_deactivate
_exit_dir
}
# Others
debug_env() {
(
set -o posix
set
)
(
set -x +e
uname -a
gcc -v
"clang-${CLANG_DEFAULT_VERSION}" -v
rustup show
)
}
exec() {
local make_jobs=${MAKE_JOBS}
local make_args=${MAKE_ARGS:-}
local build_target_dir=${BUILD_TARGET_DIR}
_ensure_enter_dir "${build_target_dir}"
_fold_start "exec:: ${*-(default)}"
# shellcheck disable=SC2086,SC2068
make -j$make_jobs JOBS=${make_jobs} $make_args $@
_fold_end
_exit_dir
}
git_version() {
local verbose=${1:-1}
# If we have a tagged version (for proper releases), then just
# release it with the tag, otherwise we use the commit hash
local current_tag
local current_commit
local current_branch
git fetch --tags &>/dev/null
current_tag=$(git tag --points-at HEAD | head -1)
current_commit=$(git rev-parse --short HEAD)
current_branch=$(git rev-parse --abbrev-ref HEAD)
local ver=""
if [[ -z $current_tag ]]; then
# Replace `/` in branch names with `-` as / is trouble
ver="${current_branch//\//-}-${current_commit}"
else
ver="${current_tag}"
# strip the 'v' infront of version tags
if [[ "$ver" =~ ^v[0-9]\.[0-9] ]]; then
ver="${ver##v}"
fi
fi
if [[ "$verbose" == "1" ]]; then
echo "> git branch: ${current_branch}"
echo "> version: ${ver}"
else
echo "$ver"
fi
}
# ------------ pkg --------------------
# Conventions:
# - pkg_*
# - pkg_install_*: for installing packages (system level)
# - pkg_update_*: distro update only
# - pkg_local_*: for pulling packages into the local dir
# - clean_pkg_local*: Make sure to have the opp.
# - pkg_setup*: setup of existing (or installed) pkgs // but not installing now
pkg_update_base() {
_fold_start "pkg-update-base"
apt-get update
apt-get install -y apt-transport-https
apt-get upgrade -y
_fold_end
}
pkg_install_deps() {
_fold_start "pkg-install-deps"
# gcc-multilib: for cross compilations
# locales: for using en-US.UTF-8 (see head of this file).
# python3-venv for settings up all python deps
apt-get install -y \
software-properties-common build-essential git libtool autotools-dev automake \
pkg-config bsdmainutils python3 python3-pip python3-venv libssl-dev libevent-dev \
libboost-chrono-dev libboost-test-dev libboost-thread-dev \
libminiupnpc-dev libzmq3-dev libqrencode-dev wget ccache \
libdb-dev libdb++-dev libdb5.3 libdb5.3-dev libdb5.3++ libdb5.3++-dev \
curl cmake zip unzip libc6-dev gcc-multilib locales locales-all python3-dev
_fold_end
}
pkg_setup_locale() {
# Not a hard requirement. We use en_US.UTF-8 to maintain coherence across
# different platforms. C.UTF-8 is not available on all platforms.
locale-gen "en_US.UTF-8"
}
pkg_install_deps_mingw_x86_64() {
_fold_start "pkg-install-deps-mingw-x86_64"
apt-get install -y \
g++-mingw-w64-x86-64 mingw-w64-x86-64-dev
_fold_end
}
pkg_setup_mingw_x86_64() {
update-alternatives --set x86_64-w64-mingw32-gcc /usr/bin/x86_64-w64-mingw32-gcc-posix
update-alternatives --set x86_64-w64-mingw32-g++ /usr/bin/x86_64-w64-mingw32-g++-posix
}
pkg_install_deps_armhf() {
_fold_start "pkg-install-deps-armhf"
apt-get install -y \
g++-arm-linux-gnueabihf binutils-arm-linux-gnueabihf libc6-dev-armhf-cross
_fold_end
}
pkg_install_deps_arm64() {
_fold_start "pkg-install-deps-arm64"
apt-get install -y \
g++-aarch64-linux-gnu binutils-aarch64-linux-gnu libc6-dev-arm64-cross
_fold_end
}
pkg_install_deps_osx_tools() {
_fold_start "pkg-install-deps-mac-tools"
apt-get install -y \
libcap-dev libbz2-dev libz-dev fonts-tuffy librsvg2-bin libtiff-tools imagemagick libtinfo5
_fold_end
}
pkg_install_gh_cli() {
_fold_start "pkg-install-gh_cli"
curl -fsSL https://cli.github.com/packages/githubcli-archive-keyring.gpg |
dd of=/usr/share/keyrings/githubcli-archive-keyring.gpg &&
chmod go+r /usr/share/keyrings/githubcli-archive-keyring.gpg
echo "deb [arch=$(dpkg --print-architecture) \
signed-by=/usr/share/keyrings/githubcli-archive-keyring.gpg] \
https://cli.github.com/packages stable main" |
tee /etc/apt/sources.list.d/github-cli.list >/dev/null
apt-get update
apt-get install -y gh
}
pkg_install_llvm() {
_fold_start "pkg-install-llvm"
# shellcheck disable=SC2086
wget -O - "https://apt.llvm.org/llvm.sh" | bash -s ${CLANG_DEFAULT_VERSION}
_fold_end
}
pkg_user_install_rust() {
_fold_start "pkg-install-rust"
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- \
--default-toolchain="${RUST_DEFAULT_VERSION}" -y
_fold_end
}
pkg_local_ensure_osx_sysroot() {
local sdk_name="Xcode-12.2-12B45b-extracted-SDK-with-libcxx-headers"
local pkg="${sdk_name}.tar.gz"
local build_depends_dir="${BUILD_DEPENDS_DIR}"
local sdk_base_dir="$build_depends_dir/SDKs"
if [[ -d "${sdk_base_dir}/${sdk_name}" ]]; then
return
fi
_fold_start "pkg-local-mac-sdk"
_ensure_enter_dir "${sdk_base_dir}"
if [[ ! -f "${pkg}" ]]; then
wget https://bitcoincore.org/depends-sources/sdks/${pkg}
fi
_tar -zxf "${pkg}"
rm "${pkg}" 2>/dev/null || true
_exit_dir
_fold_end
}
clean_pkg_local_osx_sysroot() {
local build_depends_dir="${BUILD_DEPENDS_DIR}"
_safe_rm_rf "$build_depends_dir/SDKs"
}
pkg_local_ensure_py_deps() {
local python_venv="${PYTHON_VENV_DIR}"
if [[ -d "${python_venv}" ]]; then
return
fi
pkg_local_install_py_deps
}
pkg_local_install_py_deps() {
local root_dir="${ROOT_DIR}"
_fold_start "pkg-install-py-deps"
py_env_activate
python3 -m pip install wheel
# lints, fmt, checks deps
python3 -m pip install -r "${root_dir}/test/requirements.txt"
# post dep for test
python3 -c 'from solcx import install_solc;install_solc("0.8.20")'
py_env_deactivate
_fold_end
}
clean_pkg_local_py_deps() {
local python_venv="${PYTHON_VENV_DIR}"
_safe_rm_rf "${python_venv}"
}
pkg_user_setup_rust() {
local rust_target
# shellcheck disable=SC2119
rust_target=$(get_rust_triplet)
rustup target add "${rust_target}"
}
# Clean
# ---
purge() {
local build_dir="${BUILD_DIR}"
local build_depends_dir="${BUILD_DEPENDS_DIR}"
clean_depends
_safe_rm_rf "$build_depends_dir"
clean_conf
clean_artifacts
docker_clean_all
_safe_rm_rf "$build_dir"
}
clean_artifacts() {
# If build is done out of tree, this is not needed at all. But when done
# in-tree, or helper tools that end up running configure in-tree, this is
# a helpful method to clean up left overs.
local items=(
.libs .deps obj "*.dirstamp" "*.a" "*.o" "*.Po" "*.lo")
local x
for x in "${items[@]}"; do
find src -iname "$x" -exec rm -rf \;
done
}
clean_conf() {
local top_left_overs=(
Makefile.in aclocal.m4 autom4te.cache configure configure~)
# If things were built in-tree, help clean this up as well
local in_tree_conf_left_overs=(
Makefile libtool config.log config.status)
local build_aux_left_overs=(
ar-lib compile config.guess config.sub depcomp install-sh ltmain.sh
missing test-driver)
local build_aux_m4_left_overs=(
libtool.m4 lt~obsolete.m4 ltoptions.m4 ltsugar.m4 ltversion.m4)
local left_overs=("${top_left_overs[@]}"
"${in_tree_conf_left_overs[@]}"
"${build_aux_left_overs[@]/#/build-aux/}"
"${build_aux_m4_left_overs[@]/#/build-aux/m4/}")
local individual_files=(./test/config.ini)
local x
for x in "${individual_files[@]}"; do
_safe_rm_rf "$x"
done
for x in "${left_overs[@]} "; do
_safe_rm_rf "$x"
_safe_rm_rf "src/secp256k1/$x"
_safe_rm_rf "src/univalue/$x"
done
_safe_rm_rf \
src/Makefile.in \
src/defi-config.h.{in,in~} \
src/univalue/univalue-config.h.{in,in~} \
src/secp256k1/libsecp256k1-config.h.{in,in~}
}
clean_depends() {
local root_dir="$ROOT_DIR"
local build_dir="${BUILD_DIR}"
local build_depends_dir="${BUILD_DEPENDS_DIR}"
make -C "$root_dir/depends" DESTDIR="${build_depends_dir}" clean-all || true
_ensure_enter_dir "$build_depends_dir"
clean_pkg_local_osx_sysroot
_safe_rm_rf built \
work \
sources \
x86_64* \
i686* \
mips* \
arm* \
aarch64* \
riscv32* \
riscv64*
_exit_dir
}
clean() {
local build_dir="${BUILD_TARGET_DIR}"
_ensure_enter_dir "${build_dir}"
make clean || true
_exit_dir
clean_artifacts
}
# ========
# Support methods
# ========
# Defaults
# ---
get_default_target() {
local default_target=""
if [[ "${OSTYPE}" == "darwin"* ]]; then
local macos_arch=""
macos_arch=$(uname -m || true)
if [[ "$macos_arch" == "x86_64" ]]; then
default_target="x86_64-apple-darwin"
else
default_target="aarch64-apple-darwin"
fi
elif [[ "${OSTYPE}" == "msys" ]]; then
default_target="x86_64-w64-mingw32"
else
# Note: make.sh only formally supports auto selection for
# windows under msys, mac os and debian derivatives to build on.
# Also note: Support for auto selection on make.sh does not imply
# support for the architecture.
# Only supported architectures are the ones with release builds
# enabled on the CI.
local dpkg_arch=""
dpkg_arch=$(dpkg --print-architecture || true)
if [[ "$dpkg_arch" == "armhf" ]]; then
default_target="arm-linux-gnueabihf"
elif [[ "$dpkg_arch" == "aarch64" ]]; then
default_target="aarch64-linux-gnu"
else
# Global default if we can't determine it from the
# above, which are our only supported list for auto select
default_target="x86_64-pc-linux-gnu"
fi
fi
echo "$default_target"
}
get_default_docker_file() {
local target="${TARGET}"
local dockerfiles_dir="${DOCKERFILES_DIR}"
local try_files=(
"${dockerfiles_dir}/${target}.dockerfile"
"${dockerfiles_dir}/${target}"
"${dockerfiles_dir}/noarch.dockerfile"
)