diff --git a/cargo/private/cargo_build_script.bzl b/cargo/private/cargo_build_script.bzl --- a/cargo/private/cargo_build_script.bzl +++ b/cargo/private/cargo_build_script.bzl @@ -144,9 +144,14 @@ def _rewrite_windows_exec_msvc_cc_args(toolchain, args): if toolchain.target_flag_value != toolchain.exec_triple.str or not toolchain.exec_triple.str.endswith("-pc-windows-msvc"): return args - rewritten = [] + rewritten = [ + "-target", + toolchain.target_flag_value, + ] skip_next = False - for arg in args: + for index in range(len(args)): + arg = args[index] + if skip_next: skip_next = False continue @@ -161,21 +166,58 @@ def _rewrite_windows_exec_msvc_cc_args(toolchain, args): if arg == "-nostdlibinc" or arg.startswith("--sysroot"): continue - if "mingw-w64-" in arg or "mingw_import_libraries_directory" in arg or "mingw_crt_library_search_directory" in arg: + if arg.startswith("-fstack-protector") or arg.startswith("-D_FORTIFY_SOURCE="): + continue + + if arg == "-isystem" and index + 1 < len(args): + path = args[index + 1] + if "mingw-w64-" in path or "mingw_import_libraries_directory" in path or "mingw_crt_library_search_directory" in path: + skip_next = True + continue + + rewritten.append(arg) + + return rewritten + +def _rewrite_windows_exec_msvc_link_args(toolchain, args): + """Translate GNU-flavored link args when exec-side build scripts target Windows MSVC.""" + if toolchain.target_flag_value != toolchain.exec_triple.str or not toolchain.exec_triple.str.endswith("-pc-windows-msvc"): + return args + + rewritten = [] + skip_next = False + for index in range(len(args)): + arg = args[index] + + if skip_next: + skip_next = False + continue + + if arg == "--sysroot": + skip_next = True + continue + + if arg.startswith("--sysroot="): continue - if arg.startswith("-fstack-protector"): + if arg == "-L" and index + 1 < len(args): + path = args[index + 1] + if "mingw_import_libraries_directory" in path or "mingw_crt_library_search_directory" in path: + skip_next = True + continue + rewritten.extend([arg, path]) + skip_next = True continue - if arg.startswith("-D_FORTIFY_SOURCE="): + if arg.startswith("-L") and ( + "mingw_import_libraries_directory" in arg or + "mingw_crt_library_search_directory" in arg + ): continue rewritten.append(arg) - return [ - "-target", - toolchain.target_flag_value, - ] + rewritten + return rewritten def get_cc_compile_args_and_env(cc_toolchain, feature_configuration): """Gather cc environment variables from the given `cc_toolchain` @@ -508,15 +550,23 @@ def _cargo_build_script_impl(ctx): cc_toolchain, feature_configuration = find_cc_toolchain(ctx) linker, _, link_args, linker_env = get_linker_and_args(ctx, "bin", toolchain, cc_toolchain, feature_configuration, None) env.update(**linker_env) - env["LD"] = linker - env["LDFLAGS"] = " ".join(_pwd_flags(link_args)) + use_windows_exec_msvc_path_tools = ( + toolchain.target_flag_value == toolchain.exec_triple.str and + toolchain.exec_triple.str.endswith("-pc-windows-msvc") + ) + + if not use_windows_exec_msvc_path_tools: + env["LD"] = linker + link_args = _rewrite_windows_exec_msvc_link_args(toolchain, link_args) + env["LDFLAGS"] = " ".join(_pwd_flags(link_args)) + + # Defaults for cxx flags. + env["CFLAGS"] = "" + env["CXXFLAGS"] = "" - # Defaults for cxx flags. env["ARFLAGS"] = "" - env["CFLAGS"] = "" - env["CXXFLAGS"] = "" fallback_tools = [] - if not cc_toolchain: + if not cc_toolchain and not use_windows_exec_msvc_path_tools: fallbacks = { "AR": "_fallback_ar", "CC": "_fallback_cc", @@ -542,36 +592,37 @@ def _cargo_build_script_impl(ctx): toolchain_tools.append(cc_toolchain.all_files) - env["CC"] = cc_common.get_tool_for_action( - feature_configuration = feature_configuration, - action_name = ACTION_NAMES.c_compile, - ) - env["CXX"] = cc_common.get_tool_for_action( - feature_configuration = feature_configuration, - action_name = ACTION_NAMES.cpp_compile, - ) - env["AR"] = cc_common.get_tool_for_action( - feature_configuration = feature_configuration, - action_name = ACTION_NAMES.cpp_link_static_library, - ) - - # Many C/C++ toolchains are missing an action_config for AR because - # one was never included in the unix_cc_toolchain_config. - if not env["AR"]: - env["AR"] = cc_toolchain.ar_executable - - cc_c_args = _strip_stack_protector_for_windows_llvm_mingw(toolchain, cc_c_args) - cc_cxx_args = _strip_stack_protector_for_windows_llvm_mingw(toolchain, cc_cxx_args) - cc_c_args = _rewrite_windows_exec_msvc_cc_args(toolchain, cc_c_args) - cc_cxx_args = _rewrite_windows_exec_msvc_cc_args(toolchain, cc_cxx_args) - # Populate CFLAGS and CXXFLAGS that cc-rs relies on when building from source, in particular - # to determine the deployment target when building for apple platforms (`macosx-version-min` - # for example, itself derived from the `macos_minimum_os` Bazel argument). - env["CFLAGS"] = " ".join(_pwd_flags(cc_c_args)) - env["CXXFLAGS"] = " ".join(_pwd_flags(cc_cxx_args)) - # It may be tempting to forward ARFLAGS, but cc-rs is opinionated enough - # that doing so is more likely to hurt than help. If you need to change - # ARFLAGS, make changes to cc-rs. + if not use_windows_exec_msvc_path_tools: + env["CC"] = cc_common.get_tool_for_action( + feature_configuration = feature_configuration, + action_name = ACTION_NAMES.c_compile, + ) + env["CXX"] = cc_common.get_tool_for_action( + feature_configuration = feature_configuration, + action_name = ACTION_NAMES.cpp_compile, + ) + env["AR"] = cc_common.get_tool_for_action( + feature_configuration = feature_configuration, + action_name = ACTION_NAMES.cpp_link_static_library, + ) + + # Many C/C++ toolchains are missing an action_config for AR because + # one was never included in the unix_cc_toolchain_config. + if not env["AR"]: + env["AR"] = cc_toolchain.ar_executable + + cc_c_args = _strip_stack_protector_for_windows_llvm_mingw(toolchain, cc_c_args) + cc_cxx_args = _strip_stack_protector_for_windows_llvm_mingw(toolchain, cc_cxx_args) + cc_c_args = _rewrite_windows_exec_msvc_cc_args(toolchain, cc_c_args) + cc_cxx_args = _rewrite_windows_exec_msvc_cc_args(toolchain, cc_cxx_args) + # Populate CFLAGS and CXXFLAGS that cc-rs relies on when building from source, in particular + # to determine the deployment target when building for apple platforms (`macosx-version-min` + # for example, itself derived from the `macos_minimum_os` Bazel argument). + env["CFLAGS"] = " ".join(_pwd_flags(cc_c_args)) + env["CXXFLAGS"] = " ".join(_pwd_flags(cc_cxx_args)) + # It may be tempting to forward ARFLAGS, but cc-rs is opinionated enough + # that doing so is more likely to hurt than help. If you need to change + # ARFLAGS, make changes to cc-rs. # Inform build scripts of rustc flags # https://github.com/rust-lang/cargo/issues/9600