From 77e5d66f493ec2fa07ecc7897fb7b839b80833b6 Mon Sep 17 00:00:00 2001 From: Daniel Muller Date: Mon, 10 Feb 2025 10:45:00 -0700 Subject: [PATCH 1/2] feat: Support cross compiles from linux to macos Upstreaming an internal patch we've been using for cross compiling the aspect CLI from a linux machine to mac. We've been using a slightly different version of this for quite some time now so I cleaned up the logic significatly so it could be upstreamed. --- toolchain/cc_toolchain_config.bzl | 18 +++++++++++++----- toolchain/internal/common.bzl | 1 + 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/toolchain/cc_toolchain_config.bzl b/toolchain/cc_toolchain_config.bzl index 9dbb41db4..cc2dba4cd 100644 --- a/toolchain/cc_toolchain_config.bzl +++ b/toolchain/cc_toolchain_config.bzl @@ -168,11 +168,17 @@ def cc_toolchain_config( archive_flags = [] # Linker flags: - if exec_os == "darwin" and not is_xcompile: - # lld is experimental for Mach-O, so we use the native ld64 linker. - # TODO: How do we cross-compile from Linux to Darwin? - use_lld = False + ld = "ld.lld" + if target_os == "darwin": + use_lld = True + + ld = "ld64.lld" + ld_path = toolchain_path_prefix + "/bin/" + ld + compile_flags.append("-mmacosx-version-min=12.0") link_flags.extend([ + "-mmacosx-version-min=12.0", + "-Wl,-platform_version,macos,12.0,12.0", + "--ld-path=" + ld_path, "-headerpad_max_install_names", "-fobjc-link-runtime", ]) @@ -195,7 +201,9 @@ def cc_toolchain_config( # not an option because it is not a cross-linker, so lld is the # only option. use_lld = True + ld_path = toolchain_path_prefix + "/bin/" + ld link_flags.extend([ + "--ld-path=" + ld_path, "-fuse-ld=lld", "-Wl,--build-id=md5", "-Wl,--hash-style=gnu", @@ -323,7 +331,7 @@ def cc_toolchain_config( "dwp": tools_path_prefix + "llvm-dwp", "gcc": wrapper_bin_prefix + "cc_wrapper.sh", "gcov": tools_path_prefix + "llvm-profdata", - "ld": tools_path_prefix + "ld.lld" if use_lld else "/usr/bin/ld", + "ld": tools_path_prefix + ld if use_lld else "/usr/bin/ld", "llvm-cov": tools_path_prefix + "llvm-cov", "llvm-profdata": tools_path_prefix + "llvm-profdata", "nm": tools_path_prefix + "llvm-nm", diff --git a/toolchain/internal/common.bzl b/toolchain/internal/common.bzl index 1feae9850..67733806d 100644 --- a/toolchain/internal/common.bzl +++ b/toolchain/internal/common.bzl @@ -28,6 +28,7 @@ _toolchain_tools = { for name in [ "clang-cpp", "ld.lld", + "ld64.lld", "llvm-ar", "llvm-dwp", "llvm-profdata", From 830c14bdceec1656557eb21a547e6ae0ea8e21f3 Mon Sep 17 00:00:00 2001 From: Daniel Muller Date: Mon, 10 Feb 2025 11:40:07 -0700 Subject: [PATCH 2/2] Remove acosx-version-min While this was part of our original patch, it really only helps give warning messages for sysroot version mismatches. Remove for now since its not really important to the core logic of the change. --- toolchain/cc_toolchain_config.bzl | 3 --- 1 file changed, 3 deletions(-) diff --git a/toolchain/cc_toolchain_config.bzl b/toolchain/cc_toolchain_config.bzl index cc2dba4cd..be7161fca 100644 --- a/toolchain/cc_toolchain_config.bzl +++ b/toolchain/cc_toolchain_config.bzl @@ -174,10 +174,7 @@ def cc_toolchain_config( ld = "ld64.lld" ld_path = toolchain_path_prefix + "/bin/" + ld - compile_flags.append("-mmacosx-version-min=12.0") link_flags.extend([ - "-mmacosx-version-min=12.0", - "-Wl,-platform_version,macos,12.0,12.0", "--ld-path=" + ld_path, "-headerpad_max_install_names", "-fobjc-link-runtime",