Skip to content

Commit

Permalink
Allow the timeout value for execute calls on a mac to be set via an e…
Browse files Browse the repository at this point in the history
…nvironment variable

In certain setups, these calls to xcrun during bazel setup have been reported to
sometimes take more than 2 minutes (see
bazelbuild#17437).  We have already bumped this
timeout multiple times, which is currently 60 for some calls and 120 for others.
Standardize the default timeout to 120, and allow the timeout to be override via
BAZEL_OSX_EXECUTE_TIMEOUT, to allow individual enviroments to increase that even
more if needed.

PiperOrigin-RevId: 510200188
Change-Id: I664eb7979c4fd2b46ccc87d073f319c1e6041d77
  • Loading branch information
googlewalt authored and copybara-github committed Feb 16, 2023
1 parent 53ff268 commit f76fc61
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 15 deletions.
31 changes: 22 additions & 9 deletions tools/cpp/osx_cc_configure.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,11 @@
# limitations under the License.
"""Configuring the C++ toolchain on macOS."""

load("@bazel_tools//tools/osx:xcode_configure.bzl", "run_xcode_locator")
load(
"@bazel_tools//tools/osx:xcode_configure.bzl",
"OSX_EXECUTE_TIMEOUT",
"run_xcode_locator",
)
load(
"@bazel_tools//tools/cpp:lib_cc_configure.bzl",
"escape_string",
Expand Down Expand Up @@ -54,7 +58,7 @@ def _get_escaped_xcode_cxx_inc_directories(repository_ctx, cc, xcode_toolchains)
return include_dirs

# TODO: Remove once Xcode 12 is the minimum supported version
def _compile_cc_file_single_arch(repository_ctx, src_name, out_name):
def _compile_cc_file_single_arch(repository_ctx, src_name, out_name, timeout):
env = repository_ctx.os.environ
xcrun_result = repository_ctx.execute([
"env",
Expand All @@ -71,7 +75,7 @@ def _compile_cc_file_single_arch(repository_ctx, src_name, out_name):
"-o",
out_name,
src_name,
], 60)
], timeout)
if (xcrun_result.return_code != 0):
error_msg = (
"return code {code}, stderr: {err}, stdout: {out}"
Expand All @@ -84,7 +88,7 @@ def _compile_cc_file_single_arch(repository_ctx, src_name, out_name):
"https://github.com/bazelbuild/bazel/issues with the following:\n" +
error_msg)

def _compile_cc_file(repository_ctx, src_name, out_name):
def _compile_cc_file(repository_ctx, src_name, out_name, timeout):
env = repository_ctx.os.environ
xcrun_result = repository_ctx.execute([
"env",
Expand All @@ -107,7 +111,7 @@ def _compile_cc_file(repository_ctx, src_name, out_name):
"-o",
out_name,
src_name,
], 60)
], timeout)

if xcrun_result.return_code == 0:
xcrun_result = repository_ctx.execute([
Expand All @@ -120,7 +124,7 @@ def _compile_cc_file(repository_ctx, src_name, out_name):
"--sign",
"-",
out_name,
], 60)
], timeout)
if xcrun_result.return_code != 0:
error_msg = (
"codesign return code {code}, stderr: {err}, stdout: {out}"
Expand All @@ -133,7 +137,7 @@ def _compile_cc_file(repository_ctx, src_name, out_name):
"https://github.com/bazelbuild/bazel/issues with the following:\n" +
error_msg)
else:
_compile_cc_file_single_arch(repository_ctx, src_name, out_name)
_compile_cc_file_single_arch(repository_ctx, src_name, out_name, timeout)

def configure_osx_toolchain(repository_ctx, cpu_value, overriden_tools):
"""Configure C++ toolchain on macOS.
Expand All @@ -157,6 +161,10 @@ def configure_osx_toolchain(repository_ctx, cpu_value, overriden_tools):

env = repository_ctx.os.environ
should_use_xcode = "BAZEL_USE_XCODE_TOOLCHAIN" in env and env["BAZEL_USE_XCODE_TOOLCHAIN"] == "1"
if "BAZEL_OSX_EXECUTE_TIMEOUT" in env:
timeout = int(env["BAZEL_OSX_EXECUTE_TIMEOUT"])
else:
timeout = OSX_EXECUTE_TIMEOUT
xcode_toolchains = []

# Make the following logic in sync with //tools/cpp:cc_configure.bzl#cc_autoconf_toolchains_impl
Expand Down Expand Up @@ -208,11 +216,16 @@ def configure_osx_toolchain(repository_ctx, cpu_value, overriden_tools):
libtool_check_unique_src_path = str(repository_ctx.path(
paths["@bazel_tools//tools/objc:libtool_check_unique.cc"],
))
_compile_cc_file(repository_ctx, libtool_check_unique_src_path, "libtool_check_unique")
_compile_cc_file(
repository_ctx,
libtool_check_unique_src_path,
"libtool_check_unique",
timeout,
)
wrapped_clang_src_path = str(repository_ctx.path(
paths["@bazel_tools//tools/osx/crosstool:wrapped_clang.cc"],
))
_compile_cc_file(repository_ctx, wrapped_clang_src_path, "wrapped_clang")
_compile_cc_file(repository_ctx, wrapped_clang_src_path, "wrapped_clang", timeout)
repository_ctx.symlink("wrapped_clang", "wrapped_clang_pp")

tool_paths = {}
Expand Down
24 changes: 18 additions & 6 deletions tools/osx/xcode_configure.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
installed on the local host.
"""

