Skip to content

Commit

Permalink
Make split cc_configure work on Darwin with only CLT installed
Browse files Browse the repository at this point in the history
Fixes bazelbuild#8479.

To keep this backwards compatible we have to detect xcode early in the Bazel
build (even when no C++ is being built). In cases where user knows there is
xcode, or when they know it won't be needed, I'm adding environment variable
`BAZEL_USE_XCODE_TOOLCHAIN`. When set to `1`, Bazel will not try to detect
xcode, it will assume it is there.

Makes bazelbuild#6926 a little bit more
complicated.

RELNOTES: `BAZEL_USE_XCODE_TOOLCHAIN=1` tells Bazel not to look for Xcode to
decide whether to enable toolchains for Apple rules, but to assume Xcode is
available. Can be also used when building on Darwin and no C++ or ObjC is being
built, so there is no need to detect Xcode.
  • Loading branch information
hlopko committed May 29, 2019
1 parent ab5de51 commit be65b03
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 7 deletions.
32 changes: 25 additions & 7 deletions tools/cpp/cc_configure.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,14 @@ load(
"get_cpu_value",
"resolve_labels",
)
load("@bazel_tools//tools/osx:xcode_configure.bzl", "run_xcode_locator")

def _generate_cpp_only_build_file(repository_ctx, cpu_value):
repository_ctx.template(
"BUILD",
paths["@bazel_tools//tools/cpp:BUILD.toolchains.tpl"],
{"%{name}": cpu_value},
)

def cc_autoconf_toolchains_impl(repository_ctx):
"""Generate BUILD file with 'toolchain' targets for the local host C++ toolchain.
Expand All @@ -32,21 +40,31 @@ def cc_autoconf_toolchains_impl(repository_ctx):
"@bazel_tools//tools/cpp:BUILD.toolchains.tpl",
"@bazel_tools//tools/osx/crosstool:BUILD.toolchains",
"@bazel_tools//tools/osx/crosstool:osx_archs.bzl",
"@bazel_tools//tools/osx:xcode_locator.m",
])
env = repository_ctx.os.environ
cpu_value = get_cpu_value(repository_ctx)

if "BAZEL_DO_NOT_DETECT_CPP_TOOLCHAIN" in env and env["BAZEL_DO_NOT_DETECT_CPP_TOOLCHAIN"] == "1":
repository_ctx.file("BUILD", "# C++ toolchain autoconfiguration was disabled by BAZEL_DO_NOT_DETECT_CPP_TOOLCHAIN env variable.")
elif (cpu_value == "darwin" and
("BAZEL_USE_CPP_ONLY_TOOLCHAIN" not in env or env["BAZEL_USE_CPP_ONLY_TOOLCHAIN"] != "1")):
repository_ctx.symlink(paths["@bazel_tools//tools/osx/crosstool:BUILD.toolchains"], "BUILD")
repository_ctx.symlink(paths["@bazel_tools//tools/osx/crosstool:osx_archs.bzl"], "osx_archs.bzl")
# Don't detect xcode if the user told us it should be there.
should_detect_xcode = "BAZEL_USE_XCODE_TOOLCHAIN" not in env or env["BAZEL_USE_XCODE_TOOLCHAIN"] != "1":
# TODO(#6926): Unify C++ and ObjC toolchains so we don't have to run xcode locator to generate toolchain targets.
# And also so we don't have to keep this code in sync with //tools/cpp:osx_cc_configure.bzl.
(xcode_toolchains, _xcodeloc_err) = run_xcode_locator(
repository_ctx,
paths["@bazel_tools//tools/osx:xcode_locator.m"],
)

if not should_detect_xcode or xcode_toolchains:
repository_ctx.symlink(paths["@bazel_tools//tools/osx/crosstool:BUILD.toolchains"], "BUILD")
repository_ctx.symlink(paths["@bazel_tools//tools/osx/crosstool:osx_archs.bzl"], "osx_archs.bzl")
else:
_generate_cpp_only_build_file(repository_ctx, cpu_value)
else:
repository_ctx.template(
"BUILD",
paths["@bazel_tools//tools/cpp:BUILD.toolchains.tpl"],
{"%{name}": cpu_value},
)
_generate_cpp_only_build_file(repository_ctx, cpu_value)

cc_autoconf_toolchains = repository_rule(
environ = [
Expand Down
1 change: 1 addition & 0 deletions tools/cpp/osx_cc_configure.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ def configure_osx_toolchain(repository_ctx, overriden_tools):
])

xcode_toolchains = []
# Make the following logic in sync with //tools/cpp:cc_configure.bzl#cc_autoconf_toolchains_impl
(xcode_toolchains, xcodeloc_err) = run_xcode_locator(
repository_ctx,
paths["@bazel_tools//tools/osx:xcode_locator.m"],
Expand Down

0 comments on commit be65b03

Please sign in to comment.