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

# $Id$

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

# The configure function contains all the configurable variables of
# this script. The content of these variables may be overridden by
# defining an environment variable with the same name prefixed by
# SCALA_. For example, to run socos with a big initial heap size, one
# may run the command "env SCALA_JAVA_ARGS=-Xms1024m socos".
#
# The following environment variables are also read by this script:
# - SCALA_BOOTCLASSPATH: if defined, it is used as the default
#   bootclasspath by the Scala compiler.
#
# - SCALA_CLASSPATH: if defined, it is used as the default classpath
#   by the scala compiler.
#
# - CLASSPATH: if defined and if SCALA_CLASSPATH is undefined, it is
#   used as the default classpath by the Scala compiler.
#
# All paths and path lists must be in Unix form (i.e. use '/' as file
# separator and ':' as path separator). There is only one exception to
# this rule: CLASSPATH must be in Windows form (i.e. use '\' and ';')
# on Windows platforms.

configure() {

    # Location of the Scala library sources. These sources are
    # included in the Scala distribution.
    # This variable must refer a directory.
    RUNTIME_SOURCES={#RUNTIME_SOURCES#};

    # Location of the compiled Scala library and Scala tools. This
    # library and these tools are included in the Scala distribution.
    # This variable must refer a directory or a zip or jar file.
    RUNTIME_CLASSES={#RUNTIME_CLASSES#};

    # Location of the compiled dtd2scala tool. This tool is included
    # in the Scala distribution.
    # This variable must refer a directory or a zip or jar file.
    DTD2SCALA_CLASSES={#DTD2SCALA_CLASSES#};

    # Location of the compiled fjbg library.  This library is included
    # in the Scala distribution.
    # This variable must refer a directory or a zip or jar file.
    FJBG_CLASSES={#FJBG_CLASSES#};

    # Location of the compiled msil library. This library is included
    # in the Scala distribution.
    # This variable must refer a directory or a zip or jar file.
    MSIL_CLASSES={#MSIL_CLASSES#};

    # Location of the compiled bcel library. This library may be found
    # at http://jakarta.apache.org/bcel/.
    # This variable must refer a directory or a zip or jar file.
    BCEL_CLASSES={#BCEL_CLASSES#};

    # Location of the compiled xerces library. This library may be
    # found at http://xml.apache.org/xerces2-j/.
    # This variable must refer a directory or a zip or jar file.
    XERCES_CLASSES={#XERCES_CLASSES#};

    # Command to start the Java VM.
    JAVA_EXEC="java";

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

    # Command to start subprocesses. This is mainly for debugging
    # purposes. One may, for example, display the Java command that is
    # started by "socos", by running "env SCALA_EXEC=echo socos".
    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 given Unix form file name.
get_os_filename() {
    [ $# = 1 ] || abort "internal error";
    case "$UNAME" in
        CYGWIN* ) cygpath --windows "$1";;
        *       ) echo "$@";;
    esac;
}

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

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

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

##############################################################################
# Variable configuration functions

# Configures the variable with the given name. If a variable named
# SCALA_$1 is defined, assigns its value to the variable named $1,
# otherwise does nothing.
configure_variable() {
    [ $# = 1 ] || abort "internal error";

    if set | grep "^SCALA_$1=" > /dev/null; then
        eval $1=\$SCALA_$1;
    fi;
}

# Configures and tests the path variable with the given name. If a
# variable named SCALA_$1 is defined and refers an existing directory
# or file, assigns its value to the variable named $1. If, after that,
# the variable $1 refers a non-existing path, exits with an error.
configure_path_variable() {
    [ $# = 1 ] || abort "internal error";

    if set | grep "^SCALA_$1=" > /dev/null; then
        path=`eval echo \\\$SCALA_\$1`;
        if [ -f "$path" -o -d "$path" ]; then
            eval $1=\$SCALA_$1;
        else
            warning "File refered by SCALA_$1 ($path) does not exist.";
            unset SCALA_$1;
        fi;
    fi;

    path=`eval echo \\\$\$1`;
    if [ ! -f "$path" -a ! -d "$path" ]; then
        abort "File referred by $1 ($path) does not exist." \
            "Please, fix the definition of $1 in file $SOURCE." \
            "Alternatively you may also define the environment" \
            "variable SCALA_$1.";
    fi
}

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

# Appends a named path to a named path list. First, computes variable
# $2 and then updates the path list in variable $1 by appending the
# path from variable $2.
append_path() {
    [ $# = 2 ] || abort "internal error";

    compute_variable "$2";

    if [ -z "`eval echo \\\$\$1`" ]; then
        eval $1=\$$2;
    else
        eval $1=\$$1:\$$2;
    fi;
}

##############################################################################
# Variable computation functions

# Sets SCALA_CLASSPATH to its default value.
compute_default_scala_classpath() {
    SCALA_CLASSPATH=".";
}

# Computes SCALA_CLASSPATH.
compute_scala_classpath() {
    if [ -z "$SCALA_CLASSPATH" ]; then
        if [ -n "$CLASSPATH" ]; then
            SCALA_CLASSPATH=`get_unix_pathlist "$CLASSPATH"`;
        else
            compute_default_scala_classpath;
        fi;
    fi;
}

# Sets SCALA_BOOTCLASSPATH to its default value.
compute_default_scala_bootclasspath() {
    eval SCALA_BOOTCLASSPATH="";
    append_path SCALA_BOOTCLASSPATH RUNTIME_CLASSES;
    append_path SCALA_BOOTCLASSPATH RUNTIME_SOURCES;
}


# Computes SCALA_BOOTCLASSPATH.
compute_scala_bootclasspath() {
    if [ -z "$SCALA_BOOTCLASSPATH" ]; then
        compute_default_scala_bootclasspath;
    fi;
}

# Computes variable $1.
compute_variable() {
    [ $# = 1 ] || abort "internal error";

    case "$1" in
        SCALA_CLASSPATH     ) compute_scala_classpath;;
        SCALA_BOOTCLASSPATH ) compute_scala_bootclasspath;;
        RUNTIME_SOURCES     ) configure_path_variable "$1";;
        RUNTIME_CLASSES     ) configure_path_variable "$1";;
        DTD2SCALA_CLASSES   ) configure_path_variable "$1";;
        FJBG_CLASSES        ) configure_path_variable "$1";;
        MSIL_CLASSES        ) configure_path_variable "$1";;
        BCEL_CLASSES        ) configure_path_variable "$1";;
        XERCES_CLASSES      ) configure_path_variable "$1";;
        JAVA_EXEC           ) configure_variable "$1";;
        JAVA_ARGS           ) configure_variable "$1";;
        EXEC                ) configure_variable "$1";;
    esac;
}

