From f76fc61640825dd7cf83ce02ba48a4e4f95b66ff Mon Sep 17 00:00:00 2001 From: Googler Date: Thu, 16 Feb 2023 11:34:44 -0800 Subject: [PATCH] Allow the timeout value for execute calls on a mac to be set via an environment variable In certain setups, these calls to xcrun during bazel setup have been reported to sometimes take more than 2 minutes (see https://github.com/bazelbuild/bazel/issues/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 --- tools/cpp/osx_cc_configure.bzl | 31 ++++++++++++++++++++++--------- tools/osx/xcode_configure.bzl | 24 ++++++++++++++++++------ 2 files changed, 40 insertions(+), 15 deletions(-) diff --git a/tools/cpp/osx_cc_configure.bzl b/tools/cpp/osx_cc_configure.bzl index 74cc5dc5dbdd96..1181943feb2a87 100644 --- a/tools/cpp/osx_cc_configure.bzl +++ b/tools/cpp/osx_cc_configure.bzl @@ -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", @@ -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", @@ -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}" @@ -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", @@ -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([ @@ -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}" @@ -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. @@ -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 @@ -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 = {} diff --git a/tools/osx/xcode_configure.bzl b/tools/osx/xcode_configure.bzl index f2cd7043e2c563..d10daae0b3f3a8 100644 --- a/tools/osx/xcode_configure.bzl +++ b/tools/osx/xcode_configure.bzl @@ -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. @@ -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 = [] @@ -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): @@ -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", @@ -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 = "" @@ -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 = ( @@ -188,6 +193,12 @@ 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", @@ -195,7 +206,7 @@ def _darwin_build_file(repository_ctx): "xcrun", "xcodebuild", "-version", - ], _EXECUTE_TIMEOUT) + ], timeout) (toolchains, xcodeloc_err) = run_xcode_locator( repository_ctx, @@ -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)