-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcompile_run.sh
More file actions
87 lines (70 loc) · 2.09 KB
/
compile_run.sh
File metadata and controls
87 lines (70 loc) · 2.09 KB
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
# Termination in failure.
set -euo pipefail
# Setting up the locations.
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
PROJECT_ROOT="$(cd "$SCRIPT_DIR/.." && pwd)"
SRC_DIR="$PROJECT_ROOT/compiler/src/fe"
TARGET_DIR="$PROJECT_ROOT/tests"
# Accept the filename from the user or prompt if none provided.
if [ "$#" -gt 0 ]; then
PARAMS=("$@")
else
read -rp "Enter parameter to pass to main: " USER_PARAM
PARAMS=("$USER_PARAM")
fi
# Resolve file paths for any parameters that look like files.
# If a parameter doesn't exist relative to the current working directory,
# Try to locate it under the project root (for example, $PROJECT_ROOT/tests/<name>).
for i in "${!PARAMS[@]}"; do
p="${PARAMS[i]}"
# If the path exists as given, convert to absolute path.
if [ -e "$p" ]; then
PARAMS[i]="$(cd "$(dirname "$p")" && pwd)/$(basename "$p")"
continue
fi
# Strip leading ./ for basename checks.
base="${p#./}"
if [ -e "$PROJECT_ROOT/$p" ]; then
PARAMS[i]="$PROJECT_ROOT/$p"
continue
fi
if [ -e "$PROJECT_ROOT/tests/$base" ]; then
PARAMS[i]="$PROJECT_ROOT/tests/$base"
continue
fi
if [ -e "$PROJECT_ROOT/$base" ]; then
PARAMS[i]="$PROJECT_ROOT/$base"
continue
fi
# Otherwise leave parameter as-is (it may be a flag or intended relative path).
done
# Ensure source directory exists
if [ ! -d "$SRC_DIR" ]; then
echo "ERROR: Source directory '$SRC_DIR' not found." >&2
exit 1
fi
cd "$SRC_DIR"
echo "Compiling in $SRC_DIR..."
if ! make; then
echo "ERROR: Make failed." >&2
exit 1
fi
if [ ! -f main ]; then
echo "ERROR: Compiled 'main' not found in $SRC_DIR." >&2
exit 1
fi
# Ensure target directory exists.
if [ ! -d "$TARGET_DIR" ]; then
echo "Target directory '$TARGET_DIR' does not exist. Creating it..."
mkdir -p "$TARGET_DIR"
fi
echo "Moving 'main' to $TARGET_DIR/..."
mv -f main "$TARGET_DIR/"
chmod +x "$TARGET_DIR/main"
echo "Cleaning build artifacts in $SRC_DIR..."
if ! make clean; then
echo "ERROR: make clean failed." >&2
exit 1
fi
echo "Running $TARGET_DIR/main with parameters: ${PARAMS[*]}..."
"$TARGET_DIR/main" "${PARAMS[@]}"