diff options
author | vladmos <vladmos@google.com> | 2018-06-01 04:51:21 -0700 |
---|---|---|
committer | Copybara-Service <copybara-piper@google.com> | 2018-06-01 04:52:44 -0700 |
commit | 20a042faa91acf8db78684de9f38c4e628d61dd6 (patch) | |
tree | c2fc5c6d30cb51cb3f6e30ed0fb2d25f5525585f /tools/cpp | |
parent | 2269ee2af91b7df13820381d61e8e4e26a257b3a (diff) |
Format all bzl files with buildifier
This CL aslo enables a presubmit check for correct formatting of all bzl files in //third_party/bazel.
PiperOrigin-RevId: 198857055
Diffstat (limited to 'tools/cpp')
-rw-r--r-- | tools/cpp/alias_rules.bzl | 9 | ||||
-rw-r--r-- | tools/cpp/cc_configure.bzl | 71 | ||||
-rw-r--r-- | tools/cpp/compiler_flag.bzl | 8 | ||||
-rw-r--r-- | tools/cpp/crosstool_lib.bzl | 782 | ||||
-rw-r--r-- | tools/cpp/crosstool_utils.bzl | 389 | ||||
-rw-r--r-- | tools/cpp/dummy_toolchain.bzl | 8 | ||||
-rw-r--r-- | tools/cpp/lib_cc_configure.bzl | 310 | ||||
-rw-r--r-- | tools/cpp/osx_cc_configure.bzl | 229 | ||||
-rw-r--r-- | tools/cpp/toolchain_utils.bzl | 26 | ||||
-rw-r--r-- | tools/cpp/unix_cc_configure.bzl | 823 | ||||
-rw-r--r-- | tools/cpp/windows_cc_configure.bzl | 763 |
11 files changed, 1791 insertions, 1627 deletions
diff --git a/tools/cpp/alias_rules.bzl b/tools/cpp/alias_rules.bzl index fa2423ad97..2ab85d8882 100644 --- a/tools/cpp/alias_rules.bzl +++ b/tools/cpp/alias_rules.bzl @@ -13,8 +13,9 @@ # limitations under the License. """Skylark rules that stub out C++-related alias rules.""" + def cc_toolchain_alias(name): - if hasattr(native, "cc_toolchain_alias"): - native.cc_toolchain_alias(name=name) - else: - pass + if hasattr(native, "cc_toolchain_alias"): + native.cc_toolchain_alias(name = name) + else: + pass diff --git a/tools/cpp/cc_configure.bzl b/tools/cpp/cc_configure.bzl index 01736aa09e..57954f6f71 100644 --- a/tools/cpp/cc_configure.bzl +++ b/tools/cpp/cc_configure.bzl @@ -23,35 +23,37 @@ load( ) def cc_autoconf_impl(repository_ctx, overriden_tools = dict()): - paths = resolve_labels(repository_ctx, [ - "@bazel_tools//tools/cpp:BUILD.static.freebsd", - "@bazel_tools//tools/cpp:CROSSTOOL", - "@bazel_tools//tools/cpp:dummy_toolchain.bzl", - ]) + paths = resolve_labels(repository_ctx, [ + "@bazel_tools//tools/cpp:BUILD.static.freebsd", + "@bazel_tools//tools/cpp:CROSSTOOL", + "@bazel_tools//tools/cpp:dummy_toolchain.bzl", + ]) - repository_ctx.symlink( - paths["@bazel_tools//tools/cpp:dummy_toolchain.bzl"], "dummy_toolchain.bzl") - 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.symlink(Label("@bazel_tools//tools/cpp:CROSSTOOL.empty"), "CROSSTOOL") - repository_ctx.symlink(Label("@bazel_tools//tools/cpp:BUILD.empty"), "BUILD") - elif cpu_value == "freebsd": - # This is defaulting to the static crosstool, we should eventually - # autoconfigure this platform too. Theorically, FreeBSD should be - # straightforward to add but we cannot run it in a docker container so - # skipping until we have proper tests for FreeBSD. - repository_ctx.symlink(paths["@bazel_tools//tools/cpp:CROSSTOOL"], "CROSSTOOL") - repository_ctx.symlink(paths["@bazel_tools//tools/cpp:BUILD.static.freebsd"], "BUILD") - elif cpu_value == "x64_windows": - # TODO(ibiryukov): overriden_tools are only supported in configure_unix_toolchain. - # We might want to add that to Windows too(at least for msys toolchain). - configure_windows_toolchain(repository_ctx) - elif (cpu_value == "darwin" and - ("BAZEL_USE_CPP_ONLY_TOOLCHAIN" not in env or env["BAZEL_USE_CPP_ONLY_TOOLCHAIN"] != "1")): - configure_osx_toolchain(repository_ctx, overriden_tools) - else: - configure_unix_toolchain(repository_ctx, cpu_value, overriden_tools) + repository_ctx.symlink( + paths["@bazel_tools//tools/cpp:dummy_toolchain.bzl"], + "dummy_toolchain.bzl", + ) + 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.symlink(Label("@bazel_tools//tools/cpp:CROSSTOOL.empty"), "CROSSTOOL") + repository_ctx.symlink(Label("@bazel_tools//tools/cpp:BUILD.empty"), "BUILD") + elif cpu_value == "freebsd": + # This is defaulting to the static crosstool, we should eventually + # autoconfigure this platform too. Theorically, FreeBSD should be + # straightforward to add but we cannot run it in a docker container so + # skipping until we have proper tests for FreeBSD. + repository_ctx.symlink(paths["@bazel_tools//tools/cpp:CROSSTOOL"], "CROSSTOOL") + repository_ctx.symlink(paths["@bazel_tools//tools/cpp:BUILD.static.freebsd"], "BUILD") + elif cpu_value == "x64_windows": + # TODO(ibiryukov): overriden_tools are only supported in configure_unix_toolchain. + # We might want to add that to Windows too(at least for msys toolchain). + configure_windows_toolchain(repository_ctx) + elif (cpu_value == "darwin" and + ("BAZEL_USE_CPP_ONLY_TOOLCHAIN" not in env or env["BAZEL_USE_CPP_ONLY_TOOLCHAIN"] != "1")): + configure_osx_toolchain(repository_ctx, overriden_tools) + else: + configure_unix_toolchain(repository_ctx, cpu_value, overriden_tools) cc_autoconf = repository_rule( environ = [ @@ -92,9 +94,10 @@ cc_autoconf = repository_rule( ) def cc_configure(): - """A C++ configuration rules that generate the crosstool file.""" - cc_autoconf(name="local_config_cc") - native.bind(name="cc_toolchain", actual="@local_config_cc//:toolchain") - native.register_toolchains( - # Use register_toolchain's target pattern expansion to register all toolchains in the package. - "@local_config_cc//:all") + """A C++ configuration rules that generate the crosstool file.""" + cc_autoconf(name = "local_config_cc") + native.bind(name = "cc_toolchain", actual = "@local_config_cc//:toolchain") + native.register_toolchains( + # Use register_toolchain's target pattern expansion to register all toolchains in the package. + "@local_config_cc//:all", + ) diff --git a/tools/cpp/compiler_flag.bzl b/tools/cpp/compiler_flag.bzl index 4b134a1bf3..622a0b4c99 100644 --- a/tools/cpp/compiler_flag.bzl +++ b/tools/cpp/compiler_flag.bzl @@ -17,12 +17,12 @@ load("//tools/cpp:toolchain_utils.bzl", "find_cpp_toolchain") def _compiler_flag_impl(ctx): - toolchain = find_cpp_toolchain(ctx) - return [config_common.FeatureFlagInfo(value = toolchain.compiler)] + toolchain = find_cpp_toolchain(ctx) + return [config_common.FeatureFlagInfo(value = toolchain.compiler)] compiler_flag = rule( implementation = _compiler_flag_impl, attrs = { - "_cc_toolchain": attr.label(default=Label("//tools/cpp:current_cc_toolchain")), - } + "_cc_toolchain": attr.label(default = Label("//tools/cpp:current_cc_toolchain")), + }, ) diff --git a/tools/cpp/crosstool_lib.bzl b/tools/cpp/crosstool_lib.bzl index a6c4916947..67db4c5237 100644 --- a/tools/cpp/crosstool_lib.bzl +++ b/tools/cpp/crosstool_lib.bzl @@ -14,400 +14,458 @@ # limitations under the License. """Library of common crosstool features.""" -load("@bazel_tools//tools/cpp:crosstool_utils.bzl", - "feature", - "simple_feature", - "flag_set", - "flag_group", - "flags", - "COMPILE_ACTIONS", - "LINK_ACTIONS", - "ARCHIVE_ACTIONS") - +load( + "@bazel_tools//tools/cpp:crosstool_utils.bzl", + "ARCHIVE_ACTIONS", + "COMPILE_ACTIONS", + "LINK_ACTIONS", + "feature", + "flag_group", + "flag_set", + "flags", + "simple_feature", +) def get_features_to_appear_first(platform): - """Returns standard features that should appear in the top of the toolchain. + """Returns standard features that should appear in the top of the toolchain. - Args: - platform: one of [ k8, darwin, msvc ] + Args: + platform: one of [ k8, darwin, msvc ] - Returns: - a collection of features to be put into crosstool - """ - return [ - simple_feature("no_legacy_features", [], []), - simple_feature( - "legacy_compile_flags", - COMPILE_ACTIONS, - ["%{legacy_compile_flags}"], - expand_if_all_available=["legacy_compile_flags"], - iterate_over="legacy_compile_flags"), - simple_feature( - "dependency_file", - COMPILE_ACTIONS, - ["-MD", "-MF", "%{dependency_file}"], - expand_if_all_available=["dependency_file"]), - simple_feature( - "random_seed", - COMPILE_ACTIONS, - ["-frandom-seed=%{output_file}"]), - simple_feature( - "pic", - COMPILE_ACTIONS, - ["-fPIC"], - expand_if_all_available=["pic"]), - simple_feature( - "per_object_debug_info", - COMPILE_ACTIONS, - ["-gsplit-dwarf"], - expand_if_all_available=["per_object_debug_info_file"]), - simple_feature( - "preprocessor_defines", - COMPILE_ACTIONS, - ["-D%{preprocessor_defines}"], - iterate_over="preprocessor_defines", - expand_if_all_available=["preprocessor_defines"]), - simple_feature( - "includes", - COMPILE_ACTIONS, - ["-include", "%{includes}"], - iterate_over="includes", - expand_if_all_available=["includes"]), - simple_feature( - "quote_include_paths", - COMPILE_ACTIONS, - ["-iquote", "%{quote_include_paths}"], - iterate_over="quote_include_paths", - expand_if_all_available=["quote_include_paths"]), - simple_feature( - "include_paths", - COMPILE_ACTIONS, - ["-I%{include_paths}"], - iterate_over="include_paths", - expand_if_all_available=["include_paths"]), - simple_feature( - "system_include_paths", - COMPILE_ACTIONS, - ["-isystem", "%{system_include_paths}"], - iterate_over="system_include_paths", - expand_if_all_available=["system_include_paths"]), - simple_feature( - "symbol_counts", - LINK_ACTIONS, - ["-Wl,--print-symbol-counts=%{symbol_counts_output}"], - expand_if_all_available=["symbol_counts_output"]), - simple_feature( - "shared_flag", - LINK_ACTIONS, - ["-shared"], - expand_if_all_available=["symbol_counts_output"]), - simple_feature( - "output_execpath_flags", - LINK_ACTIONS, - ["-o", "%{output_execpath}"], - expand_if_all_available=["output_execpath"]), - simple_feature( - "runtime_library_search_directories", - LINK_ACTIONS, - [_runtime_library_directory_flag(platform)], - iterate_over="runtime_library_search_directories", - expand_if_all_available=["runtime_library_search_directories"]), - simple_feature( - "library_search_directories", - LINK_ACTIONS, - ["-L%{library_search_directories}"], - iterate_over="library_search_directories", - expand_if_all_available=["library_search_directories"]), - simple_feature("_archiver_flags", ARCHIVE_ACTIONS, _archiver_flags(platform)), - feature( - "libraries_to_link", [ - flag_set(ARCHIVE_ACTIONS, [ - flag_group([ + Returns: + a collection of features to be put into crosstool + """ + return [ + simple_feature("no_legacy_features", [], []), + simple_feature( + "legacy_compile_flags", + COMPILE_ACTIONS, + ["%{legacy_compile_flags}"], + expand_if_all_available = ["legacy_compile_flags"], + iterate_over = "legacy_compile_flags", + ), + simple_feature( + "dependency_file", + COMPILE_ACTIONS, + ["-MD", "-MF", "%{dependency_file}"], + expand_if_all_available = ["dependency_file"], + ), + simple_feature( + "random_seed", + COMPILE_ACTIONS, + ["-frandom-seed=%{output_file}"], + ), + simple_feature( + "pic", + COMPILE_ACTIONS, + ["-fPIC"], + expand_if_all_available = ["pic"], + ), + simple_feature( + "per_object_debug_info", + COMPILE_ACTIONS, + ["-gsplit-dwarf"], + expand_if_all_available = ["per_object_debug_info_file"], + ), + simple_feature( + "preprocessor_defines", + COMPILE_ACTIONS, + ["-D%{preprocessor_defines}"], + iterate_over = "preprocessor_defines", + expand_if_all_available = ["preprocessor_defines"], + ), + simple_feature( + "includes", + COMPILE_ACTIONS, + ["-include", "%{includes}"], + iterate_over = "includes", + expand_if_all_available = ["includes"], + ), + simple_feature( + "quote_include_paths", + COMPILE_ACTIONS, + ["-iquote", "%{quote_include_paths}"], + iterate_over = "quote_include_paths", + expand_if_all_available = ["quote_include_paths"], + ), + simple_feature( + "include_paths", + COMPILE_ACTIONS, + ["-I%{include_paths}"], + iterate_over = "include_paths", + expand_if_all_available = ["include_paths"], + ), + simple_feature( + "system_include_paths", + COMPILE_ACTIONS, + ["-isystem", "%{system_include_paths}"], + iterate_over = "system_include_paths", + expand_if_all_available = ["system_include_paths"], + ), + simple_feature( + "symbol_counts", + LINK_ACTIONS, + ["-Wl,--print-symbol-counts=%{symbol_counts_output}"], + expand_if_all_available = ["symbol_counts_output"], + ), + simple_feature( + "shared_flag", + LINK_ACTIONS, + ["-shared"], + expand_if_all_available = ["symbol_counts_output"], + ), + simple_feature( + "output_execpath_flags", + LINK_ACTIONS, + ["-o", "%{output_execpath}"], + expand_if_all_available = ["output_execpath"], + ), + simple_feature( + "runtime_library_search_directories", + LINK_ACTIONS, + [_runtime_library_directory_flag(platform)], + iterate_over = "runtime_library_search_directories", + expand_if_all_available = ["runtime_library_search_directories"], + ), + simple_feature( + "library_search_directories", + LINK_ACTIONS, + ["-L%{library_search_directories}"], + iterate_over = "library_search_directories", + expand_if_all_available = ["library_search_directories"], + ), + simple_feature("_archiver_flags", ARCHIVE_ACTIONS, _archiver_flags(platform)), + feature( + "libraries_to_link", + [ + flag_set(ARCHIVE_ACTIONS, [ flag_group( - flags("%{libraries_to_link.name}"), - expand_if_equal=[["libraries_to_link.type", "object_file"]]), + [ + flag_group( + flags("%{libraries_to_link.name}"), + expand_if_equal = [["libraries_to_link.type", "object_file"]], + ), + flag_group( + flags("%{libraries_to_link.object_files}"), + expand_if_equal = [["libraries_to_link.type", "object_file_group"]], + iterate_over = "libraries_to_link.object_files", + ), + ], + iterate_over = "libraries_to_link", + expand_if_all_available = ["libraries_to_link"], + ), + ]), + flag_set(LINK_ACTIONS, [ flag_group( - flags("%{libraries_to_link.object_files}"), - expand_if_equal=[["libraries_to_link.type", "object_file_group"]], - iterate_over="libraries_to_link.object_files"), - ], - iterate_over="libraries_to_link", - expand_if_all_available=["libraries_to_link"]) - ]), - flag_set(LINK_ACTIONS, [ - flag_group([ - flag_group( - flags("-Wl,--start-lib"), - expand_if_equal=[["libraries_to_link.type", "object_file_group"]] - ), - ] + - _libraries_to_link_flag_groupss(platform) + [ - flag_group( - flags("-Wl,--end-lib"), - expand_if_equal=[["libraries_to_link.type", "object_file_group"]] - ), - ], - iterate_over="libraries_to_link" - ), - flag_group(flags("-Wl,@%{thinlto_param_file}"), expand_if_true=["thinlto_param_file"]) - ]) - ]), - simple_feature( - "force_pic_flags", - ["c++-link-executable"], - ["-pie"], - expand_if_all_available=["force_pic"]), - simple_feature( - "user_link_flags", - LINK_ACTIONS, - ["%{user_link_flags}"], - iterate_over="user_link_flags", - expand_if_all_available=["user_link_flags"]), - simple_feature( - "legacy_link_flags", - LINK_ACTIONS, - ["%{legacy_link_flags}"], - iterate_over="legacy_link_flags", - expand_if_all_available=["legacy_link_flags"]), - simple_feature( - "fission_support", - LINK_ACTIONS, - ["-Wl,--gdb-index"], - expand_if_all_available=["is_using_fission"]), - simple_feature( - "strip_debug_symbols", - LINK_ACTIONS, - ["-Wl,-S"], - expand_if_all_available=["strip_debug_symbols"]), - _coverage_feature(platform), - simple_feature("strip_flags", ["strip"], _strip_flags(platform)), + [ + flag_group( + flags("-Wl,--start-lib"), + expand_if_equal = [["libraries_to_link.type", "object_file_group"]], + ), + ] + + _libraries_to_link_flag_groupss(platform) + [ + flag_group( + flags("-Wl,--end-lib"), + expand_if_equal = [["libraries_to_link.type", "object_file_group"]], + ), + ], + iterate_over = "libraries_to_link", + ), + flag_group(flags("-Wl,@%{thinlto_param_file}"), expand_if_true = ["thinlto_param_file"]), + ]), + ], + ), + simple_feature( + "force_pic_flags", + ["c++-link-executable"], + ["-pie"], + expand_if_all_available = ["force_pic"], + ), + simple_feature( + "user_link_flags", + LINK_ACTIONS, + ["%{user_link_flags}"], + iterate_over = "user_link_flags", + expand_if_all_available = ["user_link_flags"], + ), + simple_feature( + "legacy_link_flags", + LINK_ACTIONS, + ["%{legacy_link_flags}"], + iterate_over = "legacy_link_flags", + expand_if_all_available = ["legacy_link_flags"], + ), + simple_feature( + "fission_support", + LINK_ACTIONS, + ["-Wl,--gdb-index"], + expand_if_all_available = ["is_using_fission"], + ), + simple_feature( + "strip_debug_symbols", + LINK_ACTIONS, + ["-Wl,-S"], + expand_if_all_available = ["strip_debug_symbols"], + ), + _coverage_feature(platform), + simple_feature("strip_flags", ["strip"], _strip_flags(platform)), ] - def get_features_to_appear_last(platform): - """Returns standard features that should appear at the end of the toolchain. + """Returns standard features that should appear at the end of the toolchain. - Args: - platform: one of [ k8, darwin, msvc ] - - Returns: - a collection of features to be put into crosstool - """ - return [ - simple_feature( - "user_compile_flags", - COMPILE_ACTIONS, - ["%{user_compile_flags}"], - expand_if_all_available=["user_compile_flags"], - iterate_over="user_compile_flags"), - simple_feature( - "sysroot", - COMPILE_ACTIONS + LINK_ACTIONS, - ["--sysroot=%{sysroot}"], - expand_if_all_available=["sysroot"]), - simple_feature( - "unfiltered_compile_flags", - COMPILE_ACTIONS, - ["%{unfiltered_compile_flags}"], - expand_if_all_available=["unfiltered_compile_flags"], - iterate_over="unfiltered_compile_flags"), - simple_feature( - "linker_param_file", - LINK_ACTIONS, - [_linker_param_file_flag(platform)], - expand_if_all_available=["linker_param_file"]), - simple_feature( - "archiver_param_file", - ARCHIVE_ACTIONS, - [_archiver_param_file_flag(platform)], - expand_if_all_available=["linker_param_file"]), - simple_feature( - "compiler_input_flags", - COMPILE_ACTIONS, - ["-c", "%{source_file}"], - expand_if_all_available=["source_file"]), - feature( - "compiler_output_flags", [ - flag_set(COMPILE_ACTIONS,[ - flag_group( - flags("-S"), - expand_if_all_available=["output_assembly_file"], - ), - flag_group( - flags("-E"), - expand_if_all_available=["output_preprocess_file"], - ), - flag_group( - flags("-o", "%{output_file}"), - expand_if_all_available=["output_file"], - ), - ]) - ] - ), - ] + Args: + platform: one of [ k8, darwin, msvc ] + Returns: + a collection of features to be put into crosstool + """ + return [ + simple_feature( + "user_compile_flags", + COMPILE_ACTIONS, + ["%{user_compile_flags}"], + expand_if_all_available = ["user_compile_flags"], + iterate_over = "user_compile_flags", + ), + simple_feature( + "sysroot", + COMPILE_ACTIONS + LINK_ACTIONS, + ["--sysroot=%{sysroot}"], + expand_if_all_available = ["sysroot"], + ), + simple_feature( + "unfiltered_compile_flags", + COMPILE_ACTIONS, + ["%{unfiltered_compile_flags}"], + expand_if_all_available = ["unfiltered_compile_flags"], + iterate_over = "unfiltered_compile_flags", + ), + simple_feature( + "linker_param_file", + LINK_ACTIONS, + [_linker_param_file_flag(platform)], + expand_if_all_available = ["linker_param_file"], + ), + simple_feature( + "archiver_param_file", + ARCHIVE_ACTIONS, + [_archiver_param_file_flag(platform)], + expand_if_all_available = ["linker_param_file"], + ), + simple_feature( + "compiler_input_flags", + COMPILE_ACTIONS, + ["-c", "%{source_file}"], + expand_if_all_available = ["source_file"], + ), + feature( + "compiler_output_flags", + [ + flag_set(COMPILE_ACTIONS, [ + flag_group( + flags("-S"), + expand_if_all_available = ["output_assembly_file"], + ), + flag_group( + flags("-E"), + expand_if_all_available = ["output_preprocess_file"], + ), + flag_group( + flags("-o", "%{output_file}"), + expand_if_all_available = ["output_file"], + ), + ]), + ], + ), + ] def _is_linux(platform): - return platform == "k8" - + return platform == "k8" def _is_darwin(platform): - return platform == "darwin" - + return platform == "darwin" def _is_msvc(platform): - return platform == "msvc" - + return platform == "msvc" def _coverage_feature(use_llvm_format): - if use_llvm_format: - compile_flags = flags("-fprofile-instr-generate", "-fcoverage-mapping") - link_flags = flags("-fprofile-instr-generate") - else: - compile_flags = flags("-fprofile-arcs", "-ftest-coverage") - link_flags = flags("--coverage") - return feature( - "coverage", - [ - flag_set(COMPILE_ACTIONS, [ flag_group(compile_flags) ]), - flag_set(LINK_ACTIONS, [ flag_group(link_flags) ]), - ], - enabled = False, - provides = "profile") - + if use_llvm_format: + compile_flags = flags("-fprofile-instr-generate", "-fcoverage-mapping") + link_flags = flags("-fprofile-instr-generate") + else: + compile_flags = flags("-fprofile-arcs", "-ftest-coverage") + link_flags = flags("--coverage") + return feature( + "coverage", + [ + flag_set(COMPILE_ACTIONS, [flag_group(compile_flags)]), + flag_set(LINK_ACTIONS, [flag_group(link_flags)]), + ], + enabled = False, + provides = "profile", + ) def _runtime_library_directory_flag(platform): - if _is_linux(platform): - return "-Wl,-rpath,$ORIGIN/%{runtime_library_search_directories}" - elif _is_darwin(platform): - return "-Wl,-rpath,@loader_path/%{runtime_library_search_directories}" - elif _is_msvc(platform): - fail("todo") - else: - fail("Unsupported platform: " + platform) - + if _is_linux(platform): + return "-Wl,-rpath,$ORIGIN/%{runtime_library_search_directories}" + elif _is_darwin(platform): + return "-Wl,-rpath,@loader_path/%{runtime_library_search_directories}" + elif _is_msvc(platform): + fail("todo") + else: + fail("Unsupported platform: " + platform) def _archiver_flags(platform): - if _is_linux(platform): - return ["rcsD", "%{output_execpath}"] - elif _is_darwin(platform): - return ["-static", "-s", "-o", "%{output_execpath}"] - elif _is_msvc(platform): - fail("todo") - else: - fail("Unsupported platform: " + platform) - + if _is_linux(platform): + return ["rcsD", "%{output_execpath}"] + elif _is_darwin(platform): + return ["-static", "-s", "-o", "%{output_execpath}"] + elif _is_msvc(platform): + fail("todo") + else: + fail("Unsupported platform: " + platform) def _library_to_link_with_worce_load(variable_type, variable, flag = "", iterate = False): - return [ - flag_group([ - flag_group( - flags( - "-Wl,-force_load," + flag + "%{" + variable + "}", - expand_if_true=["libraries_to_link.is_whole_archive"])), - flag_group( - flags( - flag + "%{" + variable + "}", - expand_if_false=["libraries_to_link.is_whole_archive"])), - ], - iterate_over=variable if iterate else None, - expand_if_equal=[["libraries_to_link.type", variable_type]]), - ] - - -def _libraries_to_link_flag_groupss(platform): - if _is_linux(platform): - return [ - flag_group( - flags("-Wl,-whole-archive"), - expand_if_true=["libraries_to_link.is_whole_archive"]), - flag_group( - flags("-Wl,--start-lib"), - expand_if_equal=[["libraries_to_link.type", "object_file_group"]]), - flag_group( - flags("%{libraries_to_link.object_files}"), - iterate_over="libraries_to_link.object_files", - expand_if_equal=[["libraries_to_link.type", "object_file_group"]]), - flag_group( - flags("-Wl,--end-lib"), - expand_if_equal=[["libraries_to_link.type", "object_file_group"]]), - flag_group( - flags("%{libraries_to_link.name}"), - expand_if_equal=[["libraries_to_link.type", "object_file"]]), - flag_group( - flags("%{libraries_to_link.name}"), - expand_if_equal=[["libraries_to_link.type", "interface_library"]]), - flag_group( - flags("%{libraries_to_link.name}"), - expand_if_equal=[["libraries_to_link.type", "static_library"]]), - flag_group( - flags("-l%{libraries_to_link.name}"), - expand_if_equal=[["libraries_to_link.type", "dynamic_library"]]), - flag_group( - flags("-l:%{libraries_to_link.name}"), - expand_if_equal=[["libraries_to_link.type", "versioned_dynamic_library"]]), - flag_group( - flags("-Wl,-no-whole-archive"), - expand_if_true=["libraries_to_link.is_whole_archive"]), - ] - if _is_darwin(platform): return [ flag_group( - flags("-Wl,--start-lib"), - expand_if_equal=[["libraries_to_link.type", "object_file_group"]]), - _library_to_link_with_worce_load( - "object_file_group", "libraries_to_link.object_files", iterate = True), - flag_group( - flags("-Wl,--end-lib"), - expand_if_equal=[["libraries_to_link.type", "object_file_group"]]), - _library_to_link_with_worce_load("object_file", "libraries_to_link.name"), - _library_to_link_with_worce_load("interface_library", "libraries_to_link.name"), - _library_to_link_with_worce_load("static_library", "libraries_to_link.name"), - _library_to_link_with_worce_load("dynamic_library", "libraries_to_link.name", flag="-l"), - _library_to_link_with_worce_load("versioned_dynamic_library", "libraries_to_link.name", flag="-l:"), + [ + flag_group( + flags( + "-Wl,-force_load," + flag + "%{" + variable + "}", + expand_if_true = ["libraries_to_link.is_whole_archive"], + ), + ), + flag_group( + flags( + flag + "%{" + variable + "}", + expand_if_false = ["libraries_to_link.is_whole_archive"], + ), + ), + ], + iterate_over = variable if iterate else None, + expand_if_equal = [["libraries_to_link.type", variable_type]], + ), ] - elif _is_msvc(platform): - fail("todo") - else: - fail("Unsupported platform: " + platform) +def _libraries_to_link_flag_groupss(platform): + if _is_linux(platform): + return [ + flag_group( + flags("-Wl,-whole-archive"), + expand_if_true = ["libraries_to_link.is_whole_archive"], + ), + flag_group( + flags("-Wl,--start-lib"), + expand_if_equal = [["libraries_to_link.type", "object_file_group"]], + ), + flag_group( + flags("%{libraries_to_link.object_files}"), + iterate_over = "libraries_to_link.object_files", + expand_if_equal = [["libraries_to_link.type", "object_file_group"]], + ), + flag_group( + flags("-Wl,--end-lib"), + expand_if_equal = [["libraries_to_link.type", "object_file_group"]], + ), + flag_group( + flags("%{libraries_to_link.name}"), + expand_if_equal = [["libraries_to_link.type", "object_file"]], + ), + flag_group( + flags("%{libraries_to_link.name}"), + expand_if_equal = [["libraries_to_link.type", "interface_library"]], + ), + flag_group( + flags("%{libraries_to_link.name}"), + expand_if_equal = [["libraries_to_link.type", "static_library"]], + ), + flag_group( + flags("-l%{libraries_to_link.name}"), + expand_if_equal = [["libraries_to_link.type", "dynamic_library"]], + ), + flag_group( + flags("-l:%{libraries_to_link.name}"), + expand_if_equal = [["libraries_to_link.type", "versioned_dynamic_library"]], + ), + flag_group( + flags("-Wl,-no-whole-archive"), + expand_if_true = ["libraries_to_link.is_whole_archive"], + ), + ] + if _is_darwin(platform): + return [ + flag_group( + flags("-Wl,--start-lib"), + expand_if_equal = [["libraries_to_link.type", "object_file_group"]], + ), + _library_to_link_with_worce_load( + "object_file_group", + "libraries_to_link.object_files", + iterate = True, + ), + flag_group( + flags("-Wl,--end-lib"), + expand_if_equal = [["libraries_to_link.type", "object_file_group"]], + ), + _library_to_link_with_worce_load("object_file", "libraries_to_link.name"), + _library_to_link_with_worce_load("interface_library", "libraries_to_link.name"), + _library_to_link_with_worce_load("static_library", "libraries_to_link.name"), + _library_to_link_with_worce_load("dynamic_library", "libraries_to_link.name", flag = "-l"), + _library_to_link_with_worce_load("versioned_dynamic_library", "libraries_to_link.name", flag = "-l:"), + ] + elif _is_msvc(platform): + fail("todo") + else: + fail("Unsupported platform: " + platform) def _strip_flags(platform): - if _is_linux(platform): - return [ "-S", "-p", "-o", "%{output_file}", - "-R", ".gnu.switches.text.quote_paths", - "-R", ".gnu.switches.text.bracket_paths", - "-R", ".gnu.switches.text.system_paths", - "-R", ".gnu.switches.text.cpp_defines", - "-R", ".gnu.switches.text.cpp_includes", - "-R", ".gnu.switches.text.cl_args", - "-R", ".gnu.switches.text.lipo_info", - "-R", ".gnu.switches.text.annotation", ] - elif _is_darwin(platform): - return ["-S", "-o", "%{output_file}"] - elif _is_msvc(platform): - fail("todo") - else: - fail("Unsupported platform: " + platform) - + if _is_linux(platform): + return [ + "-S", + "-p", + "-o", + "%{output_file}", + "-R", + ".gnu.switches.text.quote_paths", + "-R", + ".gnu.switches.text.bracket_paths", + "-R", + ".gnu.switches.text.system_paths", + "-R", + ".gnu.switches.text.cpp_defines", + "-R", + ".gnu.switches.text.cpp_includes", + "-R", + ".gnu.switches.text.cl_args", + "-R", + ".gnu.switches.text.lipo_info", + "-R", + ".gnu.switches.text.annotation", + ] + elif _is_darwin(platform): + return ["-S", "-o", "%{output_file}"] + elif _is_msvc(platform): + fail("todo") + else: + fail("Unsupported platform: " + platform) def _linker_param_file_flag(platform): - if _is_linux(platform): - return "-Wl,@%{linker_param_file}" - elif _is_darwin(platform): - return "-Wl,@%{linker_param_file}" - elif _is_msvc(platform): - fail("todo") - else: - fail("Unsupported platform: " + platform) - + if _is_linux(platform): + return "-Wl,@%{linker_param_file}" + elif _is_darwin(platform): + return "-Wl,@%{linker_param_file}" + elif _is_msvc(platform): + fail("todo") + else: + fail("Unsupported platform: " + platform) def _archiver_param_file_flag(platform): - if _is_linux(platform): - return "@%{linker_param_file}" - elif _is_darwin(platform): - return "@%{linker_param_file}" - elif _is_msvc(platform): - fail("todo") - else: - fail("Unsupported platform: " + platform) + if _is_linux(platform): + return "@%{linker_param_file}" + elif _is_darwin(platform): + return "@%{linker_param_file}" + elif _is_msvc(platform): + fail("todo") + else: + fail("Unsupported platform: " + platform) diff --git a/tools/cpp/crosstool_utils.bzl b/tools/cpp/crosstool_utils.bzl index 238d0cff6d..8ddeb86259 100644 --- a/tools/cpp/crosstool_utils.bzl +++ b/tools/cpp/crosstool_utils.bzl @@ -43,233 +43,242 @@ ARCHIVE_ACTIONS = [ # All remaining actions used by C++ rules that are configured in the CROSSTOOL OTHER_ACTIONS = [ - "strip" + "strip", ] - def action_config(action_name, tool_path): - """Emit action_config message. - - Examples: - action_config("c-compile", "/usr/bin/gcc") -> - action_config { - config_name: 'c-compile' - action_name: 'c-compile' - tool { - tool_path: '/usr/bin/gcc' + """Emit action_config message. + + Examples: + action_config("c-compile", "/usr/bin/gcc") -> + action_config { + config_name: 'c-compile' + action_name: 'c-compile' + tool { + tool_path: '/usr/bin/gcc' + } } - } - - Args: - action_name: name of the action - tool_path: absolute or CROSSTOOL-relative path to the tool - - Returns: - a string to be placed into the CROSSTOOL - """ - if action_name == None or action_name == "": - fail("action_name must be present") - if tool_path == None or tool_path == "": - fail("tool_path must be present") - return """ + + Args: + action_name: name of the action + tool_path: absolute or CROSSTOOL-relative path to the tool + + Returns: + a string to be placed into the CROSSTOOL + """ + if action_name == None or action_name == "": + fail("action_name must be present") + if tool_path == None or tool_path == "": + fail("tool_path must be present") + return """ action_config {{ config_name: '{action_name}' action_name: '{action_name}' tool {{ tool_path: '{tool_path}' }} - }}""".format(action_name=action_name, tool_path=tool_path) - + }}""".format(action_name = action_name, tool_path = tool_path) def feature(name, flag_sets, enabled = True, provides = None): - """Emit feature message. - - Examples: - feature("fully_static_link", flag_sets, enabled = False) -> - feature { - name: 'fully_static_link' - enabled = false - <flags_sets> - } - - Args: - name: name of the feature - flag_sets: a collection of flag_set messages - enabled: whether this feature is turned on by default - provides: a symbol this feature provides, used to implement mutually incompatible features - - Returns: - a string to be placed into the CROSSTOOL - """ - if name == None or name == "": - fail("feature name must be present") - return """ + """Emit feature message. + + Examples: + feature("fully_static_link", flag_sets, enabled = False) -> + feature { + name: 'fully_static_link' + enabled = false + <flags_sets> + } + + Args: + name: name of the feature + flag_sets: a collection of flag_set messages + enabled: whether this feature is turned on by default + provides: a symbol this feature provides, used to implement mutually incompatible features + + Returns: + a string to be placed into the CROSSTOOL + """ + if name == None or name == "": + fail("feature name must be present") + return """ feature {{ name: '{name}' enabled: {enabled}{provides}{flag_sets} }}""".format( - provides=("\n provides: '%s'" % provides if provides != None else ""), - name=name, - enabled=_to_proto_value(enabled), - flag_sets="".join(flag_sets)) + provides = ("\n provides: '%s'" % provides if provides != None else ""), + name = name, + enabled = _to_proto_value(enabled), + flag_sets = "".join(flag_sets), + ) +def simple_feature( + name, + actions, + flags, + enabled = True, + provides = None, + expand_if_all_available = [], + iterate_over = None): + """Sugar for emitting simple feature message. + + Examples: + simple_feature("foo", ['c-compile'], flags("-foo")) -> + feature { + name: 'foo' + flag_set { + action: 'c-compile' + flag_group { + flag: '-foo' + } + } + } -def simple_feature(name, actions, flags, enabled = True, provides = None, - expand_if_all_available = [], iterate_over = None): - """Sugar for emitting simple feature message. + Args: + name: name of the feature + actions: for which actions should flags be emitted + flags: a collection of flag messages + enabled: whether this feature is turned on by default + provides: a symbol this feature provides, used to implement mutually incompatible features + expand_if_all_available: specify which build variables need to be present + for this group to be expanded + iterate_over: expand this flag_group for every item in the build variable + + Returns: + a string to be placed into the CROSSTOOL + """ + if len(flags) == 0: + return feature(name, []) + else: + return feature( + name, + [flag_set( + actions, + [flag_group( + [flag(f) for f in flags], + iterate_over = iterate_over, + expand_if_all_available = expand_if_all_available, + )], + )], + enabled = enabled, + provides = provides, + ) + +def flag_set(actions, flag_groups): + """Emit flag_set message. - Examples: - simple_feature("foo", ['c-compile'], flags("-foo")) -> - feature { - name: 'foo' + Examples: + flag_set(['c-compile'], flag_groups) -> flag_set { action: 'c-compile' - flag_group { - flag: '-foo' - } + <flag_groups> } - } - - Args: - name: name of the feature - actions: for which actions should flags be emitted - flags: a collection of flag messages - enabled: whether this feature is turned on by default - provides: a symbol this feature provides, used to implement mutually incompatible features - expand_if_all_available: specify which build variables need to be present - for this group to be expanded - iterate_over: expand this flag_group for every item in the build variable - - Returns: - a string to be placed into the CROSSTOOL - """ - if len(flags) == 0: - return feature(name, []) - else: - return feature( - name, - [flag_set( - actions, - [flag_group( - [flag(f) for f in flags], - iterate_over=iterate_over, - expand_if_all_available=expand_if_all_available)])], - enabled = enabled, - provides = provides) - -def flag_set(actions, flag_groups): - """Emit flag_set message. - - Examples: - flag_set(['c-compile'], flag_groups) -> - flag_set { - action: 'c-compile' - <flag_groups> - } - - Args: - actions: for which actions should flags be emitted - flag_groups: a collection of flag_group messages - - Returns: - a string to be placed into the CROSSTOOL - """ - if actions == None or len(actions) == 0: - fail("empty actions list is not allowed for flag_set") - if flag_groups == None or len(flag_groups) == 0: - fail("empty flag_groups list is not allowed for flag_set") - actions_string = "" - for action in actions: actions_string += "\n action: '%s'" % action - - return """ + Args: + actions: for which actions should flags be emitted + flag_groups: a collection of flag_group messages + + Returns: + a string to be placed into the CROSSTOOL + """ + if actions == None or len(actions) == 0: + fail("empty actions list is not allowed for flag_set") + if flag_groups == None or len(flag_groups) == 0: + fail("empty flag_groups list is not allowed for flag_set") + actions_string = "" + for action in actions: + actions_string += "\n action: '%s'" % action + + return """ flag_set {{{actions}{flag_groups} - }}""".format(actions=actions_string, flag_groups="".join(flag_groups)) - + }}""".format(actions = actions_string, flag_groups = "".join(flag_groups)) def flag_group( - content, expand_if_all_available = [], expand_if_none_available = [], expand_if_true = [], - expand_if_false = [], expand_if_equal = [], iterate_over = None): - """Emit flag_group message. - - Examples: - flag_group(flags("-foo %{output_file}"), expand_if_all_available="output_file") -> - flag_group { expand_if_all_available: "output_file" - flag: "-foo %{output_file}" - } - - Args: - content: a collection of flag messages or a collection of flag_group messages - expand_if_all_available: specify which build variables need to be present - for this group to be expanded - expand_if_none_available: specify which build variables need to be missing - for this group to be expanded - expand_if_true: specify which build variables need to be truthy for this group - to be expanded - expand_if_false: specify which build variables need to be falsey for this group - to be expanded - expand_if_equal: [[var1, value1], [var2, value2]...] specify what values - should specific build variables have for this group to be expanded - iterate_over: expand this flag_group for every item in the build variable - - Returns: - a string to be placed into the CROSSTOOL - """ - if content == None or len(content)== 0: - fail("flag_group without flags is not allowed") - conditions = "" - for var in expand_if_all_available: - conditions += "\n expand_if_all_available: '%s'" % var - for var in expand_if_none_available: - conditions += "\n expand_if_none_available: '%s'" % var - for var in expand_if_true: - conditions += "\n expand_if_true: '%s'" % var - for var in expand_if_false: - conditions += "\n expand_if_false: '%s'" % var - for var in expand_if_equal: - conditions += "\n expand_if_equal { variable: '%s' value: '%s' }" % (var[0], var[1]) - return """ + content, + expand_if_all_available = [], + expand_if_none_available = [], + expand_if_true = [], + expand_if_false = [], + expand_if_equal = [], + iterate_over = None): + """Emit flag_group message. + + Examples: + flag_group(flags("-foo %{output_file}"), expand_if_all_available="output_file") -> + flag_group { expand_if_all_available: "output_file" + flag: "-foo %{output_file}" + } + + Args: + content: a collection of flag messages or a collection of flag_group messages + expand_if_all_available: specify which build variables need to be present + for this group to be expanded + expand_if_none_available: specify which build variables need to be missing + for this group to be expanded + expand_if_true: specify which build variables need to be truthy for this group + to be expanded + expand_if_false: specify which build variables need to be falsey for this group + to be expanded + expand_if_equal: [[var1, value1], [var2, value2]...] specify what values + should specific build variables have for this group to be expanded + iterate_over: expand this flag_group for every item in the build variable + + Returns: + a string to be placed into the CROSSTOOL + """ + if content == None or len(content) == 0: + fail("flag_group without flags is not allowed") + conditions = "" + for var in expand_if_all_available: + conditions += "\n expand_if_all_available: '%s'" % var + for var in expand_if_none_available: + conditions += "\n expand_if_none_available: '%s'" % var + for var in expand_if_true: + conditions += "\n expand_if_true: '%s'" % var + for var in expand_if_false: + conditions += "\n expand_if_false: '%s'" % var + for var in expand_if_equal: + conditions += "\n expand_if_equal { variable: '%s' value: '%s' }" % (var[0], var[1]) + return """ flag_group {{{conditions}{iterate_over}{content} }}""".format( - content="".join(content), - iterate_over=("\n iterate_over: '%s'" % iterate_over if iterate_over != None else ""), - conditions=conditions) - + content = "".join(content), + iterate_over = ("\n iterate_over: '%s'" % iterate_over if iterate_over != None else ""), + conditions = conditions, + ) def flag(flag): - """Emit flag field. + """Emit flag field. - Examples: - flag("-foo") -> flag: '-foo' + Examples: + flag("-foo") -> flag: '-foo' - Args: - flag: value to be emitted to the command line - - Returns: - a string to be placed into the CROSSTOOL - """ - return "\n flag: '%s'" % flag + Args: + flag: value to be emitted to the command line + Returns: + a string to be placed into the CROSSTOOL + """ + return "\n flag: '%s'" % flag def flags(*flags): - """Sugar for emitting sequence of flag fields. - - Examples: - flags("-foo", "-bar") -> - flag: '-foo' - flag: '-bar' + """Sugar for emitting sequence of flag fields. - Args: - *flags: values to be emitted to the command line + Examples: + flags("-foo", "-bar") -> + flag: '-foo' + flag: '-bar' - Returns: - a string to be placed into the CROSSTOOL - """ - return [flag(f) for f in flags] + Args: + *flags: values to be emitted to the command line + Returns: + a string to be placed into the CROSSTOOL + """ + return [flag(f) for f in flags] def _to_proto_value(boolean): - if boolean: - return "true" - else: - return "false" + if boolean: + return "true" + else: + return "false" diff --git a/tools/cpp/dummy_toolchain.bzl b/tools/cpp/dummy_toolchain.bzl index c787f7315d..45c0285d23 100644 --- a/tools/cpp/dummy_toolchain.bzl +++ b/tools/cpp/dummy_toolchain.bzl @@ -14,10 +14,10 @@ # limitations under the License. """Skylark rule that stubs a toolchain.""" + def _dummy_toolchain_impl(ctx): - ctx = ctx # unused argument - toolchain = platform_common.ToolchainInfo() - return [toolchain] + ctx = ctx # unused argument + toolchain = platform_common.ToolchainInfo() + return [toolchain] dummy_toolchain = rule(_dummy_toolchain_impl, attrs = {}) - diff --git a/tools/cpp/lib_cc_configure.bzl b/tools/cpp/lib_cc_configure.bzl index 899980638e..d7a46a3428 100644 --- a/tools/cpp/lib_cc_configure.bzl +++ b/tools/cpp/lib_cc_configure.bzl @@ -14,181 +14,185 @@ # limitations under the License. """Base library for configuring the C++ toolchain.""" - def resolve_labels(repository_ctx, labels): - """Resolves a collection of labels to their paths. - - Label resolution can cause the evaluation of Skylark functions to restart. - For functions with side-effects (like the auto-configuration functions, which - inspect the system and touch the file system), such restarts are costly. - We cannot avoid the restarts, but we can minimize their penalty by resolving - all labels upfront. + """Resolves a collection of labels to their paths. - Among other things, doing less work on restarts can cut analysis times by - several seconds and may also prevent tickling kernel conditions that cause - build failures. See https://github.com/bazelbuild/bazel/issues/5196 for - more details. + Label resolution can cause the evaluation of Skylark functions to restart. + For functions with side-effects (like the auto-configuration functions, which + inspect the system and touch the file system), such restarts are costly. + We cannot avoid the restarts, but we can minimize their penalty by resolving + all labels upfront. - Args: - repository_ctx: The context with which to resolve the labels. - labels: Labels to be resolved expressed as a list of strings. + Among other things, doing less work on restarts can cut analysis times by + several seconds and may also prevent tickling kernel conditions that cause + build failures. See https://github.com/bazelbuild/bazel/issues/5196 for + more details. - Returns: - A dictionary with the labels as keys and their paths as values. - """ - return dict([(label, repository_ctx.path(Label(label))) for label in labels]) + Args: + repository_ctx: The context with which to resolve the labels. + labels: Labels to be resolved expressed as a list of strings. + Returns: + A dictionary with the labels as keys and their paths as values. + """ + return dict([(label, repository_ctx.path(Label(label))) for label in labels]) def escape_string(arg): - """Escape percent sign (%) in the string so it can appear in the Crosstool.""" - if arg != None: - return str(arg).replace("%", "%%") - else: - return None - + """Escape percent sign (%) in the string so it can appear in the Crosstool.""" + if arg != None: + return str(arg).replace("%", "%%") + else: + return None def split_escaped(string, delimiter): - """Split string on the delimiter unless %-escaped. - - Examples: - Basic usage: - split_escaped("a:b:c", ":") -> [ "a", "b", "c" ] - - Delimeter that is not supposed to be splitten on has to be %-escaped: - split_escaped("a%:b", ":") -> [ "a:b" ] - - Literal % can be represented by escaping it as %%: - split_escaped("a%%b", ":") -> [ "a%b" ] - - Consecutive delimiters produce empty strings: - split_escaped("a::b", ":") -> [ "a", "", "", "b" ] - - Args: - string: The string to be split. - delimiter: Non-empty string not containing %-sign to be used as a - delimiter. - - Returns: - A list of substrings. - """ - if delimiter == "": fail("Delimiter cannot be empty") - if delimiter.find("%") != -1: fail("Delimiter cannot contain %-sign") - - i = 0 - result = [] - accumulator = [] - length = len(string) - delimiter_length = len(delimiter) - # Iterate over the length of string since Skylark doesn't have while loops - for _ in range(length): - if i >= length: - break - if i + 2 <= length and string[i : i + 2] == "%%": - accumulator.append("%") - i += 2 - elif (i + 1 + delimiter_length <= length and - string[i : i + 1 + delimiter_length] == "%" + delimiter): - accumulator.append(delimiter) - i += 1 + delimiter_length - elif i + delimiter_length <= length and string[i : i + delimiter_length] == delimiter: - result.append(''.join(accumulator)) - accumulator = [] - i += delimiter_length - else: - accumulator.append(string[i]) - i += 1 - - # Append the last group still in accumulator - result.append(''.join(accumulator)) - return result - + """Split string on the delimiter unless %-escaped. + + Examples: + Basic usage: + split_escaped("a:b:c", ":") -> [ "a", "b", "c" ] + + Delimeter that is not supposed to be splitten on has to be %-escaped: + split_escaped("a%:b", ":") -> [ "a:b" ] + + Literal % can be represented by escaping it as %%: + split_escaped("a%%b", ":") -> [ "a%b" ] + + Consecutive delimiters produce empty strings: + split_escaped("a::b", ":") -> [ "a", "", "", "b" ] + + Args: + string: The string to be split. + delimiter: Non-empty string not containing %-sign to be used as a + delimiter. + + Returns: + A list of substrings. + """ + if delimiter == "": + fail("Delimiter cannot be empty") + if delimiter.find("%") != -1: + fail("Delimiter cannot contain %-sign") + + i = 0 + result = [] + accumulator = [] + length = len(string) + delimiter_length = len(delimiter) + + # Iterate over the length of string since Skylark doesn't have while loops + for _ in range(length): + if i >= length: + break + if i + 2 <= length and string[i:i + 2] == "%%": + accumulator.append("%") + i += 2 + elif (i + 1 + delimiter_length <= length and + string[i:i + 1 + delimiter_length] == "%" + delimiter): + accumulator.append(delimiter) + i += 1 + delimiter_length + elif i + delimiter_length <= length and string[i:i + delimiter_length] == delimiter: + result.append("".join(accumulator)) + accumulator = [] + i += delimiter_length + else: + accumulator.append(string[i]) + i += 1 + + # Append the last group still in accumulator + result.append("".join(accumulator)) + return result def auto_configure_fail(msg): - """Output failure message when auto configuration fails.""" - red = "\033[0;31m" - no_color = "\033[0m" - fail("\n%sAuto-Configuration Error:%s %s\n" % (red, no_color, msg)) - + """Output failure message when auto configuration fails.""" + red = "\033[0;31m" + no_color = "\033[0m" + fail("\n%sAuto-Configuration Error:%s %s\n" % (red, no_color, msg)) def auto_configure_warning(msg): - """Output warning message during auto configuration.""" - yellow = "\033[1;33m" - no_color = "\033[0m" - print("\n%sAuto-Configuration Warning:%s %s\n" % (yellow, no_color, msg)) - + """Output warning message during auto configuration.""" + yellow = "\033[1;33m" + no_color = "\033[0m" + print("\n%sAuto-Configuration Warning:%s %s\n" % (yellow, no_color, msg)) def get_env_var(repository_ctx, name, default = None, enable_warning = True): - """Find an environment variable in system path. Doesn't %-escape the value!""" - if name in repository_ctx.os.environ: - return repository_ctx.os.environ[name] - if default != None: - if enable_warning: - auto_configure_warning("'%s' environment variable is not set, using '%s' as default" % (name, default)) - return default - auto_configure_fail("'%s' environment variable is not set" % name) - + """Find an environment variable in system path. Doesn't %-escape the value!""" + if name in repository_ctx.os.environ: + return repository_ctx.os.environ[name] + if default != None: + if enable_warning: + auto_configure_warning("'%s' environment variable is not set, using '%s' as default" % (name, default)) + return default + auto_configure_fail("'%s' environment variable is not set" % name) def which(repository_ctx, cmd, default = None): - """A wrapper around repository_ctx.which() to provide a fallback value. Doesn't %-escape the value!""" - result = repository_ctx.which(cmd) - return default if result == None else str(result) - + """A wrapper around repository_ctx.which() to provide a fallback value. Doesn't %-escape the value!""" + result = repository_ctx.which(cmd) + return default if result == None else str(result) def which_cmd(repository_ctx, cmd, default = None): - """Find cmd in PATH using repository_ctx.which() and fail if cannot find it. Doesn't %-escape the cmd!""" - result = repository_ctx.which(cmd) - if result != None: + """Find cmd in PATH using repository_ctx.which() and fail if cannot find it. Doesn't %-escape the cmd!""" + result = repository_ctx.which(cmd) + if result != None: + return str(result) + path = get_env_var(repository_ctx, "PATH") + if default != None: + auto_configure_warning("Cannot find %s in PATH, using '%s' as default.\nPATH=%s" % (cmd, default, path)) + return default + auto_configure_fail("Cannot find %s in PATH, please make sure %s is installed and add its directory in PATH.\nPATH=%s" % (cmd, cmd, path)) return str(result) - path = get_env_var(repository_ctx, "PATH") - if default != None: - auto_configure_warning("Cannot find %s in PATH, using '%s' as default.\nPATH=%s" % (cmd, default, path)) - return default - auto_configure_fail("Cannot find %s in PATH, please make sure %s is installed and add its directory in PATH.\nPATH=%s" % (cmd, cmd, path)) - return str(result) - - -def execute(repository_ctx, command, environment = None, - expect_failure = False): - """Execute a command, return stdout if succeed and throw an error if it fails. Doesn't %-escape the result!""" - if environment: - result = repository_ctx.execute(command, environment = environment) - else: - result = repository_ctx.execute(command) - if expect_failure != (result.return_code != 0): - if expect_failure: - auto_configure_fail( - "expected failure, command %s, stderr: (%s)" % ( - command, result.stderr)) - else: - auto_configure_fail( - "non-zero exit code: %d, command %s, stderr: (%s)" % ( - result.return_code, command, result.stderr)) - stripped_stdout = result.stdout.strip() - if not stripped_stdout: - auto_configure_fail( - "empty output from command %s, stderr: (%s)" % (command, result.stderr)) - return stripped_stdout +def execute( + repository_ctx, + command, + environment = None, + expect_failure = False): + """Execute a command, return stdout if succeed and throw an error if it fails. Doesn't %-escape the result!""" + if environment: + result = repository_ctx.execute(command, environment = environment) + else: + result = repository_ctx.execute(command) + if expect_failure != (result.return_code != 0): + if expect_failure: + auto_configure_fail( + "expected failure, command %s, stderr: (%s)" % ( + command, + result.stderr, + ), + ) + else: + auto_configure_fail( + "non-zero exit code: %d, command %s, stderr: (%s)" % ( + result.return_code, + command, + result.stderr, + ), + ) + stripped_stdout = result.stdout.strip() + if not stripped_stdout: + auto_configure_fail( + "empty output from command %s, stderr: (%s)" % (command, result.stderr), + ) + return stripped_stdout def get_cpu_value(repository_ctx): - """Compute the cpu_value based on the OS name. Doesn't %-escape the result!""" - os_name = repository_ctx.os.name.lower() - if os_name.startswith("mac os"): - return "darwin" - if os_name.find("freebsd") != -1: - return "freebsd" - if os_name.find("windows") != -1: - return "x64_windows" - # Use uname to figure out whether we are on x86_32 or x86_64 - result = repository_ctx.execute(["uname", "-m"]) - if result.stdout.strip() in ["power", "ppc64le", "ppc", "ppc64"]: - return "ppc" - if result.stdout.strip() in ["arm", "armv7l", "aarch64"]: - return "arm" - return "k8" if result.stdout.strip() in ["amd64", "x86_64", "x64"] else "piii" - + """Compute the cpu_value based on the OS name. Doesn't %-escape the result!""" + os_name = repository_ctx.os.name.lower() + if os_name.startswith("mac os"): + return "darwin" + if os_name.find("freebsd") != -1: + return "freebsd" + if os_name.find("windows") != -1: + return "x64_windows" + + # Use uname to figure out whether we are on x86_32 or x86_64 + result = repository_ctx.execute(["uname", "-m"]) + if result.stdout.strip() in ["power", "ppc64le", "ppc", "ppc64"]: + return "ppc" + if result.stdout.strip() in ["arm", "armv7l", "aarch64"]: + return "arm" + return "k8" if result.stdout.strip() in ["amd64", "x86_64", "x64"] else "piii" def is_cc_configure_debug(repository_ctx): - """Returns True if CC_CONFIGURE_DEBUG is set to 1.""" - env = repository_ctx.os.environ - return "CC_CONFIGURE_DEBUG" in env and env["CC_CONFIGURE_DEBUG"] == "1" + """Returns True if CC_CONFIGURE_DEBUG is set to 1.""" + env = repository_ctx.os.environ + return "CC_CONFIGURE_DEBUG" in env and env["CC_CONFIGURE_DEBUG"] == "1" diff --git a/tools/cpp/osx_cc_configure.bzl b/tools/cpp/osx_cc_configure.bzl index 6787141ac6..aef9e34776 100644 --- a/tools/cpp/osx_cc_configure.bzl +++ b/tools/cpp/osx_cc_configure.bzl @@ -15,125 +15,146 @@ """Configuring the C++ toolchain on macOS.""" load("@bazel_tools//tools/osx:xcode_configure.bzl", "run_xcode_locator") - load( "@bazel_tools//tools/cpp:lib_cc_configure.bzl", "escape_string", "resolve_labels", ) - load( "@bazel_tools//tools/cpp:unix_cc_configure.bzl", - "get_escaped_cxx_inc_directories", - "get_env", + "configure_unix_toolchain", "find_cc", - "configure_unix_toolchain" + "get_env", + "get_escaped_cxx_inc_directories", ) - def _get_escaped_xcode_cxx_inc_directories(repository_ctx, cc, xcode_toolchains): - """Compute the list of default C++ include paths on Xcode-enabled darwin. - - Args: - repository_ctx: The repository context. - cc: The default C++ compiler on the local system. - xcode_toolchains: A list containing the xcode toolchains available - Returns: - include_paths: A list of builtin include paths. - """ + """Compute the list of default C++ include paths on Xcode-enabled darwin. - # TODO(cparsons): Falling back to the default C++ compiler builtin include - # paths shouldn't be unnecessary once all actions are using xcrun. - include_dirs = get_escaped_cxx_inc_directories(repository_ctx, cc, "-xc++") - for toolchain in xcode_toolchains: - include_dirs.append(escape_string(toolchain.developer_dir)) - return include_dirs + Args: + repository_ctx: The repository context. + cc: The default C++ compiler on the local system. + xcode_toolchains: A list containing the xcode toolchains available + Returns: + include_paths: A list of builtin include paths. + """ + # TODO(cparsons): Falling back to the default C++ compiler builtin include + # paths shouldn't be unnecessary once all actions are using xcrun. + include_dirs = get_escaped_cxx_inc_directories(repository_ctx, cc, "-xc++") + for toolchain in xcode_toolchains: + include_dirs.append(escape_string(toolchain.developer_dir)) + return include_dirs def configure_osx_toolchain(repository_ctx, overriden_tools): - """Configure C++ toolchain on macOS.""" - paths = resolve_labels(repository_ctx, [ - "@bazel_tools//tools/cpp:osx_cc_wrapper.sh.tpl", - "@bazel_tools//tools/objc:libtool.sh", - "@bazel_tools//tools/objc:make_hashed_objlist.py", - "@bazel_tools//tools/objc:xcrunwrapper.sh", - "@bazel_tools//tools/osx/crosstool:BUILD.tpl", - "@bazel_tools//tools/osx/crosstool:CROSSTOOL.tpl", - "@bazel_tools//tools/osx/crosstool:osx_archs.bzl", - "@bazel_tools//tools/osx/crosstool:wrapped_ar.tpl", - "@bazel_tools//tools/osx/crosstool:wrapped_clang.cc", - "@bazel_tools//tools/osx/crosstool:wrapped_clang.tpl", - "@bazel_tools//tools/osx/crosstool:wrapped_clang_pp.tpl", - "@bazel_tools//tools/osx:xcode_locator.m", - ]) + """Configure C++ toolchain on macOS.""" + paths = resolve_labels(repository_ctx, [ + "@bazel_tools//tools/cpp:osx_cc_wrapper.sh.tpl", + "@bazel_tools//tools/objc:libtool.sh", + "@bazel_tools//tools/objc:make_hashed_objlist.py", + "@bazel_tools//tools/objc:xcrunwrapper.sh", + "@bazel_tools//tools/osx/crosstool:BUILD.tpl", + "@bazel_tools//tools/osx/crosstool:CROSSTOOL.tpl", + "@bazel_tools//tools/osx/crosstool:osx_archs.bzl", + "@bazel_tools//tools/osx/crosstool:wrapped_ar.tpl", + "@bazel_tools//tools/osx/crosstool:wrapped_clang.cc", + "@bazel_tools//tools/osx/crosstool:wrapped_clang.tpl", + "@bazel_tools//tools/osx/crosstool:wrapped_clang_pp.tpl", + "@bazel_tools//tools/osx:xcode_locator.m", + ]) - xcode_toolchains = [] - (xcode_toolchains, xcodeloc_err) = run_xcode_locator( - repository_ctx, - paths["@bazel_tools//tools/osx:xcode_locator.m"]) - if xcode_toolchains: - cc = find_cc(repository_ctx, overriden_tools = {}) - repository_ctx.template( - "cc_wrapper.sh", - paths["@bazel_tools//tools/cpp:osx_cc_wrapper.sh.tpl"], - { - "%{cc}": escape_string(str(cc)), - "%{env}": escape_string(get_env(repository_ctx)), - }) - repository_ctx.symlink( - paths["@bazel_tools//tools/objc:xcrunwrapper.sh"], "xcrunwrapper.sh") - repository_ctx.symlink( - paths["@bazel_tools//tools/objc:libtool.sh"], "libtool") - repository_ctx.symlink( - paths["@bazel_tools//tools/objc:make_hashed_objlist.py"], - "make_hashed_objlist.py") - repository_ctx.symlink( - paths["@bazel_tools//tools/osx/crosstool:wrapped_ar.tpl"], - "wrapped_ar") - repository_ctx.symlink( - paths["@bazel_tools//tools/osx/crosstool:BUILD.tpl"], - "BUILD") - repository_ctx.symlink( - paths["@bazel_tools//tools/osx/crosstool:osx_archs.bzl"], - "osx_archs.bzl") + xcode_toolchains = [] + (xcode_toolchains, xcodeloc_err) = run_xcode_locator( + repository_ctx, + paths["@bazel_tools//tools/osx:xcode_locator.m"], + ) + if xcode_toolchains: + cc = find_cc(repository_ctx, overriden_tools = {}) + repository_ctx.template( + "cc_wrapper.sh", + paths["@bazel_tools//tools/cpp:osx_cc_wrapper.sh.tpl"], + { + "%{cc}": escape_string(str(cc)), + "%{env}": escape_string(get_env(repository_ctx)), + }, + ) + repository_ctx.symlink( + paths["@bazel_tools//tools/objc:xcrunwrapper.sh"], + "xcrunwrapper.sh", + ) + repository_ctx.symlink( + paths["@bazel_tools//tools/objc:libtool.sh"], + "libtool", + ) + repository_ctx.symlink( + paths["@bazel_tools//tools/objc:make_hashed_objlist.py"], + "make_hashed_objlist.py", + ) + repository_ctx.symlink( + paths["@bazel_tools//tools/osx/crosstool:wrapped_ar.tpl"], + "wrapped_ar", + ) + repository_ctx.symlink( + paths["@bazel_tools//tools/osx/crosstool:BUILD.tpl"], + "BUILD", + ) + repository_ctx.symlink( + paths["@bazel_tools//tools/osx/crosstool:osx_archs.bzl"], + "osx_archs.bzl", + ) - wrapped_clang_src_path = str(repository_ctx.path( - paths["@bazel_tools//tools/osx/crosstool:wrapped_clang.cc"])) - xcrun_result = repository_ctx.execute(["env", "-i", "xcrun", "clang", "-std=c++11", "-lc++", - "-o", "wrapped_clang", wrapped_clang_src_path], 30) - if (xcrun_result.return_code == 0): - repository_ctx.symlink("wrapped_clang", "wrapped_clang_pp") - else: - # If for some reason wrapped_clang couldn't be built, fall back to - # using the bash scripts that don't support dSYM generation. This is to - # avoid completely breaking a build. This should be removed after a whole - # release cycle to keep from increasing code maintenance, if we haven't - # received any issues as requested below. - error_msg = ( - "return code {code}, stderr: {err}, stdout: {out}").format( - code=xcrun_result.return_code, - err=xcrun_result.stderr, - out=xcrun_result.stdout) - print("wrapped_clang failed to generate. This shouldn't cause " + - "problems, but please file an issue at " + - "https://github.com/bazelbuild/bazel/issues with the following:\n" + - error_msg) - repository_ctx.symlink( - paths["@bazel_tools//tools/osx/crosstool:wrapped_clang.tpl"], - "wrapped_clang") - repository_ctx.symlink( - paths["@bazel_tools//tools/osx/crosstool:wrapped_clang_pp.tpl"], - "wrapped_clang_pp") + wrapped_clang_src_path = str(repository_ctx.path( + paths["@bazel_tools//tools/osx/crosstool:wrapped_clang.cc"], + )) + xcrun_result = repository_ctx.execute([ + "env", + "-i", + "xcrun", + "clang", + "-std=c++11", + "-lc++", + "-o", + "wrapped_clang", + wrapped_clang_src_path, + ], 30) + if (xcrun_result.return_code == 0): + repository_ctx.symlink("wrapped_clang", "wrapped_clang_pp") + else: + # If for some reason wrapped_clang couldn't be built, fall back to + # using the bash scripts that don't support dSYM generation. This is to + # avoid completely breaking a build. This should be removed after a whole + # release cycle to keep from increasing code maintenance, if we haven't + # received any issues as requested below. + error_msg = ( + "return code {code}, stderr: {err}, stdout: {out}" + ).format( + code = xcrun_result.return_code, + err = xcrun_result.stderr, + out = xcrun_result.stdout, + ) + print("wrapped_clang failed to generate. This shouldn't cause " + + "problems, but please file an issue at " + + "https://github.com/bazelbuild/bazel/issues with the following:\n" + + error_msg) + repository_ctx.symlink( + paths["@bazel_tools//tools/osx/crosstool:wrapped_clang.tpl"], + "wrapped_clang", + ) + repository_ctx.symlink( + paths["@bazel_tools//tools/osx/crosstool:wrapped_clang_pp.tpl"], + "wrapped_clang_pp", + ) - escaped_include_paths = _get_escaped_xcode_cxx_inc_directories(repository_ctx, cc, xcode_toolchains) - escaped_cxx_include_directories = [] - for path in escaped_include_paths: - escaped_cxx_include_directories.append(("cxx_builtin_include_directory: \"%s\"" % path)) - if xcodeloc_err: - escaped_cxx_include_directories.append("# Error: " + xcodeloc_err + "\n") - repository_ctx.template( - "CROSSTOOL", - paths["@bazel_tools//tools/osx/crosstool:CROSSTOOL.tpl"], - {"%{cxx_builtin_include_directory}": "\n".join(escaped_cxx_include_directories)}) - else: - configure_unix_toolchain(repository_ctx, cpu_value = "darwin", overriden_tools = overriden_tools) + escaped_include_paths = _get_escaped_xcode_cxx_inc_directories(repository_ctx, cc, xcode_toolchains) + escaped_cxx_include_directories = [] + for path in escaped_include_paths: + escaped_cxx_include_directories.append(("cxx_builtin_include_directory: \"%s\"" % path)) + if xcodeloc_err: + escaped_cxx_include_directories.append("# Error: " + xcodeloc_err + "\n") + repository_ctx.template( + "CROSSTOOL", + paths["@bazel_tools//tools/osx/crosstool:CROSSTOOL.tpl"], + {"%{cxx_builtin_include_directory}": "\n".join(escaped_cxx_include_directories)}, + ) + else: + configure_unix_toolchain(repository_ctx, cpu_value = "darwin", overriden_tools = overriden_tools) diff --git a/tools/cpp/toolchain_utils.bzl b/tools/cpp/toolchain_utils.bzl index 5ab9ade50c..e879299cd1 100644 --- a/tools/cpp/toolchain_utils.bzl +++ b/tools/cpp/toolchain_utils.bzl @@ -21,20 +21,20 @@ the CppConfiguration. """ def find_cpp_toolchain(ctx): - """ - Finds the c++ toolchain. + """ + Finds the c++ toolchain. - If the c++ toolchain is in use, returns it. Otherwise, returns a c++ - toolchain derived from legacy toolchain selection. + If the c++ toolchain is in use, returns it. Otherwise, returns a c++ + toolchain derived from legacy toolchain selection. - Args: - ctx: The rule context for which to find a toolchain. + Args: + ctx: The rule context for which to find a toolchain. - Returns: - A CcToolchainProvider. - """ + Returns: + A CcToolchainProvider. + """ - if Label("@bazel_tools//tools/cpp:toolchain_type") in ctx.fragments.platform.enabled_toolchain_types: - return ctx.toolchains["@bazel_tools//tools/cpp:toolchain_type"] - else: - return ctx.attr._cc_toolchain[cc_common.CcToolchainInfo] + if Label("@bazel_tools//tools/cpp:toolchain_type") in ctx.fragments.platform.enabled_toolchain_types: + return ctx.toolchains["@bazel_tools//tools/cpp:toolchain_type"] + else: + return ctx.attr._cc_toolchain[cc_common.CcToolchainInfo] diff --git a/tools/cpp/unix_cc_configure.bzl b/tools/cpp/unix_cc_configure.bzl index d4b25c8776..82cb60d5b9 100644 --- a/tools/cpp/unix_cc_configure.bzl +++ b/tools/cpp/unix_cc_configure.bzl @@ -14,11 +14,10 @@ # limitations under the License. """Configuring the C++ toolchain on Unix platforms.""" - load( "@bazel_tools//tools/cpp:lib_cc_configure.bzl", - "auto_configure_warning", "auto_configure_fail", + "auto_configure_warning", "escape_string", "get_env_var", "resolve_labels", @@ -27,349 +26,371 @@ load( ) def _uniq(iterable): - """Remove duplicates from a list.""" + """Remove duplicates from a list.""" - unique_elements = {element: None for element in iterable} - return unique_elements.keys() + unique_elements = {element: None for element in iterable} + return unique_elements.keys() def _prepare_include_path(repo_ctx, path): - """Resolve and sanitize include path before outputting it into the crosstool. + """Resolve and sanitize include path before outputting it into the crosstool. - Args: - repo_ctx: repository_ctx object. - path: an include path to be sanitized. + Args: + repo_ctx: repository_ctx object. + path: an include path to be sanitized. - Returns: - Sanitized include path that can be written to the crosstoot. Resulting path - is absolute if it is outside the repository and relative otherwise. - """ + Returns: + Sanitized include path that can be written to the crosstoot. Resulting path + is absolute if it is outside the repository and relative otherwise. + """ - repo_root = str(repo_ctx.path(".")) - # We're on UNIX, so the path delimiter is '/'. - repo_root += "/" - path = str(repo_ctx.path(path)) - if path.startswith(repo_root): - return escape_string(path[len(repo_root):]) - return escape_string(path) + repo_root = str(repo_ctx.path(".")) + + # We're on UNIX, so the path delimiter is '/'. + repo_root += "/" + path = str(repo_ctx.path(path)) + if path.startswith(repo_root): + return escape_string(path[len(repo_root):]) + return escape_string(path) def _get_value(it): - """Convert `it` in serialized protobuf format.""" - if type(it) == "int": - return str(it) - elif type(it) == "bool": - return "true" if it else "false" - else: - return "\"%s\"" % it - - -def _build_crosstool(d, prefix=" "): - """Convert `d` to a string version of a CROSSTOOL file content.""" - lines = [] - for k in d: - if type(d[k]) == "list": - for it in d[k]: - lines.append("%s%s: %s" % (prefix, k, _get_value(it))) + """Convert `it` in serialized protobuf format.""" + if type(it) == "int": + return str(it) + elif type(it) == "bool": + return "true" if it else "false" else: - lines.append("%s%s: %s" % (prefix, k, _get_value(d[k]))) - return "\n".join(lines) - + return "\"%s\"" % it + +def _build_crosstool(d, prefix = " "): + """Convert `d` to a string version of a CROSSTOOL file content.""" + lines = [] + for k in d: + if type(d[k]) == "list": + for it in d[k]: + lines.append("%s%s: %s" % (prefix, k, _get_value(it))) + else: + lines.append("%s%s: %s" % (prefix, k, _get_value(d[k]))) + return "\n".join(lines) def _build_tool_path(d): - """Build the list of %-escaped tool_path for the CROSSTOOL file.""" - lines = [] - for k in d: - lines.append(" tool_path {name: \"%s\" path: \"%s\" }" % (k, escape_string(d[k]))) - return "\n".join(lines) + """Build the list of %-escaped tool_path for the CROSSTOOL file.""" + lines = [] + for k in d: + lines.append(" tool_path {name: \"%s\" path: \"%s\" }" % (k, escape_string(d[k]))) + return "\n".join(lines) def _find_tool(repository_ctx, tool, overriden_tools): - """Find a tool for repository, taking overriden tools into account.""" - if tool in overriden_tools: - return overriden_tools[tool] - return which(repository_ctx, tool, "/usr/bin/" + tool) + """Find a tool for repository, taking overriden tools into account.""" + if tool in overriden_tools: + return overriden_tools[tool] + return which(repository_ctx, tool, "/usr/bin/" + tool) def _get_tool_paths(repository_ctx, overriden_tools): - """Compute the path to the various tools. Doesn't %-escape the result!""" - return dict({k: _find_tool(repository_ctx, k, overriden_tools) - for k in [ - "ar", - "ld", - "cpp", - "gcc", - "dwp", - "gcov", - "nm", - "objcopy", - "objdump", - "strip", - ]}.items()) + """Compute the path to the various tools. Doesn't %-escape the result!""" + return dict({ + k: _find_tool(repository_ctx, k, overriden_tools) + for k in [ + "ar", + "ld", + "cpp", + "gcc", + "dwp", + "gcov", + "nm", + "objcopy", + "objdump", + "strip", + ] + }.items()) def _escaped_cplus_include_paths(repository_ctx): - """Use ${CPLUS_INCLUDE_PATH} to compute the %-escaped list of flags for cxxflag.""" - if "CPLUS_INCLUDE_PATH" in repository_ctx.os.environ: - result = [] - for p in repository_ctx.os.environ["CPLUS_INCLUDE_PATH"].split(":"): - p = escape_string(str(repository_ctx.path(p))) # Normalize the path - result.append("-I" + p) - return result - else: - return [] - + """Use ${CPLUS_INCLUDE_PATH} to compute the %-escaped list of flags for cxxflag.""" + if "CPLUS_INCLUDE_PATH" in repository_ctx.os.environ: + result = [] + for p in repository_ctx.os.environ["CPLUS_INCLUDE_PATH"].split(":"): + p = escape_string(str(repository_ctx.path(p))) # Normalize the path + result.append("-I" + p) + return result + else: + return [] _INC_DIR_MARKER_BEGIN = "#include <...>" # OSX add " (framework directory)" at the end of line, strip it. _OSX_FRAMEWORK_SUFFIX = " (framework directory)" -_OSX_FRAMEWORK_SUFFIX_LEN = len(_OSX_FRAMEWORK_SUFFIX) +_OSX_FRAMEWORK_SUFFIX_LEN = len(_OSX_FRAMEWORK_SUFFIX) def _cxx_inc_convert(path): - """Convert path returned by cc -E xc++ in a complete path. Doesn't %-escape the path!""" - path = path.strip() - if path.endswith(_OSX_FRAMEWORK_SUFFIX): - path = path[:-_OSX_FRAMEWORK_SUFFIX_LEN].strip() - return path - + """Convert path returned by cc -E xc++ in a complete path. Doesn't %-escape the path!""" + path = path.strip() + if path.endswith(_OSX_FRAMEWORK_SUFFIX): + path = path[:-_OSX_FRAMEWORK_SUFFIX_LEN].strip() + return path def get_escaped_cxx_inc_directories(repository_ctx, cc, lang_flag, additional_flags = []): - """Compute the list of default %-escaped C++ include directories.""" - result = repository_ctx.execute([cc, "-E", lang_flag, "-", "-v"] + additional_flags) - index1 = result.stderr.find(_INC_DIR_MARKER_BEGIN) - if index1 == -1: - return [] - index1 = result.stderr.find("\n", index1) - if index1 == -1: - return [] - index2 = result.stderr.rfind("\n ") - if index2 == -1 or index2 < index1: - return [] - index2 = result.stderr.find("\n", index2 + 1) - if index2 == -1: - inc_dirs = result.stderr[index1 + 1:] - else: - inc_dirs = result.stderr[index1 + 1:index2].strip() - - return [_prepare_include_path(repository_ctx, _cxx_inc_convert(p)) - for p in inc_dirs.split("\n")] + """Compute the list of default %-escaped C++ include directories.""" + result = repository_ctx.execute([cc, "-E", lang_flag, "-", "-v"] + additional_flags) + index1 = result.stderr.find(_INC_DIR_MARKER_BEGIN) + if index1 == -1: + return [] + index1 = result.stderr.find("\n", index1) + if index1 == -1: + return [] + index2 = result.stderr.rfind("\n ") + if index2 == -1 or index2 < index1: + return [] + index2 = result.stderr.find("\n", index2 + 1) + if index2 == -1: + inc_dirs = result.stderr[index1 + 1:] + else: + inc_dirs = result.stderr[index1 + 1:index2].strip() + return [ + _prepare_include_path(repository_ctx, _cxx_inc_convert(p)) + for p in inc_dirs.split("\n") + ] def _is_option_supported(repository_ctx, cc, option): - """Checks that `option` is supported by the C compiler. Doesn't %-escape the option.""" - result = repository_ctx.execute([ - cc, - option, - "-o", - "/dev/null", - "-c", - str(repository_ctx.path("tools/cpp/empty.cc")) - ]) - return result.stderr.find(option) == -1 - + """Checks that `option` is supported by the C compiler. Doesn't %-escape the option.""" + result = repository_ctx.execute([ + cc, + option, + "-o", + "/dev/null", + "-c", + str(repository_ctx.path("tools/cpp/empty.cc")), + ]) + return result.stderr.find(option) == -1 def _add_option_if_supported(repository_ctx, cc, option): - """Returns `[option]` if supported, `[]` otherwise. Doesn't %-escape the option.""" - return [option] if _is_option_supported(repository_ctx, cc, option) else [] - + """Returns `[option]` if supported, `[]` otherwise. Doesn't %-escape the option.""" + return [option] if _is_option_supported(repository_ctx, cc, option) else [] def _is_gold_supported(repository_ctx, cc): - """Checks that `gold` is supported by the C compiler.""" - result = repository_ctx.execute([ - cc, - "-fuse-ld=gold", - "-o", - "/dev/null", - # Some macos clang versions don't fail when setting -fuse-ld=gold, adding - # these lines to force it to. This also means that we will not detect - # gold when only a very old (year 2010 and older) is present. - "-Wl,--start-lib", - "-Wl,--end-lib", - str(repository_ctx.path("tools/cpp/empty.cc")) - ]) - return result.return_code == 0 + """Checks that `gold` is supported by the C compiler.""" + result = repository_ctx.execute([ + cc, + "-fuse-ld=gold", + "-o", + "/dev/null", + # Some macos clang versions don't fail when setting -fuse-ld=gold, adding + # these lines to force it to. This also means that we will not detect + # gold when only a very old (year 2010 and older) is present. + "-Wl,--start-lib", + "-Wl,--end-lib", + str(repository_ctx.path("tools/cpp/empty.cc")), + ]) + return result.return_code == 0 def _get_no_canonical_prefixes_opt(repository_ctx, cc): - # If the compiler sometimes rewrites paths in the .d files without symlinks - # (ie when they're shorter), it confuses Bazel's logic for verifying all - # #included header files are listed as inputs to the action. - - # The '-fno-canonical-system-headers' should be enough, but clang does not - # support it, so we also try '-no-canonical-prefixes' if first option does - # not work. - opt = _add_option_if_supported(repository_ctx, cc, - "-fno-canonical-system-headers") - if len(opt) == 0: - return _add_option_if_supported(repository_ctx, cc, - "-no-canonical-prefixes") - return opt + # If the compiler sometimes rewrites paths in the .d files without symlinks + # (ie when they're shorter), it confuses Bazel's logic for verifying all + # #included header files are listed as inputs to the action. + + # The '-fno-canonical-system-headers' should be enough, but clang does not + # support it, so we also try '-no-canonical-prefixes' if first option does + # not work. + opt = _add_option_if_supported( + repository_ctx, + cc, + "-fno-canonical-system-headers", + ) + if len(opt) == 0: + return _add_option_if_supported( + repository_ctx, + cc, + "-no-canonical-prefixes", + ) + return opt def _crosstool_content(repository_ctx, cc, cpu_value, darwin): - """Return the content for the CROSSTOOL file, in a dictionary.""" - supports_gold_linker = _is_gold_supported(repository_ctx, cc) - cc_path = repository_ctx.path(cc) - if not str(cc_path).startswith(str(repository_ctx.path(".")) + '/'): - # cc is outside the repository, set -B - bin_search_flag = ["-B" + escape_string(str(cc_path.dirname))] - else: - # cc is inside the repository, don't set -B. - bin_search_flag = [] - - escaped_cxx_include_directories = _uniq( - get_escaped_cxx_inc_directories(repository_ctx, cc, "-xc") + - get_escaped_cxx_inc_directories(repository_ctx, cc, "-xc++") + - get_escaped_cxx_inc_directories( - repository_ctx, cc, "-xc", _get_no_canonical_prefixes_opt(repository_ctx, cc)) + - get_escaped_cxx_inc_directories( - repository_ctx, cc, "-xc++", _get_no_canonical_prefixes_opt(repository_ctx, cc))) - return { - "abi_version": escape_string(get_env_var(repository_ctx, "ABI_VERSION", "local", False)), - "abi_libc_version": escape_string(get_env_var(repository_ctx, "ABI_LIBC_VERSION", "local", False)), - "builtin_sysroot": "", - "compiler": escape_string(get_env_var(repository_ctx, "BAZEL_COMPILER", "compiler", False)), - "host_system_name": escape_string(get_env_var(repository_ctx, "BAZEL_HOST_SYSTEM", "local", False)), - "needsPic": True, - "supports_gold_linker": supports_gold_linker, - "supports_incremental_linker": False, - "supports_fission": False, - "supports_interface_shared_objects": False, - "supports_normalizing_ar": False, - "supports_start_end_lib": supports_gold_linker, - "target_libc": "macosx" if darwin else escape_string(get_env_var(repository_ctx, "BAZEL_TARGET_LIBC", "local", False)), - "target_cpu": escape_string(get_env_var(repository_ctx, "BAZEL_TARGET_CPU", cpu_value, False)), - "target_system_name": escape_string(get_env_var(repository_ctx, "BAZEL_TARGET_SYSTEM", "local", False)), - "cxx_flag": [ - "-std=c++0x", - ] + _escaped_cplus_include_paths(repository_ctx), - "linker_flag": ( - ["-fuse-ld=gold"] if supports_gold_linker else [] - ) + _add_option_if_supported( - repository_ctx, cc, "-Wl,-no-as-needed" - ) + _add_option_if_supported( - repository_ctx, cc, "-Wl,-z,relro,-z,now" - ) + ([ - "-undefined", - "dynamic_lookup", - "-headerpad_max_install_names", - ] if darwin else bin_search_flag + [ - # Always have -B/usr/bin, see https://github.com/bazelbuild/bazel/issues/760. - "-B/usr/bin", - # Gold linker only? Can we enable this by default? - # "-Wl,--warn-execstack", - # "-Wl,--detect-odr-violations" - ] + _add_option_if_supported( - # Have gcc return the exit code from ld. - repository_ctx, cc, "-pass-exit-codes") - ) + split_escaped( - get_env_var(repository_ctx, "BAZEL_LINKOPTS", "-lstdc++:-lm", False), ":"), - "cxx_builtin_include_directory": escaped_cxx_include_directories, - "objcopy_embed_flag": ["-I", "binary"], - "unfiltered_cxx_flag": - _get_no_canonical_prefixes_opt(repository_ctx, cc) + [ - # Make C++ compilation deterministic. Use linkstamping instead of these - # compiler symbols. - "-Wno-builtin-macro-redefined", - "-D__DATE__=\\\"redacted\\\"", - "-D__TIMESTAMP__=\\\"redacted\\\"", - "-D__TIME__=\\\"redacted\\\"" - ], - "compiler_flag": [ - # Security hardening requires optimization. - # We need to undef it as some distributions now have it enabled by default. - "-U_FORTIFY_SOURCE", - "-fstack-protector", - # All warnings are enabled. Maybe enable -Werror as well? - "-Wall", - # Enable a few more warnings that aren't part of -Wall. - ] + (["-Wthread-safety", "-Wself-assign"] if darwin else bin_search_flag + [ - # Always have -B/usr/bin, see https://github.com/bazelbuild/bazel/issues/760. - "-B/usr/bin", - ]) + ( - # Disable problematic warnings. - _add_option_if_supported(repository_ctx, cc, "-Wunused-but-set-parameter") + - # has false positives - _add_option_if_supported(repository_ctx, cc, "-Wno-free-nonheap-object") + - # Enable coloring even if there's no attached terminal. Bazel removes the - # escape sequences if --nocolor is specified. - _add_option_if_supported(repository_ctx, cc, "-fcolor-diagnostics")) + [ - # Keep stack frames for debugging, even in opt mode. - "-fno-omit-frame-pointer", - ], - } - + """Return the content for the CROSSTOOL file, in a dictionary.""" + supports_gold_linker = _is_gold_supported(repository_ctx, cc) + cc_path = repository_ctx.path(cc) + if not str(cc_path).startswith(str(repository_ctx.path(".")) + "/"): + # cc is outside the repository, set -B + bin_search_flag = ["-B" + escape_string(str(cc_path.dirname))] + else: + # cc is inside the repository, don't set -B. + bin_search_flag = [] + + escaped_cxx_include_directories = _uniq( + get_escaped_cxx_inc_directories(repository_ctx, cc, "-xc") + + get_escaped_cxx_inc_directories(repository_ctx, cc, "-xc++") + + get_escaped_cxx_inc_directories( + repository_ctx, + cc, + "-xc", + _get_no_canonical_prefixes_opt(repository_ctx, cc), + ) + + get_escaped_cxx_inc_directories( + repository_ctx, + cc, + "-xc++", + _get_no_canonical_prefixes_opt(repository_ctx, cc), + ), + ) + return { + "abi_version": escape_string(get_env_var(repository_ctx, "ABI_VERSION", "local", False)), + "abi_libc_version": escape_string(get_env_var(repository_ctx, "ABI_LIBC_VERSION", "local", False)), + "builtin_sysroot": "", + "compiler": escape_string(get_env_var(repository_ctx, "BAZEL_COMPILER", "compiler", False)), + "host_system_name": escape_string(get_env_var(repository_ctx, "BAZEL_HOST_SYSTEM", "local", False)), + "needsPic": True, + "supports_gold_linker": supports_gold_linker, + "supports_incremental_linker": False, + "supports_fission": False, + "supports_interface_shared_objects": False, + "supports_normalizing_ar": False, + "supports_start_end_lib": supports_gold_linker, + "target_libc": "macosx" if darwin else escape_string(get_env_var(repository_ctx, "BAZEL_TARGET_LIBC", "local", False)), + "target_cpu": escape_string(get_env_var(repository_ctx, "BAZEL_TARGET_CPU", cpu_value, False)), + "target_system_name": escape_string(get_env_var(repository_ctx, "BAZEL_TARGET_SYSTEM", "local", False)), + "cxx_flag": [ + "-std=c++0x", + ] + _escaped_cplus_include_paths(repository_ctx), + "linker_flag": ( + ["-fuse-ld=gold"] if supports_gold_linker else [] + ) + _add_option_if_supported( + repository_ctx, + cc, + "-Wl,-no-as-needed", + ) + _add_option_if_supported( + repository_ctx, + cc, + "-Wl,-z,relro,-z,now", + ) + ( + [ + "-undefined", + "dynamic_lookup", + "-headerpad_max_install_names", + ] if darwin else bin_search_flag + [ + # Always have -B/usr/bin, see https://github.com/bazelbuild/bazel/issues/760. + "-B/usr/bin", + # Gold linker only? Can we enable this by default? + # "-Wl,--warn-execstack", + # "-Wl,--detect-odr-violations" + ] + _add_option_if_supported( + # Have gcc return the exit code from ld. + repository_ctx, + cc, + "-pass-exit-codes", + ) + ) + split_escaped( + get_env_var(repository_ctx, "BAZEL_LINKOPTS", "-lstdc++:-lm", False), + ":", + ), + "cxx_builtin_include_directory": escaped_cxx_include_directories, + "objcopy_embed_flag": ["-I", "binary"], + "unfiltered_cxx_flag": _get_no_canonical_prefixes_opt(repository_ctx, cc) + [ + # Make C++ compilation deterministic. Use linkstamping instead of these + # compiler symbols. + "-Wno-builtin-macro-redefined", + "-D__DATE__=\\\"redacted\\\"", + "-D__TIMESTAMP__=\\\"redacted\\\"", + "-D__TIME__=\\\"redacted\\\"", + ], + "compiler_flag": [ + # Security hardening requires optimization. + # We need to undef it as some distributions now have it enabled by default. + "-U_FORTIFY_SOURCE", + "-fstack-protector", + # All warnings are enabled. Maybe enable -Werror as well? + "-Wall", + # Enable a few more warnings that aren't part of -Wall. + ] + (["-Wthread-safety", "-Wself-assign"] if darwin else bin_search_flag + [ + # Always have -B/usr/bin, see https://github.com/bazelbuild/bazel/issues/760. + "-B/usr/bin", + ]) + ( + # Disable problematic warnings. + _add_option_if_supported(repository_ctx, cc, "-Wunused-but-set-parameter") + + # has false positives + _add_option_if_supported(repository_ctx, cc, "-Wno-free-nonheap-object") + + # Enable coloring even if there's no attached terminal. Bazel removes the + # escape sequences if --nocolor is specified. + _add_option_if_supported(repository_ctx, cc, "-fcolor-diagnostics") + ) + [ + # Keep stack frames for debugging, even in opt mode. + "-fno-omit-frame-pointer", + ], + } def _opt_content(darwin): - """Return the content of the opt specific section of the CROSSTOOL file.""" - return { - "compiler_flag": [ - # No debug symbols. - # Maybe we should enable https://gcc.gnu.org/wiki/DebugFission for opt or - # even generally? However, that can't happen here, as it requires special - # handling in Bazel. - "-g0", - - # Conservative choice for -O - # -O3 can increase binary size and even slow down the resulting binaries. - # Profile first and / or use FDO if you need better performance than this. - "-O2", - - # Security hardening on by default. - # Conservative choice; -D_FORTIFY_SOURCE=2 may be unsafe in some cases. - "-D_FORTIFY_SOURCE=1", - - # Disable assertions - "-DNDEBUG", - - # Removal of unused code and data at link time (can this increase binary size in some cases?). - "-ffunction-sections", - "-fdata-sections" - ], - "linker_flag": [] if darwin else ["-Wl,--gc-sections"] - } - + """Return the content of the opt specific section of the CROSSTOOL file.""" + return { + "compiler_flag": [ + # No debug symbols. + # Maybe we should enable https://gcc.gnu.org/wiki/DebugFission for opt or + # even generally? However, that can't happen here, as it requires special + # handling in Bazel. + "-g0", + + # Conservative choice for -O + # -O3 can increase binary size and even slow down the resulting binaries. + # Profile first and / or use FDO if you need better performance than this. + "-O2", + + # Security hardening on by default. + # Conservative choice; -D_FORTIFY_SOURCE=2 may be unsafe in some cases. + "-D_FORTIFY_SOURCE=1", + + # Disable assertions + "-DNDEBUG", + + # Removal of unused code and data at link time (can this increase binary size in some cases?). + "-ffunction-sections", + "-fdata-sections", + ], + "linker_flag": [] if darwin else ["-Wl,--gc-sections"], + } def _dbg_content(): - """Return the content of the dbg specific section of the CROSSTOOL file.""" - # Enable debug symbols - return {"compiler_flag": "-g"} + """Return the content of the dbg specific section of the CROSSTOOL file.""" + # Enable debug symbols + return {"compiler_flag": "-g"} def get_env(repository_ctx): - """Convert the environment in a list of export if in Homebrew. Doesn't %-escape the result!""" - env = repository_ctx.os.environ - if "HOMEBREW_RUBY_PATH" in env: - return "\n".join([ - "export %s='%s'" % (k, env[k].replace("'", "'\\''")) - for k in env - if k != "_" and k.find(".") == -1 - ]) - else: - return "" - + """Convert the environment in a list of export if in Homebrew. Doesn't %-escape the result!""" + env = repository_ctx.os.environ + if "HOMEBREW_RUBY_PATH" in env: + return "\n".join([ + "export %s='%s'" % (k, env[k].replace("'", "'\\''")) + for k in env + if k != "_" and k.find(".") == -1 + ]) + else: + return "" def _coverage_feature(repository_ctx, darwin): - use_llvm_cov = "1" == get_env_var( - repository_ctx, - "BAZEL_USE_LLVM_NATIVE_COVERAGE", - default="0", - enable_warning=False) - if darwin or use_llvm_cov: - compile_flags = """flag_group { + use_llvm_cov = "1" == get_env_var( + repository_ctx, + "BAZEL_USE_LLVM_NATIVE_COVERAGE", + default = "0", + enable_warning = False, + ) + if darwin or use_llvm_cov: + compile_flags = """flag_group { flag: '-fprofile-instr-generate' flag: '-fcoverage-mapping' }""" - link_flags = """flag_group { + link_flags = """flag_group { flag: '-fprofile-instr-generate' }""" - else: - # gcc requires --coverage being passed for compilation and linking - # https://gcc.gnu.org/onlinedocs/gcc/Instrumentation-Options.html#Instrumentation-Options - compile_flags = """flag_group { + else: + # gcc requires --coverage being passed for compilation and linking + # https://gcc.gnu.org/onlinedocs/gcc/Instrumentation-Options.html#Instrumentation-Options + compile_flags = """flag_group { flag: '--coverage' }""" - link_flags = """flag_group { + link_flags = """flag_group { flag: '--coverage' }""" - # Note that we also set --coverage for c++-link-nodeps-dynamic-library. The - # generated code contains references to gcov symbols, and the dynamic linker - # can't resolve them unless the library is linked against gcov. - return """ + + # Note that we also set --coverage for c++-link-nodeps-dynamic-library. The + # generated code contains references to gcov symbols, and the dynamic linker + # can't resolve them unless the library is linked against gcov. + return """ feature { name: 'coverage' provides: 'profile' @@ -392,111 +413,127 @@ def _coverage_feature(repository_ctx, darwin): """ def _find_generic(repository_ctx, name, env_name, overriden_tools, warn = False): - """Find a generic C++ toolchain tool. Doesn't %-escape the result.""" - - if name in overriden_tools: - return overriden_tools[name] - - result = name - env_value = repository_ctx.os.environ.get(env_name) - env_value_with_paren = "" - if env_value != None: - env_value = env_value.strip() - if env_value: - result = env_value - env_value_with_paren = " (%s)" % env_value - if result.startswith("/"): - # Absolute path, maybe we should make this suported by our which function. + """Find a generic C++ toolchain tool. Doesn't %-escape the result.""" + + if name in overriden_tools: + return overriden_tools[name] + + result = name + env_value = repository_ctx.os.environ.get(env_name) + env_value_with_paren = "" + if env_value != None: + env_value = env_value.strip() + if env_value: + result = env_value + env_value_with_paren = " (%s)" % env_value + if result.startswith("/"): + # Absolute path, maybe we should make this suported by our which function. + return result + result = repository_ctx.which(result) + if result == None: + msg = ("Cannot find %s or %s%s; either correct your path or set the %s" + + " environment variable") % (name, env_name, env_value_with_paren, env_name) + if warn: + auto_configure_warning(msg) + else: + auto_configure_fail(msg) return result - result = repository_ctx.which(result) - if result == None: - msg = ("Cannot find %s or %s%s; either correct your path or set the %s" - + " environment variable") % (name, env_name, env_value_with_paren, env_name) - if warn: - auto_configure_warning(msg) - else: - auto_configure_fail(msg) - return result def find_cc(repository_ctx, overriden_tools): - return _find_generic(repository_ctx, "gcc", "CC", overriden_tools) + return _find_generic(repository_ctx, "gcc", "CC", overriden_tools) def configure_unix_toolchain(repository_ctx, cpu_value, overriden_tools): - """Configure C++ toolchain on Unix platforms.""" - paths = resolve_labels(repository_ctx, [ - "@bazel_tools//tools/cpp:BUILD.tpl", - "@bazel_tools//tools/cpp:CROSSTOOL.tpl", - "@bazel_tools//tools/cpp:linux_cc_wrapper.sh.tpl", - "@bazel_tools//tools/cpp:osx_cc_wrapper.sh.tpl", - ]) - - repository_ctx.file("tools/cpp/empty.cc", "int main() {}") - darwin = cpu_value == "darwin" - - cc = _find_generic(repository_ctx, "gcc", "CC", overriden_tools) - overriden_tools = dict(overriden_tools) - overriden_tools["gcc"] = cc - overriden_tools["gcov"] = _find_generic( - repository_ctx, "gcov", "GCOV", overriden_tools, warn = True) - if darwin: - overriden_tools["gcc"] = "cc_wrapper.sh" - overriden_tools["ar"] = "/usr/bin/libtool" - - tool_paths = _get_tool_paths(repository_ctx, overriden_tools) - crosstool_content = _crosstool_content(repository_ctx, cc, cpu_value, darwin) - opt_content = _opt_content(darwin) - dbg_content = _dbg_content() - - repository_ctx.template( - "BUILD", - paths["@bazel_tools//tools/cpp:BUILD.tpl"], - { - "%{name}": cpu_value, - "%{supports_param_files}": "0" if darwin else "1", - "%{cc_compiler_deps}": ":cc_wrapper" if darwin else ":empty", - "%{compiler}": get_env_var( - repository_ctx, "BAZEL_COMPILER", "compiler", False), - }) - - cc_wrapper_src = ( - "@bazel_tools//tools/cpp:osx_cc_wrapper.sh.tpl" - if darwin else "@bazel_tools//tools/cpp:linux_cc_wrapper.sh.tpl") - repository_ctx.template( - "cc_wrapper.sh", - paths[cc_wrapper_src], - { - "%{cc}": escape_string(str(cc)), - "%{env}": escape_string(get_env(repository_ctx)), - }) - - repository_ctx.template( - "CROSSTOOL", - paths["@bazel_tools//tools/cpp:CROSSTOOL.tpl"], - { - "%{cpu}": escape_string(cpu_value), - "%{default_toolchain_name}": escape_string( - get_env_var(repository_ctx, - "CC_TOOLCHAIN_NAME", - "local", - False)), - "%{toolchain_name}": escape_string( - get_env_var(repository_ctx, "CC_TOOLCHAIN_NAME", "local", False)), - "%{content}": _build_crosstool(crosstool_content) + "\n" + - _build_tool_path(tool_paths), - "%{opt_content}": _build_crosstool(opt_content, " "), - "%{dbg_content}": _build_crosstool(dbg_content, " "), - "%{cxx_builtin_include_directory}": "", - "%{coverage}": _coverage_feature(repository_ctx, darwin), - "%{msvc_env_tmp}": "", - "%{msvc_env_path}": "", - "%{msvc_env_include}": "", - "%{msvc_env_lib}": "", - "%{msvc_cl_path}": "", - "%{msvc_ml_path}": "", - "%{msvc_link_path}": "", - "%{msvc_lib_path}": "", - "%{msys_x64_mingw_content}": "", - "%{dbg_mode_debug}": "", - "%{fastbuild_mode_debug}": "", - "%{compilation_mode_content}": "", - }) + """Configure C++ toolchain on Unix platforms.""" + paths = resolve_labels(repository_ctx, [ + "@bazel_tools//tools/cpp:BUILD.tpl", + "@bazel_tools//tools/cpp:CROSSTOOL.tpl", + "@bazel_tools//tools/cpp:linux_cc_wrapper.sh.tpl", + "@bazel_tools//tools/cpp:osx_cc_wrapper.sh.tpl", + ]) + + repository_ctx.file("tools/cpp/empty.cc", "int main() {}") + darwin = cpu_value == "darwin" + + cc = _find_generic(repository_ctx, "gcc", "CC", overriden_tools) + overriden_tools = dict(overriden_tools) + overriden_tools["gcc"] = cc + overriden_tools["gcov"] = _find_generic( + repository_ctx, + "gcov", + "GCOV", + overriden_tools, + warn = True, + ) + if darwin: + overriden_tools["gcc"] = "cc_wrapper.sh" + overriden_tools["ar"] = "/usr/bin/libtool" + + tool_paths = _get_tool_paths(repository_ctx, overriden_tools) + crosstool_content = _crosstool_content(repository_ctx, cc, cpu_value, darwin) + opt_content = _opt_content(darwin) + dbg_content = _dbg_content() + + repository_ctx.template( + "BUILD", + paths["@bazel_tools//tools/cpp:BUILD.tpl"], + { + "%{name}": cpu_value, + "%{supports_param_files}": "0" if darwin else "1", + "%{cc_compiler_deps}": ":cc_wrapper" if darwin else ":empty", + "%{compiler}": get_env_var( + repository_ctx, + "BAZEL_COMPILER", + "compiler", + False, + ), + }, + ) + + cc_wrapper_src = ( + "@bazel_tools//tools/cpp:osx_cc_wrapper.sh.tpl" if darwin else "@bazel_tools//tools/cpp:linux_cc_wrapper.sh.tpl" + ) + repository_ctx.template( + "cc_wrapper.sh", + paths[cc_wrapper_src], + { + "%{cc}": escape_string(str(cc)), + "%{env}": escape_string(get_env(repository_ctx)), + }, + ) + + repository_ctx.template( + "CROSSTOOL", + paths["@bazel_tools//tools/cpp:CROSSTOOL.tpl"], + { + "%{cpu}": escape_string(cpu_value), + "%{default_toolchain_name}": escape_string( + get_env_var( + repository_ctx, + "CC_TOOLCHAIN_NAME", + "local", + False, + ), + ), + "%{toolchain_name}": escape_string( + get_env_var(repository_ctx, "CC_TOOLCHAIN_NAME", "local", False), + ), + "%{content}": _build_crosstool(crosstool_content) + "\n" + + _build_tool_path(tool_paths), + "%{opt_content}": _build_crosstool(opt_content, " "), + "%{dbg_content}": _build_crosstool(dbg_content, " "), + "%{cxx_builtin_include_directory}": "", + "%{coverage}": _coverage_feature(repository_ctx, darwin), + "%{msvc_env_tmp}": "", + "%{msvc_env_path}": "", + "%{msvc_env_include}": "", + "%{msvc_env_lib}": "", + "%{msvc_cl_path}": "", + "%{msvc_ml_path}": "", + "%{msvc_link_path}": "", + "%{msvc_lib_path}": "", + "%{msys_x64_mingw_content}": "", + "%{dbg_mode_debug}": "", + "%{fastbuild_mode_debug}": "", + "%{compilation_mode_content}": "", + }, + ) diff --git a/tools/cpp/windows_cc_configure.bzl b/tools/cpp/windows_cc_configure.bzl index 8c3210231e..11dc0ad608 100644 --- a/tools/cpp/windows_cc_configure.bzl +++ b/tools/cpp/windows_cc_configure.bzl @@ -16,318 +16,437 @@ load( "@bazel_tools//tools/cpp:lib_cc_configure.bzl", - "escape_string", "auto_configure_fail", "auto_configure_warning", - "get_env_var", - "which", - "which_cmd", + "escape_string", "execute", + "get_env_var", "is_cc_configure_debug", "resolve_labels", + "which", + "which_cmd", ) def _get_escaped_windows_msys_crosstool_content(repository_ctx, use_mingw = False): - """Return the content of msys crosstool which is still the default CROSSTOOL on Windows.""" - bazel_sh = get_env_var(repository_ctx, "BAZEL_SH").replace("\\", "/").lower() - tokens = bazel_sh.rsplit("/", 1) - prefix = "mingw64" if use_mingw else "usr" - msys_root = None - if tokens[0].endswith("/usr/bin"): - msys_root = tokens[0][:len(tokens[0]) - len("usr/bin")] - elif tokens[0].endswith("/bin"): - msys_root = tokens[0][:len(tokens[0]) - len("bin")] - if not msys_root: - auto_configure_fail( - "Could not determine MSYS/Cygwin root from BAZEL_SH (%s)" % bazel_sh) - escaped_msys_root = escape_string(msys_root) - return ((( - ' abi_version: "local"\n' + - ' abi_libc_version: "local"\n' + - ' builtin_sysroot: ""\n' + - ' compiler: "msys-gcc"\n' + - ' host_system_name: "local"\n' + - ' needsPic: false\n' + - ' target_libc: "msys"\n' + - ' target_cpu: "x64_windows"\n' + - ' target_system_name: "local"\n') if not use_mingw else '') + - ' tool_path { name: "ar" path: "%s%s/bin/ar" }\n' % (escaped_msys_root, prefix) + - ' tool_path { name: "compat-ld" path: "%s%s/bin/ld" }\n' % (escaped_msys_root, prefix) + - ' tool_path { name: "cpp" path: "%s%s/bin/cpp" }\n' % (escaped_msys_root, prefix) + - ' tool_path { name: "dwp" path: "%s%s/bin/dwp" }\n' % (escaped_msys_root, prefix) + - ' tool_path { name: "gcc" path: "%s%s/bin/gcc" }\n' % (escaped_msys_root, prefix) + - ' artifact_name_pattern { category_name: "executable" prefix: "" extension: ".exe"}\n' + - ' cxx_flag: "-std=gnu++0x"\n' + - ' linker_flag: "-lstdc++"\n' + - ' cxx_builtin_include_directory: "%s%s/"\n' % (escaped_msys_root, prefix) + - ' tool_path { name: "gcov" path: "%s%s/bin/gcov" }\n' % (escaped_msys_root, prefix) + - ' tool_path { name: "ld" path: "%s%s/bin/ld" }\n' % (escaped_msys_root, prefix) + - ' tool_path { name: "nm" path: "%s%s/bin/nm" }\n' % (escaped_msys_root, prefix) + - ' tool_path { name: "objcopy" path: "%s%s/bin/objcopy" }\n' % (escaped_msys_root, prefix) + - ' objcopy_embed_flag: "-I"\n' + - ' objcopy_embed_flag: "binary"\n' + - ' tool_path { name: "objdump" path: "%s%s/bin/objdump" }\n' % (escaped_msys_root, prefix) + - ' tool_path { name: "strip" path: "%s%s/bin/strip" }'% (escaped_msys_root, prefix) + - ' feature { name: "targets_windows" implies: "copy_dynamic_libraries_to_binary" enabled: true }' + - ' feature { name: "copy_dynamic_libraries_to_binary" }' ) + """Return the content of msys crosstool which is still the default CROSSTOOL on Windows.""" + bazel_sh = get_env_var(repository_ctx, "BAZEL_SH").replace("\\", "/").lower() + tokens = bazel_sh.rsplit("/", 1) + prefix = "mingw64" if use_mingw else "usr" + msys_root = None + if tokens[0].endswith("/usr/bin"): + msys_root = tokens[0][:len(tokens[0]) - len("usr/bin")] + elif tokens[0].endswith("/bin"): + msys_root = tokens[0][:len(tokens[0]) - len("bin")] + if not msys_root: + auto_configure_fail( + "Could not determine MSYS/Cygwin root from BAZEL_SH (%s)" % bazel_sh, + ) + escaped_msys_root = escape_string(msys_root) + return ((( + ' abi_version: "local"\n' + + ' abi_libc_version: "local"\n' + + ' builtin_sysroot: ""\n' + + ' compiler: "msys-gcc"\n' + + ' host_system_name: "local"\n' + + " needsPic: false\n" + + ' target_libc: "msys"\n' + + ' target_cpu: "x64_windows"\n' + + ' target_system_name: "local"\n' + ) if not use_mingw else "") + + ' tool_path { name: "ar" path: "%s%s/bin/ar" }\n' % (escaped_msys_root, prefix) + + ' tool_path { name: "compat-ld" path: "%s%s/bin/ld" }\n' % (escaped_msys_root, prefix) + + ' tool_path { name: "cpp" path: "%s%s/bin/cpp" }\n' % (escaped_msys_root, prefix) + + ' tool_path { name: "dwp" path: "%s%s/bin/dwp" }\n' % (escaped_msys_root, prefix) + + ' tool_path { name: "gcc" path: "%s%s/bin/gcc" }\n' % (escaped_msys_root, prefix) + + ' artifact_name_pattern { category_name: "executable" prefix: "" extension: ".exe"}\n' + + ' cxx_flag: "-std=gnu++0x"\n' + + ' linker_flag: "-lstdc++"\n' + + ' cxx_builtin_include_directory: "%s%s/"\n' % (escaped_msys_root, prefix) + + ' tool_path { name: "gcov" path: "%s%s/bin/gcov" }\n' % (escaped_msys_root, prefix) + + ' tool_path { name: "ld" path: "%s%s/bin/ld" }\n' % (escaped_msys_root, prefix) + + ' tool_path { name: "nm" path: "%s%s/bin/nm" }\n' % (escaped_msys_root, prefix) + + ' tool_path { name: "objcopy" path: "%s%s/bin/objcopy" }\n' % (escaped_msys_root, prefix) + + ' objcopy_embed_flag: "-I"\n' + + ' objcopy_embed_flag: "binary"\n' + + ' tool_path { name: "objdump" path: "%s%s/bin/objdump" }\n' % (escaped_msys_root, prefix) + + ' tool_path { name: "strip" path: "%s%s/bin/strip" }' % (escaped_msys_root, prefix) + + ' feature { name: "targets_windows" implies: "copy_dynamic_libraries_to_binary" enabled: true }' + + ' feature { name: "copy_dynamic_libraries_to_binary" }') def _get_system_root(repository_ctx): - r"""Get System root path on Windows, default is C:\\Windows. Doesn't %-escape the result.""" - if "SYSTEMROOT" in repository_ctx.os.environ: - return escape_string(repository_ctx.os.environ["SYSTEMROOT"]) - auto_configure_warning("SYSTEMROOT is not set, using default SYSTEMROOT=C:\\Windows") - return "C:\\Windows" + """Get System root path on Windows, default is C:\\\Windows. Doesn't %-escape the result.""" + if "SYSTEMROOT" in repository_ctx.os.environ: + return escape_string(repository_ctx.os.environ["SYSTEMROOT"]) + auto_configure_warning("SYSTEMROOT is not set, using default SYSTEMROOT=C:\\Windows") + return "C:\\Windows" def _find_cuda(repository_ctx): - """Find out if and where cuda is installed. Doesn't %-escape the result.""" - if "CUDA_PATH" in repository_ctx.os.environ: - return repository_ctx.os.environ["CUDA_PATH"] - nvcc = which(repository_ctx, "nvcc.exe") - if nvcc: - return nvcc[:-len("/bin/nvcc.exe")] - return None + """Find out if and where cuda is installed. Doesn't %-escape the result.""" + if "CUDA_PATH" in repository_ctx.os.environ: + return repository_ctx.os.environ["CUDA_PATH"] + nvcc = which(repository_ctx, "nvcc.exe") + if nvcc: + return nvcc[:-len("/bin/nvcc.exe")] + return None def _find_python(repository_ctx): - """Find where is python on Windows. Doesn't %-escape the result.""" - if "BAZEL_PYTHON" in repository_ctx.os.environ: - python_binary = repository_ctx.os.environ["BAZEL_PYTHON"] - if not python_binary.endswith(".exe"): - python_binary = python_binary + ".exe" + """Find where is python on Windows. Doesn't %-escape the result.""" + if "BAZEL_PYTHON" in repository_ctx.os.environ: + python_binary = repository_ctx.os.environ["BAZEL_PYTHON"] + if not python_binary.endswith(".exe"): + python_binary = python_binary + ".exe" + return python_binary + auto_configure_warning("'BAZEL_PYTHON' is not set, start looking for python in PATH.") + python_binary = which_cmd(repository_ctx, "python.exe") + auto_configure_warning("Python found at %s" % python_binary) return python_binary - auto_configure_warning("'BAZEL_PYTHON' is not set, start looking for python in PATH.") - python_binary = which_cmd(repository_ctx, "python.exe") - auto_configure_warning("Python found at %s" % python_binary) - return python_binary def _add_system_root(repository_ctx, env): - r"""Running VCVARSALL.BAT and VCVARSQUERYREGISTRY.BAT need %SYSTEMROOT%\\system32 in PATH.""" - if "PATH" not in env: - env["PATH"] = "" - env["PATH"] = env["PATH"] + ";" + _get_system_root(repository_ctx) + "\\system32" - return env + """Running VCVARSALL.BAT and VCVARSQUERYREGISTRY.BAT need %SYSTEMROOT%\\\\system32 in PATH.""" + if "PATH" not in env: + env["PATH"] = "" + env["PATH"] = env["PATH"] + ";" + _get_system_root(repository_ctx) + "\\system32" + return env def find_vc_path(repository_ctx): - """Find Visual C++ build tools install path. Doesn't %-escape the result.""" - # 1. Check if BAZEL_VC or BAZEL_VS is already set by user. - if "BAZEL_VC" in repository_ctx.os.environ: - return repository_ctx.os.environ["BAZEL_VC"] - - if "BAZEL_VS" in repository_ctx.os.environ: - return repository_ctx.os.environ["BAZEL_VS"] + "\\VC\\" - auto_configure_warning("'BAZEL_VC' is not set, " + - "start looking for the latest Visual C++ installed.") - - # 2. Check if VS%VS_VERSION%COMNTOOLS is set, if true then try to find and use - # vcvarsqueryregistry.bat to detect VC++. - auto_configure_warning("Looking for VS%VERSION%COMNTOOLS environment variables, " + - "eg. VS140COMNTOOLS") - for vscommontools_env in ["VS140COMNTOOLS", "VS120COMNTOOLS", - "VS110COMNTOOLS", "VS100COMNTOOLS", "VS90COMNTOOLS"]: - if vscommontools_env not in repository_ctx.os.environ: - continue - vcvarsqueryregistry = repository_ctx.os.environ[vscommontools_env] + "\\vcvarsqueryregistry.bat" - if not repository_ctx.path(vcvarsqueryregistry).exists: - continue - repository_ctx.file("get_vc_dir.bat", - "@echo off\n" + - "call \"" + vcvarsqueryregistry + "\"\n" + - "echo %VCINSTALLDIR%", True) - env = _add_system_root(repository_ctx, repository_ctx.os.environ) - vc_dir = execute(repository_ctx, ["./get_vc_dir.bat"], environment=env) - + """Find Visual C++ build tools install path. Doesn't %-escape the result.""" + + # 1. Check if BAZEL_VC or BAZEL_VS is already set by user. + if "BAZEL_VC" in repository_ctx.os.environ: + return repository_ctx.os.environ["BAZEL_VC"] + + if "BAZEL_VS" in repository_ctx.os.environ: + return repository_ctx.os.environ["BAZEL_VS"] + "\\VC\\" + auto_configure_warning("'BAZEL_VC' is not set, " + + "start looking for the latest Visual C++ installed.") + + # 2. Check if VS%VS_VERSION%COMNTOOLS is set, if true then try to find and use + # vcvarsqueryregistry.bat to detect VC++. + auto_configure_warning("Looking for VS%VERSION%COMNTOOLS environment variables, " + + "eg. VS140COMNTOOLS") + for vscommontools_env in [ + "VS140COMNTOOLS", + "VS120COMNTOOLS", + "VS110COMNTOOLS", + "VS100COMNTOOLS", + "VS90COMNTOOLS", + ]: + if vscommontools_env not in repository_ctx.os.environ: + continue + vcvarsqueryregistry = repository_ctx.os.environ[vscommontools_env] + "\\vcvarsqueryregistry.bat" + if not repository_ctx.path(vcvarsqueryregistry).exists: + continue + repository_ctx.file( + "get_vc_dir.bat", + "@echo off\n" + + "call \"" + vcvarsqueryregistry + "\"\n" + + "echo %VCINSTALLDIR%", + True, + ) + env = _add_system_root(repository_ctx, repository_ctx.os.environ) + vc_dir = execute(repository_ctx, ["./get_vc_dir.bat"], environment = env) + + auto_configure_warning("Visual C++ build tools found at %s" % vc_dir) + return vc_dir + + # 3. User might clean up all environment variables, if so looking for Visual C++ through registry. + # Works for all VS versions, including Visual Studio 2017. + auto_configure_warning("Looking for Visual C++ through registry") + reg_binary = _get_system_root(repository_ctx) + "\\system32\\reg.exe" + vc_dir = None + for key, suffix in (("VC7", ""), ("VS7", "\\VC")): + for version in ["15.0", "14.0", "12.0", "11.0", "10.0", "9.0", "8.0"]: + if vc_dir: + break + result = repository_ctx.execute([reg_binary, "query", "HKEY_LOCAL_MACHINE\\SOFTWARE\\Wow6432Node\\Microsoft\\VisualStudio\\SxS\\" + key, "/v", version]) + if is_cc_configure_debug(repository_ctx): + auto_configure_warning("registry query result for VC %s:\n\nSTDOUT(start)\n%s\nSTDOUT(end)\nSTDERR(start):\n%s\nSTDERR(end)\n" % + (version, result.stdout, result.stderr)) + if not result.stderr: + for line in result.stdout.split("\n"): + line = line.strip() + if line.startswith(version) and line.find("REG_SZ") != -1: + vc_dir = line[line.find("REG_SZ") + len("REG_SZ"):].strip() + suffix + + if not vc_dir: + return None auto_configure_warning("Visual C++ build tools found at %s" % vc_dir) return vc_dir - # 3. User might clean up all environment variables, if so looking for Visual C++ through registry. - # Works for all VS versions, including Visual Studio 2017. - auto_configure_warning("Looking for Visual C++ through registry") - reg_binary = _get_system_root(repository_ctx) + "\\system32\\reg.exe" - vc_dir = None - for key, suffix in (("VC7", ""), ("VS7", "\\VC")): - for version in ["15.0", "14.0", "12.0", "11.0", "10.0", "9.0", "8.0"]: - if vc_dir: - break - result = repository_ctx.execute([reg_binary, "query", "HKEY_LOCAL_MACHINE\\SOFTWARE\\Wow6432Node\\Microsoft\\VisualStudio\\SxS\\" + key, "/v", version]) - if is_cc_configure_debug(repository_ctx): - auto_configure_warning("registry query result for VC %s:\n\nSTDOUT(start)\n%s\nSTDOUT(end)\nSTDERR(start):\n%s\nSTDERR(end)\n" % - (version, result.stdout, result.stderr)) - if not result.stderr: - for line in result.stdout.split("\n"): - line = line.strip() - if line.startswith(version) and line.find("REG_SZ") != -1: - vc_dir = line[line.find("REG_SZ") + len("REG_SZ"):].strip() + suffix - - if not vc_dir: - return None - auto_configure_warning("Visual C++ build tools found at %s" % vc_dir) - return vc_dir - def _is_vs_2017(vc_path): - """Check if the installed VS version is Visual Studio 2017.""" - # In VS 2017, the location of VC is like: - # C:\Program Files (x86)\Microsoft Visual Studio\2017\BuildTools\VC\ - # In VS 2015 or older version, it is like: - # C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\ - return vc_path.find("2017") != -1 + """Check if the installed VS version is Visual Studio 2017.""" + + # In VS 2017, the location of VC is like: + # C:\Program Files (x86)\Microsoft Visual Studio\2017\BuildTools\VC\ + # In VS 2015 or older version, it is like: + # C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\ + return vc_path.find("2017") != -1 def _find_vcvarsall_bat_script(repository_ctx, vc_path): - """Find vcvarsall.bat script. Doesn't %-escape the result.""" - if _is_vs_2017(vc_path): - vcvarsall = vc_path + "\\Auxiliary\\Build\\VCVARSALL.BAT" - else: - vcvarsall = vc_path + "\\VCVARSALL.BAT" + """Find vcvarsall.bat script. Doesn't %-escape the result.""" + if _is_vs_2017(vc_path): + vcvarsall = vc_path + "\\Auxiliary\\Build\\VCVARSALL.BAT" + else: + vcvarsall = vc_path + "\\VCVARSALL.BAT" - if not repository_ctx.path(vcvarsall).exists: - return None + if not repository_ctx.path(vcvarsall).exists: + return None - return vcvarsall + return vcvarsall def setup_vc_env_vars(repository_ctx, vc_path): - """Get environment variables set by VCVARSALL.BAT. Doesn't %-escape the result!""" - vcvarsall = _find_vcvarsall_bat_script(repository_ctx, vc_path) - if not vcvarsall: - return None - repository_ctx.file("get_env.bat", - "@echo off\n" + - "call \"" + vcvarsall + "\" amd64 > NUL \n" + - "echo PATH=%PATH%,INCLUDE=%INCLUDE%,LIB=%LIB%,WINDOWSSDKDIR=%WINDOWSSDKDIR% \n", True) - env = _add_system_root(repository_ctx, - {"PATH": "", "INCLUDE": "", "LIB": "", "WINDOWSSDKDIR": ""}) - envs = execute(repository_ctx, ["./get_env.bat"], environment=env).split(",") - env_map = {} - for env in envs: - key, value = env.split("=", 1) - env_map[key] = escape_string(value.replace("\\", "\\\\")) - return env_map + """Get environment variables set by VCVARSALL.BAT. Doesn't %-escape the result!""" + vcvarsall = _find_vcvarsall_bat_script(repository_ctx, vc_path) + if not vcvarsall: + return None + repository_ctx.file( + "get_env.bat", + "@echo off\n" + + "call \"" + vcvarsall + "\" amd64 > NUL \n" + + "echo PATH=%PATH%,INCLUDE=%INCLUDE%,LIB=%LIB%,WINDOWSSDKDIR=%WINDOWSSDKDIR% \n", + True, + ) + env = _add_system_root( + repository_ctx, + {"PATH": "", "INCLUDE": "", "LIB": "", "WINDOWSSDKDIR": ""}, + ) + envs = execute(repository_ctx, ["./get_env.bat"], environment = env).split(",") + env_map = {} + for env in envs: + key, value = env.split("=", 1) + env_map[key] = escape_string(value.replace("\\", "\\\\")) + return env_map def find_msvc_tool(repository_ctx, vc_path, tool): - """Find the exact path of a specific build tool in MSVC. Doesn't %-escape the result.""" - tool_path = "" - if _is_vs_2017(vc_path): - # For VS 2017, the tools are under a directory like: - # C:\Program Files (x86)\Microsoft Visual Studio\2017\BuildTools\VC\Tools\MSVC\14.10.24930\bin\HostX64\x64 - dirs = repository_ctx.path(vc_path + "\\Tools\\MSVC").readdir() - if len(dirs) < 1: - return None - # Normally there should be only one child directory under %VC_PATH%\TOOLS\MSVC, - # but iterate every directory to be more robust. - for path in dirs: - tool_path = str(path) + "\\bin\\HostX64\\x64\\" + tool - if repository_ctx.path(tool_path).exists: - break - else: - # For VS 2015 and older version, the tools are under: - # C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\bin\amd64 - tool_path = vc_path + "\\bin\\amd64\\" + tool - - if not repository_ctx.path(tool_path).exists: - return None + """Find the exact path of a specific build tool in MSVC. Doesn't %-escape the result.""" + tool_path = "" + if _is_vs_2017(vc_path): + # For VS 2017, the tools are under a directory like: + # C:\Program Files (x86)\Microsoft Visual Studio\2017\BuildTools\VC\Tools\MSVC\14.10.24930\bin\HostX64\x64 + dirs = repository_ctx.path(vc_path + "\\Tools\\MSVC").readdir() + if len(dirs) < 1: + return None + + # Normally there should be only one child directory under %VC_PATH%\TOOLS\MSVC, + # but iterate every directory to be more robust. + for path in dirs: + tool_path = str(path) + "\\bin\\HostX64\\x64\\" + tool + if repository_ctx.path(tool_path).exists: + break + else: + # For VS 2015 and older version, the tools are under: + # C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\bin\amd64 + tool_path = vc_path + "\\bin\\amd64\\" + tool + + if not repository_ctx.path(tool_path).exists: + return None - return tool_path + return tool_path def _find_missing_vc_tools(repository_ctx, vc_path): - """Check if any required tool is missing under given VC path.""" - missing_tools = [] - if not _find_vcvarsall_bat_script(repository_ctx, vc_path): - missing_tools.append("VCVARSALL.BAT") + """Check if any required tool is missing under given VC path.""" + missing_tools = [] + if not _find_vcvarsall_bat_script(repository_ctx, vc_path): + missing_tools.append("VCVARSALL.BAT") - for tool in ["cl.exe", "link.exe", "lib.exe", "ml64.exe"]: - if not find_msvc_tool(repository_ctx, vc_path, tool): - missing_tools.append(tool) + for tool in ["cl.exe", "link.exe", "lib.exe", "ml64.exe"]: + if not find_msvc_tool(repository_ctx, vc_path, tool): + missing_tools.append(tool) - return missing_tools + return missing_tools def _is_support_whole_archive(repository_ctx, vc_path): - """Run MSVC linker alone to see if it supports /WHOLEARCHIVE.""" - env = repository_ctx.os.environ - if "NO_WHOLE_ARCHIVE_OPTION" in env and env["NO_WHOLE_ARCHIVE_OPTION"] == "1": - return False - linker = find_msvc_tool(repository_ctx, vc_path, "link.exe") - result = execute(repository_ctx, [linker], expect_failure = True) - return result.find("/WHOLEARCHIVE") != -1 + """Run MSVC linker alone to see if it supports /WHOLEARCHIVE.""" + env = repository_ctx.os.environ + if "NO_WHOLE_ARCHIVE_OPTION" in env and env["NO_WHOLE_ARCHIVE_OPTION"] == "1": + return False + linker = find_msvc_tool(repository_ctx, vc_path, "link.exe") + result = execute(repository_ctx, [linker], expect_failure = True) + return result.find("/WHOLEARCHIVE") != -1 def _is_support_debug_fastlink(repository_ctx, vc_path): - """Run MSVC linker alone to see if it supports /DEBUG:FASTLINK.""" - linker = find_msvc_tool(repository_ctx, vc_path, "link.exe") - result = execute(repository_ctx, [linker], expect_failure = True) - return result.find("/DEBUG[:{FASTLINK|FULL|NONE}]") != -1 + """Run MSVC linker alone to see if it supports /DEBUG:FASTLINK.""" + linker = find_msvc_tool(repository_ctx, vc_path, "link.exe") + result = execute(repository_ctx, [linker], expect_failure = True) + return result.find("/DEBUG[:{FASTLINK|FULL|NONE}]") != -1 def _is_use_msvc_wrapper(repository_ctx): - """Returns True if USE_MSVC_WRAPPER is set to 1.""" - env = repository_ctx.os.environ - return "USE_MSVC_WRAPPER" in env and env["USE_MSVC_WRAPPER"] == "1" + """Returns True if USE_MSVC_WRAPPER is set to 1.""" + env = repository_ctx.os.environ + return "USE_MSVC_WRAPPER" in env and env["USE_MSVC_WRAPPER"] == "1" def _get_compilation_mode_content(): - """Return the content for adding flags for different compilation modes when using MSVC wrapper.""" - return "\n".join([ - " compilation_mode_flags {", - " mode: DBG", - " compiler_flag: '-Xcompilation-mode=dbg'", - " linker_flag: '-Xcompilation-mode=dbg'", - " }", - " compilation_mode_flags {", - " mode: FASTBUILD", - " compiler_flag: '-Xcompilation-mode=fastbuild'", - " linker_flag: '-Xcompilation-mode=fastbuild'", - " }", - " compilation_mode_flags {", - " mode: OPT", - " compiler_flag: '-Xcompilation-mode=opt'", - " linker_flag: '-Xcompilation-mode=opt'", - " }"]) + """Return the content for adding flags for different compilation modes when using MSVC wrapper.""" + return "\n".join([ + " compilation_mode_flags {", + " mode: DBG", + " compiler_flag: '-Xcompilation-mode=dbg'", + " linker_flag: '-Xcompilation-mode=dbg'", + " }", + " compilation_mode_flags {", + " mode: FASTBUILD", + " compiler_flag: '-Xcompilation-mode=fastbuild'", + " linker_flag: '-Xcompilation-mode=fastbuild'", + " }", + " compilation_mode_flags {", + " mode: OPT", + " compiler_flag: '-Xcompilation-mode=opt'", + " linker_flag: '-Xcompilation-mode=opt'", + " }", + ]) def _escaped_cuda_compute_capabilities(repository_ctx): - """Returns a %-escaped list of strings representing cuda compute capabilities.""" - - if "CUDA_COMPUTE_CAPABILITIES" not in repository_ctx.os.environ: - return ["3.5", "5.2"] - capabilities_str = escape_string(repository_ctx.os.environ["CUDA_COMPUTE_CAPABILITIES"]) - capabilities = capabilities_str.split(",") - for capability in capabilities: - # Workaround for Skylark's lack of support for regex. This check should - # be equivalent to checking: - # if re.match("[0-9]+.[0-9]+", capability) == None: - parts = capability.split(".") - if len(parts) != 2 or not parts[0].isdigit() or not parts[1].isdigit(): - auto_configure_fail("Invalid compute capability: %s" % capability) - return capabilities + """Returns a %-escaped list of strings representing cuda compute capabilities.""" + + if "CUDA_COMPUTE_CAPABILITIES" not in repository_ctx.os.environ: + return ["3.5", "5.2"] + capabilities_str = escape_string(repository_ctx.os.environ["CUDA_COMPUTE_CAPABILITIES"]) + capabilities = capabilities_str.split(",") + for capability in capabilities: + # Workaround for Skylark's lack of support for regex. This check should + # be equivalent to checking: + # if re.match("[0-9]+.[0-9]+", capability) == None: + parts = capability.split(".") + if len(parts) != 2 or not parts[0].isdigit() or not parts[1].isdigit(): + auto_configure_fail("Invalid compute capability: %s" % capability) + return capabilities def configure_windows_toolchain(repository_ctx): - """Configure C++ toolchain on Windows.""" - paths = resolve_labels(repository_ctx, [ - "@bazel_tools//tools/cpp:BUILD.static.windows", - "@bazel_tools//tools/cpp:CROSSTOOL", - "@bazel_tools//tools/cpp:CROSSTOOL.tpl", - "@bazel_tools//tools/cpp:vc_installation_error.bat.tpl", - "@bazel_tools//tools/cpp:wrapper/bin/call_python.bat.tpl", - "@bazel_tools//tools/cpp:wrapper/bin/pydir/msvc_tools.py.tpl", - ]) - - repository_ctx.symlink(paths["@bazel_tools//tools/cpp:BUILD.static.windows"], "BUILD") - - vc_path = find_vc_path(repository_ctx) - missing_tools = None - if not vc_path: - repository_ctx.template( - "vc_installation_error.bat", - paths["@bazel_tools//tools/cpp:vc_installation_error.bat.tpl"], - {"%{vc_error_message}": ""}) - else: - missing_tools = _find_missing_vc_tools(repository_ctx, vc_path) - if missing_tools: - message = "\r\n".join([ - "echo. 1>&2", - "echo Visual C++ build tools seems to be installed at %s 1>&2" % vc_path, - "echo But Bazel can't find the following tools: 1>&2", - "echo %s 1>&2" % ", ".join(missing_tools), - "echo. 1>&2", - ]) - repository_ctx.template( - "vc_installation_error.bat", - paths["@bazel_tools//tools/cpp:vc_installation_error.bat.tpl"], - {"%{vc_error_message}": message}) - - if not vc_path or missing_tools: + """Configure C++ toolchain on Windows.""" + paths = resolve_labels(repository_ctx, [ + "@bazel_tools//tools/cpp:BUILD.static.windows", + "@bazel_tools//tools/cpp:CROSSTOOL", + "@bazel_tools//tools/cpp:CROSSTOOL.tpl", + "@bazel_tools//tools/cpp:vc_installation_error.bat.tpl", + "@bazel_tools//tools/cpp:wrapper/bin/call_python.bat.tpl", + "@bazel_tools//tools/cpp:wrapper/bin/pydir/msvc_tools.py.tpl", + ]) + + repository_ctx.symlink(paths["@bazel_tools//tools/cpp:BUILD.static.windows"], "BUILD") + + vc_path = find_vc_path(repository_ctx) + missing_tools = None + if not vc_path: + repository_ctx.template( + "vc_installation_error.bat", + paths["@bazel_tools//tools/cpp:vc_installation_error.bat.tpl"], + {"%{vc_error_message}": ""}, + ) + else: + missing_tools = _find_missing_vc_tools(repository_ctx, vc_path) + if missing_tools: + message = "\r\n".join([ + "echo. 1>&2", + "echo Visual C++ build tools seems to be installed at %s 1>&2" % vc_path, + "echo But Bazel can't find the following tools: 1>&2", + "echo %s 1>&2" % ", ".join(missing_tools), + "echo. 1>&2", + ]) + repository_ctx.template( + "vc_installation_error.bat", + paths["@bazel_tools//tools/cpp:vc_installation_error.bat.tpl"], + {"%{vc_error_message}": message}, + ) + + if not vc_path or missing_tools: + repository_ctx.template( + "CROSSTOOL", + paths["@bazel_tools//tools/cpp:CROSSTOOL.tpl"], + { + "%{cpu}": "x64_windows", + "%{default_toolchain_name}": "msvc_x64", + "%{toolchain_name}": "msys_x64", + "%{msvc_env_tmp}": "", + "%{msvc_env_path}": "", + "%{msvc_env_include}": "", + "%{msvc_env_lib}": "", + "%{msvc_cl_path}": "vc_installation_error.bat", + "%{msvc_ml_path}": "vc_installation_error.bat", + "%{msvc_link_path}": "vc_installation_error.bat", + "%{msvc_lib_path}": "vc_installation_error.bat", + "%{dbg_mode_debug}": "/DEBUG", + "%{fastbuild_mode_debug}": "/DEBUG", + "%{compilation_mode_content}": "", + "%{content}": _get_escaped_windows_msys_crosstool_content(repository_ctx), + "%{msys_x64_mingw_content}": _get_escaped_windows_msys_crosstool_content(repository_ctx, use_mingw = True), + "%{opt_content}": "", + "%{dbg_content}": "", + "%{link_content}": "", + "%{cxx_builtin_include_directory}": "", + "%{coverage}": "", + }, + ) + return + + env = setup_vc_env_vars(repository_ctx, vc_path) + escaped_paths = escape_string(env["PATH"]) + escaped_include_paths = escape_string(env["INCLUDE"]) + escaped_lib_paths = escape_string(env["LIB"]) + escaped_tmp_dir = escape_string( + get_env_var(repository_ctx, "TMP", "C:\\Windows\\Temp").replace("\\", "\\\\"), + ) + msvc_cl_path = find_msvc_tool(repository_ctx, vc_path, "cl.exe").replace("\\", "/") + msvc_ml_path = find_msvc_tool(repository_ctx, vc_path, "ml64.exe").replace("\\", "/") + msvc_link_path = find_msvc_tool(repository_ctx, vc_path, "link.exe").replace("\\", "/") + msvc_lib_path = find_msvc_tool(repository_ctx, vc_path, "lib.exe").replace("\\", "/") + escaped_cxx_include_directories = [] + compilation_mode_content = "" + + if _is_use_msvc_wrapper(repository_ctx): + if _is_support_whole_archive(repository_ctx, vc_path): + support_whole_archive = "True" + else: + support_whole_archive = "False" + nvcc_tmp_dir_name = escaped_tmp_dir + "\\\\nvcc_inter_files_tmp_dir" + + # Make sure nvcc.exe is in PATH + cuda_path = _find_cuda(repository_ctx) + if cuda_path: + escaped_paths = escape_string(cuda_path.replace("\\", "\\\\") + "/bin;") + escaped_paths + escaped_compute_capabilities = _escaped_cuda_compute_capabilities(repository_ctx) + repository_ctx.template( + "wrapper/bin/pydir/msvc_tools.py", + paths["@bazel_tools//tools/cpp:wrapper/bin/pydir/msvc_tools.py.tpl"], + { + "%{lib_tool}": escape_string(msvc_lib_path), + "%{support_whole_archive}": support_whole_archive, + "%{cuda_compute_capabilities}": ", ".join( + ["\"%s\"" % c for c in escaped_compute_capabilities], + ), + "%{nvcc_tmp_dir_name}": nvcc_tmp_dir_name, + }, + ) + + # nvcc will generate some source files under %{nvcc_tmp_dir_name} + # The generated files are guranteed to have unique name, so they can share the same tmp directory + escaped_cxx_include_directories += ["cxx_builtin_include_directory: \"%s\"" % nvcc_tmp_dir_name] + msvc_wrapper = repository_ctx.path( + paths["@bazel_tools//tools/cpp:CROSSTOOL"], + ).dirname.get_child( + "wrapper", + ).get_child("bin") + for f in ["msvc_cl.bat", "msvc_link.bat", "msvc_nop.bat"]: + repository_ctx.symlink(msvc_wrapper.get_child(f), "wrapper/bin/" + f) + msvc_wrapper = msvc_wrapper.get_child("pydir") + for f in ["msvc_cl.py", "msvc_link.py"]: + repository_ctx.symlink(msvc_wrapper.get_child(f), "wrapper/bin/pydir/" + f) + python_binary = _find_python(repository_ctx) + repository_ctx.template( + "wrapper/bin/call_python.bat", + paths["@bazel_tools//tools/cpp:wrapper/bin/call_python.bat.tpl"], + {"%{python_binary}": escape_string(python_binary)}, + ) + msvc_cl_path = "wrapper/bin/msvc_cl.bat" + msvc_link_path = "wrapper/bin/msvc_link.bat" + msvc_lib_path = "wrapper/bin/msvc_link.bat" + compilation_mode_content = _get_compilation_mode_content() + + for path in escaped_include_paths.split(";"): + if path: + escaped_cxx_include_directories.append("cxx_builtin_include_directory: \"%s\"" % path) + + support_debug_fastlink = _is_support_debug_fastlink(repository_ctx, vc_path) + repository_ctx.template( "CROSSTOOL", paths["@bazel_tools//tools/cpp:CROSSTOOL.tpl"], @@ -335,111 +454,23 @@ def configure_windows_toolchain(repository_ctx): "%{cpu}": "x64_windows", "%{default_toolchain_name}": "msvc_x64", "%{toolchain_name}": "msys_x64", - "%{msvc_env_tmp}": "", - "%{msvc_env_path}": "", - "%{msvc_env_include}": "", - "%{msvc_env_lib}": "", - "%{msvc_cl_path}": "vc_installation_error.bat", - "%{msvc_ml_path}": "vc_installation_error.bat", - "%{msvc_link_path}": "vc_installation_error.bat", - "%{msvc_lib_path}": "vc_installation_error.bat", - "%{dbg_mode_debug}": "/DEBUG", - "%{fastbuild_mode_debug}": "/DEBUG", - "%{compilation_mode_content}": "", + "%{msvc_env_tmp}": escaped_tmp_dir, + "%{msvc_env_path}": escaped_paths, + "%{msvc_env_include}": escaped_include_paths, + "%{msvc_env_lib}": escaped_lib_paths, + "%{msvc_cl_path}": msvc_cl_path, + "%{msvc_ml_path}": msvc_ml_path, + "%{msvc_link_path}": msvc_link_path, + "%{msvc_lib_path}": msvc_lib_path, + "%{dbg_mode_debug}": "/DEBUG:FULL" if support_debug_fastlink else "/DEBUG", + "%{fastbuild_mode_debug}": "/DEBUG:FASTLINK" if support_debug_fastlink else "/DEBUG", + "%{compilation_mode_content}": compilation_mode_content, "%{content}": _get_escaped_windows_msys_crosstool_content(repository_ctx), "%{msys_x64_mingw_content}": _get_escaped_windows_msys_crosstool_content(repository_ctx, use_mingw = True), "%{opt_content}": "", "%{dbg_content}": "", "%{link_content}": "", - "%{cxx_builtin_include_directory}": "", + "%{cxx_builtin_include_directory}": "\n".join(escaped_cxx_include_directories), "%{coverage}": "", - }) - return - - env = setup_vc_env_vars(repository_ctx, vc_path) - escaped_paths = escape_string(env["PATH"]) - escaped_include_paths = escape_string(env["INCLUDE"]) - escaped_lib_paths = escape_string(env["LIB"]) - escaped_tmp_dir = escape_string( - get_env_var(repository_ctx, "TMP", "C:\\Windows\\Temp").replace("\\", "\\\\")) - msvc_cl_path = find_msvc_tool(repository_ctx, vc_path, "cl.exe").replace("\\", "/") - msvc_ml_path = find_msvc_tool(repository_ctx, vc_path, "ml64.exe").replace("\\", "/") - msvc_link_path = find_msvc_tool(repository_ctx, vc_path, "link.exe").replace("\\", "/") - msvc_lib_path = find_msvc_tool(repository_ctx, vc_path, "lib.exe").replace("\\", "/") - escaped_cxx_include_directories = [] - compilation_mode_content = "" - - if _is_use_msvc_wrapper(repository_ctx): - if _is_support_whole_archive(repository_ctx, vc_path): - support_whole_archive = "True" - else: - support_whole_archive = "False" - nvcc_tmp_dir_name = escaped_tmp_dir + "\\\\nvcc_inter_files_tmp_dir" - # Make sure nvcc.exe is in PATH - cuda_path = _find_cuda(repository_ctx) - if cuda_path: - escaped_paths = escape_string(cuda_path.replace("\\", "\\\\") + "/bin;") + escaped_paths - escaped_compute_capabilities = _escaped_cuda_compute_capabilities(repository_ctx) - repository_ctx.template( - "wrapper/bin/pydir/msvc_tools.py", - paths["@bazel_tools//tools/cpp:wrapper/bin/pydir/msvc_tools.py.tpl"], - { - "%{lib_tool}": escape_string(msvc_lib_path), - "%{support_whole_archive}": support_whole_archive, - "%{cuda_compute_capabilities}": ", ".join( - ["\"%s\"" % c for c in escaped_compute_capabilities]), - "%{nvcc_tmp_dir_name}": nvcc_tmp_dir_name, - }) - # nvcc will generate some source files under %{nvcc_tmp_dir_name} - # The generated files are guranteed to have unique name, so they can share the same tmp directory - escaped_cxx_include_directories += [ "cxx_builtin_include_directory: \"%s\"" % nvcc_tmp_dir_name ] - msvc_wrapper = repository_ctx.path( - paths["@bazel_tools//tools/cpp:CROSSTOOL"]).dirname.get_child( - "wrapper").get_child("bin") - for f in ["msvc_cl.bat", "msvc_link.bat", "msvc_nop.bat"]: - repository_ctx.symlink(msvc_wrapper.get_child(f), "wrapper/bin/" + f) - msvc_wrapper = msvc_wrapper.get_child("pydir") - for f in ["msvc_cl.py", "msvc_link.py"]: - repository_ctx.symlink(msvc_wrapper.get_child(f), "wrapper/bin/pydir/" + f) - python_binary = _find_python(repository_ctx) - repository_ctx.template( - "wrapper/bin/call_python.bat", - paths["@bazel_tools//tools/cpp:wrapper/bin/call_python.bat.tpl"], - {"%{python_binary}": escape_string(python_binary)}) - msvc_cl_path = "wrapper/bin/msvc_cl.bat" - msvc_link_path = "wrapper/bin/msvc_link.bat" - msvc_lib_path = "wrapper/bin/msvc_link.bat" - compilation_mode_content = _get_compilation_mode_content() - - for path in escaped_include_paths.split(";"): - if path: - escaped_cxx_include_directories.append("cxx_builtin_include_directory: \"%s\"" % path) - - support_debug_fastlink = _is_support_debug_fastlink(repository_ctx, vc_path) - - repository_ctx.template( - "CROSSTOOL", - paths["@bazel_tools//tools/cpp:CROSSTOOL.tpl"], - { - "%{cpu}": "x64_windows", - "%{default_toolchain_name}": "msvc_x64", - "%{toolchain_name}": "msys_x64", - "%{msvc_env_tmp}": escaped_tmp_dir, - "%{msvc_env_path}": escaped_paths, - "%{msvc_env_include}": escaped_include_paths, - "%{msvc_env_lib}": escaped_lib_paths, - "%{msvc_cl_path}": msvc_cl_path, - "%{msvc_ml_path}": msvc_ml_path, - "%{msvc_link_path}": msvc_link_path, - "%{msvc_lib_path}": msvc_lib_path, - "%{dbg_mode_debug}": "/DEBUG:FULL" if support_debug_fastlink else "/DEBUG", - "%{fastbuild_mode_debug}": "/DEBUG:FASTLINK" if support_debug_fastlink else "/DEBUG", - "%{compilation_mode_content}": compilation_mode_content, - "%{content}": _get_escaped_windows_msys_crosstool_content(repository_ctx), - "%{msys_x64_mingw_content}": _get_escaped_windows_msys_crosstool_content(repository_ctx, use_mingw = True), - "%{opt_content}": "", - "%{dbg_content}": "", - "%{link_content}": "", - "%{cxx_builtin_include_directory}": "\n".join(escaped_cxx_include_directories), - "%{coverage}": "", - }) + }, + ) |