-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathxcompile_main.sh
More file actions
executable file
·181 lines (172 loc) · 4.32 KB
/
xcompile_main.sh
File metadata and controls
executable file
·181 lines (172 loc) · 4.32 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
169
170
171
172
173
174
175
176
177
178
179
180
181
#!/bin/bash
GREEN='\033[0;32m'
BLUE='\033[0;34m'
CYAN='\033[0;36m'
YELLOW='\033[1;33m'
RED='\033[0;31m'
NC='\033[0m'
set -e
MODE=""
REBUILD=false
MAIN_FILE=""
COMPILER="clang"
WASM=false
XMAKE_ARGS=()
while [[ $# -gt 0 ]]; do
case $1 in
--debug)
MODE="debug"
shift
;;
--release)
MODE="release"
shift
;;
--asan)
MODE="asan"
shift
;;
--rebuild)
REBUILD=true
shift
;;
--main)
MAIN_FILE="$2"
shift 2
;;
--gcc)
COMPILER="gcc"
shift
;;
--clang)
COMPILER="clang"
shift
;;
--wasm)
WASM=true
shift
;;
*)
XMAKE_ARGS+=("$1")
shift
;;
esac
done
find_compiler() {
local binary="$1"
local min_version="${2:-0}"
local best_bin=""
local best_version=0
IFS=':' read -ra path_dirs <<< "$PATH"
for dir in "${path_dirs[@]}"; do
for bin in "$dir"/${binary}-*; do
[[ -x "$bin" ]] || continue
local name=$(basename "$bin")
if [[ $name =~ ^${binary}-([0-9]+)$ ]]; then
local ver="${BASH_REMATCH[1]}"
if (( ver >= min_version && ver > best_version )); then
best_version=$ver
best_bin=$bin
fi
fi
done
done
if [[ -z "$best_bin" ]] && command -v "$binary" >/dev/null 2>&1; then
best_bin=$(command -v "$binary")
fi
if [[ -z "$best_bin" ]]; then
printf "\033[0;31mError: No suitable %s found (>= %d)\033[0m\n" "$binary" "$min_version" >&2
return 1
fi
echo "$best_bin"
}
if [[ "$WASM" == true ]]; then
# Verify emscripten is available
if ! command -v emcc &> /dev/null; then
echo -e "${RED}Error: emcc not found. Source your emsdk_env.sh first:${NC}"
echo " source /path/to/emsdk/emsdk_env.sh"
exit 1
fi
EMSDK_ROOT=$(dirname $(dirname $(which emcc)))
CONFIG_ARGS=("-p" "wasm" "--sdk=${EMSDK_ROOT}/upstream/emscripten")
if [[ -n "$MAIN_FILE" ]]; then
CONFIG_ARGS+=("--main=$MAIN_FILE")
fi
if [[ -n "$MODE" ]]; then
CONFIG_ARGS+=("-m" "$MODE")
fi
echo -e "${CYAN}Wasm build using:${NC} $(emcc --version | head -1)"
else
if [[ "$COMPILER" == "gcc" ]]; then
CXX=$(find_compiler "g++" 0)
CC=$(find_compiler "gcc" 0)
TOOLCHAIN="gcc"
else
CXX=$(find_compiler "clang++" 20)
CC=$(find_compiler "clang" 20)
TOOLCHAIN="clang"
fi
CONFIG_ARGS=("--compiler=$COMPILER" "--toolchain=$TOOLCHAIN" "--cc=$CC" "--cxx=$CXX")
if [[ -n "$MAIN_FILE" ]]; then
CONFIG_ARGS+=("--main=$MAIN_FILE")
fi
if [[ -n "$MODE" ]]; then
CONFIG_ARGS+=("-m" "$MODE")
fi
fi
if [[ "$REBUILD" == true ]]; then
echo -e "${BLUE}[1/3]${NC} Cleaning build directory..."
rm -rf build .xmake
echo ""
echo -e "${BLUE}[2/3]${NC} Configuring..."
if ! xmake f -c "${CONFIG_ARGS[@]}" "${XMAKE_ARGS[@]}"; then
echo -e "${RED}✗ XMake configuration failed!${NC}"
exit 1
fi
echo ""
echo -e "${BLUE}[3/3]${NC} Building..."
else
echo -e "${BLUE}Configuring & Building...${NC}"
if ! xmake f "${CONFIG_ARGS[@]}" "${XMAKE_ARGS[@]}"; then
echo -e "${RED}✗ XMake configuration failed!${NC}"
exit 1
fi
fi
if [[ "$WASM" == false ]]; then
echo -e "${CYAN}Compiler:${NC} $($CXX --version | head -1)"
fi
if ! xmake -j$(nproc) "${XMAKE_ARGS[@]}"; then
echo -e "${RED}✗ XMake build failed!${NC}"
exit 1
fi
if [[ "$WASM" == true ]]; then
# Find the .html output
html_path=$(find build -type f -name "*.html" | head -n1)
if [ -z "$html_path" ]; then
echo -e "${RED}Error:${NC} Built .html not found in build/"
exit 1
fi
# Copy html + wasm + js together
base=$(basename "$html_path" .html)
dir=$(dirname "$html_path")
for ext in html wasm js; do
f="$dir/$base.$ext"
if [ -f "$f" ]; then
cp "$f" .
echo -e "${GREEN}✓ Copied:${NC} $f → ./$base.$ext"
fi
done
else
target_name=$(grep -E 'target\("([^"]+\.exe)"' xmake.lua | sed -E 's/target\("([^"]+\.exe)".*/\1/' | head -n1)
if [ -z "$target_name" ]; then
echo -e "${RED}Error:${NC} Could not find target name ending with .exe in xmake.lua"
exit 1
fi
exe_path=$(find build -type f -name "${target_name}" | head -n1)
if [ -z "$exe_path" ]; then
echo -e "${RED}Error:${NC} Built executable for target '${target_name}' not found"
exit 1
fi
cp "$exe_path" .
echo -e "${GREEN}✓ Copied:${NC} ${exe_path} → ./$(basename "$exe_path")"
fi