~eliasnaur/gio

2 2

Cross-Compiling for MacOS

Details
Message ID
<CAHe4cPmjd3RkZ9NsANc4Y7cdsSv9YUWkqwEEKZLhgVJ4emsNpA@mail.gmail.com>
DKIM signature
pass
Download raw message
Hi all,

I was recently able to cross-compile GIO programs for MacOS on a Linux
host system using osxcross, as suggested in the GIO website's install
section [1]. However, the process was not straightforward due to unex-
pected compilation errors in the Mac SDK. To make it easier for others
trying to do the same thing, I thought I'd document the process that
worked for me. Is there a better place to document this other than the
mailing list?

Here are the steps:

# Cross-Compiling Anvil for MacOS

Compiling Anvil for MacOS from Linux requires a C cross-compiler and the
MacOS SDK. One way to get this is using osxcross [2]. I've successfully
performed this using osxcross as of commit be6ffb3cbc6c0228614e-
be6a4b5cd2726339ecc9, and Apple XCode 15.4 on Debian 11.2 for a program
using gioui.org v0.6.0.

The steps needed to setup the cross compiler are those described in the
osxcross README, and in this case:

  * Download Xcode 15.4 from the Apple developer site as noted in the
    osxcross readme
  * Clone osxcross and cd into it
  * Installed the dependencies listed in the osxcross readme
  * Run `./tools/gen_sdk_package_pbzx.sh ../Xcode_15.4.xip`
  * Copy the resulting SDK package to the subdirectory tarballs/
  * Run `./build.sh`

Once that is completed, GIO programs can be compiled by setting environ-
ment variables and then using `go build`. The correct environment vari-
ables to set depend on the target architecture:

  * PATH: update PATH to include the target/bin subdirectory of the osx-
    cross repo so that the compilers are in the PATH
  * GOOS: set to `darwin`
  * GOARCH: one of `amd64` or `arm64`
  * CC: For `amd64` use `x86_64-apple-darwin23.5-cc`, and for `arm64`
    use `arm64-apple-darwin23.5-cc`
  * CXX: For `amd64` use `x86_64-apple-darwin23.5-c++`, and for `arm64`
    use `arm64-apple-darwin23.5-c++`
  * CGO_ENABLED: Set to 1 so that CGO is allowed for cross-compiling
  * CGO_CFLAGS: Set to:
    -D_Nullable_result= -DNS_FORMAT_ARGUMENT(A)= -DTARGET_OS_OSX

The CGO_CFLAGS setting is required to avoid compilation errors.

The defines '-D_Nullable_result=' and '-DNS_FORMAT_ARGUMENT(A)=' disable
function attributes that cause compilation errors in the Mac SDK. The
first attribute seems to only be applicable to Swift, and the second is
meant to catch errors related to printf-style format strings at compile
time, but is itself not handled properly in the compiler. Both are meant
to catch internal SDK compile-time errors which I assume would have
already been caught before the SDK was released, so are safe to disable.

The '-DTARGET_OS_OSX' defines the target OS to be OSX. Some important
SDK headers are not included unless the target os is defined thus caus-
ing compile errors, so we define it.

[1] https://gioui.org/doc/install#cross-compilation
[2] https://github.com/tpoechtrager/osxcross
Details
Message ID
<CAFcc3FSX2Lspp56acE_FUeKrzoGvoMCsvJnVPRcBODqt6p7uiw@mail.gmail.com>
In-Reply-To
<CAHe4cPmjd3RkZ9NsANc4Y7cdsSv9YUWkqwEEKZLhgVJ4emsNpA@mail.gmail.com> (view parent)
DKIM signature
pass
Download raw message
Hi Jeff,

This is a great resource. We can link to this post from the website
unless you'd prefer to put up a proper webpage for people to
reference.

Cheers,
Chris
Details
Message ID
<CAHe4cP=YxqbmUSfX2bGj6usBycnzUKAr56-7yrSbtnayzZTSbg@mail.gmail.com>
In-Reply-To
<CAFcc3FSX2Lspp56acE_FUeKrzoGvoMCsvJnVPRcBODqt6p7uiw@mail.gmail.com> (view parent)
DKIM signature
pass
Download raw message
Sure, I think linking to the post from the website is fine.
Reply to thread Export thread (mbox)