Skip to content

Commit f785425

Browse files
committed
Support zig compiler
1 parent 2294510 commit f785425

File tree

3 files changed

+154
-0
lines changed

3 files changed

+154
-0
lines changed

.github/workflows/zig.yml

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
name: Zig compiler
2+
on:
3+
push:
4+
branches: [ '*' ]
5+
pull_request:
6+
branches: [ '*' ]
7+
concurrency:
8+
group: ${{ github.workflow }}-${{ github.ref_name }}
9+
cancel-in-progress: true
10+
jobs:
11+
zig:
12+
if: github.repository_owner == 'aws'
13+
runs-on: ${{ matrix.config.host }}
14+
env:
15+
CFLAGS: "-fno-sanitize=all"
16+
CXXFLAGS: "-fno-sanitize=all"
17+
strategy:
18+
fail-fast: false
19+
matrix:
20+
config:
21+
- host: windows-latest
22+
target_arch: x86_64
23+
target_system: Windows
24+
- host: ubuntu-latest
25+
target_arch: x86_64
26+
target_system: Linux
27+
- host: macos-latest
28+
target_arch: aarch64
29+
target_system: Darwin
30+
steps:
31+
- name: Install NASM
32+
uses: ilammy/[email protected]
33+
- name: Checkout
34+
uses: actions/checkout@v4
35+
- name: Install ninja-build tool
36+
uses: seanmiddleditch/gha-setup-ninja@v4
37+
- uses: actions/setup-python@v5
38+
with:
39+
python-version: '3.13'
40+
- uses: actions/setup-go@v4
41+
with:
42+
go-version: '>= 1.18'
43+
- name: Install zigcc
44+
uses: jiacai2050/my-works@main
45+
with:
46+
zig-version: 0.15.1
47+
- name: Locate zig not on Windows
48+
if: matrix.os.name != 'windows-latest'
49+
shell: bash
50+
run: |
51+
echo "ZIGCC=${PWD}/util/zig-cc" >> $GITHUB_ENV
52+
echo "ZIGCXX=${PWD}/util/zig-c++" >> $GITHUB_ENV
53+
- name: Locate zig on Windows
54+
if: matrix.os.name == 'windows-latest'
55+
shell: bash
56+
run: |
57+
ZIGCC="python3 $(cygpath -m $(which zigcc))"
58+
ZIGCXX="python3 $(cygpath -m $(which zigcxx))"
59+
echo "ZIGCC=${ZIGCC}" >> $GITHUB_ENV
60+
echo "ZIGCXX=${ZIGCXX}" >> $GITHUB_ENV
61+
- name: Create toolchain
62+
shell: bash
63+
run: |
64+
cat <<EOF > ./toolchain.cmake
65+
set(CMAKE_C_COMPILER ${ZIGCC})
66+
set(CMAKE_SYSTEM_NAME ${{ matrix.config.target_system }})
67+
set(CMAKE_SYSTEM_PROCESSOR ${{ matrix.config.target_arch }})
68+
set(CMAKE_CXX_COMPILER ${ZIGCXX})
69+
set(CMAKE_ASM_COMPILER ${ZIGCC})
70+
set(CMAKE_VERBOSE_MAKEFILE ON)
71+
set(CMAKE_MESSAGE_LOG_LEVEL DEBUG)
72+
EOF
73+
- name: Setup CMake
74+
shell: bash
75+
run: |
76+
printenv | sort
77+
which zigcc
78+
which zigcxx
79+
cat ./toolchain.cmake
80+
cmake '.' -B ./build -G Ninja -DCMAKE_TOOLCHAIN_FILE=./toolchain.cmake -DCMAKE_BUILD_TYPE=Release
81+
- name: Build Project
82+
shell: bash
83+
run: |
84+
cmake --build ./build --target run_tests --verbose

util/zig-c++

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#!/usr/bin/env bash
2+
# Zig C++ compiler wrapper
3+
4+
# Check if zig is available
5+
if ! command -v zig &> /dev/null; then
6+
echo "Error: zig compiler not found in PATH" >&2
7+
echo "Please install zig from https://ziglang.org/download/" >&2
8+
exit 1
9+
fi
10+
11+
# Function to map problematic architecture flags to zig equivalents
12+
filter_problematic_flags() {
13+
local args=()
14+
15+
for arg in "$@"; do
16+
case "$arg" in
17+
-march=armv8.4-a+sha3)
18+
# Replace the specific problematic flag with a compatible one
19+
args+=("-mcpu=generic" "-mattr=+v8.4a,+sha3")
20+
;;
21+
*)
22+
# Keep all other arguments including mcpu, mtune, and other march flags
23+
args+=("$arg")
24+
;;
25+
esac
26+
done
27+
28+
printf '%s\n' "${args[@]}"
29+
}
30+
31+
# Process arguments to filter only problematic architecture flags
32+
filtered_args=($(filter_problematic_flags "$@"))
33+
34+
# Execute zig c++ with sanitizers disabled by default and filtered arguments
35+
exec zig c++ -fno-sanitize=undefined -fno-sanitize=address -fno-sanitize=memory "${filtered_args[@]}"

util/zig-cc

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#!/usr/bin/env bash
2+
# Zig C compiler wrapper
3+
4+
# Check if zig is available
5+
if ! command -v zig &> /dev/null; then
6+
echo "Error: zig compiler not found in PATH" >&2
7+
echo "Please install zig from https://ziglang.org/download/" >&2
8+
exit 1
9+
fi
10+
11+
# Function to map problematic architecture flags to zig equivalents
12+
filter_problematic_flags() {
13+
local args=()
14+
15+
for arg in "$@"; do
16+
case "$arg" in
17+
-march=armv8.4-a+sha3)
18+
# Replace the specific problematic flag with a compatible one
19+
args+=("-mcpu=generic" "-mattr=+v8.4a,+sha3")
20+
;;
21+
*)
22+
# Keep all other arguments
23+
args+=("$arg")
24+
;;
25+
esac
26+
done
27+
28+
printf '%s\n' "${args[@]}"
29+
}
30+
31+
# Process arguments to filter only problematic architecture flags
32+
filtered_args=($(filter_problematic_flags "$@"))
33+
34+
# Execute zig cc with sanitizers disabled by default and filtered arguments
35+
exec zig cc -fno-sanitize=undefined -fno-sanitize=address -fno-sanitize=memory "${filtered_args[@]}"

0 commit comments

Comments
 (0)