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