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

Mark module extension as reproducible #328

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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")
Copy link
Collaborator

Choose a reason for hiding this comment

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

Is this file rename a breaking change for users? We have a blurb in the README that references the old name.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It would be, yes. I think it's better to wait for apple_support to drop Bazel 5 support and then land this commit without the need for this breaking change.

use_repo(apple_cc_configure, "local_config_apple_cc", "local_config_apple_cc_toolchains")

register_toolchains("@local_config_apple_cc_toolchains//:all")
10 changes: 10 additions & 0 deletions crosstool/apple_cc_configure_extension.bzl
Original file line number Diff line number Diff line change
@@ -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)
Copy link
Collaborator

Choose a reason for hiding this comment

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

In this repo we use BUILD instead of BUILD.bazel if possible.

Empty file.
69 changes: 69 additions & 0 deletions crosstool/internal/setup.bzl
Original file line number Diff line number Diff line change
@@ -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,
)
78 changes: 3 additions & 75 deletions crosstool/setup.bzl
Original file line number Diff line number Diff line change
@@ -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)
6 changes: 3 additions & 3 deletions lib/repositories.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down