summaryrefslogtreecommitdiff
path: root/sources/bin/.scala_wrapper.tmpl
blob: b2b396556473dd9951679d75078078232fbd7c07 (plain) (blame)
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
#!/bin/sh
##############################################################################
#                      __                                                    #
#      ________ ___   / /  ___     Scala Tools Launch Script                 #
#     / __/ __// _ | / /  / _ |    (c) 2002-2003, LAMP/EPFL                  #
#   __\ \/ /__/ __ |/ /__/ __ |                                              #
#  /____/\___/_/ |_/____/_/ | |                                              #
#                           |/                                               #
##############################################################################

# $Id$

##############################################################################
# Configuration

# The configure function contains all the configurable variables of
# this script. Each of these variables may be overridden by defining
# an environment variable with the same but where the "default_" has
# been replaced by "SCALA_". For example, to start scalaint with a big
# initial heap size, one may run the following command:
#
#   env SCALA_SCALA_ARGS=-Xms1024m scalaint".
#
# These variables must use the Unix form for paths and path lists
# (i.e. use '/' as file separator and ':' as path separator). There
# are two exceptions: JAVA_ARGS and SCALA_ARGS which must use the
# paltform-specific form.
#
# The following environment variables are also read by this script:
#
# - SCALA_CLASSPATH: if defined, it is used as the default classpath
#   by all Scala tools.
#
# - CLASSPATH: if defined and SCALA_CLASSPATH is undefined, it is used
#   as the default classpath by all Scala tools.
#
# - SCALA_SOURCEPATH: if defined, it is used as the default sourcepath
#   by all Scala tools.
#
# - SCALA_BOOTCLASSPATH: if defined, it is used as the default
#   bootclasspath by all Scala tools.
#
# - SCALA_EXTDIRS: if defined, it is used as the default extdirs by
#   all Scala tools.
#
# These variables must use the platform-specific form for paths and
# path lists.

