From e93470227b54243de33695d2042854ec85b27309 Mon Sep 17 00:00:00 2001 From: Fabian Meumertzheim Date: Thu, 16 May 2024 11:02:21 +0200 Subject: [PATCH 1/4] Mark module extension as reproducible This removes an unnecessary lockfile entry. --- MODULE.bazel | 2 +- crosstool/setup.bzl | 5 +++-- lib/repositories.bzl | 6 +++--- 3 files changed, 7 insertions(+), 6 deletions(-) diff --git a/MODULE.bazel b/MODULE.bazel index 20dea49..1f5334d 100644 --- a/MODULE.bazel +++ b/MODULE.bazel @@ -7,7 +7,7 @@ module( ) bazel_dep(name = "bazel_features", version = "1.10.0") -bazel_dep(name = "bazel_skylib", version = "1.3.0") +bazel_dep(name = "bazel_skylib", version = "1.6.1") bazel_dep(name = "platforms", version = "0.0.9") bazel_dep( diff --git a/crosstool/setup.bzl b/crosstool/setup.bzl index f3f7090..51ddeef 100644 --- a/crosstool/setup.bzl +++ b/crosstool/setup.bzl @@ -1,5 +1,6 @@ """Configure the Apple CC toolchain""" +load("@bazel_skylib//lib:modules.bzl", "modules") load("//crosstool:osx_cc_configure.bzl", "configure_osx_toolchain") _DISABLE_ENV_VAR = "BAZEL_NO_APPLE_CPP_TOOLCHAIN" @@ -77,8 +78,8 @@ def apple_cc_configure(): "@local_config_apple_cc_toolchains//:all", ) -def _apple_cc_configure_extension_impl(_): +def _apple_cc_configure_extension_impl(): _apple_cc_autoconf_toolchains(name = "local_config_apple_cc_toolchains") _apple_cc_autoconf(name = "local_config_apple_cc") -apple_cc_configure_extension = module_extension(implementation = _apple_cc_configure_extension_impl) +apple_cc_configure_extension = modules.as_extension(_apple_cc_configure_extension_impl) diff --git a/lib/repositories.bzl b/lib/repositories.bzl index 9f8de07..53136d4 100644 --- a/lib/repositories.bzl +++ b/lib/repositories.bzl @@ -40,10 +40,10 @@ def apple_support_dependencies(): http_archive, name = "bazel_skylib", urls = [ - "https://github.com/bazelbuild/bazel-skylib/releases/download/1.3.0/bazel-skylib-1.3.0.tar.gz", - "https://mirror.bazel.build/github.com/bazelbuild/bazel-skylib/releases/download/1.3.0/bazel-skylib-1.3.0.tar.gz", + "https://mirror.bazel.build/github.com/bazelbuild/bazel-skylib/releases/download/1.6.1/bazel-skylib-1.6.1.tar.gz", + "https://github.com/bazelbuild/bazel-skylib/releases/download/1.6.1/bazel-skylib-1.6.1.tar.gz", ], - sha256 = "74d544d96f4a5bb630d465ca8bbcfe231e3594e5aae57e1edbf17a6eb3ca2506", + sha256 = "9f38886a40548c6e96c106b752f242130ee11aaa068a56ba7e56f4511f33e4f2", ) _maybe( From 859bd659df647824ccaaf3d1e3ffc8b8c2729905 Mon Sep 17 00:00:00 2001 From: Fabian Meumertzheim Date: Thu, 16 May 2024 11:40:56 +0200 Subject: [PATCH 2/4] Restructure to avoid WORKSPACE loads --- MODULE.bazel | 2 +- crosstool/apple_cc_configure_extension.bzl | 10 ++++++++++ crosstool/setup.bzl | 15 ++++----------- 3 files changed, 15 insertions(+), 12 deletions(-) create mode 100644 crosstool/apple_cc_configure_extension.bzl diff --git a/MODULE.bazel b/MODULE.bazel index 1f5334d..b4ed672 100644 --- a/MODULE.bazel +++ b/MODULE.bazel @@ -17,7 +17,7 @@ bazel_dep( repo_name = "io_bazel_stardoc", ) -apple_cc_configure = use_extension("//crosstool:setup.bzl", "apple_cc_configure_extension") +apple_cc_configure = use_extension("//crosstool:apple_cc_configure_extension.bzl", "apple_cc_configure_extension") use_repo(apple_cc_configure, "local_config_apple_cc", "local_config_apple_cc_toolchains") register_toolchains("@local_config_apple_cc_toolchains//:all") diff --git a/crosstool/apple_cc_configure_extension.bzl b/crosstool/apple_cc_configure_extension.bzl new file mode 100644 index 0000000..8caeace --- /dev/null +++ b/crosstool/apple_cc_configure_extension.bzl @@ -0,0 +1,10 @@ +"""Extension configuring the C++ toolchain on macOS.""" + +load("@bazel_skylib//lib:modules.bzl", "modules") +load(":setup.bzl", "apple_cc_autoconf", "apple_cc_autoconf_toolchains") + +def _apple_cc_configure_extension_impl(): + apple_cc_autoconf_toolchains(name = "local_config_apple_cc_toolchains") + apple_cc_autoconf(name = "local_config_apple_cc") + +apple_cc_configure_extension = modules.as_extension(_apple_cc_configure_extension_impl) diff --git a/crosstool/setup.bzl b/crosstool/setup.bzl index 51ddeef..4c5f4ed 100644 --- a/crosstool/setup.bzl +++ b/crosstool/setup.bzl @@ -1,6 +1,5 @@ """Configure the Apple CC toolchain""" -load("@bazel_skylib//lib:modules.bzl", "modules") load("//crosstool:osx_cc_configure.bzl", "configure_osx_toolchain") _DISABLE_ENV_VAR = "BAZEL_NO_APPLE_CPP_TOOLCHAIN" @@ -28,7 +27,7 @@ def _apple_cc_autoconf_toolchains_impl(repository_ctx): else: repository_ctx.file("BUILD", "# Apple CC toolchain autoconfiguration was disabled because you're not running on macOS") -_apple_cc_autoconf_toolchains = repository_rule( +apple_cc_autoconf_toolchains = repository_rule( environ = [ _DISABLE_ENV_VAR, _OLD_DISABLE_ENV_VAR, @@ -53,7 +52,7 @@ def _apple_cc_autoconf_impl(repository_ctx): else: repository_ctx.file("BUILD", "# Apple CC autoconfiguration was disabled because you're not on macOS") -_apple_cc_autoconf = repository_rule( +apple_cc_autoconf = repository_rule( environ = [ _DISABLE_ENV_VAR, _OLD_DISABLE_ENV_VAR, @@ -71,15 +70,9 @@ _apple_cc_autoconf = repository_rule( # buildifier: disable=unnamed-macro def apple_cc_configure(): - _apple_cc_autoconf_toolchains(name = "local_config_apple_cc_toolchains") - _apple_cc_autoconf(name = "local_config_apple_cc") + apple_cc_autoconf_toolchains(name = "local_config_apple_cc_toolchains") + apple_cc_autoconf(name = "local_config_apple_cc") native.register_toolchains( # Use register_toolchain's target pattern expansion to register all toolchains in the package. "@local_config_apple_cc_toolchains//:all", ) - -def _apple_cc_configure_extension_impl(): - _apple_cc_autoconf_toolchains(name = "local_config_apple_cc_toolchains") - _apple_cc_autoconf(name = "local_config_apple_cc") - -apple_cc_configure_extension = modules.as_extension(_apple_cc_configure_extension_impl) From a775661c1827180223c9f293d05e5d458b7f3daf Mon Sep 17 00:00:00 2001 From: Fabian Meumertzheim Date: Thu, 16 May 2024 14:30:32 +0200 Subject: [PATCH 3/4] Extract internal symbols into separate file --- crosstool/apple_cc_configure_extension.bzl | 2 +- crosstool/setup.bzl | 68 +-------------------- crosstool/setup_internal.bzl | 71 ++++++++++++++++++++++ 3 files changed, 73 insertions(+), 68 deletions(-) create mode 100644 crosstool/setup_internal.bzl diff --git a/crosstool/apple_cc_configure_extension.bzl b/crosstool/apple_cc_configure_extension.bzl index 8caeace..7b588c5 100644 --- a/crosstool/apple_cc_configure_extension.bzl +++ b/crosstool/apple_cc_configure_extension.bzl @@ -1,7 +1,7 @@ """Extension configuring the C++ toolchain on macOS.""" load("@bazel_skylib//lib:modules.bzl", "modules") -load(":setup.bzl", "apple_cc_autoconf", "apple_cc_autoconf_toolchains") +load(":setup_internal.bzl", "apple_cc_autoconf", "apple_cc_autoconf_toolchains") def _apple_cc_configure_extension_impl(): apple_cc_autoconf_toolchains(name = "local_config_apple_cc_toolchains") diff --git a/crosstool/setup.bzl b/crosstool/setup.bzl index 4c5f4ed..9be00bb 100644 --- a/crosstool/setup.bzl +++ b/crosstool/setup.bzl @@ -1,72 +1,6 @@ """Configure the Apple CC toolchain""" -load("//crosstool:osx_cc_configure.bzl", "configure_osx_toolchain") - -_DISABLE_ENV_VAR = "BAZEL_NO_APPLE_CPP_TOOLCHAIN" -_OLD_DISABLE_ENV_VAR = "BAZEL_USE_CPP_ONLY_TOOLCHAIN" - -def _apple_cc_autoconf_toolchains_impl(repository_ctx): - """Generate BUILD file with 'toolchain' targets for the local host C++ toolchain. - - Args: - repository_ctx: repository context - """ - env = repository_ctx.os.environ - should_disable = _DISABLE_ENV_VAR in env and env[_DISABLE_ENV_VAR] == "1" - old_should_disable = _OLD_DISABLE_ENV_VAR in env and env[_OLD_DISABLE_ENV_VAR] == "1" - - if should_disable or old_should_disable: - repository_ctx.file("BUILD", "# Apple CC toolchain autoconfiguration was disabled by {} env variable.".format( - _DISABLE_ENV_VAR if should_disable else _OLD_DISABLE_ENV_VAR, - )) - elif repository_ctx.os.name.startswith("mac os"): - repository_ctx.symlink( - repository_ctx.path(Label("@build_bazel_apple_support//crosstool:BUILD.toolchains")), - "BUILD", - ) - else: - repository_ctx.file("BUILD", "# Apple CC toolchain autoconfiguration was disabled because you're not running on macOS") - -apple_cc_autoconf_toolchains = repository_rule( - environ = [ - _DISABLE_ENV_VAR, - _OLD_DISABLE_ENV_VAR, - ], - implementation = _apple_cc_autoconf_toolchains_impl, - configure = True, -) - -def _apple_cc_autoconf_impl(repository_ctx): - env = repository_ctx.os.environ - should_disable = _DISABLE_ENV_VAR in env and env[_DISABLE_ENV_VAR] == "1" - old_should_disable = _OLD_DISABLE_ENV_VAR in env and env[_OLD_DISABLE_ENV_VAR] == "1" - - if should_disable or old_should_disable: - repository_ctx.file("BUILD", "# Apple CC autoconfiguration was disabled by {} env variable.".format( - _DISABLE_ENV_VAR if should_disable else _OLD_DISABLE_ENV_VAR, - )) - elif repository_ctx.os.name.startswith("mac os"): - success, error = configure_osx_toolchain(repository_ctx) - if not success: - fail("Failed to configure Apple CC toolchain, if you only have the command line tools installed and not Xcode, you cannot use this toolchain. You should either remove it or temporarily set '{}=1' in the environment: {}".format(_DISABLE_ENV_VAR, error)) - else: - repository_ctx.file("BUILD", "# Apple CC autoconfiguration was disabled because you're not on macOS") - -apple_cc_autoconf = repository_rule( - environ = [ - _DISABLE_ENV_VAR, - _OLD_DISABLE_ENV_VAR, - "APPLE_SUPPORT_LAYERING_CHECK_BETA", - "BAZEL_ALLOW_NON_APPLICATIONS_XCODE", # Signals to configure_osx_toolchain that some Xcodes may live outside of /Applications and we need to probe further when detecting/configuring them. - "DEVELOPER_DIR", # Used for making sure we use the right Xcode for compiling toolchain binaries - "GCOV", # TODO: Remove this - "USE_CLANG_CL", # Kept as a hack for those who rely on this invaliding the toolchain - "USER", # Used to allow paths for custom toolchains to be used by C* compiles - "XCODE_VERSION", # Force re-computing the toolchain by including the current Xcode version info in an env var - ], - implementation = _apple_cc_autoconf_impl, - configure = True, -) +load(":setup_internal.bzl", "apple_cc_autoconf", "apple_cc_autoconf_toolchains") # buildifier: disable=unnamed-macro def apple_cc_configure(): diff --git a/crosstool/setup_internal.bzl b/crosstool/setup_internal.bzl new file mode 100644 index 0000000..c6c6755 --- /dev/null +++ b/crosstool/setup_internal.bzl @@ -0,0 +1,71 @@ +"""Configure the Apple CC toolchain (internal part)""" + +load("//crosstool:osx_cc_configure.bzl", "configure_osx_toolchain") + +visibility("private") + +_DISABLE_ENV_VAR = "BAZEL_NO_APPLE_CPP_TOOLCHAIN" +_OLD_DISABLE_ENV_VAR = "BAZEL_USE_CPP_ONLY_TOOLCHAIN" + +def _apple_cc_autoconf_toolchains_impl(repository_ctx): + """Generate BUILD file with 'toolchain' targets for the local host C++ toolchain. + + Args: + repository_ctx: repository context + """ + env = repository_ctx.os.environ + should_disable = _DISABLE_ENV_VAR in env and env[_DISABLE_ENV_VAR] == "1" + old_should_disable = _OLD_DISABLE_ENV_VAR in env and env[_OLD_DISABLE_ENV_VAR] == "1" + + if should_disable or old_should_disable: + repository_ctx.file("BUILD", "# Apple CC toolchain autoconfiguration was disabled by {} env variable.".format( + _DISABLE_ENV_VAR if should_disable else _OLD_DISABLE_ENV_VAR, + )) + elif repository_ctx.os.name.startswith("mac os"): + repository_ctx.symlink( + repository_ctx.path(Label("@build_bazel_apple_support//crosstool:BUILD.toolchains")), + "BUILD", + ) + else: + repository_ctx.file("BUILD", "# Apple CC toolchain autoconfiguration was disabled because you're not running on macOS") + +apple_cc_autoconf_toolchains = repository_rule( + environ = [ + _DISABLE_ENV_VAR, + _OLD_DISABLE_ENV_VAR, + ], + implementation = _apple_cc_autoconf_toolchains_impl, + configure = True, +) + +def _apple_cc_autoconf_impl(repository_ctx): + env = repository_ctx.os.environ + should_disable = _DISABLE_ENV_VAR in env and env[_DISABLE_ENV_VAR] == "1" + old_should_disable = _OLD_DISABLE_ENV_VAR in env and env[_OLD_DISABLE_ENV_VAR] == "1" + + if should_disable or old_should_disable: + repository_ctx.file("BUILD", "# Apple CC autoconfiguration was disabled by {} env variable.".format( + _DISABLE_ENV_VAR if should_disable else _OLD_DISABLE_ENV_VAR, + )) + elif repository_ctx.os.name.startswith("mac os"): + success, error = configure_osx_toolchain(repository_ctx) + if not success: + fail("Failed to configure Apple CC toolchain, if you only have the command line tools installed and not Xcode, you cannot use this toolchain. You should either remove it or temporarily set '{}=1' in the environment: {}".format(_DISABLE_ENV_VAR, error)) + else: + repository_ctx.file("BUILD", "# Apple CC autoconfiguration was disabled because you're not on macOS") + +apple_cc_autoconf = repository_rule( + environ = [ + _DISABLE_ENV_VAR, + _OLD_DISABLE_ENV_VAR, + "APPLE_SUPPORT_LAYERING_CHECK_BETA", + "BAZEL_ALLOW_NON_APPLICATIONS_XCODE", # Signals to configure_osx_toolchain that some Xcodes may live outside of /Applications and we need to probe further when detecting/configuring them. + "DEVELOPER_DIR", # Used for making sure we use the right Xcode for compiling toolchain binaries + "GCOV", # TODO: Remove this + "USE_CLANG_CL", # Kept as a hack for those who rely on this invaliding the toolchain + "USER", # Used to allow paths for custom toolchains to be used by C* compiles + "XCODE_VERSION", # Force re-computing the toolchain by including the current Xcode version info in an env var + ], + implementation = _apple_cc_autoconf_impl, + configure = True, +) From 8ab815bee71cc5e5f102ae67273f6a2b5e77876c Mon Sep 17 00:00:00 2001 From: Fabian Meumertzheim Date: Thu, 16 May 2024 21:06:43 +0200 Subject: [PATCH 4/4] Move to internal package --- crosstool/apple_cc_configure_extension.bzl | 2 +- crosstool/internal/BUILD.bazel | 0 crosstool/{setup_internal.bzl => internal/setup.bzl} | 2 -- crosstool/setup.bzl | 2 +- 4 files changed, 2 insertions(+), 4 deletions(-) create mode 100644 crosstool/internal/BUILD.bazel rename crosstool/{setup_internal.bzl => internal/setup.bzl} (99%) diff --git a/crosstool/apple_cc_configure_extension.bzl b/crosstool/apple_cc_configure_extension.bzl index 7b588c5..b682024 100644 --- a/crosstool/apple_cc_configure_extension.bzl +++ b/crosstool/apple_cc_configure_extension.bzl @@ -1,7 +1,7 @@ """Extension configuring the C++ toolchain on macOS.""" load("@bazel_skylib//lib:modules.bzl", "modules") -load(":setup_internal.bzl", "apple_cc_autoconf", "apple_cc_autoconf_toolchains") +load("//crosstool/internal:setup.bzl", "apple_cc_autoconf", "apple_cc_autoconf_toolchains") def _apple_cc_configure_extension_impl(): apple_cc_autoconf_toolchains(name = "local_config_apple_cc_toolchains") diff --git a/crosstool/internal/BUILD.bazel b/crosstool/internal/BUILD.bazel new file mode 100644 index 0000000..e69de29 diff --git a/crosstool/setup_internal.bzl b/crosstool/internal/setup.bzl similarity index 99% rename from crosstool/setup_internal.bzl rename to crosstool/internal/setup.bzl index c6c6755..3369e02 100644 --- a/crosstool/setup_internal.bzl +++ b/crosstool/internal/setup.bzl @@ -2,8 +2,6 @@ load("//crosstool:osx_cc_configure.bzl", "configure_osx_toolchain") -visibility("private") - _DISABLE_ENV_VAR = "BAZEL_NO_APPLE_CPP_TOOLCHAIN" _OLD_DISABLE_ENV_VAR = "BAZEL_USE_CPP_ONLY_TOOLCHAIN" diff --git a/crosstool/setup.bzl b/crosstool/setup.bzl index 9be00bb..3d1065d 100644 --- a/crosstool/setup.bzl +++ b/crosstool/setup.bzl @@ -1,6 +1,6 @@ """Configure the Apple CC toolchain""" -load(":setup_internal.bzl", "apple_cc_autoconf", "apple_cc_autoconf_toolchains") +load("//crosstool/internal:setup.bzl", "apple_cc_autoconf", "apple_cc_autoconf_toolchains") # buildifier: disable=unnamed-macro def apple_cc_configure():