-
Notifications
You must be signed in to change notification settings - Fork 43
/
Copy pathbuild.sh
executable file
·583 lines (525 loc) · 11.4 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
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
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
#!/usr/bin/env bash
# Build script for github.com/mubaris/yes
# -------------------------------------
INSTALLED() {
local loc;
loc="$(which "$1" 2>&1)"
if [ $? -eq 0 ] && [ -n "$loc" ]; then
return 0
fi
return 1
}
BUILT() {
# Force it to be consider built.
case "$1" in
"yes"|"YES"|"true"|"TRUE"|1)
BUILD_LASTEXIT=0
BUILD_COMPLETE=true
BUILD_EXECUTED=true
return 0
;;
esac
# Check if it's built.
if [ "${BUILD_COMPLETE}" = "true" ]; then
return 0
fi
if [ "${BUILD_LASTEXIT}" = "FAIL" ]; then
return 0
fi
return 1
}
BUILD-FIND() {
for program in "$@"; do
if INSTALLED "$program"; then
printf "%s" "$program"
return 0
fi
done
return 1
}
BUILD-START() {
BUILD_LASTERROR=""
BUILD_LASTEXIT=""
BUILD_EXECUTED=false
SRC_FILENAME="$2"
OUT_FILENAME="$(sed 's/\./-/' <<< "$(basename "$2")")"
SRC_FILE="${SRC}/${SRC_FILENAME}"
OUT_FILE="${BIN}/${OUT_FILENAME}"
printf "\033[33mBuilding \033[0m%-16s\033[3m... " "$1"
}
BUILD-RUN() {
local first=true
for arg in "$@"; do
if "$first"; then
first=false
printf "\n\033[36m%s\033[0m" "$arg"
else
printf " %s" "$arg"
fi
done
BUILD_EXECUTED=true
BUILD_LASTERROR="$($@ 2>&1)"
BUILD_LASTEXIT=$?
printf "\033[2K\033[A\033[G\033[29C"
return $ret
}
BUILD-END() {
# Fail command.
if [ "${BUILD_LASTEXIT}" = "FAIL" ]; then
return 0;
fi
# Nothing executed.
if ! [ "${BUILD_EXECUTED}" = "true" ]; then
printf "\033[31mNo compiler.\033[0m\n"
return 0
fi
# Success/error.
if [ "${BUILD_LASTEXIT}" -eq 0 ]; then
printf "\033[32mSuccess.\033[0m\n"
else
printf "\033[31mFailure.\033[0m\n"
if "$OPT_SHOW_ERROR"; then
printf "%s\033[0m\n" "${BUILD_LASTERROR}"
fi
fi
}
BUILD-FAIL() {
printf "\033[31m%s\033[0m\n" "$1"
BUILD_LASTEXIT="FAIL"
}
INTERPRETED-COPY() {
if "${OPT_FORCE_WRAP}"; then
return 1
fi
if [ "$(head -c2 < "${SRC_FILE}")" = "#!" ]; then
cp "${SRC_FILE}" "${OUT_FILE}"
chmod +x "${OUT_FILE}"
return 0
fi
return 1
}
INTERPRETED-WRAP() {
printf "#!/usr/bin/env bash\n"
local first=true
for arg in "$@"; do
if "$first"; then
first=false
printf "%q" "$arg"
else
case "$arg" in
'{$@}')
printf ' $@'
;;
*)
printf " %q" "$arg"
;;
esac
fi
done
printf "\nexit $?\n"
}
# -------------------------------------
# Preparation
DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
BIN="${DIR}/bin"
OBJ="${DIR}/obj"
SRC="${DIR}"
OPT_SHOW_ERROR=false
OPT_FORCE_WRAP=false
for arg in "$@"; do
case "$arg" in
-E|--show-errors)
OPT_SHOW_ERROR=true
;;
-W|--force-wrap)
OPT_FORCE_WRAP=true
;;
esac
done
mkdir -p "${BIN}" &>/dev/null
mkdir -p "${OBJ}" &>/dev/null
# -------------------------------------
# AppleScript
BUILD-START "AppleScript" "yes.applescript"
case "$(BUILD-FIND osascript)" in
osascript)
if INTERPRETED-COPY; then
BUILT TRUE
else
INTERPRETED-WRAP osascript "${OBJ}/${SRC_FILENAME}" '{$@}' > "${OUT_FILE}"
chmod +x "${OUT_FILE}"
cp "${SRC_FILE}" "${OBJ}/${SRC_FILENAME}"
BUILT TRUE
fi
;;
esac
BUILD-END
# -------------------------------------
# Awk
BUILD-START "Awk" "yes.awk"
case "$(BUILD-FIND awk)" in
awk)
if INTERPRETED-COPY; then
BUILT true
else
INTERPRETED-WRAP awk "${OBJ}/${SRC_FILENAME}" -- '{$@}' > "${OUT_FILE}"
chmod +x "${OUT_FILE}"
cp "${SRC_FILE}" "${OBJ}/${SRC_FILENAME}"
BUILT TRUE
fi
;;
esac
BUILD-END
# -------------------------------------
# Brainfuck
BUILD-START "Brainfuck" "yes.bf"
case "$(BUILD-FIND brainfuck)" in
brainfuck)
if INTERPRETED-COPY; then
BUILT true
else
INTERPRETED-WRAP brainfuck "${OBJ}/${SRC_FILENAME}" > "${OUT_FILE}"
chmod +x "${OUT_FILE}"
cp "${SRC_FILE}" "${OBJ}/${SRC_FILENAME}"
BUILT TRUE
fi
;;
esac
BUILD-END
# -------------------------------------
# Brainfuck (Custom)
BUILD-START "Brainfuck (Args)" "yes.custom.bf"
case "$(BUILD-FIND brainfuck)" in
brainfuck)
if INTERPRETED-COPY; then
BUILT true
else
INTERPRETED-WRAP brainfuck "${OBJ}/${SRC_FILENAME}" > "${OUT_FILE}"
chmod +x "${OUT_FILE}"
cp "${SRC_FILE}" "${OBJ}/${SRC_FILENAME}"
BUILT TRUE
fi
;;
esac
BUILD-END
# -------------------------------------
# C
BUILD-START "C" "yes.c"
case "$(BUILD-FIND clang gcc)" in
clang)
BUILD-RUN clang -O3 -o "${OUT_FILE}" "${SRC_FILE}"
;;
gcc)
BUILD-RUN gcc -O3 -o "${OUT_FILE}" "${SRC_FILE}"
;;
esac
BUILD-END
# -------------------------------------
# C++
BUILD-START "C++" "yes.cpp"
case "$(BUILD-FIND clang++ g++)" in
clang++)
BUILD-RUN clang++ -O3 -o "${OUT_FILE}" "${SRC_FILE}"
;;
g++)
BUILD-RUN g++ -O3 -o "${OUT_FILE}" "${SRC_FILE}"
;;
esac
BUILD-END
# -------------------------------------
# C#
BUILD-START "C#" "yes.cs"
case "$(BUILD-FIND mcs)" in
mcs)
BUILD-RUN mcs -optimize+ -out:"${OBJ}/${OUT_FILENAME}.exe" "${SRC_FILE}"
INTERPRETED-WRAP mono "${OBJ}/${OUT_FILENAME}.exe" '{$@}' > "${OUT_FILE}"
chmod +x "${OUT_FILE}"
;;
esac
BUILD-END
# -------------------------------------
# D
BUILD-START "D" "yes.d"
case "$(BUILD-FIND rdmd)" in
rdmd)
rdmd --build-only -of"${BIN}/yes-d" yes.d
BUILT true
;;
esac
BUILD-END
# -------------------------------------
# Erlang
BUILD-START "Erlang" "yes.escript"
BUILD-FAIL "TODO BUILD SCRIPT"
BUILD-END
# -------------------------------------
# Fish
BUILD-START "Fish" "yes.fish"
case "$(BUILD-FIND fish)" in
fish)
if INTERPRETED-COPY; then
BUILT true
else
INTERPRETED-WRAP fish "${OBJ}/${SRC_FILENAME}" '{$@}' > "${OUT_FILE}"
cp "${SRC_FILE}" "${OBJ}/${SRC_FILENAME}"
chmod +x "${OUT_FILE}"
BUILT TRUE
fi
;;
esac
BUILD-END
# -------------------------------------
# Go
BUILD-START "Go" "yes.go"
case "$(BUILD-FIND go)" in
go)
if INTERPRETED-COPY; then
BUILT true
else
INTERPRETED-WRAP go run "${OBJ}/${SRC_FILENAME}" '{$@}' > "${OUT_FILE}"
cp "${SRC_FILE}" "${OBJ}/${SRC_FILENAME}"
chmod +x "${OUT_FILE}"
BUILT TRUE
fi
;;
esac
BUILD-END
# -------------------------------------
# Haskell
BUILD-START "Haskell" "yes.hs"
case "$(BUILD-FIND ghc)" in
ghc)
BUILD-RUN ghc "-outputdir ${OBJ} -dynamic -o ${OUT_FILE} ${SRC_FILE}"
;;
esac
BUILD-END
# -------------------------------------
# IL
BUILD-START "IL" "yes.il"
case "$(BUILD-FIND ilasm)" in
ilasm)
BUILD-RUN ilasm /out:"${OBJ}/${OUT_FILENAME}.exe" "${SRC_FILE}"
INTERPRETED-WRAP mono "${OBJ}/${OUT_FILENAME}.exe" '{$@}' > "${OUT_FILE}"
chmod +x "${OUT_FILE}"
;;
esac
BUILD-END
# -------------------------------------
# Java
BUILD-START "Java" "yes.java"
case "$(BUILD-FIND javac)" in
javac)
BUILD-RUN javac -d "${OBJ}" "${SRC_FILE}"
INTERPRETED-WRAP java -cp "${OBJ}" "yes" '{$@}' > "${OUT_FILE}"
chmod +x "${OUT_FILE}"
;;
esac
BUILD-END
# -------------------------------------
# Groovy
BUILD-START "Groovy" "yes.groovy"
case "$(BUILD-FIND groovy)" in
groovy)
if INTERPRETED-COPY; then
BUILT true
else
INTERPRETED-WRAP groovy "${OBJ}/${SRC_FILENAME}" '{$@}' > "${OUT_FILE}"
cp "${SRC_FILE}" "${OBJ}/${SRC_FILENAME}"
chmod +x "${OUT_FILE}"
BUILT TRUE
fi
;;
esac
BUILD-END
# -------------------------------------
# JavaScript
BUILD-START "JavaScript" "yes.js"
case "$(BUILD-FIND node)" in
node)
if INTERPRETED-COPY; then
BUILT true
else
INTERPRETED-WRAP node "${OBJ}/${SRC_FILENAME}" '{$@}' > "${OUT_FILE}"
cp "${SRC_FILE}" "${OBJ}/${SRC_FILENAME}"
chmod +x "${OUT_FILE}"
BUILT TRUE
fi
;;
esac
BUILD-END
# -------------------------------------
# Lisp
BUILD-START "Lisp" "yes.lisp"
BUILD-FAIL "TODO BUILD SCRIPT"
BUILD-END
# -------------------------------------
# Lua
BUILD-START "Lua" "yes.lua"
BUILD-FAIL "TODO BUILD SCRIPT"
BUILD-END
# -------------------------------------
# Objective-C
BUILD-START "Objective-C" "yes.m"
case "$(BUILD-FIND clang)" in
clang)
BUILD-RUN clang -O3 -framework Foundation -o "${OUT_FILE}" "${SRC_FILE}"
;;
esac
BUILD-END
# -------------------------------------
# OCaml
BUILD-START "OCaml" "yes.ml"
case "$(BUILD-FIND ocaml)" in
ocaml)
BASE_FILENAME="${SRC_FILENAME%%.*}"
BUILD-RUN ocamlopt -o "${OUT_FILE}" "${SRC_FILE}"
mv ${BASE_FILENAME}.cmi ${BASE_FILENAME}.cmx ${BASE_FILENAME}.o ${OBJ}
;;
esac
BUILD-END
# -------------------------------------
# PHP
BUILD-START "PHP" "yes.php"
case "$(BUILD-FIND php)" in
php)
if INTERPRETED-COPY; then
BUILT true
else
INTERPRETED-WRAP php "${OBJ}/${SRC_FILENAME}" '{$@}' > "${OUT_FILE}"
cp "${SRC_FILE}" "${OBJ}/${SRC_FILENAME}"
chmod +x "${OUT_FILE}"
BUILT TRUE
fi
;;
esac
BUILD-END
# -------------------------------------
# Perl
BUILD-START "Perl" "yes.pl"
case "$(BUILD-FIND perl)" in
perl)
if INTERPRETED-COPY; then
BUILT true
else
INTERPRETED-WRAP perl "${OBJ}/${SRC_FILENAME}" '{$@}' > "${OUT_FILE}"
cp "${SRC_FILE}" "${OBJ}/${SRC_FILENAME}"
chmod +x "${OUT_FILE}"
BUILT TRUE
fi
;;
esac
BUILD-END
# -------------------------------------
# Python
BUILD-START "Python" "yes.py"
case "$(BUILD-FIND python)" in
python)
if INTERPRETED-COPY; then
BUILT true
else
INTERPRETED-WRAP python "${OBJ}/${SRC_FILENAME}" '{$@}' > "${OUT_FILE}"
cp "${SRC_FILE}" "${OBJ}/${SRC_FILENAME}"
chmod +x "${OUT_FILE}"
BUILT TRUE
fi
;;
esac
BUILD-END
# -------------------------------------
# Ruby
BUILD-START "Ruby" "yes.rb"
case "$(BUILD-FIND ruby)" in
ruby)
if INTERPRETED-COPY; then
BUILT true
else
INTERPRETED-WRAP ruby "${OBJ}/${SRC_FILENAME}" '{$@}' > "${OUT_FILE}"
cp "${SRC_FILE}" "${OBJ}/${SRC_FILENAME}"
chmod +x "${OUT_FILE}"
BUILT TRUE
fi
;;
esac
BUILD-END
# -------------------------------------
# Rust
BUILD-START "Rust" "yes.rs"
case "$(BUILD-FIND rustc)" in
rustc)
BUILD-RUN rustc -o "${OUT_FILE}" "${SRC_FILE}"
;;
esac
BUILD-END
# -------------------------------------
# Scala
BUILD-START "Scala" "yes.scala"
BUILD-FAIL "TODO BUILD SCRIPT"
BUILD-END
# -------------------------------------
# Shell
BUILD-START "Shell" "yes.sh"
case "$(BUILD-FIND sh)" in
sh)
if INTERPRETED-COPY; then
BUILT true
else
INTERPRETED-WRAP sh "${OBJ}/${SRC_FILENAME}" '{$@}' > "${OUT_FILE}"
cp "${SRC_FILE}" "${OBJ}/${SRC_FILENAME}"
chmod +x "${OUT_FILE}"
BUILT TRUE
fi
;;
esac
BUILD-END
# -------------------------------------
# Swift
BUILD-START "Swift" "yes.swift"
case "$(BUILD-FIND swift)" in
swift)
if INTERPRETED-COPY; then
BUILT true
else
INTERPRETED-WRAP swift "${OBJ}/${SRC_FILENAME}" '{$@}' > "${OUT_FILE}"
cp "${SRC_FILE}" "${OBJ}/${SRC_FILENAME}"
chmod +x "${OUT_FILE}"
BUILT TRUE
fi
;;
esac
BUILD-END
# -------------------------------------
# Vala
BUILD-START "Vala" "yes.vala"
case "$(BUILD-FIND valac)" in
valac)
BUILD-RUN valac -o "${OUT_FILE}" "${SRC_FILE}"
;;
esac
BUILD-END
# -------------------------------------
# VB
BUILD-START "VB" "yes.vb"
case "$(BUILD-FIND vbnc)" in
vbnc)
BUILD-RUN vbnc -optimize+ -out:"${OBJ}/${OUT_FILENAME}.exe" "${SRC_FILE}"
INTERPRETED-WRAP mono "${OBJ}/${OUT_FILENAME}.exe" '{$@}' > "${OUT_FILE}"
chmod +x "${OUT_FILE}"
;;
esac
BUILD-END
# -------------------------------------
# Guile
BUILD-START "Guile" "yes.guile"
case "$(BUILD-FIND guile)" in
guile)
if INTERPRETED-COPY; then
BUILT true
else
INTERPRETED-WRAP sh "${OBJ}/${SRC_FILENAME}" '{$@}' > "${OUT_FILE}"
cp "${SRC_FILE}" "${OBJ}/${SRC_FILENAME}"
chmod +x "${OUT_FILE}"
BUILT TRUE
fi
;;
esac
BUILD-END