_EXECUTE_TIMEOUT = 120
OSX_EXECUTE_TIMEOUT = 120

def _search_string(fullstring, prefix, suffix):
"""Returns the substring between two given substrings of a larger string.
Expand Down Expand Up @@ -45,7 +45,7 @@ def _search_sdk_output(output, sdkname):
"""Returns the sdk version given xcodebuild stdout and an sdkname."""
return _search_string(output, "(%s" % sdkname, ")")

def _xcode_version_output(repository_ctx, name, version, aliases, developer_dir):
def _xcode_version_output(repository_ctx, name, version, aliases, developer_dir, timeout):
"""Returns a string containing an xcode_version build target."""
build_contents = ""
decorated_aliases = []
Expand All @@ -55,7 +55,7 @@ def _xcode_version_output(repository_ctx, name, version, aliases, developer_dir)
repository_ctx.report_progress("Fetching SDK information for Xcode %s" % version)
xcodebuild_result = repository_ctx.execute(
["xcrun", "xcodebuild", "-version", "-sdk"],
_EXECUTE_TIMEOUT,
timeout,
{"DEVELOPER_DIR": developer_dir},
)
if (xcodebuild_result.return_code != 0):
Expand Down Expand Up @@ -118,6 +118,11 @@ def run_xcode_locator(repository_ctx, xcode_locator_src_label):
repository_ctx.report_progress("Building xcode-locator")
xcodeloc_src_path = str(repository_ctx.path(xcode_locator_src_label))
env = repository_ctx.os.environ
if "BAZEL_OSX_EXECUTE_TIMEOUT" in env:
timeout = int(env["BAZEL_OSX_EXECUTE_TIMEOUT"])
else:
timeout = OSX_EXECUTE_TIMEOUT

xcrun_result = repository_ctx.execute([
"env",
"-i",
Expand All @@ -135,7 +140,7 @@ def run_xcode_locator(repository_ctx, xcode_locator_src_label):
"-o",
"xcode-locator-bin",
xcodeloc_src_path,
], _EXECUTE_TIMEOUT)
], timeout)

if (xcrun_result.return_code != 0):
suggestion = ""
Expand All @@ -156,7 +161,7 @@ def run_xcode_locator(repository_ctx, xcode_locator_src_label):
repository_ctx.report_progress("Running xcode-locator")
xcode_locator_result = repository_ctx.execute(
["./xcode-locator-bin", "-v"],
_EXECUTE_TIMEOUT,
timeout,
)
if (xcode_locator_result.return_code != 0):
error_msg = (
Expand Down Expand Up @@ -188,14 +193,20 @@ def _darwin_build_file(repository_ctx):
"""Evaluates local system state to create xcode_config and xcode_version targets."""
repository_ctx.report_progress("Fetching the default Xcode version")
env = repository_ctx.os.environ

if "BAZEL_OSX_EXECUTE_TIMEOUT" in env:
timeout = int(env["BAZEL_OSX_EXECUTE_TIMEOUT"])
else:
timeout = OSX_EXECUTE_TIMEOUT

xcodebuild_result = repository_ctx.execute([
"env",
"-i",
"DEVELOPER_DIR={}".format(env.get("DEVELOPER_DIR", default = "")),
"xcrun",
"xcodebuild",
"-version",
], _EXECUTE_TIMEOUT)
], timeout)

(toolchains, xcodeloc_err) = run_xcode_locator(
repository_ctx,
Expand Down Expand Up @@ -229,6 +240,7 @@ def _darwin_build_file(repository_ctx):
version,
aliases,
developer_dir,
timeout,
)
target_label = "':%s'" % target_name
target_names.append(target_label)
Expand Down

0 comments on commit f76fc61

Please sign in to comment.