configure() {

    # Location of the Scala library sources.
    default_LIBRARY_SOURCES={#LIBRARY_SOURCES#};

    # Location of the Scala library.
    default_LIBRARY_CLASSES={#LIBRARY_CLASSES#};

    # Location of the Scala tools library.
    default_TOOLS_CLASSES={#TOOLS_CLASSES#};

    # Location of the FJBG library.
    default_FJBG_CLASSES={#FJBG_CLASSES#};

    # Location of the MSIL library.
    default_MSIL_CLASSES={#MSIL_CLASSES#};

    # Command to start the Java VM.
    default_JAVA_CMD={#JAVA_CMD#};

    # Additional arguments to pass to the Java VM.
    default_JAVA_ARGS={#JAVA_ARGS#};

    # Command to start the Scala VM.
    default_SCALA_CMD={#SCALA_CMD#};

    # Additional arguments to pass to the Scala VM.
    default_SCALA_ARGS={#SCALA_ARGS#};

    # Command to start subprocesses. This is mainly for debugging
    # purposes. For example, one may display the Java command that is
    # started by "scala", by running "env SCALA_EXEC=echo scala".
    default_EXEC="exec";

}

##############################################################################
# Error functions

# Prints a warning message on stderr.
warning() {
    echo "$0: warning:" "$@" 1>&2;
}

# Prints an error message on stderr.
error() {
    echo "$0:" "$@" 1>&2;
}

# Prints an error message on stderr and exits with a non-zero status.
abort() {
    error "$@";
    exit 1;
}

##############################################################################
# File name and path list conversion functions

# Prints the OS-specific form of the specified Unix form file name.
get_os_filename() {
    [ $# = 1 ] || abort "internal error";
    case "$UNAME" in
        CYGWIN* ) cygpath --windows "$1";;
        *       ) echo "$@";;
    esac;
}

# Prints the Unix form of the specified OS-specific form file name.
get_unix_filename() {
    [ $# = 1 ] || abort "internal error";
    case "$UNAME" in
        CYGWIN* ) cygpath --unix "$1";;
        *       ) echo "$@";;
    esac;
}

# Prints the OS-specific form of the specified Unix form path list.
get_os_pathlist() {
    [ $# = 1 ] || abort "internal error";
    case "$UNAME" in
        CYGWIN* ) cygpath --window --path "$1";;
        *       ) echo "$@";;
    esac;
}

# Prints the Unix form of the specified OS-specific form path list.
get_unix_pathlist() {
    [ $# = 1 ] || abort "internal error";
    case "$UNAME" in
        CYGWIN* ) cygpath --unix --path "$1";;
        *       ) echo "$@";;
    esac;
}

##############################################################################
# Path list construction functions

# Prints the concatenation of two path lists.
append_pathlists() {
    [ $# = 2 ] || abort "internal error";

    if [ -z "$1" ]; then
        echo "$2";
    elif [ -z "$2" ]; then
        echo "$1";
    else
        echo "$1:$2";
    fi;
}

##############################################################################
# Value computation functions

# Computes and stores the value "$1" in the variable "current_$1". If
# the variable "SCALA_$1" is defined, its content is used. Otherwise,
# the content of the variable "default_$1" is used.
compute_value() {
    [ $# = 1 ] || abort "internal error";

    eval current_$1=\"\$default_$1\";
    case "$1" in

        LIBRARY_SOURCES | \
        LIBRARY_CLASSES | \
        TOOLS_CLASSES   | \
        FJBG_CLASSES    | \
        MSIL_CLASSES    )
            if eval [ -n \"\$SCALA_$1\" ]; then
                if eval [ -f \"\$SCALA_$1\" -o -d \"\$SCALA_$1\" ]; then
                    eval current_$1=\"\$SCALA_$1\";
                else
                    eval warning \""File referred by SCALA_$1 (\$SCALA_$1)" \
                        "does not exist, using default value instead."\";
                    unset SCALA_$1;
                fi;
            fi;
            if eval [ -f \"\$current_$1\" -o -d \"\$current_$1\" ]; then
                :;
            else
                eval abort \""File referred by default_$1 (\$default_$1)" \
                    "does not exist. Please, fix definition of default_$1 in" \
                    "file $SOURCE or define environment variable SCALA_$1."\";
            fi;
            ;;

        JAVA_CMD   | \
        JAVA_ARGS  | \
        SCALA_CMD  | \
        SCALA_ARGS | \
        EXEC       )
            if eval [ -n \"\$SCALA_$1\" ]; then
                eval current_$1=\"\$SCALA_$1\";
            fi;
            ;;

        * )
            abort "internal error: $1";
            ;;

    esac;
}

##############################################################################
# Default paths

# Prints the default scala classpath or nothing if it's unspecified.
get_scala_classpath() {
    if [ -n "$SCALA_CLASSPATH" ]; then
        get_unix_pathlist "$SCALA_CLASSPATH";
    elif [ -n "$CLASSPATH" ]; then
        get_unix_pathlist "$CLASSPATH";
    fi;
}

# Prints the default scala sourcepath or nothing if it's unspecified.
get_scala_sourcepath() {
    if [ -n "$SCALA_SOURCEPATH" ]; then
        get_unix_pathlist "$SCALA_SOURCEPATH";
    fi;
}

# Prints the default scala bootclasspath or nothing if it's unspecified.
get_scala_bootclasspath() {
    if [ -n "$SCALA_BOOTCLASSPATH" ]; then
        get_unix_pathlist "$SCALA_BOOTCLASSPATH";
    fi;
}

# Prints the default scala extdirs or nothing if it's unspecified.
get_scala_extdirs() {
    if [ -n "$SCALA_EXTDIRS" ]; then
        get_unix_pathlist "$SCALA_EXTDIRS";
    fi;
}

##############################################################################
# VM invocation functions

# Prints the VM script-suffix-specific arguments.
vm_get_script_suffix_args() {
    case "$SCRIPT" in
        *-debug* ) echo "-Djava.compiler=NONE -ea";;
    esac;
}

# Starts a Java VM with the specified arguments.
vm_start_java() {
    # compute values
    compute_value EXEC;
    compute_value JAVA_CMD;
    compute_value JAVA_ARGS;

    # start Java VM
    $current_EXEC $current_JAVA_CMD $current_JAVA_ARGS \
        `vm_get_script_suffix_args` "$@";
}

# Starts a Scala VM with the specified arguments.
vm_start_scala() {
    # compute values
    compute_value EXEC;
    compute_value SCALA_CMD;
    compute_value SCALA_ARGS;

    # start Java VM
    $current_EXEC $current_SCALA_CMD $current_SCALA_ARGS \
        `vm_get_script_suffix_args` "$@";
}

##############################################################################
# Scala invocation functions

# Returns true if the given arguments contain a flag -Xbootclasspath.
scala_has_bootclasspath() {
    while [ $# != 0 ]; do
        case "$1" in
            -Xbootclasspath:* ) return 0;;
            -cp | -classpath  ) shift 2;;
            -jar              ) return 1;;
            -*                ) shift 1;;
            *                 ) return 1;;
        esac;
    done;
    return 1;
}

# Implements the command "scala".
scala() {
    if scala_has_bootclasspath "$@"; then
        vm_start_java "$@";
    else
        compute_value LIBRARY_CLASSES;
        classes="$current_LIBRARY_CLASSES";
        vm_start_java "-Xbootclasspath/a:`get_os_filename "$classes"`" "$@";
    fi;
}

##############################################################################
# Tool invocation functions

# Starts a Scala tool. The first argument is added to the runtime
# classpath. The following ones are passed to vm_start_scala. They
# must, at least, contain the name of the main class.
tool_start() {
    [ $# -gt 1 ] || abort "internal error";
    classpath="$1"; shift 1;

    # compute value
    compute_value LIBRARY_SOURCES;
    compute_value LIBRARY_CLASSES;
    compute_value TOOLS_CLASSES;

    # append tools library to classpath
    classpath=`append_pathlists "$classpath" "$current_TOOLS_CLASSES"`;

    # start Scala VM
    tool_start0 -classpath "`get_os_pathlist "$classpath"`" "$@";
}

tool_start0() {
    if [ -n "`get_scala_extdirs`" ]; then
        tool_start1 -Dscala.ext.dirs="`get_scala_extdirs`" "$@";
    else
        tool_start1 "$@";
    fi;
}

tool_start1() {
    if [ -n "`get_scala_bootclasspath`" ]; then
        tool_start2 -Dscala.boot.class.path="`get_scala_bootclasspath`" "$@";
    else
        tool_start2 "$@";
    fi;
}

tool_start2() {
    if [ -n "`get_scala_sourcepath`" ]; then
        tool_start3 -Dscala.source.path="`get_scala_sourcepath`" "$@";
    else
        tool_start3 "$@";
    fi;
}

tool_start3() {
    if [ -n "`get_scala_classpath`" ]; then
        tool_start4 -Dscala.class.path="`get_scala_classpath`" "$@";
    else
        tool_start4 "$@";
    fi;
}

tool_start4() {
    sources="$current_LIBRARY_SOURCES";
    classes="$current_LIBRARY_CLASSES";
    vm_start_scala \
        -Dscala.product="$SCRIPT" \
        -Dscala.version="$VERSION" \
        -Dscala.home="$PREFIX" \
        -Dscala.library.source.path="`get_os_filename "$sources"`" \
        -Dscala.library.class.path="`get_os_filename "$classes"`" \
        "$@";
}

##############################################################################
# Compiler invocation functions

# Starts a Scala compiler. The first argument is added to the runtime
# classpath. The following ones are passed to vm_start_scala. They
# must, at least, contain the name of the main class.
compiler_start() {
    [ $# -gt 1 ] || abort "internal error";
    classpath="$1"; shift 1;

    # compute values
    compute_value FJBG_CLASSES;
    compute_value MSIL_CLASSES;

    # append FJBG and MSIL libraries to classpath
    classpath=`append_pathlists "$classpath" "$current_FJBG_CLASSES"`;
    classpath=`append_pathlists "$classpath" "$current_MSIL_CLASSES"`;

    # start tool
    tool_start "$classpath" "$@";
}

##############################################################################
# Implementation of scala-info

# Prints given error message, prints usage and exits with error code 1.
info_abort() {
    error "$@";
    info_print_usage 1>&2;
    exit 1;
}

# Prints value of variable $1.
info_print_variable() {
    [ $# = 1 ] || abort "internal error";
    eval echo \"\$$1\";
}

# Prints default value of value $1.
info_print_default_value() {
    [ $# = 1 ] || abort "internal error";
    info_print_variable "default_$1";
}

# Prints current value of value $1.
info_print_current_value() {
    [ $# = 1 ] || abort "internal error";
    compute_value "$1";
    info_print_variable "current_$1";
}

# Implements "scala-info --home".
info_option_home() {
    [ $# -gt 0 ] && abort "too many arguments";
    echo "$PREFIX";
}

# Implements "scala-info --version".
info_option_version() {
    [ $# -gt 0 ] && abort "too many arguments";
    echo "$VERSION";
}

# Implements "scala-info --$1 <value>"
info_option_x_value() {
    [ $# -lt 2 ] && abort "missing value name";
    [ $# -gt 2 ] && abort "too many arguments";
    case "$2" in
        LIBRARY_SOURCES     ) value=$2;;
        LIBRARY_CLASSES     ) value=$2;;
        TOOLS_CLASSES       ) value=$2;;
        FJBG_CLASSES        ) value=$2;;
        MSIL_CLASSES        ) value=$2;;
        JAVA_CMD            ) value=$2;;
        JAVA_ARGS           ) value=$2;;
        SCALA_CMD           ) value=$2;;
        SCALA_ARGS          ) value=$2;;
        EXEC                ) value=$2;;
        *                   ) abort "Unknown value \`$2'";;
    esac;
    info_print_$1_value $value;
}

# Implements "scala-info --help".
info_option_help() {
    echo "usage: $0 <option>";
    echo "where possible options include:";
    echo "  --home            Print Scala home directory";
    echo "  --version         Print Scala version";
    echo "  --default <name>  Print default value of value <name>";
    echo "  --current <name>  Print current value of value <name>";
    echo "  -? --help         Print this help message";
    echo "";
    echo "valid value names include:";
    echo "  LIBRARY_SOURCES   Location of the Scala library sources";
    echo "  LIBRARY_CLASSES   Location of the Scala library";
    echo "  TOOLS_CLASSES     Location of the Scala tools";
    echo "  FJBG_CLASSES      Location of the fjbg library";
    echo "  MSIL_CLASSES      Location of the msil library";
    echo "  JAVA_CMD          Command to start the Java VM";
    echo "  JAVA_ARGS         Additional arguments to pass to the Java VM";
    echo "  SCALA_CMD         Command to start the Scala VM";
    echo "  SCALA_ARGS        Additional arguments to pass to the Scala VM";
    echo "  EXEC              Command to start subprocesses";
}

# Entry point of scala-info.
info() {
    [ $# = 0 ] && abort "missing option";

    case "$1" in
        --home      ) shift 1; info_option_home "$@";;
        --version   ) shift 1; info_option_version "$@";;
        --default   ) shift 1; info_option_x_value default "$@";;
        --current   ) shift 1; info_option_x_value current "$@";;
        -? | --help ) shift 1; info_option_help "$@";;
        -*          ) abort "unrecognised option \`$1'";;
        *           ) abort "illegal argument \`$1'";;
    esac;
}

