-
Notifications
You must be signed in to change notification settings - Fork 13
/
build.sh
executable file
·503 lines (455 loc) · 12.3 KB
/
build.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
#!/bin/sh
#
# COPYRIGHT (c) 2022 The Fellowship of SML/NJ (http://www.smlnj.org)
# All rights reserved.
#
# Build and installation script for SML/NJ System.
#
# usage: build.sh [ options ]
#
# TODO:
# add support for fetching the boot files
# dump output from build process to a log (instead of to the terminal)
cmd=$0
here=$(pwd)
complain() {
echo "$cmd: !!! $@"
exit 1
}
usage() {
echo "usage: build.sh [ options ]"
echo "options:"
echo " -h,-help print this message and exit"
echo " -nolib skip building libraries/tools"
echo " -verbose emit feedback messages"
echo " -doc generate documentation"
echo " -debug debug installation (enables verbose mode)"
echo " -dev developer install (includes cross compiler support)"
echo " -debug-llvm build a debug version of the LLVM libraries"
exit 1
}
# process options
NOLIB=no
QUIET=yes
INSTALL_DEBUG=no
INSTALL_DEV=no
MAKE_DOC=no
BUILD_LLVM_FLAGS=""
while [ "$#" != "0" ] ; do
arg=$1; shift
case $arg in
-help|-h) usage ;;
-nolib) NOLIB=yes ;;
-verbose) QUIET=no ;;
-debug) INSTALL_DEBUG=yes ; QUIET=no ;;
-dev)
INSTALL_DEV=yes;
BUILD_LLVM_FLAGS="--all-targets $BUILD_LLVM_FLAGS"
;;
-doc) MAKE_DOC=yes ;;
-debug-llvm) BUILD_LLVM_FLAGS="--debug $BUILD_LLVM_FLAGS" ;;
*) usage ;;
esac
done
# feedback messages for verbose mode
#
vsay() {
if [ x${QUIET} = xno ] ; then
echo "$@"
fi
}
# feedback messages for debug mode
#
dsay() {
if [ x${INSTALL_DEBUG} = xyes ] ; then
echo "$@"
fi
}
export CM_VERBOSE
if [ x${QUIET} = xyes ] ; then
CM_VERBOSE=false
else
CM_VERBOSE=true
fi
#
# create the preloads.standard file
#
if [ ! -r config/preloads ]; then
complain "File config/preloads is missing."
fi
cp config/preloads preloads.standard
SHELL=/bin/sh
dsay "$cmd: Using shell $SHELL."
#
# set the SML root directory
#
cd $(dirname $cmd)
SMLNJ_ROOT=$(pwd)
vsay "$cmd: SML root is $SMLNJ_ROOT."
cd $here
cd "${INSTALLDIR:=$SMLNJ_ROOT}"
INSTALLDIR=`pwd`
cd "$SMLNJ_ROOT"
vsay "$cmd: Installation directory is ${INSTALLDIR}."
#
# set the various directory and file pathname variables
#
CONFIGDIR=$SMLNJ_ROOT/config
RUNTIMEDIR=$SMLNJ_ROOT/runtime
LLVMDIR=$SMLNJ_ROOT/llvm
#
# installation directories
#
BINDIR=$INSTALLDIR/bin # main dir for binary stuff
HEAPDIR=$BINDIR/.heap # where heap images live
RUNDIR=$BINDIR/.run # where executables (i.e., the RTS) live
LIBDIR=$INSTALLDIR/lib # where libraries live
export SMLNJ_ROOT INSTALLDIR CONFIGDIR BINDIR
#
# old root environment variable (for compatibility)
#
ROOT=$SMLNJ_ROOT
export ROOT
#
# files to be deleted after we are done...
#
tmpfiles=""
tmpfiles="$tmpfiles preloads.standard"
#
# make sure we always clean up after ourselves...
#
trap 'cd "$SMLNJ_ROOT"; rm -f $tmpfiles' 0 1 2 3 15
#
# set the CM configuration variables (these are environment variables
# that will be queried by the bootstrap code)
# Especially important is CM_PATHCONFIG.
#
export CM_PATHCONFIG
CM_PATHCONFIG=$LIBDIR/pathconfig
#
# the release version that we are installing
#
VERSION=$(cat "$CONFIGDIR/version")
vsay "$cmd: Installing version $VERSION."
#
# the URL for the (usually remote) source archive
#
SRCARCHIVEURL=https://smlnj.cs.uchicago.edu/dist/working/${VERSION}/
vsay "$cmd: URL of source archive is $SRCARCHIVEURL."
######################################################################
## UTILITY SCRIPTS
######################################################################
#
# Function to install a "driver" script...
# This takes care of patching the source of the script with the SHELL,
# BINDIR, and VERSION variables to use.
#
installdriver() {
vsay "$cmd: installing $BINDIR/$2"
dsrc=$1
ddst=$2
rm -f "$BINDIR"/"$ddst"
cat "$CONFIGDIR"/"$dsrc" | \
sed -e "s,@SHELL@,$SHELL,g" \
-e "s,@BINDIR@,$BINDIR," \
-e "s,@LIBDIR@,$LIBDIR," \
-e "s,@VERSION@,$VERSION," \
-e "s,@CMDIRARC@,${CM_DIR_ARC:-dummy},"\
> "$BINDIR"/"$ddst"
chmod 555 "$BINDIR"/"$ddst"
if [ ! -x "$BINDIR"/"$ddst" ]; then
complain "Installation of $BINDIR/${ddst} failed."
fi
}
#
# Fish out the CM metadata directory name from library files
# and store it in ORIG_CM_DIR_ARC.
# The single argument is the name of the directory containing
# a single subdirectory which is a CM metadata directory:
#
fish() {
cd "$1"
ORIG_CM_DIR_ARC=unknown
for i in * .[a-zA-Z0-9]* ; do
if [ -d $i ] ; then
ORIG_CM_DIR_ARC=$i
break
fi
done
if [ $ORIG_CM_DIR_ARC = unknown ] ; then
complain "Could not determine CM metadata directory name"
else
vsay "$cmd: CM metadata directory name is \"${ORIG_CM_DIR_ARC}\""
fi
}
# A function to move all stable library files to a parallel directory
# hierarchy.
# The first argument must be a simple path (no / inside), and
# the second argument must be an absolute path.
move() {
if [ -L "$1" ] ; then
rm -f "$1" # remove symbolic link made by diracs (see below)
elif [ -d "$1" ] ; then
if [ ! -d "$2" ] ; then
if [ -f "$2" ] ; then
complain "$2 exists as a non-directory."
fi
mkdir "$2"
fi
cd "$1"
for i in * .[a-zA-Z0-9]* ; do
move "$i" "$2"/"$i"
done
cd ..
elif [ -f "$1" ] ; then
rm -f "$2"
mv "$1" "$2"
fi
}
#
# Traverse the directory tree rooted at $3 (must be single arc!).
# Find all directories named $1, rename them into $2 and make
# and establish $1 as a symbolic link to $2:
#
dirarcs() {
if [ -d "$3" ] ; then
if [ "$3" = "$1" ] ; then
mv "$1" "$2"
ln -s "$2" "$1"
else
cd "$3"
for d in * .[a-zA-Z0-9]* ; do
dirarcs "$1" "$2" "$d"
done
cd ..
fi
fi
}
######################################################################
#
# create the various sub directories
#
for dir in "$BINDIR" "$HEAPDIR" "$RUNDIR" "$LIBDIR" ; do
if [ x"$QUIET" = xyes ] ; then
mkdir -p "$dir" || exit 1
else
mkdir -p -v "$dir" || exit 1
fi
done
#
# install the script that tests architecture and os...
#
installdriver _arch-n-opsys .arch-n-opsys
#
# run it to figure out what architecture and os we are using, define
# corresponding variables...
#
ARCH_N_OPSYS=`"$BINDIR"/.arch-n-opsys`
if [ "$?" != "0" ]; then
complain "$BINDIR/.arch-n-opsys fails on this machine; please patch by hand and repeat the installation."
exit 2
else
vsay "$cmd: Script $BINDIR/.arch-n-opsys reports $ARCH_N_OPSYS."
fi
eval $ARCH_N_OPSYS
#
# now install most of the other driver scripts
# (except ml-build, since we don't know $CM_DIR_ARC yet)
#
installdriver _run-sml .run-sml
installdriver _link-sml .link-sml
installdriver _ml-makedepend ml-makedepend
## TODO: install-sml-wrapper script
#
# we optimistically install heap2exec, but will remove it if heap2asm
# is not installed
#
installdriver _heap2exec heap2exec
#
# set allocation size; for the x86, this gets reset in .run-sml
#
ALLOC=1M
# OS-specific things for building the runtime system
#
RT_MAKEFILE=mk.$ARCH-$OPSYS
case $OPSYS in
darwin)
EXTRA_DEFS="AS_ACCEPTS_SDK=yes"
;;
linux)
EXTRA_DEFS=$("$CONFIGDIR/chk-global-names.sh")
if [ "$?" != "0" ]; then
complain "Problems checking for underscores in asm names."
fi
EXTRA_DEFS="XDEFS=$EXTRA_DEFS"
;;
esac
#
# the name of the bin files directory
#
# FIXME: should make these file names more consistent!!
#
BOOT_ARCHIVE=boot.$ARCH-unix.tgz
BOOT_FILES=sml.boot.$ARCH-unix
#
# build the run-time system
#
if [ -x "$RUNDIR"/run.$ARCH-$OPSYS ]; then
vsay $cmd: Run-time system already exists.
else
#
# first we configure and build the LLVM library. Note that if the "-dev"
# option was given, then we rebuild LLVM even if it is already built, since
# we want to assure that the cross compiler is supported.
#
if [ x"$INSTALL_DEV" = xyes ] ; then
vsay $cmd: Building LLVM for all targets
cd "$LLVMDIR"
dsay ./build-llvm.sh $BUILD_LLVM_FLAGS
./build-llvm.sh $BUILD_LLVM_FLAGS || complain "Unable to build LLVM"
elif [ ! -x "$RUNTIMEDIR/bin/llvm-config" ] ; then
vsay $cmd: Building LLVM
cd "$LLVMDIR"
dsay ./build-llvm.sh $BUILD_LLVM_FLAGS
./build-llvm.sh $BUILD_LLVM_FLAGS || complain "Unable to build LLVM"
fi
cd "$RUNTIMEDIR/objs"
vsay $cmd: Compiling the run-time system.
make -f $RT_MAKEFILE $EXTRA_DEFS
if [ -x run.$ARCH-$OPSYS ]; then
mv run.$ARCH-$OPSYS "$RUNDIR"
if [ -f runx.$ARCH-$OPSYS ]; then
mv runx.$ARCH-$OPSYS "$RUNDIR"
fi
if [ -f run.$ARCH-$OPSYS.so ]; then
mv run.$ARCH-$OPSYS.so "$RUNDIR"
fi
if [ -f run.$ARCH-$OPSYS.a ]; then
mv run.$ARCH-$OPSYS.a "$RUNDIR"
fi
make MAKE=make clean
else
complain "Run-time system build failed for some reason."
fi
fi
cd "$SMLNJ_ROOT"
#
# boot the base SML system
#
if [ -r "$HEAPDIR"/sml.$HEAP_SUFFIX ]; then
vsay "$cmd: Heap image $HEAPDIR/sml.$HEAP_SUFFIX already exists."
fish "$LIBDIR"/smlnj/basis
# ignore requested arc name since we have to live with what is there:
export CM_DIR_ARC
CM_DIR_ARC=$ORIG_CM_DIR_ARC
# now re-dump the heap image:
vsay "$cmd: Re-creating a (customized) heap image..."
"$BINDIR"/sml @CMredump "$SMLNJ_ROOT"/sml
cd "$SMLNJ_ROOT"
if [ -r sml.$HEAP_SUFFIX ]; then
mv sml.$HEAP_SUFFIX "$HEAPDIR"
else
complain "Unable to re-create heap image (sml.$HEAP_SUFFIX)."
fi
else
cd "$SMLNJ_ROOT"
if [ ! -d "$BOOT_FILES"/smlnj/basis ] ; then
if [ -f "$BOOT_ARCHIVE" ] ; then
vsay "$cmd: unpacking the boot files"
tar -xzf "$BOOT_ARCHIVE"
else
complain "Missing $BOOT_ARCHIVE"
fi
else
vsay "$cmd: boot files are already unpacked"
fi
fish "$SMLNJ_ROOT"/"$BOOT_FILES"/smlnj/basis
# Target arc:
export CM_DIR_ARC
CM_DIR_ARC=${CM_DIR_ARC:-".cm"}
if [ $CM_DIR_ARC != $ORIG_CM_DIR_ARC ] ; then
# now we have to make a symbolic link for each occurrence of
# $ORIG_CM_DIR_ARC to $CM_DIR_ARC
dirarcs "$ORIG_CM_DIR_ARC" "$CM_DIR_ARC" "$BOOT_FILES"
fi
cd "$SMLNJ_ROOT"/"$BOOT_FILES"
# now link (boot) the system and let it initialize itself...
dsay "$BINDIR"/.link-sml @SMLheap="$SMLNJ_ROOT"/sml @SMLboot=BOOTLIST @SMLalloc=$ALLOC
if "$BINDIR"/.link-sml @SMLheap="$SMLNJ_ROOT"/sml @SMLboot=BOOTLIST @SMLalloc=$ALLOC ; then
cd "$SMLNJ_ROOT"
if [ -r sml.$HEAP_SUFFIX ]; then
mv sml.$HEAP_SUFFIX "$HEAPDIR"
cd "$BINDIR"
ln -s .run-sml sml
#
# Now move all stable libraries to $LIBDIR and generate
# the pathconfig file.
#
cd "$SMLNJ_ROOT"/"$BOOT_FILES"
for anchor in * ; do
if [ -d $anchor ] ; then
dsay "move $anchor to $LIBDIR"
echo $anchor $anchor >> $CM_PATHCONFIG
move $anchor "$LIBDIR"/$anchor
fi
done
cd "$SMLNJ_ROOT"
# $BOOT_FILES is now only an empty skeleton, let's get rid of it.
rm -rf "$BOOT_FILES"
else
complain "No heap image generated (sml.$HEAP_SUFFIX)."
fi
else
complain "Boot code failed, no heap image (sml.$HEAP_SUFFIX)."
fi
fi
#
# now that we know CM_DIR_ARC we can install the ml-build driver...
#
installdriver _ml-build ml-build
#
# Now do all the rest using the precompiled installer
# (see system/smlnj/installer for details)
#
if [ x"$NOLIB" = xno ] ; then
vsay "$cmd: Installing other libraries and programs:"
CM_TOLERATE_TOOL_FAILURES=true
export CM_TOLERATE_TOOL_FAILURES
if "$BINDIR"/sml -m \$smlnj/installer.cm ; then
# because we create heap2exec without knowing if heap2asm is going
# to be installed, we need this hack to remove heap2exec when heap2asm
# is not available
if [ ! -x "$BINDIR"/heap2asm ] ; then
rm -f "$BINDIR"/heap2exec
fi
vsay $cmd: Installation complete.
else
complain "Installation of libraries and programs failed."
fi
fi
#
# generate the documentation and manual pages (if requested)
#
if [ x"$MAKE_DOC" = xyes ] ; then
vsay "$cmd: Generating documentation."
#
# first we clear CM related shell variables so that the documentation tool
# builds are not confused.
#
unset CM_PATHCONFIG CM_DIR_ARC CM_TOLERATE_TOOL_FAILURES
export SMLNJ_HOME
SMLNJ_HOME=$here # gives access to the version of SML/NJ that we are building
cd doc
if autoconf -Iconfig ; then
:
else
complain "Error configuring documentation."
fi
./configure
if make doc && make distclean ; then
vsay $cmd: Documentation generation complete.
else
complain "Error generating documentation."
fi
fi
exit 0