|
| 1 | +#!/bin/sh |
| 2 | +# Copyright 2014 The Rust Project Developers. See the COPYRIGHT |
| 3 | +# file at the top-level directory of this distribution and at |
| 4 | +# http://rust-lang.org/COPYRIGHT. |
| 5 | +# |
| 6 | +# Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0> or the MIT license |
| 8 | +# <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your |
| 9 | +# option. This file may not be copied, modified, or distributed |
| 10 | +# except according to those terms. |
| 11 | + |
| 12 | +set -ue |
| 13 | + |
| 14 | +msg() { |
| 15 | + echo "make-tarballs: ${1-}" |
| 16 | +} |
| 17 | + |
| 18 | +step_msg() { |
| 19 | + msg |
| 20 | + msg "$1" |
| 21 | + msg |
| 22 | +} |
| 23 | + |
| 24 | +warn() { |
| 25 | + echo "make-tarballs: WARNING: $1" >&2 |
| 26 | +} |
| 27 | + |
| 28 | +err() { |
| 29 | + echo "make-tarballs: error: $1" >&2 |
| 30 | + exit 1 |
| 31 | +} |
| 32 | + |
| 33 | +need_ok() { |
| 34 | + if [ $? -ne 0 ] |
| 35 | + then |
| 36 | + err "$1" |
| 37 | + fi |
| 38 | +} |
| 39 | + |
| 40 | +need_cmd() { |
| 41 | + if command -v $1 >/dev/null 2>&1 |
| 42 | + then msg "found $1" |
| 43 | + else err "need $1" |
| 44 | + fi |
| 45 | +} |
| 46 | + |
| 47 | +putvar() { |
| 48 | + local t |
| 49 | + local tlen |
| 50 | + eval t=\$$1 |
| 51 | + eval tlen=\${#$1} |
| 52 | + if [ $tlen -gt 35 ] |
| 53 | + then |
| 54 | + printf "make-tarballs: %-20s := %.35s ...\n" $1 "$t" |
| 55 | + else |
| 56 | + printf "make-tarballs: %-20s := %s %s\n" $1 "$t" |
| 57 | + fi |
| 58 | +} |
| 59 | + |
| 60 | +valopt() { |
| 61 | + VAL_OPTIONS="$VAL_OPTIONS $1" |
| 62 | + |
| 63 | + local op=$1 |
| 64 | + local default=$2 |
| 65 | + shift |
| 66 | + shift |
| 67 | + local doc="$*" |
| 68 | + if [ $HELP -eq 0 ] |
| 69 | + then |
| 70 | + local uop=$(echo $op | tr '[:lower:]' '[:upper:]' | tr '\-' '\_') |
| 71 | + local v="CFG_${uop}" |
| 72 | + eval $v="$default" |
| 73 | + for arg in $CFG_ARGS |
| 74 | + do |
| 75 | + if echo "$arg" | grep -q -- "--$op=" |
| 76 | + then |
| 77 | + local val=$(echo "$arg" | cut -f2 -d=) |
| 78 | + eval $v=$val |
| 79 | + fi |
| 80 | + done |
| 81 | + putvar $v |
| 82 | + else |
| 83 | + if [ -z "$default" ] |
| 84 | + then |
| 85 | + default="<none>" |
| 86 | + fi |
| 87 | + op="${op}=[${default}]" |
| 88 | + printf " --%-30s %s\n" "$op" "$doc" |
| 89 | + fi |
| 90 | +} |
| 91 | + |
| 92 | +opt() { |
| 93 | + BOOL_OPTIONS="$BOOL_OPTIONS $1" |
| 94 | + |
| 95 | + local op=$1 |
| 96 | + local default=$2 |
| 97 | + shift |
| 98 | + shift |
| 99 | + local doc="$*" |
| 100 | + local flag="" |
| 101 | + |
| 102 | + if [ $default -eq 0 ] |
| 103 | + then |
| 104 | + flag="enable" |
| 105 | + else |
| 106 | + flag="disable" |
| 107 | + doc="don't $doc" |
| 108 | + fi |
| 109 | + |
| 110 | + if [ $HELP -eq 0 ] |
| 111 | + then |
| 112 | + for arg in $CFG_ARGS |
| 113 | + do |
| 114 | + if [ "$arg" = "--${flag}-${op}" ] |
| 115 | + then |
| 116 | + op=$(echo $op | tr 'a-z-' 'A-Z_') |
| 117 | + flag=$(echo $flag | tr 'a-z' 'A-Z') |
| 118 | + local v="CFG_${flag}_${op}" |
| 119 | + eval $v=1 |
| 120 | + putvar $v |
| 121 | + fi |
| 122 | + done |
| 123 | + else |
| 124 | + if [ ! -z "$META" ] |
| 125 | + then |
| 126 | + op="$op=<$META>" |
| 127 | + fi |
| 128 | + printf " --%-30s %s\n" "$flag-$op" "$doc" |
| 129 | + fi |
| 130 | +} |
| 131 | + |
| 132 | +flag() { |
| 133 | + BOOL_OPTIONS="$BOOL_OPTIONS $1" |
| 134 | + |
| 135 | + local op=$1 |
| 136 | + shift |
| 137 | + local doc="$*" |
| 138 | + |
| 139 | + if [ $HELP -eq 0 ] |
| 140 | + then |
| 141 | + for arg in $CFG_ARGS |
| 142 | + do |
| 143 | + if [ "$arg" = "--${op}" ] |
| 144 | + then |
| 145 | + op=$(echo $op | tr 'a-z-' 'A-Z_') |
| 146 | + local v="CFG_${op}" |
| 147 | + eval $v=1 |
| 148 | + putvar $v |
| 149 | + fi |
| 150 | + done |
| 151 | + else |
| 152 | + if [ ! -z "$META" ] |
| 153 | + then |
| 154 | + op="$op=<$META>" |
| 155 | + fi |
| 156 | + printf " --%-30s %s\n" "$op" "$doc" |
| 157 | + fi |
| 158 | +} |
| 159 | + |
| 160 | +validate_opt () { |
| 161 | + for arg in $CFG_ARGS |
| 162 | + do |
| 163 | + local is_arg_valid=0 |
| 164 | + for option in $BOOL_OPTIONS |
| 165 | + do |
| 166 | + if test --disable-$option = $arg |
| 167 | + then |
| 168 | + is_arg_valid=1 |
| 169 | + fi |
| 170 | + if test --enable-$option = $arg |
| 171 | + then |
| 172 | + is_arg_valid=1 |
| 173 | + fi |
| 174 | + if test --$option = $arg |
| 175 | + then |
| 176 | + is_arg_valid=1 |
| 177 | + fi |
| 178 | + done |
| 179 | + for option in $VAL_OPTIONS |
| 180 | + do |
| 181 | + if echo "$arg" | grep -q -- "--$option=" |
| 182 | + then |
| 183 | + is_arg_valid=1 |
| 184 | + fi |
| 185 | + done |
| 186 | + if [ "$arg" = "--help" ] |
| 187 | + then |
| 188 | + echo |
| 189 | + echo "No more help available for Configure options," |
| 190 | + echo "check the Wiki or join our IRC channel" |
| 191 | + break |
| 192 | + else |
| 193 | + if test $is_arg_valid -eq 0 |
| 194 | + then |
| 195 | + err "Option '$arg' is not recognized" |
| 196 | + fi |
| 197 | + fi |
| 198 | + done |
| 199 | +} |
| 200 | + |
| 201 | +# Prints the absolute path of a directory to stdout |
| 202 | +abs_path() { |
| 203 | + local path="$1" |
| 204 | + # Unset CDPATH because it causes havok: it makes the destination unpredictable |
| 205 | + # and triggers 'cd' to print the path to stdout. Route `cd`'s output to /dev/null |
| 206 | + # for good measure. |
| 207 | + (unset CDPATH && cd "$path" > /dev/null && pwd) |
| 208 | +} |
| 209 | + |
| 210 | +msg "looking for programs" |
| 211 | +msg |
| 212 | + |
| 213 | +need_cmd tar |
| 214 | +need_cmd rm |
| 215 | +need_cmd mkdir |
| 216 | +need_cmd echo |
| 217 | +need_cmd tr |
| 218 | +need_cmd find |
| 219 | +need_cmd rev |
| 220 | +need_cmd sort |
| 221 | +need_cmd gzip |
| 222 | +need_cmd 7z |
| 223 | + |
| 224 | +CFG_ARGS="$@" |
| 225 | + |
| 226 | +HELP=0 |
| 227 | +if [ "$1" = "--help" ] |
| 228 | +then |
| 229 | + HELP=1 |
| 230 | + shift |
| 231 | + echo |
| 232 | + echo "Usage: $0 [options]" |
| 233 | + echo |
| 234 | + echo "Options:" |
| 235 | + echo |
| 236 | +else |
| 237 | + step_msg "processing arguments" |
| 238 | +fi |
| 239 | + |
| 240 | +OPTIONS="" |
| 241 | +BOOL_OPTIONS="" |
| 242 | +VAL_OPTIONS="" |
| 243 | + |
| 244 | +valopt input "package" "The input folder to be compressed" |
| 245 | +valopt output "./dist" "The prefix of the tarballs" |
| 246 | +valopt work-dir "./workdir" "The folder in which the input is to be found" |
| 247 | + |
| 248 | +if [ $HELP -eq 1 ] |
| 249 | +then |
| 250 | + echo |
| 251 | + exit 0 |
| 252 | +fi |
| 253 | + |
| 254 | +step_msg "validating arguments" |
| 255 | +validate_opt |
| 256 | + |
| 257 | +rm -Rf "$CFG_OUTPUT.tar.gz" |
| 258 | +need_ok "couldn't delete old gz tarball" |
| 259 | + |
| 260 | +rm -Rf "$CFG_OUTPUT.tar.xz" |
| 261 | +need_ok "couldn't delete old xz tarball" |
| 262 | + |
| 263 | +# Make a tarball |
| 264 | +cd "$CFG_WORK_DIR" |
| 265 | + |
| 266 | +# Sort files by their suffix, to group files with the same name from |
| 267 | +# different locations (likely identical) and files with the same |
| 268 | +# extension (likely containing similar data). |
| 269 | +find "$CFG_INPUT" \( -type d -empty \) -or \( -not -type d \) \ |
| 270 | + | rev | sort | rev | tar -cf "$CFG_OUTPUT.tar" -T - |
| 271 | +need_ok "failed to tar" |
| 272 | + |
| 273 | +# xz -9 -T2 --keep "$CFG_OUTPUT.tar" |
| 274 | +7z a -bd -txz -mx=9 -mmt=off "$CFG_OUTPUT.tar.xz" "$CFG_OUTPUT.tar" |
| 275 | +need_ok "failed to xz" |
| 276 | + |
| 277 | +gzip "$CFG_OUTPUT.tar" |
| 278 | +need_ok "failed to gzip" |
0 commit comments