Skip to content

Commit fff16b4

Browse files
committed
refactor our
1 parent e408a2c commit fff16b4

File tree

2 files changed

+101
-101
lines changed

2 files changed

+101
-101
lines changed

src/SPC/store/pkg/Zig.php

Lines changed: 2 additions & 101 deletions
Original file line numberDiff line numberDiff line change
@@ -130,107 +130,8 @@ public static function getEnvironment(): array
130130

131131
private function createZigCcScript(string $bin_dir): void
132132
{
133-
$script_content = <<<'EOF'
134-
#!/usr/bin/env bash
135-
136-
LIBDIR="$(realpath "$(dirname "$(gcc -print-file-name=libc.so)")")"
137-
SCRIPT_DIR="$(dirname "${BASH_SOURCE[0]}")"
138-
BUILDROOT_ABS="$(realpath "$SCRIPT_DIR/../../buildroot/include" 2>/dev/null || echo "")"
139-
PARSED_ARGS=()
140-
141-
while [[ $# -gt 0 ]]; do
142-
case "$1" in
143-
-isystem)
144-
shift
145-
ARG="$1"
146-
[[ -n "$ARG" ]] && shift || break
147-
ARG_ABS="$(realpath "$ARG" 2>/dev/null || echo "")"
148-
if [[ -n "$ARG_ABS" && "$ARG_ABS" == "$BUILDROOT_ABS" ]]; then
149-
PARSED_ARGS+=("-I$ARG")
150-
else
151-
PARSED_ARGS+=("-isystem" "$ARG")
152-
fi
153-
;;
154-
-isystem*)
155-
ARG="${1#-isystem}"
156-
shift
157-
ARG_ABS="$(realpath "$ARG" 2>/dev/null || echo "")"
158-
if [[ -n "$ARG_ABS" && "$ARG_ABS" == "$BUILDROOT_ABS" ]]; then
159-
PARSED_ARGS+=("-I$ARG")
160-
else
161-
PARSED_ARGS+=("-isystem$ARG")
162-
fi
163-
;;
164-
*)
165-
PARSED_ARGS+=("$1")
166-
shift
167-
;;
168-
esac
169-
done
170-
171-
SPC_TARGET_WAS_SET=1
172-
if [ -z "${SPC_TARGET+x}" ]; then
173-
SPC_TARGET_WAS_SET=0
174-
fi
175-
176-
UNAME_M="$(uname -m)"
177-
UNAME_S="$(uname -s)"
178-
179-
case "$UNAME_M" in
180-
x86_64) ARCH="x86_64" ;;
181-
aarch64|arm64) ARCH="aarch64" ;;
182-
*) echo "Unsupported architecture: $UNAME_M" >&2; exit 1 ;;
183-
esac
184-
185-
case "$UNAME_S" in
186-
Linux) OS="linux" ;;
187-
Darwin) OS="macos" ;;
188-
*) echo "Unsupported OS: $UNAME_S" >&2; exit 1 ;;
189-
esac
190-
191-
SPC_TARGET="${SPC_TARGET:-$ARCH-$OS}"
192-
193-
if [ "$SPC_LIBC" = "glibc" ]; then
194-
SPC_LIBC="gnu"
195-
fi
196-
197-
if [ "$SPC_TARGET_WAS_SET" -eq 0 ] && [ -z "$SPC_LIBC" ] && [ -z "$SPC_LIBC_VERSION" ]; then
198-
exec zig cc "${PARSED_ARGS[@]}"
199-
elif [ -z "$SPC_LIBC" ] && [ -z "$SPC_LIBC_VERSION" ]; then
200-
exec zig cc -target ${SPC_TARGET} "${PARSED_ARGS[@]}"
201-
else
202-
TARGET="${SPC_TARGET}-${SPC_LIBC}"
203-
[ -n "$SPC_LIBC_VERSION" ] && TARGET="${TARGET}.${SPC_LIBC_VERSION}"
204-
205-
output=$(zig cc -target "$TARGET" -L"$LIBDIR" -lstdc++ "${PARSED_ARGS[@]}" 2>&1)
206-
status=$?
207-
208-
filtered_output=$(echo "$output" | grep -v "version '.*' in target triple")
209-
210-
if [ $status -eq 0 ]; then
211-
echo "$filtered_output"
212-
exit 0
213-
fi
214-
215-
if echo "$output" | grep -q "version '.*' in target triple"; then
216-
TARGET_FALLBACK="${SPC_TARGET}-${SPC_LIBC}"
217-
output=$(zig cc -target "$TARGET_FALLBACK" -L"$LIBDIR" -lstdc++ "${PARSED_ARGS[@]}" 2>&1)
218-
status=$?
219-
220-
filtered_output=$(echo "$output" | grep -v "version '.*' in target triple")
221-
222-
if [ $status -eq 0 ]; then
223-
echo "$filtered_output"
224-
exit 0
225-
else
226-
exec zig cc -target "$TARGET_FALLBACK" "${PARSED_ARGS[@]}"
227-
fi
228-
else
229-
echo "$filtered_output"
230-
exec zig cc -target "$TARGET" "${PARSED_ARGS[@]}"
231-
fi
232-
fi
233-
EOF;
133+
$script_path = __DIR__ . '/../scripts/zig-cc.sh';
134+
$script_content = file_get_contents($script_path);
234135

