-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuilder.sh
More file actions
168 lines (149 loc) · 3.61 KB
/
Copy pathbuilder.sh
File metadata and controls
168 lines (149 loc) · 3.61 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
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
#!/bin/bash
AUTHOR="©2026 Alexandre Raduly <alexander14k28@gmail.com>"
LICENSE="AGPL-3.0-or-later"
OS=""
ARCH=""
COMPILER=""
BIN="cpprunner"
is_empty() { [[ -z "${1:-}" ]]; }
to_lower() { echo "$1" | tr '[:upper:]' '[:lower:]'; }
trim() { echo "$1" | xargs; }
log() { echo "$@"; }
detect_env() {
OS=$(uname -s | tr '[:upper:]' '[:lower:]')
ARCH=$(uname -m)
if [[ "$OS" == "darwin" ]]; then OS="macos"; fi
if [[ "$OS" == mingw* ]] || [[ "$OS" == msys* ]]; then
OS="win32"
fi
if command -v cl &> /dev/null; then
COMPILER="msvc"
elif command -v gcc &> /dev/null; then
COMPILER="gcc"
elif command -v clang &> /dev/null; then
COMPILER="clang"
else
echo "no compiler"
exit 1
fi
}
build() {
local build_type="$1"
detect_env
local out_dir="out/${OS}/${ARCH}/${COMPILER}/${build_type}"
mkdir -p "$out_dir"
local src=$(find app -name "*.cpp")
local bin="$out_dir/$BIN"
if [[ "$COMPILER" == "msvc" ]]; then
local cflags="/std:c++17 /EHsc /Iapp"
if [[ "$build_type" == "debug" ]]; then
cflags="$cflags /Od /Zi"
else
cflags="$cflags /O2"
fi
bin="${bin}.exe"
log "compiling $bin ($build_type)"
cl.exe $cflags $src /link /OUT:"$bin" fmt.lib
else
local cflags="-std=c++17 -Iapp $(pkg-config --cflags fmt)"
local ldflags="$(pkg-config --libs fmt) -ldl -rdynamic"
if [[ "$build_type" == "debug" ]]; then
cflags="$cflags -g -O0"
else
cflags="$cflags -O2"
fi
if [[ "$OS" == "win32" ]]; then bin="${bin}.exe"; fi
log "compiling $bin ($build_type)"
g++ $cflags $src -o "$bin" $ldflags
fi
if [ $? -eq 0 ]; then
log "done: $bin"
return 0
else
log "failed"
return 1
fi
}
run_tests() {
detect_env
local bin="out/${OS}/${ARCH}/${COMPILER}/release/${BIN}"
if [[ "$OS" == "win32" ]]; then bin="${bin}.exe"; fi
if [[ ! -f "$bin" ]]; then
log "binary not found. build first."
return 1
fi
pushd tester > /dev/null
BIN="../$bin" ./tester.sh
popd > /dev/null
}
clean() {
rm -rf out
log "cleaned"
}
prompt_read() {
local -n _result="$1"
local prompt="$2"
read -rp "$prompt" _result
}
show_prompt() {
echo -n "[b]uild [c]lean [d]ebug [t]est e[x]it > "
}
resolve_command() {
local char="$1"
case "$(to_lower "$char")" in
b) echo "build" ;;
c) echo "clean" ;;
d) echo "debug" ;;
t) echo "test" ;;
x) echo "exit" ;;
*) echo "unknown" ;;
esac
}
dispatch() {
local cmd="$1"
case "$cmd" in
build) build "release" ;;
clean) clean ;;
debug)
if build "debug"; then
local d="out/${OS}/${ARCH}/${COMPILER}/debug/${BIN}"
if [[ "$OS" == "win32" ]]; then d="${d}.exe"; fi
gdb "$d"
fi
;;
test) run_tests ;;
exit) return 1 ;;
*) log 'what!?' ;;
esac
return 0
}
handle_input() {
local raw="$1"
local cmd
raw=$(trim "$raw")
is_empty "$raw" && return 0
cmd=$(resolve_command "$raw")
dispatch "$cmd"
}
main_loop() {
local input
log "author: $AUTHOR "
log "license: $LICENSE"
while true; do
show_prompt
prompt_read input ""
handle_input "$input" || break
done
}
do_main() {
main_loop
log "bye!"
}
if [ -t 0 ]; then
do_main
else
title="$BIN"
xfce4-terminal\
--title="$title"\
-e "bash -c '$0 $@; exec bash'"
fi