##############################################################################
# Java invocation functions

# Invokes Java with the given arguments (which must, at least, contain
# the name of the main class) and the following additional arguments:
# - arguments specified by variable JAVA_ARGS
# - script-suffix-specific arguments
# - definition of Java properties scala.product and scala.version
# - classpath set to $JAVA_CLASSPATH
exec_java() {
    [ $# -gt 0 ] || abort "internal error";

    # configure variables
    compute_variable EXEC;
    compute_variable JAVA_EXEC;
    compute_variable JAVA_ARGS;

    # append script-suffix-specific arguments
    case "$SCRIPT" in
        *-debug* ) JAVA_ARGS="$JAVA_ARGS -Djava.compiler=NONE";;
    esac;

    # invoke Java
    $EXEC $JAVA_EXEC $JAVA_ARGS  \
        -classpath "`get_os_pathlist "$JAVA_CLASSPATH"`" \
        -Dscala.product="$SCRIPT" \
        -Dscala.version="$VERSION" \
        "$@";
}

# Starts a Java program using the Scala compiler. The given arguments
# are passed to exec_java. They must, at least, contain the name of
# the main class.
exec_compile() {
    [ $# -gt 0 ] || abort "internal error";

    # compute Java classpath
    append_path JAVA_CLASSPATH RUNTIME_CLASSES;
    append_path JAVA_CLASSPATH BCEL_CLASSES;
    append_path JAVA_CLASSPATH FJBG_CLASSES;
    append_path JAVA_CLASSPATH MSIL_CLASSES;

    compute_scala_classpath;
    compute_scala_bootclasspath;

    # invoke Java
    exec_java \
        -Dscala.class.path=`get_os_pathlist "$SCALA_CLASSPATH"` \
        -Dscala.boot.class.path=`get_os_pathlist "$SCALA_BOOTCLASSPATH"` \
        "$@";
}

# Starts a program using dtd2scala. The given arguments are passed to
# exec_java. They must, at least, contain the name of the main class.
exec_dtd2scala() {
    [ $# -gt 0 ] || abort "internal error";

    # compute Java classpath
    append_path JAVA_CLASSPATH DTD2SCALA_CLASSES;
    append_path JAVA_CLASSPATH XERCES_PATH;

    # invoke Java
    exec_java \
        "$@";
}

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

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

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

# Prints default value of $1.
scala_info_print_variable_default() {
    [ $# = 1 ] || abort "internal error";
    case "$1" in
        SCALA_CLASSPATH     ) compute_default_scala_classpath;;
        SCALA_BOOTCLASSPATH ) compute_default_scala_bootclasspath;;
    esac;
    scala_info_print_variable "$1";
}

# Prints current value of $1.
scala_info_print_variable_current() {
    [ $# = 1 ] || abort "internal error";
    compute_variable "$1";
    scala_info_print_variable "$1";
}

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

# Implements "scala-info --$1 <variable>"
scala_info_option_x_variable() {
    [ $# -lt 2 ] && abort "missing variable name";
    [ $# -gt 2 ] && abort "too many arguments";
    case "$2" in
        CLASSPATH           ) variable=SCALA_$2;;
        BOOTCLASSPATH       ) variable=SCALA_$2;;
        RUNTIME_SOURCES     ) variable=$2;;
        RUNTIME_CLASSES     ) variable=$2;;
        DTD2SCALA_CLASSES   ) variable=$2;;
        FJBG_CLASSES        ) variable=$2;;
        MSIL_CLASSES        ) variable=$2;;
        BCEL_CLASSES        ) variable=$2;;
        XERCES_CLASSES      ) variable=$2;;
        JAVA_EXEC           ) variable=$2;;
        JAVA_ARGS           ) variable=$2;;
        EXEC                ) variable=$2;;
        *                   ) abort "Unknown variable \`$1'";;
    esac;
    scala_info_print_variable_$1 $variable;
}

