Debian Codegraph @

contrib/build_tools_util.sh

raw symbols SCIP
1 #!/usr/bin/env bash
2
3 set -e
4
5 # Set a fixed umask as this leaks into docker containers
6 umask 0022
7
8 RED='\033[0;31m'
9 BLUE='\033[0;34m'
10 YELLOW='\033[0;33m'
11 NC='\033[0m' # No Color
12 function info {
13 printf "\r💬 ${BLUE}INFO:${NC} ${1}\n"
14 }
15 function fail {
16 printf "\r🗯 ${RED}ERROR:${NC} ${1}\n"
17 exit 1
18 }
19 function warn {
20 printf "\r⚠️ ${YELLOW}WARNING:${NC} ${1}\n"
21 }
22
23
24 # based on https://superuser.com/questions/497940/script-to-verify-a-signature-with-gpg
25 function verify_signature() {
26 local file=$1 keyring=$2 out=
27 if out=$(gpg --no-default-keyring --keyring "$keyring" --status-fd 1 --verify "$file" 2>/dev/null) &&
28 echo "$out" | grep -qs "^\[GNUPG:\] VALIDSIG "; then
29 return 0
30 else
31 echo "$out" >&2
32 exit 1
33 fi
34 }
35
36 function verify_hash() {
37 local file=$1 expected_hash=$2
38 actual_hash=$(sha256sum "$file" | awk '{print $1}')
39 if [ "$actual_hash" == "$expected_hash" ]; then
40 return 0
41 else
42 echo "$file $actual_hash (unexpected hash)" >&2
43 rm "$file"
44 exit 1
45 fi
46 }
47
48 function download_if_not_exist() {
49 local file_name=$1 url=$2
50 if [ ! -e "$file_name" ] ; then
51 wget -O "$file_name" "$url"
52 fi
53 }
54
55 # Function to clone or update a git repository to a specific commit
56 clone_or_update_repo() {
57 local repo_url=$1
58 local commit_hash=$2
59 local repo_dir=$3
60
61 if [ -z "$repo_url" ] || [ -z "$commit_hash" ] || [ -z "$repo_dir" ]; then
62 fail "clone_or_update_repo: invalid arguments: repo_url='$repo_url', commit_hash='$commit_hash', repo_dir='$repo_dir'"
63 fi
64
65 if [ -d "$repo_dir" ]; then
66 info "Repository $repo_url exists in $repo_dir, updating..."
67 git -C "$repo_dir" clean -ffxd >/dev/null 2>&1 || fail "Failed to clean repository $repo_dir"
68 git -C "$repo_dir" fetch --all >/dev/null 2>&1 || fail "Failed to fetch from repository"
69 git -C "$repo_dir" reset --hard "$commit_hash^{commit}" >/dev/null 2>&1 || fail "Failed to reset to commit $commit_hash"
70 else
71 info "Cloning repository: $repo_url to $repo_dir"
72 git clone "$repo_url" "$repo_dir" >/dev/null 2>&1 || fail "Failed to clone repository $repo_url"
73 git -C "$repo_dir" checkout "$commit_hash^{commit}" >/dev/null 2>&1 || fail "Failed to checkout commit $commit_hash"
74 fi
75 }
76
77 apply_patch() {
78 local patch=$1
79 local path=$2
80
81 if [ -z "$patch" ] || [ -z "$path" ]; then
82 fail "apply_patch: invalid arguments: patch='$patch', path='$path'"
83 fi
84
85 if [ -d "$path" ]; then
86 info "Patching: $patch"
87 cd "$path"
88 patch -p1 <"$patch"
89 cd -
90 else
91 fail "apply_patch: path='$path' not found"
92 fi
93 }
94
95 # https://github.com/travis-ci/travis-build/blob/master/lib/travis/build/templates/header.sh
96 function retry() {
97 local result=0
98 local count=1
99 while [ $count -le 3 ]; do
100 [ $result -ne 0 ] && {
101 echo -e "\nThe command \"$@\" failed. Retrying, $count of 3.\n" >&2
102 }
103 ! { "$@"; result=$?; }
104 [ $result -eq 0 ] && break
105 count=$(($count + 1))
106 sleep 1
107 done
108
109 [ $count -gt 3 ] && {
110 echo -e "\nThe command \"$@\" failed 3 times.\n" >&2
111 }
112
113 return $result
114 }
115
116 function gcc_with_triplet()
117 {
118 TRIPLET="$1"
119 CMD="$2"
120 shift 2
121 if [ -n "$TRIPLET" ] ; then
122 "$TRIPLET-$CMD" "$@"
123 else
124 "$CMD" "$@"
125 fi
126 }
127
128 function gcc_host()
129 {
130 gcc_with_triplet "$GCC_TRIPLET_HOST" "$@"
131 }
132
133 function gcc_build()
134 {
135 gcc_with_triplet "$GCC_TRIPLET_BUILD" "$@"
136 }
137
138 function host_strip()
139 {
140 if [ "$GCC_STRIP_BINARIES" -ne "0" ] ; then
141 case "$BUILD_TYPE" in
142 linux|wine)
143 gcc_host strip "$@"
144 ;;
145 darwin)
146 # TODO: Strip on macOS?
147 ;;
148 esac
149 fi
150 }
151
152 # on MacOS, there is no realpath by default
153 if ! [ -x "$(command -v realpath)" ]; then
154 function realpath() {
155 [[ $1 = /* ]] && echo "$1" || echo "$PWD/${1#./}"
156 }
157 fi
158
159
160 export SOURCE_DATE_EPOCH=1530212462
161 export ZERO_AR_DATE=1 # for macOS
162 export PYTHONHASHSEED=22
163 # Set the build type, overridden by wine build
164 export BUILD_TYPE="${BUILD_TYPE:-$(uname | tr '[:upper:]' '[:lower:]')}"
165 # Add host / build flags if the triplets are set
166 if [ -n "$GCC_TRIPLET_HOST" ] ; then
167 export AUTOCONF_FLAGS="$AUTOCONF_FLAGS --host=$GCC_TRIPLET_HOST"
168 fi
169 if [ -n "$GCC_TRIPLET_BUILD" ] ; then
170 export AUTOCONF_FLAGS="$AUTOCONF_FLAGS --build=$GCC_TRIPLET_BUILD"
171 fi
172
173 export GCC_STRIP_BINARIES="${GCC_STRIP_BINARIES:-0}"
174
175 export CPU_COUNT="$(nproc 2> /dev/null || sysctl -n hw.ncpu)"
176 info "Found $CPU_COUNT CPUs, which we might use for building."
177
178
179 function break_legacy_easy_install() {
180 # We don't want setuptools sneakily installing dependencies, invisible to pip.
181 # This ensures that if setuptools calls distutils which then calls easy_install,
182 # easy_install will not download packages over the network.
183 # see https://pip.pypa.io/en/stable/reference/pip_install/#controlling-setup-requires
184 # see https://github.com/pypa/setuptools/issues/1916#issuecomment-743350566
185 info "Intentionally breaking legacy easy_install."
186 DISTUTILS_CFG="${HOME}/.pydistutils.cfg"
187 DISTUTILS_CFG_BAK="${HOME}/.pydistutils.cfg.orig"
188 # If we are not inside docker, we might be overwriting a config file on the user's system...
189 if [ -e "$DISTUTILS_CFG" ] && [ ! -e "$DISTUTILS_CFG_BAK" ]; then
190 warn "Overwriting python distutils config file at '$DISTUTILS_CFG'. A copy will be saved at '$DISTUTILS_CFG_BAK'."
191 mv "$DISTUTILS_CFG" "$DISTUTILS_CFG_BAK"
192 fi
193 cat <<EOF > "$DISTUTILS_CFG"
194 [easy_install]
195 index_url = ''
196 find_links = ''
197 EOF
198 }
199