235136
file_put_contents("{$bin_dir}/zig-cc", $script_content);
236137
chmod("{$bin_dir}/zig-cc", 0755);

src/SPC/store/scripts/zig-cc.sh

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
#!/usr/bin/env bash
2+
3+
LIBDIR="$(realpath "$(dirname "$(gcc -print-file-name=libc.so)")")"
4+
SCRIPT_DIR="$(dirname "${BASH_SOURCE[0]}")"
5+
BUILDROOT_ABS="$(realpath "$SCRIPT_DIR/../../buildroot/include" 2>/dev/null || echo "")"
6+
PARSED_ARGS=()
7+
8+
while [[ $# -gt 0 ]]; do
9+
case "$1" in
10+
-isystem)
11+
shift
12+
ARG="$1"
13+
[[ -n "$ARG" ]] && shift || break
14+
ARG_ABS="$(realpath "$ARG" 2>/dev/null || echo "")"
15+
if [[ -n "$ARG_ABS" && "$ARG_ABS" == "$BUILDROOT_ABS" ]]; then
16+
PARSED_ARGS+=("-I$ARG")
17+
else
18+
PARSED_ARGS+=("-isystem" "$ARG")
19+
fi
20+
;;
21+
-isystem*)
22+
ARG="${1#-isystem}"
23+
shift
24+
ARG_ABS="$(realpath "$ARG" 2>/dev/null || echo "")"
25+
if [[ -n "$ARG_ABS" && "$ARG_ABS" == "$BUILDROOT_ABS" ]]; then
26+
PARSED_ARGS+=("-I$ARG")
27+
else
28+
PARSED_ARGS+=("-isystem$ARG")
29+
fi
30+
;;
31+
*)
32+
PARSED_ARGS+=("$1")
33+
shift
34+
;;
35+
esac
36+
done
37+
38+
SPC_TARGET_WAS_SET=1
39+
if [ -z "${SPC_TARGET+x}" ]; then
40+
SPC_TARGET_WAS_SET=0
41+
fi
42+
43+
UNAME_M="$(uname -m)"
44+
UNAME_S="$(uname -s)"
45+
46+
case "$UNAME_M" in
47+
x86_64) ARCH="x86_64" ;;
48+
aarch64|arm64) ARCH="aarch64" ;;
49+
*) echo "Unsupported architecture: $UNAME_M" >&2; exit 1 ;;
50+
esac
51+
52+
case "$UNAME_S" in
53+
Linux) OS="linux" ;;
54+
Darwin) OS="macos" ;;
55+
*) echo "Unsupported OS: $UNAME_S" >&2; exit 1 ;;
56+
esac
57+
58+
SPC_TARGET="${SPC_TARGET:-$ARCH-$OS}"
59+
60+
if [ "$SPC_LIBC" = "glibc" ]; then
61+
SPC_LIBC="gnu"
62+
fi
63+
64+
if [ "$SPC_TARGET_WAS_SET" -eq 0 ] && [ -z "$SPC_LIBC" ] && [ -z "$SPC_LIBC_VERSION" ]; then
65+
exec zig cc "${PARSED_ARGS[@]}"
66+
elif [ -z "$SPC_LIBC" ] && [ -z "$SPC_LIBC_VERSION" ]; then
67+
exec zig cc -target ${SPC_TARGET} "${PARSED_ARGS[@]}"
68+
else
69+
TARGET="${SPC_TARGET}-${SPC_LIBC}"
70+
[ -n "$SPC_LIBC_VERSION" ] && TARGET="${TARGET}.${SPC_LIBC_VERSION}"
71+
72+
output=$(zig cc -target "$TARGET" -L"$LIBDIR" -lstdc++ "${PARSED_ARGS[@]}" 2>&1)
73+
status=$?
74+
75+
filtered_output=$(echo "$output" | grep -v "version '.*' in target triple")
76+
77+
if [ $status -eq 0 ]; then
78+
echo "$filtered_output"
79+
exit 0
80+
fi
81+
82+
if echo "$output" | grep -q "version '.*' in target triple"; then
83+
TARGET_FALLBACK="${SPC_TARGET}-${SPC_LIBC}"
84+
output=$(zig cc -target "$TARGET_FALLBACK" -L"$LIBDIR" -lstdc++ "${PARSED_ARGS[@]}" 2>&1)
85+
status=$?
86+
87+
filtered_output=$(echo "$output" | grep -v "version '.*' in target triple")
88+
89+
if [ $status -eq 0 ]; then
90+
echo "$filtered_output"
91+
exit 0
92+
else
93+
exec zig cc -target "$TARGET_FALLBACK" "${PARSED_ARGS[@]}"
94+
fi
95+
else
96+
echo "$filtered_output"
97+
exec zig cc -target "$TARGET" "${PARSED_ARGS[@]}"
98+
fi
99+
fi

0 commit comments

Comments
 (0)