Skip to content

Commit

Permalink
Merge pull request RobotLocomotion#12224 from jamiesnape/fix-mac-baze…
Browse files Browse the repository at this point in the history
…l-1.1

Fix compiler identification for Bazel 1.1.0 on macOS
  • Loading branch information
jamiesnape authored Oct 22, 2019
2 parents 194f844 + f5803f9 commit 26a998d
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 20 deletions.
6 changes: 6 additions & 0 deletions common/test/drake_assert_test_compile_variants.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@ find . # Get some debugging output.
capture_cc_env="$1"
drake_assert_test_compile_cc="$2"

# TODO(jamiesnape): Determine this information from Bazel.
if [[ "$(uname -s)" == Darwin ]]; then
export DEVELOPER_DIR="$(xcode-select --print-path)"
export SDKROOT="$(xcrun --show-sdk-path)"
fi

# Make sure we know what C++ compiler to use.
source "$capture_cc_env"
[[ ! -z "$BAZEL_CC" ]]
Expand Down
6 changes: 6 additions & 0 deletions tools/cc_toolchain/print_host_settings.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@
# This should only be invoked via Bazel.
set -eux

# TODO(jamiesnape): Determine this information from Bazel.
if [[ "$(uname -s)" == Darwin ]]; then
export DEVELOPER_DIR="$(xcode-select --print-path)"
export SDKROOT="$(xcrun --show-sdk-path)"
fi

capture_cc_env="$1"
source "$capture_cc_env"

Expand Down
41 changes: 26 additions & 15 deletions tools/workspace/cc/repository.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ Argument:
"""

load("@bazel_tools//tools/cpp:unix_cc_configure.bzl", "find_cc")
load("@drake//tools/workspace:execute.bzl", "execute_or_fail")

def _impl(repository_ctx):
file_content = """# -*- python -*-
Expand All @@ -48,37 +49,47 @@ def _impl(repository_ctx):
executable = False,
)

# https://github.com/bazelbuild/bazel/blob/0.14.1/tools/cpp/cc_configure.bzl
# https://github.com/bazelbuild/bazel/blob/1.1.0/tools/cpp/cc_configure.bzl
if repository_ctx.os.environ.get("BAZEL_DO_NOT_DETECT_CPP_TOOLCHAIN", "0") == "1": # noqa
fail("Could NOT identify C/C++ compiler because CROSSTOOL is empty.")

if repository_ctx.os.name == "mac os x" and repository_ctx.os.environ.get("BAZEL_USE_CPP_ONLY_TOOLCHAIN", "0") != "1": # noqa
# https://github.com/bazelbuild/bazel/blob/0.14.1/tools/cpp/osx_cc_configure.bzl
cc = repository_ctx.path(Label("@local_config_cc//:cc_wrapper.sh"))
# https://github.com/bazelbuild/bazel/blob/1.1.0/tools/cpp/osx_cc_configure.bzl
cc = repository_ctx.path(Label("@local_config_cc//:wrapped_clang"))

result = execute_or_fail(repository_ctx, [
"xcode-select",
"--print-path",
])
developer_dir = result.stdout.strip()

result = execute_or_fail(repository_ctx, [
"xcrun",
"--show-sdk-path",
])
sdkroot = result.stdout.strip()

cc_environment = {
"DEVELOPER_DIR": developer_dir,
"SDKROOT": sdkroot,
}

else:
# https://github.com/bazelbuild/bazel/blob/0.14.1/tools/cpp/unix_cc_configure.bzl
# https://github.com/bazelbuild/bazel/blob/1.1.0/tools/cpp/unix_cc_configure.bzl
cc = find_cc(repository_ctx, overriden_tools = {})
cc_environment = {}

executable = repository_ctx.path("identify_compiler")
result = repository_ctx.execute([
execute_or_fail(repository_ctx, [
cc,
repository_ctx.path(
Label("@drake//tools/workspace/cc:identify_compiler.cc"),
),
"-o",
executable,
])
if result.return_code != 0:
fail(
"Could NOT identify C/C++ compiler because compilation failed.",
result.stderr,
)

result = repository_ctx.execute([executable])
if result.return_code != 0:
fail("Could NOT identify C/C++ compiler.", result.stderr)
], environment = cc_environment)

result = execute_or_fail(repository_ctx, [executable])
output = result.stdout.strip().split(" ")
if len(output) != 3:
fail("Could NOT identify C/C++ compiler.")
Expand Down
14 changes: 9 additions & 5 deletions tools/workspace/execute.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,16 @@ def which(repo_ctx, program, additional_search_paths = []):
return None
return repo_ctx.path(exec_result.stdout.strip())

def execute_and_return(repo_ctx, command, additional_search_paths = []):
def execute_and_return(
repo_ctx,
command,
additional_search_paths = [],
**kwargs):
"""Runs the `command` (list) and returns a status value. The return value
is a struct with a field `error` that will be None on success or else a
detailed message on command failure.
"""
if "/" in command[0]:
if "/" in str(command[0]):
program = command[0]
else:
program = which(repo_ctx, command[0], additional_search_paths)
Expand All @@ -38,7 +42,7 @@ def execute_and_return(repo_ctx, command, additional_search_paths = []):
command[0],
)
return struct(error = error)
exec_result = repo_ctx.execute([program] + command[1:])
exec_result = repo_ctx.execute([program] + command[1:], **kwargs)
if exec_result.return_code == 0:
error = None
else:
Expand All @@ -54,10 +58,10 @@ def execute_and_return(repo_ctx, command, additional_search_paths = []):
stdout = exec_result.stdout,
)

def execute_or_fail(repo_ctx, command):
def execute_or_fail(repo_ctx, command, **kwargs):
"""Runs the `command` (list) and immediately fails on any error.
Returns a struct with the stdout value."""
result = execute_and_return(repo_ctx, command)
result = execute_and_return(repo_ctx, command, **kwargs)
if result.error:
fail("Unable to complete setup for @{} repository: {}".format(
repo_ctx.name,
Expand Down

0 comments on commit 26a998d

Please sign in to comment.