diff options
author | 2017-06-06 05:32:34 -0400 | |
---|---|---|
committer | 2017-06-06 09:51:00 -0400 | |
commit | 284a611ce8e70861d7d812c964e2aac7fc086385 (patch) | |
tree | badbffefa081afc6a8d7c77065f68d2786904cd2 | |
parent | 775598b3d70a704321c53239f686918696ba1652 (diff) |
Use a wrapper-free MSVC toolchain when NO_MSVC_WRAPPER=1
To enable a wrapper free MSVC CROSSTOOL,
set NO_MSVC_WRAPPER=1 or add --action_env=NO_MSVC_WRAPPER=1 flag.
Note that we still use windows_cc_wrapper.bat to invoke cl.exe
and link.exe for different actions.
Result for bazel build src/main/cpp:client :
NO_WRAPPER:
INFO: Elapsed time: 76.833s, Critical Path: 16.70s
WRAPPER:
INFO: Elapsed time: 138.517s, Critical Path: 27.78s
Change-Id: Iaf6b67fc791ae1f8b62d332e47f96f719510bda1
PiperOrigin-RevId: 158118110
-rw-r--r-- | tools/cpp/CROSSTOOL.tpl | 83 | ||||
-rw-r--r-- | tools/cpp/cc_configure.bzl | 54 | ||||
-rw-r--r-- | tools/cpp/windows_cc_wrapper.bat.tpl | 23 | ||||
-rw-r--r-- | tools/cpp/wrapper/bin/pydir/msvc_cl.py | 19 |
4 files changed, 108 insertions, 71 deletions
diff --git a/tools/cpp/CROSSTOOL.tpl b/tools/cpp/CROSSTOOL.tpl index 60a771172c..2b99d89bd4 100644 --- a/tools/cpp/CROSSTOOL.tpl +++ b/tools/cpp/CROSSTOOL.tpl @@ -130,15 +130,15 @@ toolchain { tool_path { name: "ar" - path: "wrapper/bin/msvc_link.bat" + path: "%{msvc_lib_path}" } tool_path { name: "cpp" - path: "wrapper/bin/msvc_cl.bat" + path: "%{msvc_cl_path}" } tool_path { name: "gcc" - path: "wrapper/bin/msvc_cl.bat" + path: "%{msvc_cl_path}" } tool_path { name: "gcov" @@ -146,7 +146,7 @@ toolchain { } tool_path { name: "ld" - path: "wrapper/bin/msvc_link.bat" + path: "%{msvc_link_path}" } tool_path { name: "nm" @@ -278,6 +278,18 @@ toolchain { } feature { + name: "use_linker" + env_set { + action: "c++-link-executable" + action: "c++-link-dynamic-library" + env_entry { + key: "USE_LINKER" + value: "1" + } + } + } + + feature { name: 'include_paths' flag_set { action: 'preprocess-assemble' @@ -335,7 +347,7 @@ toolchain { config_name: 'c-compile' action_name: 'c-compile' tool { - tool_path: 'wrapper/bin/msvc_cl.bat' + tool_path: '%{msvc_cl_path}' } flag_set { flag_group { @@ -371,7 +383,7 @@ toolchain { config_name: 'c++-compile' action_name: 'c++-compile' tool { - tool_path: 'wrapper/bin/msvc_cl.bat' + tool_path: '%{msvc_cl_path}' } flag_set { flag_group { @@ -407,26 +419,25 @@ toolchain { config_name: 'c++-link-executable' action_name: 'c++-link-executable' tool { - tool_path: 'wrapper/bin/msvc_link.bat' + tool_path: '%{msvc_link_path}' } implies: 'nologo' - implies: 'strip_debug_symbols' implies: 'linkstamps' implies: 'output_execpath_flags' implies: 'input_param_flags' implies: 'legacy_link_flags' implies: 'linker_param_file' implies: 'msvc_env' + implies: 'use_linker' } action_config { config_name: 'c++-link-dynamic-library' action_name: 'c++-link-dynamic-library' tool { - tool_path: 'wrapper/bin/msvc_link.bat' + tool_path: '%{msvc_link_path}' } implies: 'nologo' - implies: 'strip_debug_symbols' implies: 'shared_flag' implies: 'linkstamps' implies: 'output_execpath_flags' @@ -435,13 +446,14 @@ toolchain { implies: 'legacy_link_flags' implies: 'linker_param_file' implies: 'msvc_env' + implies: 'use_linker' } action_config { config_name: 'c++-link-static-library' action_name: 'c++-link-static-library' tool { - tool_path: 'wrapper/bin/msvc_link.bat' + tool_path: '%{msvc_lib_path}' } implies: 'nologo' implies: 'archiver_flags' @@ -454,7 +466,7 @@ toolchain { config_name: 'c++-link-alwayslink-static-library' action_name: 'c++-link-alwayslink-static-library' tool { - tool_path: 'wrapper/bin/msvc_link.bat' + tool_path: '%{msvc_lib_path}' } implies: 'nologo' implies: 'archiver_flags' @@ -469,7 +481,7 @@ toolchain { config_name: 'c++-link-pic-static-library' action_name: 'c++-link-pic-static-library' tool { - tool_path: 'wrapper/bin/msvc_link.bat' + tool_path: '%{msvc_lib_path}' } implies: 'nologo' implies: 'archiver_flags' @@ -482,7 +494,7 @@ toolchain { config_name: 'c++-link-alwayslink-pic-static-library' action_name: 'c++-link-alwayslink-pic-static-library' tool { - tool_path: 'wrapper/bin/msvc_link.bat' + tool_path: '%{msvc_lib_path}' } implies: 'nologo' implies: 'archiver_flags' @@ -495,10 +507,9 @@ toolchain { config_name: 'c++-link-interface-dynamic-library' action_name: 'c++-link-interface-dynamic-library' tool { - tool_path: 'wrapper/bin/msvc_link.bat' + tool_path: '%{msvc_lib_path}' } implies: 'nologo' - implies: 'strip_debug_symbols' implies: 'linker_param_file' implies: 'msvc_env' } @@ -518,19 +529,6 @@ toolchain { } feature { - name: 'strip_debug_symbols' - flag_set { - action: 'c++-link-executable' - action: 'c++-link-dynamic-library' - action: 'c++-link-interface-dynamic-library' - flag_group { - expand_if_all_available: 'strip_debug_symbols' - flag: '-Wl,-S' - } - } - } - - feature { name: 'shared_flag' flag_set { action: 'c++-link-dynamic-library' @@ -573,8 +571,7 @@ toolchain { action: 'c++-link-pic-static-library' action: 'c++-link-alwayslink-pic-static-library' flag_group { - flag: 'rcsD' - flag: '%{output_execpath}' + flag: '/OUT:%{output_execpath}' } } } @@ -705,12 +702,6 @@ toolchain { expand_if_all_available: 'linker_param_file' action: 'c++-link-executable' action: 'c++-link-dynamic-library' - flag_group { - flag: '-Wl,@%{linker_param_file}' - } - } - flag_set { - expand_if_all_available: 'linker_param_file' action: 'c++-link-static-library' action: 'c++-link-alwayslink-static-library' action: 'c++-link-pic-static-library' @@ -825,22 +816,6 @@ toolchain { implies: 'link_crt_library' } - 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" - } +%{compilation_mode_content} } diff --git a/tools/cpp/cc_configure.bzl b/tools/cpp/cc_configure.bzl index d9423307ba..e23f014032 100644 --- a/tools/cpp/cc_configure.bzl +++ b/tools/cpp/cc_configure.bzl @@ -635,6 +635,32 @@ def _get_crt_library(repository_ctx, debug = False): crt_library += "d" return crt_library + ".lib" +def _is_no_msvc_wrapper(repository_ctx): + """Returns True if NO_MSVC_WRAPPER is set to 1.""" + env = repository_ctx.os.environ + return "NO_MSVC_WRAPPER" in env and env["NO_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'", + " }"]) + + def _escaped_cuda_compute_capabilities(repository_ctx): """Returns a %-escaped list of strings representing cuda compute capabilities.""" @@ -763,7 +789,9 @@ def _impl(repository_ctx): python_dir = python_binary[0:-10].replace("\\", "\\\\") escaped_include_paths = env["INCLUDE"] + (python_dir + "include") escaped_lib_paths = _escape_string(env["LIB"] + (python_dir + "libs")) - lib_tool = _find_msvc_tool(repository_ctx, vc_path, "lib.exe").replace("\\", "\\\\") + msvc_cl_path = _find_msvc_tool(repository_ctx, vc_path, "cl.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("\\", "/") if _is_support_whole_archive(repository_ctx, vc_path): support_whole_archive = "True" else: @@ -777,11 +805,24 @@ def _impl(repository_ctx): escaped_paths = _escape_string(cuda_path.replace("\\", "\\\\") + "/bin;") + escaped_paths escaped_compute_capabilities = _escaped_cuda_compute_capabilities(repository_ctx) _tpl(repository_ctx, "wrapper/bin/pydir/msvc_tools.py", { - "%{lib_tool}": _escape_string(lib_tool), + "%{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]), }) + _tpl(repository_ctx, "windows_cc_wrapper.bat", { + "%{msvc_cl_path}": msvc_cl_path, + "%{msvc_link_path}": msvc_link_path, + }, "windows_cc_wrapper.bat") + + if _is_no_msvc_wrapper(repository_ctx): + msvc_cl_path = "windows_cc_wrapper.bat" + compilation_mode_content = "" + else: + 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() # nvcc will generate some source files under tmp_dir escaped_cxx_include_directories = [ "cxx_builtin_include_directory: \"%s\"" % escaped_tmp_dir ] @@ -796,6 +837,10 @@ def _impl(repository_ctx): "%{msvc_env_path}": escaped_paths, "%{msvc_env_include}": escaped_include_paths, "%{msvc_env_lib}": escaped_lib_paths, + "%{msvc_cl_path}": msvc_cl_path, + "%{msvc_link_path}": msvc_link_path, + "%{msvc_lib_path}": msvc_lib_path, + "%{compilation_mode_content}": compilation_mode_content, "%{content}": _get_escaped_windows_msys_crosstool_content(repository_ctx), "%{crt_option}": _get_crt_option(repository_ctx), "%{crt_debug_option}": _get_crt_option(repository_ctx, debug=True), @@ -890,6 +935,10 @@ def _impl(repository_ctx): "%{crt_debug_option}": "", "%{crt_library}": "", "%{crt_debug_library}": "", + "%{msvc_cl_path}": "", + "%{msvc_link_path}": "", + "%{msvc_lib_path}": "", + "%{compilation_mode_content}": "", }) cc_autoconf = repository_rule( @@ -914,6 +963,7 @@ cc_autoconf = repository_rule( "HOMEBREW_RUBY_PATH", "NO_WHOLE_ARCHIVE_OPTION", "USE_DYNAMIC_CRT", + "NO_MSVC_WRAPPER", "SYSTEMROOT", "VS90COMNTOOLS", "VS100COMNTOOLS", diff --git a/tools/cpp/windows_cc_wrapper.bat.tpl b/tools/cpp/windows_cc_wrapper.bat.tpl new file mode 100644 index 0000000000..f086033c48 --- /dev/null +++ b/tools/cpp/windows_cc_wrapper.bat.tpl @@ -0,0 +1,23 @@ +:: Copyright 2016 The Bazel Authors. All rights reserved. +:: +:: Licensed under the Apache License, Version 2.0 (the "License"); +:: you may not use this file except in compliance with the License. +:: You may obtain a copy of the License at +:: +:: http://www.apache.org/licenses/LICENSE-2.0 +:: +:: Unless required by applicable law or agreed to in writing, software +:: distributed under the License is distributed on an "AS IS" BASIS, +:: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +:: See the License for the specific language governing permissions and +:: limitations under the License. + +@echo OFF +setlocal enabledelayedexpansion + +if "%USE_LINKER%" == "1" ( + "%{msvc_link_path}" %* + exit !errorlevel! +) + +"%{msvc_cl_path}" %* diff --git a/tools/cpp/wrapper/bin/pydir/msvc_cl.py b/tools/cpp/wrapper/bin/pydir/msvc_cl.py index e35fdcb00e..e1f3771e11 100644 --- a/tools/cpp/wrapper/bin/pydir/msvc_cl.py +++ b/tools/cpp/wrapper/bin/pydir/msvc_cl.py @@ -69,24 +69,13 @@ GCCPATTERNS = [ ] -def _IsLink(args): +def _IsLink(): """Determines whether we need to link rather than compile. - A set of arguments is for linking if they contain -static, -shared, are adding - adding library search paths through -L, or libraries via -l. - - Args: - args: List of arguments - Returns: - Boolean whether this is a link operation or not. + True if USE_LINKER is set to 1. """ - for arg in args: - # Certain flags indicate we are linking. - if (arg in ['-shared', '-static'] or arg[:2] in ['-l', '-L'] or - arg[:3] == '-Wl'): - return True - return False + return 'USE_LINKER' in os.environ and os.environ['USE_LINKER'] == '1' class MsvcCompiler(msvc_tools.WindowsRunner): @@ -138,7 +127,7 @@ class MsvcCompiler(msvc_tools.WindowsRunner): def main(argv): # If we are supposed to link create a static library. - if _IsLink(argv[1:]): + if _IsLink(): return msvc_link.main(argv) else: return MsvcCompiler().Run(argv[1:]) |