]> git.proxmox.com Git - rustc.git/blob - src/tools/rust-installer/test/rust-installer-v2/gen-installer.sh
New upstream version 1.20.0+dfsg1
[rustc.git] / src / tools / rust-installer / test / rust-installer-v2 / gen-installer.sh
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 msg() {
13 echo "gen-installer: $1"
14 }
15
16 step_msg() {
17 msg
18 msg "$1"
19 msg
20 }
21
22 warn() {
23 echo "gen-installer: WARNING: $1"
24 }
25
26 err() {
27 echo "gen-installer: error: $1"
28 exit 1
29 }
30
31 need_ok() {
32 if [ $? -ne 0 ]
33 then
34 err "$1"
35 fi
36 }
37
38 need_cmd() {
39 if command -v $1 >/dev/null 2>&1
40 then msg "found $1"
41 else err "need $1"
42 fi
43 }
44
45 putvar() {
46 local T
47 eval T=\$$1
48 eval TLEN=\${#$1}
49 if [ $TLEN -gt 35 ]
50 then
51 printf "gen-installer: %-20s := %.35s ...\n" $1 "$T"
52 else
53 printf "gen-installer: %-20s := %s %s\n" $1 "$T" "$2"
54 fi
55 }
56
57 valopt() {
58 VAL_OPTIONS="$VAL_OPTIONS $1"
59
60 local OP=$1
61 local DEFAULT=$2
62 shift
63 shift
64 local DOC="$*"
65 if [ $HELP -eq 0 ]
66 then
67 local UOP=$(echo $OP | tr '[:lower:]' '[:upper:]' | tr '\-' '\_')
68 local V="CFG_${UOP}"
69 eval $V="$DEFAULT"
70 for arg in $CFG_ARGS
71 do
72 if echo "$arg" | grep -q -- "--$OP="
73 then
74 val=$(echo "$arg" | cut -f2 -d=)
75 eval $V=$val
76 fi
77 done
78 putvar $V
79 else
80 if [ -z "$DEFAULT" ]
81 then
82 DEFAULT="<none>"
83 fi
84 OP="${OP}=[${DEFAULT}]"
85 printf " --%-30s %s\n" "$OP" "$DOC"
86 fi
87 }
88
89 opt() {
90 BOOL_OPTIONS="$BOOL_OPTIONS $1"
91
92 local OP=$1
93 local DEFAULT=$2
94 shift
95 shift
96 local DOC="$*"
97 local FLAG=""
98
99 if [ $DEFAULT -eq 0 ]
100 then
101 FLAG="enable"
102 else
103 FLAG="disable"
104 DOC="don't $DOC"
105 fi
106
107 if [ $HELP -eq 0 ]
108 then
109 for arg in $CFG_ARGS
110 do
111 if [ "$arg" = "--${FLAG}-${OP}" ]
112 then
113 OP=$(echo $OP | tr 'a-z-' 'A-Z_')
114 FLAG=$(echo $FLAG | tr 'a-z' 'A-Z')
115 local V="CFG_${FLAG}_${OP}"
116 eval $V=1
117 putvar $V
118 fi
119 done
120 else
121 if [ ! -z "$META" ]
122 then
123 OP="$OP=<$META>"
124 fi
125 printf " --%-30s %s\n" "$FLAG-$OP" "$DOC"
126 fi
127 }
128
129 flag() {
130 BOOL_OPTIONS="$BOOL_OPTIONS $1"
131
132 local OP=$1
133 shift
134 local DOC="$*"
135
136 if [ $HELP -eq 0 ]
137 then
138 for arg in $CFG_ARGS
139 do
140 if [ "$arg" = "--${OP}" ]
141 then
142 OP=$(echo $OP | tr 'a-z-' 'A-Z_')
143 local V="CFG_${OP}"
144 eval $V=1
145 putvar $V
146 fi
147 done
148 else
149 if [ ! -z "$META" ]
150 then
151 OP="$OP=<$META>"
152 fi
153 printf " --%-30s %s\n" "$OP" "$DOC"
154 fi
155 }
156
157 validate_opt () {
158 for arg in $CFG_ARGS
159 do
160 isArgValid=0
161 for option in $BOOL_OPTIONS
162 do
163 if test --disable-$option = $arg
164 then
165 isArgValid=1
166 fi
167 if test --enable-$option = $arg
168 then
169 isArgValid=1
170 fi
171 if test --$option = $arg
172 then
173 isArgValid=1
174 fi
175 done
176 for option in $VAL_OPTIONS
177 do
178 if echo "$arg" | grep -q -- "--$option="
179 then
180 isArgValid=1
181 fi
182 done
183 if [ "$arg" = "--help" ]
184 then
185 echo
186 echo "No more help available for Configure options,"
187 echo "check the Wiki or join our IRC channel"
188 break
189 else
190 if test $isArgValid -eq 0
191 then
192 err "Option '$arg' is not recognized"
193 fi
194 fi
195 done
196 }
197
198 msg "looking for programs"
199 msg
200
201 need_cmd tar
202 need_cmd cp
203 need_cmd rm
204 need_cmd mkdir
205 need_cmd echo
206 need_cmd tr
207 need_cmd awk
208
209 CFG_SRC_DIR="$(cd $(dirname $0) && pwd)"
210 CFG_SELF="$0"
211 CFG_ARGS="$@"
212
213 HELP=0
214 if [ "$1" = "--help" ]
215 then
216 HELP=1
217 shift
218 echo
219 echo "Usage: $CFG_SELF [options]"
220 echo
221 echo "Options:"
222 echo
223 else
224 step_msg "processing $CFG_SELF args"
225 fi
226
227 valopt product-name "Product" "The name of the product, for display"
228 valopt component-name "component" "The name of the component, distinct from other installed components"
229 valopt package-name "package" "The name of the package, tarball"
230 valopt verify-bin "" "The command to run with --version to verify the install works"
231 valopt rel-manifest-dir "${CFG_PACKAGE_NAME}lib" "The directory under lib/ where the manifest lives"
232 valopt success-message "Installed." "The string to print after successful installation"
233 valopt legacy-manifest-dirs "" "Places to look for legacy manifests to uninstall"
234 valopt non-installed-prefixes "" "Path prefixes that should be included but not installed"
235 valopt bulk-dirs "" "Path prefixes of directories that should be installed/uninstalled in bulk"
236 valopt image-dir "./install-image" "The directory containing the installation medium"
237 valopt work-dir "./workdir" "The directory to do temporary work"
238 valopt output-dir "./dist" "The location to put the final image and tarball"
239
240 if [ $HELP -eq 1 ]
241 then
242 echo
243 exit 0
244 fi
245
246 step_msg "validating $CFG_SELF args"
247 validate_opt
248
249 RUST_INSTALLER_VERSION=`cat "$CFG_SRC_DIR/rust-installer-version"`
250
251 if [ ! -d "$CFG_IMAGE_DIR" ]
252 then
253 err "image dir $CFG_IMAGE_DIR does not exist"
254 fi
255
256 mkdir -p "$CFG_WORK_DIR"
257 need_ok "couldn't create work dir"
258
259 rm -Rf "$CFG_WORK_DIR/$CFG_PACKAGE_NAME"
260 need_ok "couldn't delete work package dir"
261
262 mkdir -p "$CFG_WORK_DIR/$CFG_PACKAGE_NAME"
263 need_ok "couldn't create work package dir"
264
265 cp -r "$CFG_IMAGE_DIR/"* "$CFG_WORK_DIR/$CFG_PACKAGE_NAME"
266 need_ok "couldn't copy source image"
267
268 # Create the manifest
269 MANIFEST=`(cd "$CFG_WORK_DIR/$CFG_PACKAGE_NAME" && find . -type f | sed 's/^\.\///') | sort`
270
271 # Remove non-installed files from manifest
272 NON_INSTALLED_PREFIXES=`echo "$CFG_NON_INSTALLED_PREFIXES" | tr "," " "`
273 for prefix in $NON_INSTALLED_PREFIXES; do
274 # This adds the escapes to '/' in paths to make them '\/' so sed doesn't puke.
275 # I figured this out by adding backslashes until it worked. Holy shit.
276 prefix=`echo "$prefix" | sed s/\\\//\\\\\\\\\\\//g`
277 MANIFEST=`echo "$MANIFEST" | sed /^$prefix/d`
278 done
279
280 # Remove files in bulk dirs
281 BULK_DIRS=`echo "$CFG_BULK_DIRS" | tr "," " "`
282 for bulk_dir in $BULK_DIRS; do
283 bulk_dir=`echo "$bulk_dir" | sed s/\\\//\\\\\\\\\\\//g`
284 MANIFEST=`echo "$MANIFEST" | sed /^$bulk_dir/d`
285 done
286
287 # Add 'file:' installation directives.
288 # The -n prevents adding a blank file: if the manifest is empty
289 MANIFEST=`/bin/echo -n "$MANIFEST" | sed s/^/file:/`
290
291 # Add 'dir:' directives
292 for bulk_dir in $BULK_DIRS; do
293 MANIFEST=`echo "$MANIFEST" && echo "dir:$bulk_dir"`
294 done
295
296 # The above step may have left a leading empty line if there were only
297 # bulk dirs. Remove it.
298 MANIFEST=`echo "$MANIFEST" | sed /^$/d`
299
300 MANIFEST_FILE="$CFG_WORK_DIR/$CFG_PACKAGE_NAME/manifest-$CFG_COMPONENT_NAME.in"
301 COMPONENT_FILE="$CFG_WORK_DIR/$CFG_PACKAGE_NAME/components"
302 VERSION_FILE="$CFG_WORK_DIR/$CFG_PACKAGE_NAME/rust-installer-version"
303
304 # Write the manifest
305 echo "$MANIFEST" > "$MANIFEST_FILE"
306
307 # Write the component name
308 echo "$CFG_COMPONENT_NAME" > "$COMPONENT_FILE"
309
310 # Write the installer version (only used by combine-installers.sh)
311 echo "$RUST_INSTALLER_VERSION" > "$VERSION_FILE"
312
313 # Generate the install script
314 "$CFG_SRC_DIR/gen-install-script.sh" \
315 --product-name="$CFG_PRODUCT_NAME" \
316 --verify-bin="$CFG_VERIFY_BIN" \
317 --rel-manifest-dir="$CFG_REL_MANIFEST_DIR" \
318 --success-message="$CFG_SUCCESS_MESSAGE" \
319 --legacy-manifest-dirs="$CFG_LEGACY_MANIFEST_DIRS" \
320 --output-script="$CFG_WORK_DIR/$CFG_PACKAGE_NAME/install.sh"
321
322 need_ok "failed to generate install script"
323
324 mkdir -p "$CFG_OUTPUT_DIR"
325 need_ok "couldn't create output dir"
326
327 rm -Rf "$CFG_OUTPUT_DIR/$CFG_PACKAGE_NAME.tar.gz"
328 need_ok "couldn't delete old tarball"
329
330 # Make a tarball
331 tar -czf "$CFG_OUTPUT_DIR/$CFG_PACKAGE_NAME.tar.gz" -C "$CFG_WORK_DIR" "$CFG_PACKAGE_NAME"
332 need_ok "failed to tar"