Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Some experiments with cross-compilation using Zig's C/C++ cross-compilation support. My efforts are focused on getting
file-events
to cross-compile, though they should be relatively simple to extend to the rest ofnative-platform
.I used Zig 0.13, the currently released version. I installed it with
brew install zig
. The recommendation is to use the nightly, though, as Zig is moving fast; perhaps that would take us further in some ways.Attempt 1: rewire Gradle
At first I tried getting Gradle to use Zig's commands to build by adding this under
model {}
:...and adding a couple bash scripts to invoke
zig cc
andzig c++
for those names. This got me to compile the code for the local platform on macOS, and I could run the tests and all. 🎉However, I couldn't get multi-platform working this way, as I could get Zig's linker to step in.
Attempt 2: use Zig build
I then went with another direction, and tried to re-phrase the native part of the build in Zig.
Cross-compiling on macOS to Linux
This worked all the way, and I can now cross-compile on macOS like this:
🎉
I haven't tried the library yet from Java, but I think it's highly likely that it would work.
Cross-compilation to
aarch64
works, too:Compiling locally on macOS
Compilation works well, and I got object files. However, the linker fails because it can't find theCoreFoundation
library. I'm not completely sure how to fix this.Compilation and linking works when not cross-compiling.
It also looks like if we wanted to cross-compile macOS on Linux, we'd need something like https://github.com/tpoechtrager/osxcrossd.Notes on cross-compiling on Linux: ziglang/zig#1349. We essentially need the macOS SDKs packaged up and available, as detailed in https://github.com/tpoechtrager/osxcross#packaging-the-sdk.
Another option is to do cross-compilation for all platforms on a Mac where the SDK is available.
Cross-compiling to Windows
This worked the worst. IIUC, MSVC C++ support is just not there yet in Zig (ziglang/zig#5312). We could use the GNU toolchain, which would require some slight adjustments to the code. I did not try that yet.Got this working with the GNU toolchain with a few small modifications to the code:
I could also cross-compile to
aarch64
:Shrinking
This is as simple as passing
-Doptimize=ReleaseSmall
on the command-line. Each platform/arch builds to a ~500 kB library that compresses down to around 140 kB withgzip
(the size we need to ship). This is somewhat larger for macOS where we currently ship ~80 kB gzip'd, but much smaller for Windows and Linux where we ship 3-400 kB for each platform/arch. Overall we'd save space, even after adding more platforms like Windows and Linux ARM.TODO
native-platform
(not justfile-events
)