diff --git a/MODULE.bazel b/MODULE.bazel index 20dea49..b4ed672 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( @@ -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..b682024 --- /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("//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") + apple_cc_autoconf(name = "local_config_apple_cc") + +apple_cc_configure_extension = modules.as_extension(_apple_cc_configure_extension_impl) diff --git a/crosstool/internal/BUILD.bazel b/crosstool/internal/BUILD.bazel new file mode 100644 index 0000000..e69de29 diff --git a/crosstool/internal/setup.bzl b/crosstool/internal/setup.bzl new file mode 100644 index 0000000..3369e02 --- /dev/null +++ b/crosstool/internal/setup.bzl @@ -0,0 +1,69 @@ +"""Configure the Apple CC toolchain (internal part)""" + +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, +) diff --git a/crosstool/setup.bzl b/crosstool/setup.bzl index f3f7090..3d1065d 100644 --- a/crosstool/setup.bzl +++ b/crosstool/setup.bzl @@ -1,84 +1,12 @@ """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("//crosstool/internal:setup.bzl", "apple_cc_autoconf", "apple_cc_autoconf_toolchains") # 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 = module_extension(implementation = _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(