/
ocamlbrew
executable file
·554 lines (498 loc) · 16.1 KB
/
ocamlbrew
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
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
#!/usr/bin/env bash
# Fail if we try to use an undefined value
set -u
# Quit on errors
set -o pipefail
set -o errtrace
#
# Display usage message then quit
#
function help_then_exit() {
echo Usage: "$0" [flags]
echo
echo "Possible flags:"
echo "-h Display this message"
echo "-b [path] Use [path] as \$OCAMLBREW_BASE"
echo "-v [version] Install the given version of OCaml (i.e. 4.03.0)"
echo "-c \"[flags]\" Flags to pass to OCaml's configure"
echo "-p [patch] Patch to apply to OCaml"
echo "-a Install everything with no prompts"
echo "-o Install OCaml, no prompts"
echo "-f Install OCaml and findlib, no prompts"
echo "-r Install OCaml and opam, no prompts"
echo "-x Install OCaml, findlib, opam, and oasis (via opam), no prompts"
echo "-s [path] Install OCaml from Subversion [path]"
echo "-g [url] Install OCaml from Git [url]"
echo "-G [revision] Checkout Git [revision] before build"
echo "-t Install OCaml from Subversion trunk"
echo "-n [name] Install under \$OCAMLBREW_BASE/[name]"
echo
echo "The build and installation will occur under \$OCAMLBREW_BASE"
echo
echo "Subversion path should be relative to $OCAML_SVN_ROOT"
echo
echo "OCaml patches can be absolute paths on the file system, or relative"
echo "to the OCaml source root"
exit 1
}
function undo_redirect() {
exec 2>&4 1>&3
}
function error_message() {
echo Something errored out, please check the log file for details:
echo "$OCAMLBREW_LOGFILE"
echo Exiting...
}
#
# Print a message when quitting due to an error
#
function handle_error() {
undo_redirect
error_message
exit 1
}
#
# Initialize output redirection
#
function init_redirect() {
exec 3>&1 4>&2
exec 1>"$OCAMLBREW_LOGFILE" 2>&1
}
#
# Output which should be displayed on screen and logged
#
function say() {
echo "$*" >&3
echo "$*"
}
#
# Read input without logging the input prompt
#
function prompt() {
echo -n "$*" >&3
read
}
#
# Initialize lots and lots of global build and installation variables
#
function init_variables() {
# Installation prefix for the software being installed
: ${OCAMLBREW_BASE="$HOME"/ocamlbrew}
# By default, there is no custom OCaml installation directory name
: ${OCAMLBREW_NAME=""}
# Specific installation set options
: ${INSTALL_ALL="false"}
: ${INSTALL_OCAML_ONLY="false"}
: ${INSTALL_OCAML_FINDLIB_ONLY="false"}
: ${INSTALL_OASIS_ONLY="false"}
: ${INSTALL_OPAM_ONLY="false"}
: ${OCAMLBREW_INSTALL_SVN="false"}
: ${OCAMLBREW_INSTALL_GIT="false"}
: ${OCAMLBREW_GIT_URL=""}
: ${OCAMLBREW_GIT_REVISION=""}
# Specify the 'make' executable to use
: ${OCAMLBREW_MAKE="make"}
# Set configuration options such as installation and build locations here
# OCaml from Subversion
: ${OCAMLBREW_SVN_PATH=trunk}
# The version of OCaml we are installing and download URL
: ${OCAML_MAJOR_VERSION=4}
: ${OCAML_MINOR_VERSION=03}
: ${OCAML_PATCH_VERSION=0}
: ${OCAML_VERSION=$OCAML_MAJOR_VERSION.$OCAML_MINOR_VERSION.$OCAML_PATCH_VERSION}
: ${OCAML_URL=http://caml.inria.fr/pub/distrib/ocaml-$OCAML_MAJOR_VERSION.$OCAML_MINOR_VERSION/ocaml-$OCAML_VERSION.tar.gz}
# findlib version we are installing and download URL
: ${FINDLIB_MAJOR_VERSION=1}
: ${FINDLIB_MINOR_VERSION=6}
: ${FINDLIB_PATCH_VERSION=2}
: ${FINDLIB_VERSION=$FINDLIB_MAJOR_VERSION.$FINDLIB_MINOR_VERSION.$FINDLIB_PATCH_VERSION}
: ${FINDLIB_URL=http://download.camlcity.org/download/findlib-$FINDLIB_VERSION.tar.gz}
# opam version we are installing and download URL
: ${OPAM_MAJOR_VERSION=1}
: ${OPAM_MINOR_VERSION=2}
: ${OPAM_PATCH_VERSION=2}
: ${OPAM_VERSION=$OPAM_MAJOR_VERSION.$OPAM_MINOR_VERSION.$OPAM_PATCH_VERSION}
: ${OPAM_URL=https://github.com/ocaml/opam/releases/download/$OPAM_VERSION/opam-full-$OPAM_VERSION.tar.gz}
# Patch to apply to the OCaml source tree
: ${OCAML_PATCH=""}
if [ "$OCAMLBREW_NAME" != "" ]; then
# Install path based on a custom directory name
OCAML_BASE="$OCAMLBREW_BASE"/"$OCAMLBREW_NAME"
elif [ "$OCAMLBREW_INSTALL_SVN" = "true" ]; then
# Install path based on location in the Subversion tree
OCAML_BASE="$OCAMLBREW_BASE"/ocaml-svn/"$OCAMLBREW_SVN_PATH"
else
# Install path based on release version of OCaml
OCAML_BASE="$OCAMLBREW_BASE"/ocaml-"$OCAML_VERSION"
fi
# Parent directory where software will be built
: ${BUILD_DIR="$OCAML_BASE"/build}
# Configuration flags for OCaml
: ${OCAML_CONFIG_FLAGS=""}
# opam installation path
: ${OCAMLBREW_OPAMROOT="$OCAML_BASE/.opam"}
# Configuration flags for opam
: ${OPAM_FLAGS="--no-setup"}
# Output will be redirected to this file
: ${OCAMLBREW_LOGFILE=`mktemp -t ocamlbrew.XXXXXXXXXX`}
}
#
# Handle command line arguments
#
function parse_opts() {
while getopts "hb:c:p:aorfxs:g:G:tn:v:" OPTION; do
case $OPTION in
h)
help_then_exit ;;
b)
OCAMLBREW_BASE="$OPTARG"
;;
c)
OCAML_CONFIG_FLAGS="$(tr [=] [\ ] <<< "$OPTARG")"
;;
p)
OCAML_PATCH="$OPTARG"
;;
a)
INSTALL_ALL="true"
;;
o)
INSTALL_OCAML_ONLY="true"
;;
r)
INSTALL_OPAM_ONLY="true"
;;
f)
INSTALL_OCAML_FINDLIB_ONLY="true"
;;
x)
INSTALL_OASIS_ONLY="true"
;;
s)
OCAMLBREW_INSTALL_SVN="true"
OCAMLBREW_SVN_PATH="$OPTARG"
;;
g)
OCAMLBREW_INSTALL_GIT="true"
OCAMLBREW_GIT_URL="$OPTARG"
;;
G)
OCAMLBREW_GIT_REVISION="$OPTARG"
;;
t)
OCAMLBREW_INSTALL_SVN="true"
;;
n)
OCAMLBREW_NAME="$OPTARG"
;;
v)
# split version into components using read
IFS=. read -r OCAML_MAJOR_VERSION OCAML_MINOR_VERSION OCAML_PATCH_VERSION <<<"$OPTARG"
OCAML_VERSION=$OCAML_MAJOR_VERSION.$OCAML_MINOR_VERSION.$OCAML_PATCH_VERSION
OCAML_URL=http://caml.inria.fr/pub/distrib/ocaml-$OCAML_MAJOR_VERSION.$OCAML_MINOR_VERSION/ocaml-$OCAML_VERSION.tar.gz
;;
?)
help_then_exit ;;
esac
done
}
#
# Display greeting, optional prompts, and summary message
#
function greet() {
say Welcome to ocamlbrew!
say
if [ "$OCAMLBREW_INSTALL_SVN" = "true" ]; then
say Working with "$OCAML_SVN_ROOT""$OCAMLBREW_SVN_PATH"
elif [ "$OCAMLBREW_INSTALL_GIT" = "true" ]; then
say Working with "$OCAMLBREW_GIT_URL"
else
say Working with "$OCAML_URL"
fi
say Software will be built and installed under "$OCAML_BASE"
say Output will be written to "$OCAMLBREW_LOGFILE"
say You can \"tail -f "$OCAMLBREW_LOGFILE"\" in another terminal to track
say the build process.
say
SHOW_CONTINUE_PROMPT="true"
OPAM_INSTALL="false"
FINDLIB_INSTALL="false"
OASIS_INSTALL="false"
UTOP_INSTALL="false"
BATTERIES_INSTALL="false"
OCAMLSCRIPT_INSTALL="false"
if [ "$INSTALL_ALL" = "true" ]; then
SHOW_CONTINUE_PROMPT="false"
OPAM_INSTALL="true"
OASIS_INSTALL="true"
UTOP_INSTALL="true"
BATTERIES_INSTALL="true"
OCAMLSCRIPT_INSTALL="true"
elif [ "$INSTALL_OCAML_ONLY" = "true" ]; then
# Nothing to do, the defaults get us here
SHOW_CONTINUE_PROMPT="false"
OCAML_ONLY="true"
elif [ "$INSTALL_OCAML_FINDLIB_ONLY" = "true" ]; then
SHOW_CONTINUE_PROMPT="false"
FINDLIB_INSTALL="true"
elif [ "$INSTALL_OASIS_ONLY" = "true" ]; then
SHOW_CONTINUE_PROMPT="false"
OPAM_INSTALL="true"
FINDLIB_INSTALL="true"
OASIS_INSTALL="true"
elif [ "$INSTALL_OPAM_ONLY" = "true" ]; then
SHOW_CONTINUE_PROMPT="false"
OPAM_INSTALL="true"
else
# Ask the user what components they would like to install
prompt "Would you like to install findlib? (y/n) "
reply_lower="$(tr [A-Z] [a-z] <<< "$REPLY")"
if [ "$reply_lower" = "y" ]; then
FINDLIB_INSTALL="true"
prompt "Would you like to install opam? (y/n) "
reply_lower="$(tr [A-Z] [a-z] <<< "$REPLY")"
if [ "$reply_lower" = "y" ]; then
OPAM_INSTALL="true"
prompt "Would you like to install oasis? (y/n) "
reply_lower="$(tr [A-Z] [a-z] <<< "$REPLY")"
if [ "$reply_lower" = "y" ]; then
OASIS_INSTALL="true"
else
OASIS_INSTALL="false"
fi
prompt "Would you like to install utop? (y/n) "
reply_lower="$(tr [A-Z] [a-z] <<< "$REPLY")"
if [ "$reply_lower" = "y" ]; then
UTOP_INSTALL="true"
else
UTOP_INSTALL="false"
fi
prompt "Would you like to install Batteries? (y/n) "
reply_lower="$(tr [A-Z] [a-z] <<< "$REPLY")"
if [ "$reply_lower" = "y" ]; then
BATTERIES_INSTALL="true"
else
BATTERIES_INSTALL="false"
fi
prompt "Would you like to install ocamlscript? (y/n) "
reply_lower="$(tr [A-Z] [a-z] <<< "$REPLY")"
if [ "$reply_lower" = "y" ]; then
OCAMLSCRIPT_INSTALL="true"
else
OCAMLSCRIPT_INSTALL="false"
fi
else
OPAM_INSTALL="false"
fi
else
FINDLIB_INSTALL="false"
fi
fi
# Print a summary of what we are going to do, then continue
say This script is about to:
say Install OCaml
if "$FINDLIB_INSTALL" = "true"; then say and install findlib; fi
if "$OPAM_INSTALL" = "true"; then say and install opam; fi
if "$OASIS_INSTALL" = "true"; then say and install oasis; fi
if "$UTOP_INSTALL" = "true"; then say and install utop; fi
if "$BATTERIES_INSTALL" = "true"; then say and install Batteries; fi
if "$OCAMLSCRIPT_INSTALL" = "true"; then say and install ocamlscript; fi
say
if [ "$SHOW_CONTINUE_PROMPT" = "true" ]; then
prompt "Continue (y/n)? "
reply_lower="$(tr [A-Z] [a-z] <<< "$REPLY")"
if [ "$reply_lower" != "y" ]; then
say Exiting...
rm -f "$OCAMLBREW_LOGFILE"
exit 1
fi
fi
}
#
# Create the build directory
#
function setup_build_dir() {
# Create and move to the build directory
mkdir -p "$BUILD_DIR"
pushd "$BUILD_DIR"
}
#
# Handle OCaml brewing
#
function brew_ocaml() {
case $1 in
retrieve)
say Retrieving OCaml
if [ "$OCAMLBREW_INSTALL_SVN" = "true" ]; then
mkdir build
svn checkout "$OCAML_SVN_ROOT""$OCAMLBREW_SVN_PATH" build
cd build
elif [ "$OCAMLBREW_INSTALL_GIT" = "true" ]; then
git clone $OCAMLBREW_GIT_URL
cd ocaml
if [ "$OCAMLBREW_GIT_REVISION" != "" ]; then
git checkout "$OCAMLBREW_GIT_REVISION"
fi
else
curl -O "$OCAML_URL"
tar xzf ocaml-$OCAML_VERSION.tar.gz
cd ocaml-$OCAML_VERSION
fi
;;
build)
if [ -n "$OCAML_PATCH" ]; then
say Patching OCaml with "$OCAML_PATCH"
patch -p0 < "$OCAML_PATCH"
fi
say Building OCaml
./configure -prefix "$OCAML_BASE" $OCAML_CONFIG_FLAGS
"$OCAMLBREW_MAKE" world.opt
;;
install)
say Installing OCaml
"$OCAMLBREW_MAKE" install
# Install the compiler libraries as well. They are needed for utop and some
# other projects.
if [ "$OCAML_MAJOR_VERSION" = "3" ]; then
mkdir -p "$OCAML_BASE"/lib/ocaml/compiler-libs/typing
mkdir -p "$OCAML_BASE"/lib/ocaml/compiler-libs/parsing
mkdir -p "$OCAML_BASE"/lib/ocaml/compiler-libs/utils
cp typing/*.cmi "$OCAML_BASE"/lib/ocaml/compiler-libs/typing/
cp parsing/*.cmi "$OCAML_BASE"/lib/ocaml/compiler-libs/parsing/
cp utils/*.cmi "$OCAML_BASE"/lib/ocaml/compiler-libs/utils/
fi
cd ..
;;
esac
}
#
# Handle findlib brewing
#
function brew_findlib() {
case $1 in
retrieve)
say Retrieving findlib
curl -O "$FINDLIB_URL"
;;
build)
say Extracting and building findlib
tar xzf findlib-$FINDLIB_VERSION.tar.gz
cd findlib-$FINDLIB_VERSION
./configure
"$OCAMLBREW_MAKE" all opt
;;
install)
say Installing and configuring findlib
"$OCAMLBREW_MAKE" install
# Extra configuration for OCaml libraries with C stubs, as recommended by the
# findlib documentation.
mkdir `ocamlfind printconf destdir`/stublibs
echo `ocamlfind printconf destdir`/stublibs >> `ocamlfind printconf ldconf`
;;
esac
}
#
# Handle opam brewing
#
function brew_opam() {
case $1 in
retrieve)
say Retrieving the latest version of opam
curl -kLO $OPAM_URL
tar xzf opam-full-$OPAM_VERSION.tar.gz
cd opam-full-$OPAM_VERSION
;;
build)
say Building opam
./configure --prefix="$OCAML_BASE"
"$OCAMLBREW_MAKE" lib-ext
"$OCAMLBREW_MAKE"
;;
install)
say Installing opam
"$OCAMLBREW_MAKE" install
{
echo export OPAMROOT="$OCAMLBREW_OPAMROOT"
echo 'eval `opam config env`'
} >> "$OCAML_BASE"/etc/ocamlbrew.bashrc
# Initialize opam
export OPAMROOT="$OCAMLBREW_OPAMROOT"
export OPAMYES="true"
opam init "$OPAM_FLAGS"
eval `opam config env`
;;
esac
}
#
# Install packages with opam
#
function install_with_opam() {
say Installing "$1" with opam
opam install "$1"
}
#
# Print a friendly closing message
#
function closing_message() {
say
say "You may want to add the following line to your ~/.bashrc so that OCaml is
readily available to use:
source $OCAML_BASE/etc/ocamlbrew.bashrc
Enjoy!"
}
# Print out a message to let the user know they should check the output log
# when an error occurs
trap "handle_error" ERR
################################
# This is where the fun begins #
################################
# Root for Subversion checkouts
OCAML_SVN_ROOT="http://caml.inria.fr/svn/ocaml/"
# Preparation
parse_opts "$@"
init_variables
init_redirect
greet
setup_build_dir
# OCaml
brew_ocaml "retrieve"
brew_ocaml "build"
brew_ocaml "install"
# Setup the path so that OCaml is visible
PATH="$OCAML_BASE"/bin:$PATH
# Make sure we have an etc directory to put configuration files in
mkdir -p "$OCAML_BASE"/etc
# Create a sourceable environment
echo export PATH="$OCAML_BASE"/bin:\$PATH > "$OCAML_BASE"/etc/ocamlbrew.bashrc
# findlib
if [ "$FINDLIB_INSTALL" = "true" ]; then
brew_findlib "retrieve"
brew_findlib "build"
brew_findlib "install"
fi
# opam
if [ "$OPAM_INSTALL" = "true" ]; then
brew_opam "retrieve"
brew_opam "build"
brew_opam "install"
fi
# oasis
if [ "$OASIS_INSTALL" = "true" ]; then
install_with_opam "oasis"
fi
# utop
if [ "$UTOP_INSTALL" = "true" ]; then
install_with_opam "utop"
fi
# Batteries
if [ "$BATTERIES_INSTALL" = "true" ]; then
install_with_opam "batteries"
fi
# ocamlscript
if [ "$OCAMLSCRIPT_INSTALL" = "true" ]; then
install_with_opam "ocamlscript"
fi
# All done!
say Done!
popd
closing_message