|
| 1 | +#!/bin/sh -e |
| 2 | + |
| 3 | +#----------------------------------------------------------------------------- |
| 4 | +# Bundle release |
| 5 | +#----------------------------------------------------------------------------- |
| 6 | + |
| 7 | +QUIET="" |
| 8 | +FORCE="" |
| 9 | +OSARCH="" |
| 10 | +OSNAME="" |
| 11 | +MI_TAG="" |
| 12 | +MI_COMMIT="" |
| 13 | + |
| 14 | +#--------------------------------------------------------- |
| 15 | +# Helper functions |
| 16 | +#--------------------------------------------------------- |
| 17 | + |
| 18 | +info() { |
| 19 | + if [ -z "$QUIET" ] ; then |
| 20 | + echo "$@" |
| 21 | + fi |
| 22 | +} |
| 23 | + |
| 24 | +warn() { |
| 25 | + echo "$@" >&2 |
| 26 | +} |
| 27 | + |
| 28 | +stop() { |
| 29 | + warn $@ |
| 30 | + exit 1 |
| 31 | +} |
| 32 | + |
| 33 | +has_cmd() { |
| 34 | + command -v "$1" > /dev/null 2>&1 |
| 35 | +} |
| 36 | + |
| 37 | +on_path() { |
| 38 | + echo ":$PATH:" | grep -q :"$1": |
| 39 | +} |
| 40 | + |
| 41 | + |
| 42 | +#--------------------------------------------------------- |
| 43 | +# Detect OS and cpu architecture |
| 44 | +#--------------------------------------------------------- |
| 45 | + |
| 46 | +contains() { |
| 47 | + if echo "$1" | grep -i -E "$2" > /dev/null; then |
| 48 | + return 0 |
| 49 | + else |
| 50 | + return 1 |
| 51 | + fi |
| 52 | +} |
| 53 | + |
| 54 | +detect_osarch() { |
| 55 | + arch="$(uname -m)" |
| 56 | + case "$arch" in |
| 57 | + x86_64*|amd64*) |
| 58 | + arch="x64";; |
| 59 | + x86*|i[35678]86*) |
| 60 | + arch="x86";; |
| 61 | + arm64*|aarch64*|armv8*) |
| 62 | + arch="arm64";; |
| 63 | + arm*) |
| 64 | + arch="arm";; |
| 65 | + parisc*) |
| 66 | + arch="hppa";; |
| 67 | + esac |
| 68 | + |
| 69 | + OSNAME="linux" |
| 70 | + case "$(uname)" in |
| 71 | + [Ll]inux) |
| 72 | + OSNAME="linux";; |
| 73 | + [Dd]arwin) |
| 74 | + OSNAME="macos";; |
| 75 | + [Ff]ree[Bb][Ss][Dd]) |
| 76 | + OSNAME="unix-freebsd";; |
| 77 | + [Oo]pen[Bb][Ss][Dd]) |
| 78 | + OSNAME="unix-openbsd";; |
| 79 | + *) |
| 80 | + info "Warning: assuming generic Linux" |
| 81 | + esac |
| 82 | + OSARCH="$OSNAME-$arch" |
| 83 | + |
| 84 | + if [ "$OSNAME" = "linux" ]; then |
| 85 | + distrocfg=`cat $(find /etc/*-release -type f)` |
| 86 | + if contains "$distrocfg" "rhel"; then |
| 87 | + OSDISTRO="rhel" |
| 88 | + elif contains "$distrocfg" "opensuse"; then |
| 89 | + OSDISTRO="opensuse" |
| 90 | + elif contains "$distrocfg" "alpine"; then |
| 91 | + OSDISTRO="alpine" |
| 92 | + elif contains "$distrocfg" "arch"; then |
| 93 | + OSDISTRO="arch" |
| 94 | + elif contains "$distrocfg" "ubuntu|debian"; then |
| 95 | + OSDISTRO="ubuntu" |
| 96 | + else |
| 97 | + OSDISTRO="ubuntu" # default |
| 98 | + fi |
| 99 | + fi |
| 100 | +} |
| 101 | + |
| 102 | + |
| 103 | + |
| 104 | +#--------------------------------------------------------- |
| 105 | +# Command line options |
| 106 | +#--------------------------------------------------------- |
| 107 | + |
| 108 | +process_options() { |
| 109 | + while : ; do |
| 110 | + flag="$1" |
| 111 | + case "$flag" in |
| 112 | + *=*) flag_arg="${flag#*=}";; |
| 113 | + *) flag_arg="yes" ;; |
| 114 | + esac |
| 115 | + # echo "option: $flag, arg: $flag_arg" |
| 116 | + case "$flag" in |
| 117 | + "") break;; |
| 118 | + -q|--quiet) |
| 119 | + QUIET="yes";; |
| 120 | + -p) shift |
| 121 | + PREFIX="$1";; |
| 122 | + -p=*|--prefix=*) |
| 123 | + PREFIX=`eval echo $flag_arg`;; # no quotes so ~ gets expanded (issue #412) |
| 124 | + -t) shift |
| 125 | + MI_TAG="$1";; |
| 126 | + -t=*|--tag=*) |
| 127 | + MI_TAG="$flag_arg";; |
| 128 | + -h|--help|-\?|help|\?) |
| 129 | + MODE="help";; |
| 130 | + *) case "$flag" in |
| 131 | + *) warn "warning: unknown option \"$1\".";; |
| 132 | + esac;; |
| 133 | + esac |
| 134 | + shift |
| 135 | + done |
| 136 | + |
| 137 | + if [ -z "$MI_COMMIT" ] ; then |
| 138 | + MI_COMMIT=`git log -n 1 --pretty=format:"%H"` |
| 139 | + fi |
| 140 | + if [ -z "$MI_TAG" ] ; then |
| 141 | + MI_TAG=`git describe --tag` |
| 142 | + fi |
| 143 | + info "Tag : $MI_TAG" |
| 144 | + info "Commit : $MI_COMMIT" |
| 145 | +} |
| 146 | + |
| 147 | + |
| 148 | + |
| 149 | +#--------------------------------------------------------- |
| 150 | +# Download |
| 151 | +#--------------------------------------------------------- |
| 152 | + |
| 153 | +download_failed() { # <program> <url> |
| 154 | + warn "" |
| 155 | + warn "unable to download: $2" |
| 156 | + stop "" |
| 157 | +} |
| 158 | + |
| 159 | +download_file() { # <url|file> <destination file> |
| 160 | + case "$1" in |
| 161 | + ftp://*|http://*|https://*) |
| 162 | + info "Downloading: $1" |
| 163 | + if has_cmd curl ; then |
| 164 | + if ! curl ${QUIET:+-sS} --proto =https --tlsv1.2 -f -L -o "$2" "$1"; then |
| 165 | + download_failed "curl" $1 |
| 166 | + fi |
| 167 | + elif has_cmd wget ; then |
| 168 | + if ! wget ${QUIET:+-q} --https-only "-O$2" "$1"; then |
| 169 | + download_failed "wget" $1 |
| 170 | + fi |
| 171 | + else |
| 172 | + stop "Neither 'curl' nor 'wget' is available; install one to continue." |
| 173 | + fi;; |
| 174 | + *) |
| 175 | + # echo "cp $1 to $2" |
| 176 | + info "Copying: $1" |
| 177 | + if ! cp $1 $2 ; then |
| 178 | + stop "Unable to copy from $1" |
| 179 | + fi;; |
| 180 | + esac |
| 181 | +} |
| 182 | + |
| 183 | +download_available() { # <url|file> |
| 184 | + case "$1" in |
| 185 | + ftp://*|http://*|https://*) |
| 186 | + if has_cmd curl ; then |
| 187 | + if ! curl -sS --proto =https --tlsv1.2 -L -I "$1" | grep -E "^HTTP/2 200" ; then # -I is headers only |
| 188 | + return 1 |
| 189 | + fi |
| 190 | + fi;; |
| 191 | + *) |
| 192 | + if ! [ -f "$1" ] ; then |
| 193 | + return 1 |
| 194 | + fi;; |
| 195 | + esac |
| 196 | + return 0 |
| 197 | +} |
| 198 | + |
| 199 | +download_source_at_tag() { # <tag> <output file> |
| 200 | + download_file "https://github.com/microsoft/mimalloc/archive/refs/tags/$1.tar.gz" "$2" |
| 201 | +} |
| 202 | + |
| 203 | +download_source_at_commit() { # <commit> <output file> |
| 204 | + download_file "https://github.com/microsoft/mimalloc/archive/$1.tar.gz" "$2" |
| 205 | +} |
| 206 | + |
| 207 | +build_test_install() { # <type> <bundledir> <prefix> <cmake args> |
| 208 | + info "Build, test, install: $1 to $3" |
| 209 | + build_dir="$2/$1" |
| 210 | + mkdir -p "$build_dir" |
| 211 | + cmake . -B "$build_dir" $4 |
| 212 | + cmake --build "$build_dir" |
| 213 | + ctest --test-dir "$build_dir" |
| 214 | + cmake --install "$build_dir" --prefix "$3" |
| 215 | +} |
| 216 | + |
| 217 | +#--------------------------------------------------------- |
| 218 | +# Main |
| 219 | +#--------------------------------------------------------- |
| 220 | + |
| 221 | +main_bundle() { |
| 222 | + # config |
| 223 | + bundle_dir="out/bundle" |
| 224 | + mkdir -p "$bundle_dir" |
| 225 | + |
| 226 | + # source archive |
| 227 | + info "Download source archive for $MI_TAG" |
| 228 | + source_archive="$bundle_dir/mimalloc-$MI_TAG-source.tar.gz" |
| 229 | + download_source_at_commit "$MI_COMMIT" "$source_archive" |
| 230 | + |
| 231 | + # build |
| 232 | + prefix_dir="$bundle_dir/prefix" |
| 233 | + build_test_install "debug" "$bundle_dir" "$prefix_dir" "-DCMAKE_BUILD_TYPE=Debug" |
| 234 | + build_test_install "release" "$bundle_dir" "$prefix_dir" "-DCMAKE_BUILD_TYPE=Release -DMI_OPT_ARCH=ON" |
| 235 | + build_test_install "secure" "$bundle_dir" "$prefix_dir" "-DCMAKE_BUILD_TYPE=Release -DMI_OPT_ARCH=ON -DMI_SECURE=ON" |
| 236 | + |
| 237 | + # archive binaries |
| 238 | + binary_archive_name="mimalloc-$MI_TAG-$OSARCH.tar.gz" |
| 239 | + binary_archive="$bundle_dir/$binary_archive_name" |
| 240 | + info "Create binary archive: $binary_archive_name" |
| 241 | + pushd $bundle_dir |
| 242 | + tar -czvf "$binary_archive_name" prefix |
| 243 | + popd |
| 244 | + |
| 245 | + # done |
| 246 | + info "" |
| 247 | + info "Created:" |
| 248 | + info " - $binary_archive" |
| 249 | + info " - $source_archive" |
| 250 | + info "" |
| 251 | + info "Done." |
| 252 | +} |
| 253 | + |
| 254 | + |
| 255 | +main_help() { |
| 256 | + info "command:" |
| 257 | + info " ./bin/bundle.sh [options]" |
| 258 | + info "" |
| 259 | + info "options:" |
| 260 | + info " -q, --quiet suppress output" |
| 261 | + info " -f, --force continue without prompting" |
| 262 | + info " -p, --prefix=<dir> prefix directory ($PREFIX)" |
| 263 | + info "" |
| 264 | +} |
| 265 | + |
| 266 | +main_start() { |
| 267 | + detect_osarch |
| 268 | + process_options $@ |
| 269 | + if [ "$MODE" = "help" ] ; then |
| 270 | + main_help |
| 271 | + else |
| 272 | + main_bundle |
| 273 | + fi |
| 274 | +} |
| 275 | + |
| 276 | +# note: only start executing commands now to guard against partial downloads |
| 277 | +main_start $@ |
0 commit comments