cargo-zigbuild icon indicating copy to clipboard operation
cargo-zigbuild copied to clipboard

Windows unable to build `aarch64-apple-darwin` due to syspath being specified

Open xobs opened this issue 8 months ago • 4 comments

Synopsis

Windows is unable to build for aarch64-apple-darwin due to two reasons:

  1. Programs don't seem to extract the SDK properly, necessitating a script to recreate directory symlinks
  2. For some reason, the --sysroot variable needs to be omitted on Windows

Extracting the SDK

To extract the SDK, use 7z.exe:

PS> &"C:\Program Files\7-Zip\7z.exe" x .\MacOSX11.3.tar

7-Zip 24.09 (x64) : Copyright (c) 1999-2024 Igor Pavlov : 2024-11-29

Scanning the drive for archives:
1 file, 557420032 bytes (532 MiB)

Extracting archive: .\MacOSX11.3.tar
--
Path = .\MacOSX11.3.tar
Type = tar
Physical Size = 557420032
Headers Size = 22302208
Code Page = UTF-8
Characteristics = POSIX PREFIX PAX linkpath mtime ASCII

Everything is Ok

Folders: 5392
Files: 33662
Size:       526332601
Compressed: 557420032
PS>

Next, use relink.ps1 (reproduced below) to update all symlinks. This is because 7zip extracts directories as ordinary symlinks, but they need to be created with mklink /d:

PS> .\relink.ps1 .\MacOSX11.3.sdk\
symbolic link created for E:\Code\MacOSX11.3.sdk\System\iOSSupport\System\Library\Frameworks\AddressBook.framework\Headers <<===>> Versions\Current\Headers
symbolic link created for E:\Code\MacOSX11.3.sdk\System\iOSSupport\System\Library\Frameworks\AddressBook.framework\Modules <<===>> Versions\Current\Modules
symbolic link created for E:\Code\MacOSX11.3.sdk\System\iOSSupport\System\Library\Frameworks\AddressBook.framework\Versions\Current <<===>> A
...
PS>

Finally, set SDKROOT to point to this path. E.g.:

PS> $env:SDKROOT="E:\Code\MacOSX11.3.sdk"

Running cargo-zigbuild

If you try to run cargo-zigbuild now, it will fail at the linking step. This is because paths will all have the SDKROOT prefixed to them. For example:

  = note: some arguments are omitted. use `--verbose` to show all linker arguments
  = note: warning: unable to open library directory 'E:\Code\MacOSX11.3.sdk\\Code\\probe-rs\\target\\aarch64-apple-darwin\\release\\build\\capstone-sys-12813f0a28c731fd\\out': FileNotFound
          warning: unable to open library directory 'E:\Code\MacOSX11.3.sdk\\Code\\probe-rs\\target\\aarch64-apple-darwin\\release\\build\\hidapi-aacb4e286d176179\\out': FileNotFound
          warning: unable to open library directory 'E:\Code\MacOSX11.3.sdk\Code\MacOSX11.3.sdk\usr\lib': FileNotFound
          warning: unable to open library directory 'E:\Code\MacOSX11.3.sdk\Users\Sean\AppData\Local\cargo-zigbuild\0.19.8\deps': FileNotFound
          error: unable to find dynamic system library 'iconv' using strategy 'paths_first'. searched paths: none
          error: unable to find dynamic system library 'charset' using strategy 'paths_first'. searched paths: none
          ␍
          E:\Code\probe-rs>"E:\Code\cargo-zigbuild\target\debug\cargo-zigbuild.exe" zig cc -- -g -target aarch64-macos-none @C:\Users\Sean\AppData\Local\Temp\rustcpL3ZYD\linker-arguments ␍


error: could not compile `probe-rs-tools` (bin "probe-rs") due to 1 previous error

Notice how it's trying to find files in E:\Code\MacOSX11.3.sdk\Code\MacOSX11.3.sdk\usr\lib -- the correct path should be E:\Code\MacOSX11.3.sdk\usr\lib

This is corrected with the following patch:

diff --git a/src/zig.rs b/src/zig.rs
index 825572f..d4161e4 100644
--- a/src/zig.rs
+++ b/src/zig.rs
@@ -388,12 +388,6 @@ impl Zig {
         zig_version: &semver::Version,
     ) -> Result<()> {
         let sdkroot = Self::macos_sdk_root();
-        if (zig_version.major, zig_version.minor) >= (0, 12) {
-            // Zig 0.12.0 requires passing `--sysroot`
-            if let Some(ref sdkroot) = sdkroot {
-                new_cmd_args.push(format!("--sysroot={}", sdkroot.display()));
-            }
-        }
         if let Some(ref sdkroot) = sdkroot {
             new_cmd_args.extend_from_slice(&[
                 "-isystem".to_string(),

zig cc Example

This can be demonstrated with zig cc directly. With a hello.c that does nothing but print Hello, world!:

PS> zig cc -g -o hello hello.c -target aarch64-macos-none -liconv "-isystem" "E:\Code\MacOSX11.3.sdk\usr\include" "-LE:\Code\MacOSX11.3.sdk\usr\lib" --sysroot=E:\Code\MacOSX11.3.sdk
warning: unable to open library directory 'E:\Code\MacOSX11.3.sdk\Code\MacOSX11.3.sdk\usr\lib': FileNotFound
error: unable to find dynamic system library 'iconv' using strategy 'paths_first'. searched paths: none
PS> zig cc -g -o hello hello.c -target aarch64-macos-none -liconv "-isystem" "E:\Code\MacOSX11.3.sdk\usr\include" "-LE:\Code\MacOSX11.3.sdk\usr\lib"

Zig Version

This is running with zig 0.14.0, and the same behaviour was observed with zig 0.13.0.

xobs avatar Mar 10 '25 07:03 xobs