##############################################################################
# Definition of UNAME, SOURCE, SCRIPT, PREFIX and VERSION

unset SCRIPT;
UNAME=`uname`;
SOURCE=$0;
SCRIPT=`basename "$SOURCE"`;
while [ -h "$SOURCE" ]; do
    SCRIPT=`basename "$SOURCE"`;
    LOOKUP=`ls -ld "$SOURCE"`;
    TARGET=`expr "$LOOKUP" : '.*-> \(.*\)$'`;
    if expr "${TARGET:-.}/" : '/.*/$' > /dev/null; then
        SOURCE=${TARGET:-.};
    else
        SOURCE=`dirname "$SOURCE"`/${TARGET:-.};
    fi;
done;
PREFIX=`dirname "$SOURCE"`/..;
PREFIX=`cd "$PREFIX"; pwd`;

VERSION={#VERSION#};
VERSION=${VERSION:-"unknown version"};

##############################################################################
# Test of invocation method

if [ -z "$SCRIPT" ]; then
    abort "Illegal direct invocation; invoke me through a symbolic link.";
fi;

##############################################################################
# Configuration and invocation of $SCRIPT

configure;

case "$SCRIPT" in
    scala-info ) info "$@";;
    scala      ) scala "$@";;
    scala-*    ) scala "$@";;
    scalac*    ) compiler_start "" scala.tools.scalac.Main "$@";;
    scalarun*  ) compiler_start "" scala.tools.scalai.Main "$@";;
    scalaint*  ) compiler_start "" scala.tools.scalai.Main -interactive "$@";;
    scaladoc*  ) tool_start "" scala.tools.scaladoc.Main "$@";;
    scalap*    ) tool_start "" scala.tools.scalap.Main "$@";;
    dtd2scala* ) tool_start "" scala.tools.dtd2scala.Main "$@";;
    scalatest* ) tool_start "" scala.tools.scalatest.Main "$@";;
    *          ) abort "Don't know what to do for $SCRIPT.";;
esac;

##############################################################################