ziglang / zig

General-purpose programming language and toolchain for maintaining robust, optimal, and reusable software.

Home Page:https://ziglang.org

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

ability to link against darwin frameworks (such as CoreFoundation) when cross compiling

kristate opened this issue · comments

The std library now has code that must be linked against CoreFoundation on macOS and iOS.

What is the best way to do so?

Here's what we need to accomplish:

  • Ability to link against darwin frameworks when cross-compiling, without any kind of SDK installed.
  • Only link against frameworks that are actually used. This is currently accomplished for dynamic libraries with the extern "libname" syntax.

Zig, and zig ld in particular, has the ability to link against frameworks when cross-compiling, however, a copy of the Darwin's sysroot (also called the SDK) is not bundled with Zig installation, therefore it is necessary to provide Zig valid paths to the sysroot and any search directories that may be required.

A typical invocation may look as follows,

$ zig cc -target aarch64-macos --sysroot=/home/kubkon/macos-SDK -I/usr/include -L/usr/lib -F/System/Library/Frameworks -framework CoreFoundation -o hello hello.c

@kubkon I suspect the answer to this will be "Copy it from a Mac" but...

Any hints on where to get a sysroot bundle for MacOS?

If the answer is copy it, do you know where I can get a list of what bits I need to grab?

@kubkon I suspect the answer to this will be "Copy it from a Mac" but...

Any hints on where to get a sysroot bundle for MacOS?

If the answer is copy it, do you know where I can get a list of what bits I need to grab?

You can get macOS 12 SDK from Hexops for example. If you are looking for an older SDK version, you can get it here. In general tho, if you have a Mac, you just need to grab whatever xcrun --sdk macosx --show-sdk-path. Finally, there is also this website I am aware of but downloading the SDKs may require an active Apple Developer account: xcodereleases.com.

I got zig to cross-compile targeting aarch64 macOS on a Ubuntu Jammy host.

sdk contains a macOS SDK. Instructions on doing this can be found on the osxcross project. I packaged the SDK, and then unzipped it into sdk

Dockerfile:

  FROM ubuntu:jammy
  COPY sdk /sdk
  RUN apt update
  RUN apt install -y wget xz-utils
  WORKDIR /workspace
  RUN wget -O zig.tar.xz https://ziglang.org/download/0.10.1/zig-linux-aarch64-0.10.1.tar.xz
  RUN tar -xf zig.tar.xz
  RUN rm zig.tar.xz
  RUN mv zig* zig
  ENV PATH=$PATH:/workspace/zig
  COPY samples samples
  RUN zig cc -target aarch64-macos --sysroot=/sdk -I/sdk/usr/include -L/sdk/usr/lib -F/sdk/System/Library/Frameworks -framework CoreFoundation -o hello-c samples/hello.c