@@ -48,13 +48,83 @@ fn appendUniqueCFlag(
4848 return merged ;
4949}
5050
51+ fn addMacosSdkAutoDownloadStep (
52+ b : * std.Build ,
53+ cache_dir : []const u8 ,
54+ sdk_dir : []const u8 ,
55+ ) * std.Build.Step.Run {
56+ const script =
57+ \\set -euo pipefail
58+ \\cache_dir="$1"
59+ \\sdk_dir="$2"
60+ \\metadata_url="$3"
61+ \\release_json="$cache_dir/latest-release.json"
62+ \\archive_path="$cache_dir/macos-sdk.tar.xz"
63+ \\staging_dir="$cache_dir/staging"
64+ \\
65+ \\if [ -d "$sdk_dir" ]; then
66+ \\ exit 0
67+ \\fi
68+ \\
69+ \\mkdir -p "$cache_dir"
70+ \\rm -rf "$staging_dir"
71+ \\trap 'rm -rf "$staging_dir"' EXIT
72+ \\
73+ \\echo "Downloading macOS SDK metadata from $metadata_url" >&2
74+ \\curl --fail --silent --show-error --location \
75+ \\ -H 'Accept: application/vnd.github+json' \
76+ \\ -H 'User-Agent: codex-manager-build' \
77+ \\ "$metadata_url" -o "$release_json"
78+ \\
79+ \\asset_match="$(grep -m 1 -Eo '"browser_download_url":"[^"]*MacOSX[^"]*sdk.tar.xz"' "$release_json" || true)"
80+ \\asset_url="${asset_match#\"browser_download_url\":\"}"
81+ \\asset_url="${asset_url%\"}"
82+ \\if [ -z "$asset_url" ]; then
83+ \\ echo "Could not find a MacOSX*.sdk.tar.xz asset in $metadata_url." >&2
84+ \\ echo "Set -Dmacos_sdk=<path> or MACOS_SDK_ROOT to use a local SDK instead." >&2
85+ \\ echo "Metadata excerpt:" >&2
86+ \\ sed -n '1,80p' "$release_json" >&2 || true
87+ \\ exit 1
88+ \\fi
89+ \\
90+ \\echo "Downloading macOS SDK archive from $asset_url" >&2
91+ \\curl --fail --silent --show-error --location \
92+ \\ -H 'User-Agent: codex-manager-build' \
93+ \\ "$asset_url" -o "$archive_path"
94+ \\
95+ \\mkdir -p "$staging_dir"
96+ \\tar -xf "$archive_path" -C "$staging_dir"
97+ \\
98+ \\found_sdk="$(find "$staging_dir" -type d -name 'MacOSX*.sdk' -print -quit)"
99+ \\if [ -z "$found_sdk" ]; then
100+ \\ echo "Downloaded archive did not contain a MacOSX*.sdk directory." >&2
101+ \\ exit 1
102+ \\fi
103+ \\
104+ \\rm -rf "$sdk_dir"
105+ \\mv "$found_sdk" "$sdk_dir"
106+ \\rm -f "$release_json" "$archive_path"
107+ ;
108+
109+ return b .addSystemCommand (&.{
110+ "bash" ,
111+ "-lc" ,
112+ script ,
113+ "download-macos-sdk" ,
114+ cache_dir ,
115+ sdk_dir ,
116+ "https://api.github.com/repos/joseluisq/macosx-sdks/releases/latest" ,
117+ });
118+ }
119+
51120fn addMatrixInstallCommand (
52121 b : * std.Build ,
53122 step : * std.Build.Step ,
54123 target_triple : []const u8 ,
55124 install_name : []const u8 ,
56125 maybe_sysroot : ? []const u8 ,
57126 frontend_prebuild_step : ? * std.Build.Step ,
127+ extra_dependency_step : ? * std.Build.Step ,
58128 optimize : std.builtin.OptimizeMode ,
59129 strip_symbols : bool ,
60130) void {
@@ -83,6 +153,9 @@ fn addMatrixInstallCommand(
83153 if (frontend_prebuild_step ) | frontend_step | {
84154 cmd .step .dependOn (frontend_step );
85155 }
156+ if (extra_dependency_step ) | dependency_step | {
157+ cmd .step .dependOn (dependency_step );
158+ }
86159 step .dependOn (& cmd .step );
87160}
88161
@@ -101,6 +174,19 @@ pub fn build(b: *std.Build) void {
101174 "macos_sdk" ,
102175 "Path to a macOS SDK for macOS cross-compilation (sysroot)." ,
103176 );
177+ const auto_download_macos_sdk = b .option (
178+ bool ,
179+ "auto_download_macos_sdk" ,
180+ "Automatically download a cached macOS SDK on Linux when build-all-targets needs one." ,
181+ ) orelse true ;
182+ const macos_sdk_cache_dir = absolutizePath (
183+ b ,
184+ b .option (
185+ []const u8 ,
186+ "macos_sdk_cache_dir" ,
187+ "Directory used to cache an auto-downloaded macOS SDK on Linux." ,
188+ ) orelse ".zig-cache/macos-sdk" ,
189+ );
104190
105191 var macos_sdk_path = resolveMacosSdkPath (b , macos_sdk_option );
106192 if (macos_sdk_path ) | sdk | {
@@ -270,6 +356,19 @@ pub fn build(b: *std.Build) void {
270356 "build-all-targets" ,
271357 "Compile release binaries for supported target matrix into zig-out/bin" ,
272358 );
359+ const auto_downloaded_macos_sdk_path = b .fmt ("{s}/sdk" , .{macos_sdk_cache_dir });
360+ var matrix_macos_sdk_path = macos_sdk_path ;
361+ var matrix_macos_sdk_step : ? * std.Build.Step = null ;
362+
363+ if (builtin .os .tag == .linux and matrix_macos_sdk_path == null and auto_download_macos_sdk ) {
364+ const download_macos_sdk = addMacosSdkAutoDownloadStep (
365+ b ,
366+ macos_sdk_cache_dir ,
367+ auto_downloaded_macos_sdk_path ,
368+ );
369+ matrix_macos_sdk_path = auto_downloaded_macos_sdk_path ;
370+ matrix_macos_sdk_step = & download_macos_sdk .step ;
371+ }
273372
274373 switch (builtin .os .tag ) {
275374 .linux = > {
@@ -280,6 +379,7 @@ pub fn build(b: *std.Build) void {
280379 "codex-manager-linux-x86_64" ,
281380 null ,
282381 frontend_build_step ,
382+ null ,
283383 optimize ,
284384 strip_symbols ,
285385 );
@@ -290,6 +390,7 @@ pub fn build(b: *std.Build) void {
290390 "codex-manager-linux-aarch64" ,
291391 null ,
292392 frontend_build_step ,
393+ null ,
293394 optimize ,
294395 strip_symbols ,
295396 );
@@ -300,6 +401,7 @@ pub fn build(b: *std.Build) void {
300401 "codex-manager-windows-x86_64.exe" ,
301402 null ,
302403 frontend_build_step ,
404+ null ,
303405 optimize ,
304406 strip_symbols ,
305407 );
@@ -310,11 +412,12 @@ pub fn build(b: *std.Build) void {
310412 "codex-manager-windows-aarch64.exe" ,
311413 null ,
312414 frontend_build_step ,
415+ null ,
313416 optimize ,
314417 strip_symbols ,
315418 );
316419
317- if (macos_sdk_path ) | macos_sdk_root | {
420+ if (matrix_macos_sdk_path ) | macos_sdk_root | {
318421 if (macos_sdk_root .len > 0 ) {
319422 addMatrixInstallCommand (
320423 b ,
@@ -323,6 +426,7 @@ pub fn build(b: *std.Build) void {
323426 "codex-manager-macos-x86_64" ,
324427 macos_sdk_root ,
325428 frontend_build_step ,
429+ matrix_macos_sdk_step ,
326430 optimize ,
327431 strip_symbols ,
328432 );
@@ -333,13 +437,14 @@ pub fn build(b: *std.Build) void {
333437 "codex-manager-macos-aarch64" ,
334438 macos_sdk_root ,
335439 frontend_build_step ,
440+ matrix_macos_sdk_step ,
336441 optimize ,
337442 strip_symbols ,
338443 );
339444 }
340445 } else {
341446 const missing_macos_sdk = b .addFail (
342- "build-all-targets on Linux requires a macOS SDK. Set -Dmacos_sdk=<path> or MACOS_SDK_ROOT ." ,
447+ "build-all-targets on Linux could not resolve a macOS SDK. Set -Dmacos_sdk=<path>, MACOS_SDK_ROOT, or leave auto-download enabled ." ,
343448 );
344449 build_all_targets_step .dependOn (& missing_macos_sdk .step );
345450 }
@@ -352,6 +457,7 @@ pub fn build(b: *std.Build) void {
352457 "codex-manager-macos-x86_64" ,
353458 null ,
354459 frontend_build_step ,
460+ null ,
355461 optimize ,
356462 strip_symbols ,
357463 );
@@ -362,6 +468,7 @@ pub fn build(b: *std.Build) void {
362468 "codex-manager-macos-aarch64" ,
363469 null ,
364470 frontend_build_step ,
471+ null ,
365472 optimize ,
366473 strip_symbols ,
367474 );
@@ -374,6 +481,7 @@ pub fn build(b: *std.Build) void {
374481 "codex-manager-windows-x86_64.exe" ,
375482 null ,
376483 frontend_build_step ,
484+ null ,
377485 optimize ,
378486 strip_symbols ,
379487 );
@@ -384,6 +492,7 @@ pub fn build(b: *std.Build) void {
384492 "codex-manager-windows-aarch64.exe" ,
385493 null ,
386494 frontend_build_step ,
495+ null ,
387496 optimize ,
388497 strip_symbols ,
389498 );
0 commit comments