# Implements "scala-info --help".
scala_info_option_help() {
    echo "usage: $0 <option>";
    echo "where possible options include:";
    echo "  --home                Print Scala home directory";
    echo "  --default <variable>  Print default value of variable";
    echo "  --current <variable>  Print current value of variable";
    echo "  -? --help             Print this help message";
    echo "";
    echo "valid variables include:";
    echo "  SCALA_CLASSPATH       Default classpath";
    echo "  SCALA_BOOTCLASSPATH   Default bootclasspath";
    echo "  RUNTIME_SOURCES       Location of the Scala library sources";
    echo "  RUNTIME_CLASSES       Location of the Scala library and tools";
    echo "  DTD2SCALA_CLASSES     Location of the dtd2scala tool";
    echo "  FJBG_CLASSES          Location of the fjbg library";
    echo "  MSIL_CLASSES          Location of the msil library";
    echo "  BCEL_CLASSES          Location of the bcel library";
    echo "  XERCES_CLASSES        Location of the xerces library";
    echo "  JAVA_EXEC             Command to start the Java VM";
    echo "  JAVA_ARGS             Additional arguments to pass to the Java VM";
    echo "  EXEC                  Command to start subprocesses";
}

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

    case "$1" in
        --home      ) shift 1; scala_info_option_home "$@";;
        --default   ) shift 1; scala_info_option_x_variable default "$@";;
        --current   ) shift 1; scala_info_option_x_variable current "$@";;
        -? | --help ) shift 1; scala_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;
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

unset JAVA_CLASSPATH;
configure;

case "$SCRIPT" in
    scala-info ) scala_info "$@";;
    scalac*    ) exec_compile scalac.Main "$@";;
    scaladoc*  ) exec_compile scalac.Main -doc "$@";;
    scalarun*  ) exec_compile scalai.Main "$@";;
    scalaint*  ) exec_compile scalai.Main -interactive "$@";;
    dtd2scala* ) exec_dtd2scala dtd2scala.Main "$@";;
    socos*     )
        warning "Deprecated command, use scalac${SCRIPT#socos} instead.";
        exec_compile scalac.Main "$@";;
    surus*     )
        warning "Deprecated command, use scalarun${SCRIPT#surus} instead.";
        exec_compile scalai.Main "$@";;
    siris*     )
        warning "Deprecated command, use scalaint${SCRIPT#siris} instead.";
        exec_compile scalai.Main -interactive "$@";;
    *          ) abort "Don't know what to do for $SCRIPT.";;
esac;

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