]> zdv2.bktei.com Git - BK-2023-05.git/blob - src/kr_exercises/ch1/s1.9/compile.sh
feat(src/kr/ch1/s1.1/p6):Add hello world example
[BK-2023-05.git] / src / kr_exercises / ch1 / s1.9 / compile.sh
1 #!/usr/bin/env bash
2 # Desc: Check sytnax and compiles file using gcc
3 # Usage: compile.sh mysource.c
4
5 yell() { echo "$0: $*" >&2; } # print script path and all args to stderr
6 die() { yell "$*"; exit 111; } # same as yell() but non-zero exit status
7 try() { "$@" || die "cannot $*"; } # runs args as command, reports args if command fails
8 main() {
9 # Check arg
10 if [[ $# -ne 1 ]]; then die "FATAL:Incorrect argument count:$#"; fi;
11 if [[ ! -f $1 ]]; then die "FATAL:Not a file:$1"; else f="$1"; fi;
12
13 # Set var names
14 f="$(readlink -f $f)"; fe="${f%.c}";
15 yell "DEBUG:f :$f";
16 yell "DEBUG:fe:$fe";
17
18 # Check syntax
19 if ! try gcc -fsyntax-only "$f"; then die "FATAL:Syntax Error."; fi;
20
21 # Compile
22 time { try gcc -o "$fe" "$f"; };
23
24 return 0;
25 }; # main program
26
27 main "$@";
28