Skip to content

Commit 62a0b47

Browse files
committed
Standardize build scripts: support cross-compilation via Zig on Mac and Windows
1 parent 1ec1e17 commit 62a0b47

3 files changed

Lines changed: 65 additions & 3 deletions

File tree

README.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,9 +127,16 @@ cd tinyMem
127127

128128
**Cross-Compilation (on Mac):**
129129
To build Windows or Linux binaries on a Mac, you need C cross-compilers:
130-
- **For Windows**: `brew install mingw-w64`
130+
- **For Windows (Intel/AMD)**: `brew install mingw-w64`
131+
- **For Windows (ARM64)**: `brew install zig`
131132
- **For Linux**: `brew install FiloSottile/musl-cross/musl-cross` (static) or `brew install zig`
132133

134+
**Cross-Compilation (on Windows):**
135+
To build macOS or Linux binaries on Windows, you need Zig:
136+
- `winget install zig.zig`
137+
138+
*Tip: `zig` is the recommended way to enable cross-compilation for all platforms with a single tool, regardless of whether you are on Mac or Windows.*
139+
133140
---
134141

135142
## 💻 Usage

build/build.bat

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,15 @@ REM ------------------------------------------------
3030
REM Safety checks for Release Mode
3131
REM ------------------------------------------------
3232
if "%IS_RELEASE%"=="true" (
33+
git status -s > temp_status.txt
34+
set /p STATUS=<temp_status.txt
35+
del temp_status.txt
36+
if not "!STATUS!"=="" (
37+
echo ❌ Error: Working directory is not clean. Commit or stash changes before releasing.
38+
git status -s
39+
exit /b 1
40+
)
41+
3342
where gh >nul 2>nul
3443
if errorlevel 1 (
3544
echo ❌ Error: GitHub CLI (gh) not installed. Required for releases.
@@ -88,6 +97,9 @@ if not "%TINYMEM_EXTRA_BUILD_TAGS%"=="" (
8897
set TAGS_FLAG=-tags "%BUILD_TAGS%"
8998
set LDFLAGS=-X github.com/daverage/tinymem/internal/version.Version=%VERSION%
9099

100+
REM Clear previous releases
101+
if exist "%OUT_DIR%\*" del /q "%OUT_DIR%\*"
102+
91103
echo → Windows AMD64
92104
set CGO_ENABLED=1
93105
set GOOS=windows
@@ -100,6 +112,44 @@ set GOOS=windows
100112
set GOARCH=arm64
101113
go build %TAGS_FLAG% -ldflags "%LDFLAGS%" -o "%OUT_DIR%\tinymem-windows-arm64.exe" .\cmd\tinymem
102114

115+
REM ------------------------------------------------
116+
REM Cross-compilation (if Zig is present)
117+
REM ------------------------------------------------
118+
where zig >nul 2>nul
119+
if not errorlevel 1 (
120+
echo → macOS ARM64 (Cross-compiling via zig cc)
121+
set CGO_ENABLED=1
122+
set GOOS=darwin
123+
set GOARCH=arm64
124+
set CC=zig cc -target aarch64-macos
125+
go build %TAGS_FLAG% -ldflags "%LDFLAGS%" -o "%OUT_DIR%\tinymem-darwin-arm64" .\cmd\tinymem
126+
127+
echo → macOS AMD64 (Cross-compiling via zig cc)
128+
set CGO_ENABLED=1
129+
set GOOS=darwin
130+
set GOARCH=amd64
131+
set CC=zig cc -target x86_64-macos
132+
go build %TAGS_FLAG% -ldflags "%LDFLAGS%" -o "%OUT_DIR%\tinymem-darwin-amd64" .\cmd\tinymem
133+
134+
echo → Linux AMD64 (Cross-compiling via zig cc)
135+
set CGO_ENABLED=1
136+
set GOOS=linux
137+
set GOARCH=amd64
138+
set CC=zig cc -target x86_64-linux-musl
139+
go build %TAGS_FLAG% -ldflags "%LDFLAGS%" -o "%OUT_DIR%\tinymem-linux-amd64" .\cmd\tinymem
140+
141+
echo → Linux ARM64 (Cross-compiling via zig cc)
142+
set CGO_ENABLED=1
143+
set GOOS=linux
144+
set GOARCH=arm64
145+
set CC=zig cc -target aarch64-linux-musl
146+
go build %TAGS_FLAG% -ldflags "%LDFLAGS%" -o "%OUT_DIR%\tinymem-linux-arm64" .\cmd\tinymem
147+
148+
set CC=
149+
) else (
150+
echo Skipping cross-compilation (zig not found). To enable: winget install zig.zig
151+
)
152+
103153
REM ------------------------------------------------
104154
REM Finalize Release
105155
REM ------------------------------------------------
@@ -154,4 +204,4 @@ if "%IS_RELEASE%"=="true" (
154204
echo Build complete. Artifacts in %OUT_DIR%
155205
)
156206

157-
exit /b 0
207+
exit /b 0

build/build.sh

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,11 +109,16 @@ fi
109109
if [[ "$OSTYPE" == msys* || "$OSTYPE" == cygwin* ]]; then
110110
build_target "Windows AMD64" windows amd64 "$OUT_DIR/tinymem-windows-amd64.exe"
111111
build_target "Windows ARM64" windows arm64 "$OUT_DIR/tinymem-windows-arm64.exe"
112+
elif command -v zig >/dev/null 2>&1; then
113+
echo "→ Windows (Cross-compiling via zig cc)"
114+
CC="zig cc -target x86_64-windows-gnu" build_target "Windows AMD64" windows amd64 "$OUT_DIR/tinymem-windows-amd64.exe"
115+
CC="zig cc -target aarch64-windows-gnu" build_target "Windows ARM64" windows arm64 "$OUT_DIR/tinymem-windows-arm64.exe"
112116
elif command -v x86_64-w64-mingw32-gcc >/dev/null 2>&1; then
113117
echo "→ Windows (Cross-compiling via mingw-w64)"
114118
CC=x86_64-w64-mingw32-gcc build_target "Windows AMD64" windows amd64 "$OUT_DIR/tinymem-windows-amd64.exe"
119+
echo "Skipping Windows ARM64 build (requires zig or llvm-mingw)."
115120
else
116-
echo "Skipping Windows builds (mingw-w64 not found). To enable on Mac: brew install mingw-w64"
121+
echo "Skipping Windows builds (mingw-w64 or zig not found). To enable on Mac: brew install mingw-w64 or brew install zig"
117122
fi
118123

119124

0 commit comments

Comments
 (0)