Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft: Attempt at Mac OS compilation #184

Draft
wants to merge 27 commits into
base: main
Choose a base branch
from
Draft

Conversation

chrisirhc
Copy link

@chrisirhc chrisirhc commented Jun 16, 2024

Attempt to get a working macos branch.
This is a continuation of the work in https://github.com/motiejus/hermetic_cc_toolchain/tree/macos .

NOTE: I have no idea what I'm doing. I'm piecing together the previous threads about sysroot from the readme.
I have no idea what the zig or gcc options mean.

There are a number of hacks in this branch, so this is still WIP.

TODOs:

  • Commits are messy and aren't yet cleaned up for review
  • Identify what's hacky/needs to be removed
  • Verify the options
  • Test with more use cases

Fixes #10

motiejus and others added 7 commits June 13, 2024 22:51
    motiejus@mtwork:/code/bazel-zig-cc$ bazel build  --spawn_strategy=local  --platforms @zig_sdk//libc_aware/platform:macos_aarch64_sdk.13.1 test/c:which_libc
    INFO: Analyzed target //test/c:which_libc (0 packages loaded, 0 targets configured).
    INFO: Found 1 target...
    Target //test/c:which_libc up-to-date:
      bazel-bin/test/c/which_libc
    INFO: Elapsed time: 0.071s, Critical Path: 0.00s
    INFO: 1 process: 1 internal.
    INFO: Build completed successfully, 1 total action
    motiejus@mtwork:/code/bazel-zig-cc$ file bazel-bin/test/c/which_libc
    bazel-bin/test/c/which_libc: Mach-O 64-bit arm64 executable, flags:<NOUNDEFS|DYLDLINK|TWOLEVEL|PIE>
    motiejus@mtwork:/code/bazel-zig-cc$
But need to go back to see what hacks I added
 bazel build  --spawn_strategy=local  --platforms @zig_sdk//libc_aware/platform:macos_aarch64_sdk.14.2 test/c:which_libc --sandbox_debug -s

INFO: Build completed successfully, 4 total actions
Comment on lines 58 to 62
"external/hermetic_cc_toolchain~~toolchains~macos_sdk_{}".format(macos_sdk_version),
"-F",
"external/hermetic_cc_toolchain~~toolchains~macos_sdk_{}/Frameworks".format(macos_sdk_version),
"-L",
"external/hermetic_cc_toolchain~~toolchains~macos_sdk_{}/lib".format(macos_sdk_version),
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure why external/macos_sdk_… path doesn't work. Is it how the repository was defined? I'm not sure if this is idiomatic bazel to depend on an external path with ~s, I'd imagine these are 'non-public' paths.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These are bzlmod canonical repo paths, which are not guaranteed to be deterministic. You'll want to instead resolve them with some combination of Label and repository_ctx.path

Comment on lines +295 to +304
// HACK: if OS is macos, then force the ABI triple to "none".
if (!mem.eql(u8, got_os, "macos"))
return RunMode{ .cc = triple };

if (mem.eql(u8, got_arch, "aarch64"))
return RunMode{ .cc = "aarch64-macos-none" };

if (mem.eql(u8, got_arch, "x86_64"))
return RunMode{ .cc = "x86_64-macos-none"};

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think these hacks are to get macos-arm64-sdk.14.2 to be recognized by zig with an expected target ABI.

Comment on lines 65 to 86
"libc/darwin",
"libc/include/any-macos-any",
"libc/include/any-macos-any",
] + _INCLUDE_TAIL,
linkopts = ["-Wl,-headerpad_max_install_names"],
linkopts = macos_sdk_opts,
dynamic_library_linkopts = ["-Wl,-undefined=dynamic_lookup"],
supports_dynamic_linker = True,
cxx_builtin_include_directories = [
"external/hermetic_cc_toolchain~~toolchains~macos_sdk_{}/include".format(macos_sdk_version)
],
sdk_include_files = [
"@macos_sdk_{}//:Frameworks".format(macos_sdk_version),
# "@macos_sdk_{}//:usr_include".format(macos_sdk_version),
],
sdk_lib_files = ["@macos_sdk_{}//:usr_lib".format(macos_sdk_version)],
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I found these by trial and error and compiling against a target that expects Mac OS SDK ( the example in #10 (comment) )

@pcj
Copy link

pcj commented Jun 30, 2024

@chrisirhc nice work so far! Happy to help test this when ready.

@chrisirhc
Copy link
Author

chrisirhc commented Jul 11, 2024

Realized that hexops is an incomplete sysroot, so I went with unpacking a pkg.
Now I'm hitting:

external/go_sdk/pkg/tool/darwin_amd64/link: running external/hermetic_cc_toolchain~~toolchains~zig_sdk/tools/aarch64-macos-sdk.14.4/c++ failed: exit status 1
error: symbol _runtime.covctrs not attached to any (sub)section
    note: while parsing /var/folders/rg/8yzx68lj705cjn8q0pzxv59w0000gn/T/go-link-1625103760/go.o

link: error running subcommand external/go_sdk/pkg/tool/darwin_amd64/link: exit status 2

I believe it's the issue reported in ziglang/zig#20243

But I guess this is progress.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Support local OSX SDK
4 participants