| 1 | #!/usr/bin/envscip-shell filesystem . . `/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 infoscip-shell shell shell-project 0.0.0 info(). { |
| 13 | printfscip-shell system . . printf. "\r💬 ${BLUE}INFO:${NC} ${1}\n" |
| 14 | } |
| 15 | function failscip-shell shell shell-project 0.0.0 fail(). { |
| 16 | printfscip-shell system . . printf. "\r🗯 ${RED}ERROR:${NC} ${1}\n" |
| 17 | exit 1 |
| 18 | } |
| 19 | function warnscip-shell shell shell-project 0.0.0 warn(). { |
| 20 | printfscip-shell system . . 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_signaturescip-shell shell shell-project 0.0.0 verify_signature().() { |
| 26 | local file=$1 keyring=$2 out= |
| 27 | if out=$(gpgscip-shell system . . gpg. --no-default-keyring --keyring "$keyring" --status-fd 1 --verify "$file" 2>/dev/nullscip-shell filesystem . . `/dev/null`.) && |
| 28 | echoscip-shell system . . echo. "$out" | grepscip-shell system . . grep. -qs "^\[GNUPG:\] VALIDSIG "; then |
| 29 | return 0 |
| 30 | else |
| 31 | echoscip-shell system . . echo. "$out" >&2 |
| 32 | exit 1 |
| 33 | fi |
| 34 | } |
| 35 | |
| 36 | function verify_hashscip-shell shell shell-project 0.0.0 verify_hash().() { |
| 37 | local file=$1 expected_hash=$2 |
| 38 | actual_hash=$(sha256sumscip-shell system . . sha256sum. "$file" | awkscip-shell system . . awk. '{print $1}') |
| 39 | if [scip-shell system . . `[`. "$actual_hash" == "$expected_hash" ]; then |
| 40 | return 0 |
| 41 | else |
| 42 | echoscip-shell system . . echo. "$file $actual_hash (unexpected hash)" >&2 |
| 43 | rmscip-shell system . . rm. "$file" |
| 44 | exit 1 |
| 45 | fi |
| 46 | } |
| 47 | |
| 48 | function download_if_not_existscip-shell shell shell-project 0.0.0 download_if_not_exist().() { |
| 49 | local file_name=$1 url=$2 |
| 50 | if [scip-shell system . . `[`. ! -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_reposcip-shell shell shell-project 0.0.0 clone_or_update_repo().() { |
| 57 | local repo_url=$1 |
| 58 | local commit_hash=$2 |
| 59 | local repo_dir=$3 |
| 60 | |
| 61 | if [scip-shell system . . `[`. -z "$repo_url" ] || [scip-shell system . . `[`. -z "$commit_hash" ] || [scip-shell system . . `[`. -z "$repo_dir" ]; then |
| 62 | failscip-shell shell shell-project 0.0.0 fail(). "clone_or_update_repo: invalid arguments: repo_url='$repo_url', commit_hash='$commit_hash', repo_dir='$repo_dir'" |
| 63 | fi |
| 64 | |
| 65 | if [scip-shell system . . `[`. -d "$repo_dir" ]; then |
| 66 | infoscip-shell shell shell-project 0.0.0 info(). "Repository $repo_url exists in $repo_dir, updating..." |
| 67 | gitscip-shell system . . git. -C "$repo_dir" clean -ffxd >/dev/nullscip-shell filesystem . . `/dev/null`. 2>&1 || failscip-shell shell shell-project 0.0.0 fail(). "Failed to clean repository $repo_dir" |
| 68 | gitscip-shell system . . git. -C "$repo_dir" fetch --all >/dev/nullscip-shell filesystem . . `/dev/null`. 2>&1 || failscip-shell shell shell-project 0.0.0 fail(). "Failed to fetch from repository" |
| 69 | gitscip-shell system . . git. -C "$repo_dir" reset --hard "$commit_hash^{commit}" >/dev/nullscip-shell filesystem . . `/dev/null`. 2>&1 || failscip-shell shell shell-project 0.0.0 fail(). "Failed to reset to commit $commit_hash" |
| 70 | else |
| 71 | infoscip-shell shell shell-project 0.0.0 info(). "Cloning repository: $repo_url to $repo_dir" |
| 72 | gitscip-shell system . . git. clone "$repo_url" "$repo_dir" >/dev/nullscip-shell filesystem . . `/dev/null`. 2>&1 || failscip-shell shell shell-project 0.0.0 fail(). "Failed to clone repository $repo_url" |
| 73 | gitscip-shell system . . git. -C "$repo_dir" checkout "$commit_hash^{commit}" >/dev/nullscip-shell filesystem . . `/dev/null`. 2>&1 || failscip-shell shell shell-project 0.0.0 fail(). "Failed to checkout commit $commit_hash" |
| 74 | fi |
| 75 | } |
| 76 | |
| 77 | apply_patchscip-shell shell shell-project 0.0.0 apply_patch().() { |
| 78 | local patch=$1 |
| 79 | local path=$2 |
| 80 | |
| 81 | if [scip-shell system . . `[`. -z "$patch" ] || [scip-shell system . . `[`. -z "$path" ]; then |
| 82 | failscip-shell shell shell-project 0.0.0 fail(). "apply_patch: invalid arguments: patch='$patch', path='$path'" |
| 83 | fi |
| 84 | |
| 85 | if [scip-shell system . . `[`. -d "$path" ]; then |
| 86 | infoscip-shell shell shell-project 0.0.0 info(). "Patching: $patch" |
| 87 | cd "$path" |
| 88 | patchscip-shell system . . patch. -p1 <"$patch" |
| 89 | cd - |
| 90 | else |
| 91 | failscip-shell shell shell-project 0.0.0 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 retryscip-shell shell shell-project 0.0.0 retry().() { |
| 97 | local result=0 |
| 98 | local count=1 |
| 99 | while [scip-shell system . . `[`. $count -le 3 ]; do |
| 100 | [scip-shell system . . `[`. $result -ne 0 ] && { |
| 101 | echoscip-shell system . . echo. -e "\nThe command \"$@\" failed. Retrying, $count of 3.\n" >&2 |
| 102 | } |
| 103 | ! { "$@"; result=$?; } |
| 104 | [scip-shell system . . `[`. $result -eq 0 ] && break |
| 105 | count=$(($count + 1)) |
| 106 | sleepscip-shell system . . sleep. 1 |
| 107 | done |
| 108 | |
| 109 | [scip-shell system . . `[`. $count -gt 3 ] && { |
| 110 | echoscip-shell system . . echo. -e "\nThe command \"$@\" failed 3 times.\n" >&2 |
| 111 | } |
| 112 | |
| 113 | return $result |
| 114 | } |
| 115 | |
| 116 | function gcc_with_tripletscip-shell shell shell-project 0.0.0 gcc_with_triplet().() |
| 117 | { |
| 118 | TRIPLET="$1" |
| 119 | CMD="$2" |
| 120 | shift 2 |
| 121 | if [scip-shell system . . `[`. -n "$TRIPLET" ] ; then |
| 122 | "$TRIPLET-$CMD" "$@" |
| 123 | else |
| 124 | "$CMD" "$@" |
| 125 | fi |
| 126 | } |
| 127 | |
| 128 | function gcc_hostscip-shell shell shell-project 0.0.0 gcc_host().() |
| 129 | { |
| 130 | gcc_with_tripletscip-shell shell shell-project 0.0.0 gcc_with_triplet(). "$GCC_TRIPLET_HOST" "$@" |
| 131 | } |
| 132 | |
| 133 | function gcc_buildscip-shell shell shell-project 0.0.0 gcc_build().() |
| 134 | { |
| 135 | gcc_with_tripletscip-shell shell shell-project 0.0.0 gcc_with_triplet(). "$GCC_TRIPLET_BUILD" "$@" |
| 136 | } |
| 137 | |
| 138 | function host_stripscip-shell shell shell-project 0.0.0 host_strip().() |
| 139 | { |
| 140 | if [scip-shell system . . `[`. "$GCC_STRIP_BINARIES" -ne "0" ] ; then |
| 141 | case "$BUILD_TYPE" in |
| 142 | linux|wine) |
| 143 | gcc_hostscip-shell shell shell-project 0.0.0 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 ! [scip-shell system . . `[`. -x "$(command -v realpath)" ]; then |
| 154 | function realpathscip-shell shell shell-project 0.0.0 realpath().() { |
| 155 | [[ $1 = /* ]] && echoscip-shell system . . echo. "$1" || echoscip-shell system . . 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:-$(unamescip-shell system . . uname. | trscip-shell system . . tr. '[:upper:]' '[:lower:]')}" |
| 165 | # Add host / build flags if the triplets are set |
| 166 | if [scip-shell system . . `[`. -n "$GCC_TRIPLET_HOST" ] ; then |
| 167 | export AUTOCONF_FLAGS="$AUTOCONF_FLAGS --host=$GCC_TRIPLET_HOST" |
| 168 | fi |
| 169 | if [scip-shell system . . `[`. -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="$(nprocscip-shell system . . nproc. 2> /dev/nullscip-shell filesystem . . `/dev/null`. || sysctlscip-shell system . . sysctl. -n hw.ncpu)" |
| 176 | infoscip-shell shell shell-project 0.0.0 info(). "Found $CPU_COUNT CPUs, which we might use for building." |
| 177 | |
| 178 | |
| 179 | function break_legacy_easy_installscip-shell shell shell-project 0.0.0 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 | infoscip-shell shell shell-project 0.0.0 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 [scip-shell system . . `[`. -e "$DISTUTILS_CFG" ] && [scip-shell system . . `[`. ! -e "$DISTUTILS_CFG_BAK" ]; then |
| 190 | warnscip-shell shell shell-project 0.0.0 warn(). "Overwriting python distutils config file at '$DISTUTILS_CFG'. A copy will be saved at '$DISTUTILS_CFG_BAK'." |
| 191 | mvscip-shell system . . mv. "$DISTUTILS_CFG" "$DISTUTILS_CFG_BAK" |
| 192 | fi |
| 193 | catscip-shell system . . cat. <<EOF > "$DISTUTILS_CFG" |
| 194 | [easy_install] |
| 195 | index_url = '' |
| 196 | find_links = '' |
| 197 | EOF |
| 198 | } |
| 199 |