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 @@ -142,40 +142,82 @@ def _strip_stack_protector_for_windows_llvm_mingw(toolchain, args): def _rewrite_windows_exec_msvc_cc_args(toolchain, args): """Translate GNU-flavored cc 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 arg in args: - if skip_next: - skip_next = False - continue + rewritten = [ + "-target", + toolchain.target_flag_value, + ] + skip_next = False + for index in range(len(args)): + arg = args[index] + + if skip_next: + skip_next = False + continue if arg == "-target": - skip_next = True + skip_next = True continue if arg.startswith("-target=") or arg.startswith("--target="): continue 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.startswith("-fstack-protector"): - continue - - if 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 [ - "-target", - toolchain.target_flag_value, - ] + rewritten + 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 == "-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("-L") and ( + "mingw_import_libraries_directory" in arg or + "mingw_crt_library_search_directory" in arg + ): + continue + + rewritten.append(arg) + + return rewritten def get_cc_compile_args_and_env(cc_toolchain, feature_configuration): """Gather cc environment variables from the given `cc_toolchain` @@ -509,6 +550,7 @@ def _construct_build_script_env( linker, _, link_args, linker_env = get_linker_and_args(ctx, "bin", toolchain, cc_toolchain, feature_configuration, None) env.update(**linker_env) 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.