-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.sh
501 lines (449 loc) · 15.1 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
#!/usr/bin/env bash
#
# Copyright (c) 2018-2025 Stéphane Micheloud
#
# Licensed under the MIT License.
#
##############################################################################
## Subroutines
getHome() {
local source="${BASH_SOURCE[0]}"
while [[ -h "$source" ]]; do
local linked="$(readlink "$source")"
local dir="$( cd -P $(dirname "$source") && cd -P $(dirname "$linked") && pwd )"
source="$dir/$(basename "$linked")"
done
( cd -P "$(dirname "$source")" && pwd )
}
debug() {
local DEBUG_LABEL="[46m[DEBUG][0m"
[[ $DEBUG -eq 1 ]] && echo "$DEBUG_LABEL $1" 1>&2
}
warning() {
local WARNING_LABEL="[46m[WARNING][0m"
echo "$WARNING_LABEL $1" 1>&2
}
error() {
local ERROR_LABEL="[91mError:[0m"
echo "$ERROR_LABEL $1" 1>&2
}
# use variables EXITCODE, TIMER_START
cleanup() {
[[ $1 =~ ^[0-1]$ ]] && EXITCODE=$1
debug "EXITCODE=$EXITCODE"
exit $EXITCODE
}
args() {
[[ $# -eq 0 ]] && HELP=1 && return 1
for arg in "$@"; do
case "$arg" in
## options
-debug) DEBUG=1 ;;
-help) HELP=1 ;;
-nightly) NIGHTLY=1 ;;
-verbose) VERBOSE=1 ;;
-*)
error "Unknown option $arg"
EXITCODE=1 && return 0
;;
## subcommands
clean) CLEAN=1 ;;
compile) COMPILE=1 ;;
decompile) COMPILE=1 && DECOMPILE=1 ;;
doc) DOC=1 ;;
help) HELP=1 ;;
run) COMPILE=1 && RUN=1 ;;
test) COMPILE=1 && TEST=1 ;;
*)
error "Unknown subcommand $arg"
EXITCODE=1 && return 0
;;
esac
done
if [[ $NIGHTLY -eq 1 ]]; then
local nightly_jar=
for f in $(find "$FLIX_HOME/" -type f -name "flix-*.jar" 2>/dev/null); do
nightly_jar="$f"
done
if [[ -f "$nightly_jar" ]]; then
if [[ $DEBUG -eq 1 ]]; then
debug "Nightly build \"$nightly_jar\" was selected"
elif [[ $VERBOSE -eq 1 ]]; then
echo "Nightly build \"$nightly_jar\" was selected" 1>&2
fi
set FLIX_JAR="$nightly_jar"
else
set NIGHTLY=0
warning "Nightly build of Flix not found (use release version instead)"
warning " It can be downloaded from https://flix.dev/nightly/."
fi
fi
if [[ $DECOMPILE -eq 1 ]] && [[ ! -x "$CFR_CMD" ]]; then
warning "cfr installation not found"
DECOMPILE=0
fi
debug "Options : DEBUG=$DEBUG NIGHTLY=$NIGHTLY VERBOSE=$VERBOSE"
debug "Subcommands: CLEAN=$CLEAN COMPILE=$COMPILE DECOMPILE=$DECOMPILE DOC=$DOC HELP=$HELP RUN=$RUN"
[[ -n "$CFR_HOME" ]] && debug "Variables : CFR_HOME=$CFR_HOME"
debug "Variables : FLIX_HOME=$FLIX_HOME"
debug "Variables : GRADLE_HOME=$GRADLE_HOME"
debug "Variables : JAVA_HOME=$JAVA_HOME"
debug "Variables : MAKE_HOME=$MAKE_HOME"
debug "Variables : SCALA_HOME=$SCALA_HOME"
}
help() {
cat << EOS
Usage: $BASENAME { <option> | <subcommand> }
Options:
-debug print commands executed by this script
-nightly use latest Flix nightly build if locally available
-verbose print progress messages
Subcommands:
clean delete generated files
compile compile Flix source files
decompile decompile generated code with CFR
doc generate HTML documentation
help print this help message
run execute Flix program "$PROJECT_NAME"
test run the unit tests
EOS
}
clean() {
if [[ -d "$TARGET_DIR" ]]; then
if [[ $DEBUG -eq 1 ]]; then
debug "Delete directory \"$TARGET_DIR\""
elif [[ $VERBOSE -eq 1 ]]; then
echo "Delete directory \"${TARGET_DIR/$ROOT_DIR\//}\"" 1>&2
fi
rm -rf "$TARGET_DIR"
[[ $? -eq 0 ]] || ( EXITCODE=1 && return 0 )
fi
}
compile() {
[[ -d "$TARGET_APP_DIR" ]] || mkdir -p "$TARGET_APP_DIR"
compile_init
local is_required=0
is_required="$(action_required "$APP_JAR" "$SOURCE_MAIN_DIR/" "*.scala")"
if [[ $is_required -eq 1 ]]; then
compile_scala
[[ $? -eq 0 ]] || ( EXITCODE=1 && return 0 )
fi
is_required="$(action_required "$APP_JAR" "$SOURCE_MAIN_DIR/" "*.flix")"
if [[ $is_required -eq 1 ]]; then
compile_flix
[[ $? -eq 0 ]] || ( EXITCODE=1 && return 0 )
fi
}
compile_init() {
[[ -d "$TARGET_APP_DIR/build" ]] && return 1
if [[ $DEBUG -eq 1 ]]; then
debug "$JAVA_CMD -jar \"$(mixed_path $FLIX_JAR)\" init"
elif [[ $VERBOSE -eq 1 ]]; then
echo "Initialize directory \"${TARGET_APP_DIR/$ROOT_DIR\//}\"" 1>&2
fi
pushd "$TARGET_APP_DIR" 1>/dev/null
eval "$JAVA_CMD" -jar "$(mixed_path $FLIX_JAR)" init
if [[ $? -ne 0 ]]; then
popd 1>/dev/null
error "Failed to initialize \"${TARGET_APP_DIR/$ROOT_DIR\//}\""
cleanup 1
fi
popd 1>/dev/null
rm -rf "$TARGET_APP_DIR/src/"*.flix
rm -rf "$TARGET_APP_DIR/test/"*.flix
local target_src_dir="$TARGET_APP_DIR/src"
if [[ $DEBUG -eq 1 ]]; then
debug "cp -r \"$SOURCE_MAIN_DIR/\"*.flix \"$target_src_dir/\""
elif [[ $VERBOSE -eq 1 ]]; then
echo "Copy Flix source files to directory \"${target_src_dir/$ROOT_DIR\//}\"" 1>&2
fi
cp -r "$SOURCE_MAIN_DIR/"*.flix "$target_src_dir/"
[[ -z "$(ls -R $SOURCE_TEST_DIR/*.flix 2>/dev/null)" ]] && return 1
local target_test_dir="$TARGET_APP_DIR/test"
if [[ $DEBUG -eq 1 ]]; then
debug "cp -r \"$SOURCE_TEST_DIR/\"*.flix \"$target_test_dir/\""
elif [[ $VERBOSE -eq 1 ]]; then
echo "Copy Flix test source files to directory \"${target_test_dir/$ROOT_DIR\//}\"" 1>&2
fi
cp -r "$SOURCE_TEST_DIR/"*.flix "$target_test_dir/"
}
action_required() {
local target_file=$1
local search_path=$2
local search_pattern=$3
local source_file=
for f in $(find "$search_path" -type f -name $search_pattern 2>/dev/null); do
[[ $f -nt $source_file ]] && source_file=$f
done
if [[ -z "$source_file" ]]; then
## Do not compile if no source file
echo 0
elif [[ ! -f "$target_file" ]]; then
## Do compile if target file doesn't exist
echo 1
else
## Do compile if target file is older than most recent source file
[[ $source_file -nt $target_file ]] && echo 1 || echo 0
fi
}
compile_scala() {
local opts_file="$TARGET_DIR/scalac_opts.txt"
echo -color never -classpath "$(mixed_path $TARGET_LIB_DIR)" -d "$(mixed_path $TARGET_LIB_DIR)" > "$opts_file"
local sources_file="$TARGET_LIB_DIR/scalac_sources.txt"
[[ -f "$sources_file" ]] && rm "$sources_file"
local n=0
for f in $(find "$SOURCE_MAIN_DIR/" -type f -name "*.scala" 2>/dev/null); do
echo $(mixed_path $f) >> "$sources_file"
n=$((n + 1))
done
local n_files="$n Scala source file"
[[ $n -gt 1 ]] && n_files="${n_files}s"
if [[ $DEBUG -eq 1 ]]; then
debug "$SCALAC_CMD @$(mixed_path $opts_file) @$(mixed_path $sources_file)"
elif [[ $VERBOSE -eq 1 ]]; then
echo "Compile $n_files to directory \"${TARGET_LIB_DIR/$ROOT_DIR\//}\"" 1>&2
fi
eval "$SCALAC_CMD" "@$(mixed_path $opts_file)" "@$(mixed_path $sources_file)"
if [[ $? -ne 0 ]]; then
error "Failed to compile $n_files to directory \"${TARGET_LIB_DIR/$ROOT_DIR\//}\""
cleanup 1
fi
}
compile_flix() {
local n=0
for f in $(find "$TARGET_APP_DIR/src/" -type f -name "*.flix" 2>/dev/null); do
n=$((n + 1))
done
for f in $(find "$TARGET_APP_DIR/test/" -type f -name "*.flix" 2>/dev/null); do
n=$((n + 1))
done
local n_files="$n Flix source file"
[[ $n -gt 1 ]] && n_files="${n_files}s"
if [[ $DEBUG -eq 1 ]]; then
debug "$JAVA_CMD -jar \"$(mixed_path $FLIX_JAR)\" build"
elif [[ $VERBOSE -eq 1 ]]; then
echo "Compile $n_files to directory \"${TARGET_BUILD_DIR/$ROOT_DIR\//}\"" 1>&2
fi
pushd "$TARGET_APP_DIR" 1>/dev/null
eval "$JAVA_CMD" -jar "$(mixed_path $FLIX_JAR)" build
if [[ $? -ne 0 ]]; then
popd 1>/dev/null
error "Failed to compile $n_files to directory \"${TARGET_BUILD_DIR/$ROOT_DIR\//}\""
cleanup 1
fi
if [[ $DEBUG -eq 1 ]]; then
debug "$JAVA_CMD -jar \"$(mixed_path $FLIX_JAR)\" build-jar"
elif [[ $VERBOSE -eq 1 ]]; then
echo "Create JAR file \"${APP_JAR/$ROOT_DIR\//}\"" 1>&2
fi
eval "$JAVA_CMD" -jar "$(mixed_path $FLIX_JAR)" build-jar
if [[ $? -ne 0 ]]; then
popd 1>/dev/null
error "Failed to create JAR file \"${APP_JAR/$ROOT_DIR\//}\""
cleanup 1
fi
popd 1>/dev/null
}
mixed_path() {
if [[ -x "$CYGPATH_CMD" ]]; then
$CYGPATH_CMD -am $1
elif [[ $(($mingw + $msys)) -gt 0 ]]; then
echo $1 | sed 's|/|\\\\|g'
else
echo $1
fi
}
decompile() {
local output_dir="$TARGET_DIR/cfr-sources"
[[ -d "$output_dir" ]] || mkdir -p "$output_dir"
## --extraclasspath "$(extra_cpath)"
local cfr_opts="--outputdir "$(mixed_path $output_dir)""
local n="$(ls -n $TARGET_BUILD_DIR/*.class | wc -l)"
local class_dirs=
[[ $n -gt 0 ]] && class_dirs="$TARGET_BUILD_DIR"
for f in $(ls -d $TARGET_BUILD_DIR 2>/dev/null); do
n="$(ls -n $f/*.class | wc -l)"
[[ $n -gt 0 ]] && class_dirs="$class_dirs $f"
done
[[ $VERBOSE -eq 1 ]] && echo "Decompile Java bytecode to directory \"${output_dir/$ROOT_DIR\//}\"" 1>&2
for f in $class_dirs; do
debug "$CFR_CMD $cfr_opts $(mixed_path $f)/*.class"
if $DEBUG; then
eval "$CFR_CMD" $cfr_opts "$(mixed_path $f)/*.class"
else
eval "$CFR_CMD" $cfr_opts "$(mixed_path $f)/*.class" 2>/dev/null
fi
if [[ $? -ne 0 ]]; then
error "Failed to decompile generated code in directory \"$f\""
cleanup 1
fi
done
local version="$($JAVA_CMD -jar $FLIX_JAR --version | cut -d " " -f 5)"
## output file contains Scala and CFR headers
local output_file="$TARGET_DIR/cfr-sources_$version.java"
echo "// Compiled with Flix $version" > "$output_file"
if [[ $DEBUG -eq 1 ]]; then
debug "cat $output_dir/*.java >> $output_file"
elif [[ $VERBOSE -eq 1 ]]; then
echo "Save generated Java source files to file \"${output_file/$ROOT_DIR\//}\"" 1>&2
fi
local java_files=
for f in $(find "$output_dir/" -type f -name "*.java" 2>/dev/null); do
java_files="$java_files $(mixed_path $f)"
done
[[ -n "$java_files" ]] && cat $java_files >> "$output_file"
if [[ ! -x "$DIFF_CMD" ]]; then
if [[ $DEBUG -eq 1 ]]; then
warning "diff command not found"
elif [[ $VERBOSE -eq 1 ]]; then
echo "diff command not found" 1>&2
fi
return 0
fi
local diff_opts=--strip-trailing-cr
local check_file="$SOURCE_DIR/build/cfr-source_$version.java"
if [[ -f "$check_file" ]]; then
if [[ $DEBUG -eq 1 ]]; then
debug "$DIFF_CMD $diff_opts $(mixed_path $output_file) $(mixed_path $check_file)"
elif [[ $VERBOSE -eq 1 ]]; then
echo "Compare output file with check file \"${check_file/$ROOT_DIR\//}\"" 1>&2
fi
eval "$DIFF_CMD" $diff_opts "$(mixed_path $output_file)" "$(mixed_path $check_file)"
if [[ $? -ne 0 ]]; then
error "Output file and check file differ"
cleanup 1
fi
fi
}
doc() {
warning "NYI"
}
run() {
local boot_cpath=
for f in $(find "$TARGET_LIB_DIR/" -type f -name "*.jar" 2>/dev/null); do
boot_cpath="$boot_cpath$PSEP$(mixed_path $f)"
done
local java_opts=
[[ -n "$boot_cpath" ]] && java_opts="-Xbootclasspath/a:\"$boot_cpath\"" $java_opts
if [[ $DEBUG -eq 1 ]]; then
debug "$JAVA_CMD $java_opts -jar \"$(mixed_path $APP_JAR)\""
elif [[ $VERBOSE -eq 1 ]]; then
echo "Execute JAR file \"${APP_JAR/$ROOT_DIR\//}\"" 1>&2
fi
eval "$JAVA_CMD" $java_opts -jar "$(mixed_path $APP_JAR)"
if [[ $? -ne 0 ]]; then
error "Failed to execute JAR file \"${APP_JAR/$ROOT_DIR\//}\""
cleanup 1
fi
}
run_tests() {
pushd "$TARGET_APP_DIR" 1>/dev/null
if [[ $DEBUG -eq 1 ]]; then
debug "$JAVA_CMD -jar \"$(mixed_path $FLIX_JAR)\" test"
elif [[ $VERBOSE -eq 1 ]]; then
echo "Run the unit tests for \"${APP_JAR/$ROOT_DIR\//}\"" 1>&2
fi
eval "$JAVA_CMD" -jar "$(mixed_path $FLIX_JAR)" test
if [[ $? -ne 0 ]]; then
popd 1>/dev/null
error "Failed to run the unit tests for \"${APP_JAR/$ROOT_DIR\//}\""
cleanup 1
fi
popd 1>/dev/null
}
##############################################################################
## Environment setup
BASENAME=$(basename "${BASH_SOURCE[0]}")
EXITCODE=0
ROOT_DIR="$(getHome)"
PROJECT_NAME="$(basename $ROOT_DIR)"
PROJECT_URL="github.com/$USER/flix-examples"
PROJECT_VERSION="1.0-SNAPSHOT"
SOURCE_DIR="$ROOT_DIR/src"
SOURCE_MAIN_DIR="$SOURCE_DIR/main"
SOURCE_TEST_DIR="$SOURCE_DIR/test"
TARGET_DIR="$ROOT_DIR/target"
TARGET_APP_DIR="$TARGET_DIR/$PROJECT_NAME"
TARGET_BUILD_DIR="$TARGET_APP_DIR/build"
TARGET_LIB_DIR="$TARGET_APP_DIR/lib"
## Starting with version 0.35.0 Flix generates the jar file into directory 'artifact'.
APP_JAR="$TARGET_APP_DIR/artifact/$PROJECT_NAME.jar"
## We refrain from using `true` and `false` which are Bash commands
## (see https://man7.org/linux/man-pages/man1/false.1.html)
CLEAN=0
COMPILE=0
DEBUG=0
DECOMPILE=0
DOC=0
HELP=0
NIGHTLY=0
RUN=0
TEST=0
VERBOSE=0
COLOR_START="[32m"
COLOR_END="[0m"
cygwin=0
mingw=0
msys=0
darwin=0
case "$(uname -s)" in
CYGWIN*) cygwin=1 ;;
MINGW*) mingw=1 ;;
MSYS*) msys=1 ;;
Darwin*) darwin=1
esac
unset CYGPATH_CMD
PSEP=":"
if [[ $(($cygwin + $mingw + $msys)) -gt 0 ]]; then
CYGPATH_CMD="$(which cygpath 2>/dev/null)"
PSEP=";"
[[ -n "$CFR_HOME" ]] && CFR_HOME="$(mixed_path $CFR_HOME)"
[[ -n "$FLIX_HOME" ]] && FLIX_HOME="$(mixed_path $FLIX_HOME)"
[[ -n "$GIT_HOME" ]] && GIT_HOME="$(mixed_path $GIT_HOME)"
[[ -n "$JAVA_HOME" ]] && JAVA_HOME="$(mixed_path $JAVA_HOME)"
[[ -n "$SCALA_HOME" ]] && SCALA_HOME="$(mixed_path $SCALA_HOME)"
DIFF_CMD="$GIT_HOME/usr/bin/diff.exe"
else
DIFF_CMD="$(which diff)"
fi
if [[ ! -x "$JAVA_HOME/bin/java" ]]; then
error "Java SDK installation not found"
cleanup 1
fi
JAVA_CMD="$JAVA_HOME/bin/java"
if [[ ! -x "$SCALA_HOME/bin/scalac" ]]; then
error "Scala 2 installation not found"
cleanup 1
fi
SCALAC_CMD="$SCALA_HOME/bin/scalac"
unset CFR_CMD
[[ -x "$CFR_HOME/bin/cfr" ]] && CFR_CMD="$CFR_HOME/bin/cfr"
if [[ ! -f "$FLIX_HOME/flix.jar" ]]; then
error "Flix installation not found $FLIX_HOME"
cleanup 1
fi
FLIX_JAR="$FLIX_HOME/flix.jar"
args "$@"
[[ $EXITCODE -eq 0 ]] || cleanup 1
##############################################################################
## Main
[[ $HELP -eq 1 ]] && help && cleanup
if [[ $CLEAN -eq 1 ]]; then
clean || cleanup 1
fi
if [[ $COMPILE -eq 1 ]]; then
compile || cleanup 1
fi
if $DECOMPILE -eq 1 ]]; then
decompile || cleanup 1
fi
if [[ $DOC -eq 1 ]]; then
doc || cleanup 1
fi
if [[ $RUN -eq 1 ]]; then
run || cleanup 1
fi
if [[ $TEST -eq 1 ]]; then
run_tests || cleanup 1
fi
cleanup