From 1e5e51f16173ad68cdd6df149aa08a229cd89c05 Mon Sep 17 00:00:00 2001 From: Caleb Zulawski Date: Sat, 10 Jan 2026 14:49:30 -0500 Subject: [PATCH] Support MSVC with rule-based toolchain --- .bazelci/presubmit.yml | 14 + cc/toolchains/args/BUILD | 25 +- cc/toolchains/args/archiver_flags/BUILD | 38 +- cc/toolchains/args/compiler_input_flags/BUILD | 40 +- .../args/compiler_output_flags/BUILD | 149 +++- cc/toolchains/args/dependency_file/BUILD | 14 +- .../args/external_include_paths/BUILD | 43 +- cc/toolchains/args/generate_linkmap/BUILD | 28 +- cc/toolchains/args/include_flags/BUILD | 96 ++- cc/toolchains/args/libraries_to_link/BUILD | 129 ++- .../args/library_search_directories/BUILD | 8 +- cc/toolchains/args/link_flags/BUILD | 171 +++- cc/toolchains/args/preprocessor_defines/BUILD | 35 +- cc/toolchains/args/random_seed/BUILD | 8 +- .../runtime_library_search_directories/BUILD | 14 +- cc/toolchains/args/shared_flag/BUILD | 24 +- cc/toolchains/args/strip_debug_symbols/BUILD | 18 +- cc/toolchains/args/strip_flags/BUILD | 8 +- cc/toolchains/args/windows/BUILD | 757 ++++++++++++++++++ cc/toolchains/capabilities/BUILD | 12 + cc/toolchains/cc_toolchain_info.bzl | 1 + cc/toolchains/compiler/BUILD | 33 + cc/toolchains/features/legacy/BUILD | 12 + cc/toolchains/impl/BUILD | 9 + cc/toolchains/impl/args_for_compiler.bzl | 104 +++ cc/toolchains/impl/legacy_converter.bzl | 2 +- cc/toolchains/impl/toolchain_config.bzl | 21 +- cc/toolchains/impl/toolchain_config_info.bzl | 7 +- cc/toolchains/variables/BUILD | 72 +- examples/rule_based_toolchain/.bazelrc | 6 + examples/rule_based_toolchain/MODULE.bazel | 22 + examples/rule_based_toolchain/README.md | 13 +- .../asm_answer/BUILD.bazel | 6 +- .../asm_answer/asm_answer_win.asm | 21 + .../constraint/BUILD.bazel | 42 + .../dynamic_answer/BUILD.bazel | 28 +- .../dynamic_answer/dynamic_answer.def | 3 + .../toolchains/BUILD.bazel | 6 + .../toolchains/args/BUILD.bazel | 110 ++- .../toolchains/clang_cl/BUILD.bazel | 89 ++ .../toolchains/clang_cl/clang.BUILD | 73 ++ .../toolchains/clang_cl/tools/BUILD.bazel | 133 +++ .../toolchains/msvc/BUILD.bazel | 88 ++ .../toolchains/msvc/tools/BUILD.bazel | 80 ++ .../toolchains/msvc/tools/msvc_nop.bat | 2 + .../toolchains/windows/BUILD.bazel | 59 ++ .../toolchains/windows/msvc_repo.bzl | 269 +++++++ .../legacy_features_as_args/BUILD | 1 + .../goldens/macos/archiver_flags.textproto | 37 + .../goldens/macos/libraries_to_link.textproto | 77 ++ ...ntime_library_search_directories.textproto | 26 + .../goldens/unix/archiver_flags.textproto | 37 + .../goldens/unix/libraries_to_link.textproto | 77 ++ ...ntime_library_search_directories.textproto | 26 + .../goldens/unix/shared_flag.textproto | 12 + .../goldens/windows/archiver_flags.textproto | 77 ++ tests/rule_based_toolchain/subjects.bzl | 1 + .../toolchain_config_test.bzl | 45 +- 58 files changed, 3267 insertions(+), 91 deletions(-) create mode 100644 cc/toolchains/args/windows/BUILD create mode 100644 cc/toolchains/compiler/BUILD create mode 100644 cc/toolchains/impl/args_for_compiler.bzl create mode 100644 examples/rule_based_toolchain/asm_answer/asm_answer_win.asm create mode 100644 examples/rule_based_toolchain/dynamic_answer/dynamic_answer.def create mode 100644 examples/rule_based_toolchain/toolchains/clang_cl/BUILD.bazel create mode 100644 examples/rule_based_toolchain/toolchains/clang_cl/clang.BUILD create mode 100644 examples/rule_based_toolchain/toolchains/clang_cl/tools/BUILD.bazel create mode 100644 examples/rule_based_toolchain/toolchains/msvc/BUILD.bazel create mode 100644 examples/rule_based_toolchain/toolchains/msvc/tools/BUILD.bazel create mode 100644 examples/rule_based_toolchain/toolchains/msvc/tools/msvc_nop.bat create mode 100644 examples/rule_based_toolchain/toolchains/windows/BUILD.bazel create mode 100644 examples/rule_based_toolchain/toolchains/windows/msvc_repo.bzl create mode 100755 tests/rule_based_toolchain/legacy_features_as_args/goldens/windows/archiver_flags.textproto diff --git a/.bazelci/presubmit.yml b/.bazelci/presubmit.yml index 0bd25f84e..6c97c1a32 100644 --- a/.bazelci/presubmit.yml +++ b/.bazelci/presubmit.yml @@ -151,3 +151,17 @@ tasks: - "--enable_bzlmod" build_targets: - "//..." + + windows_rule_based_toolchains: + name: Windows rule-based toolchains (msvc-cl) + platform: windows + working_directory: examples/rule_based_toolchain + build_flags: + - "--enable_bzlmod" + - "--config=msvc-cl" + build_targets: + - "//..." + test_flags: + - "--config=msvc-cl" + test_targets: + - "//..." diff --git a/cc/toolchains/args/BUILD b/cc/toolchains/args/BUILD index 84560353f..09e9ef431 100644 --- a/cc/toolchains/args/BUILD +++ b/cc/toolchains/args/BUILD @@ -1,4 +1,3 @@ -load("//cc/toolchains:feature.bzl", "cc_feature") load("//cc/toolchains:feature_set.bzl", "cc_feature_set") package(default_visibility = ["//visibility:public"]) @@ -15,7 +14,6 @@ package(default_visibility = ["//visibility:public"]) cc_feature_set( name = "experimental_replace_legacy_action_config_features", all_of = [ - ":backfill_legacy_args", "//cc/toolchains/args/archiver_flags:feature", "//cc/toolchains/args/pic_flags:feature", "//cc/toolchains/args/libraries_to_link:feature", @@ -30,9 +28,19 @@ cc_feature_set( "//cc/toolchains/args/soname_flags:feature", "//cc/toolchains/args/static_libgcc:feature", "//cc/toolchains/args/include_flags:feature", - "//cc/toolchains/args/compiler_input_flags:feature", "//cc/toolchains/args/compiler_output_flags:feature", + "//cc/toolchains/args/compiler_input_flags:feature", # the inputs must follow the outputs, or ml.exe assembler doesn't work + "//cc/toolchains/args/windows:def_file_feature", + "//cc/toolchains/args/windows:default_compile_flags_feature", + "//cc/toolchains/args/windows:default_cpp_std_feature", + "//cc/toolchains/args/windows:cpp20_module_compile_flags_feature", + "//cc/toolchains/args/windows:determinism_feature", + "//cc/toolchains/args/windows:nologo_feature", + "//cc/toolchains/args/windows:windows_machine_flags_feature", + "//cc/toolchains/args/windows:remove_unreferenced_code_feature", + "//cc/toolchains/args/windows:ignore_noisy_warnings_feature", "//cc/toolchains/args/fission_flags:feature", + "//cc/toolchains/args/windows:dynamic_link_msvcrt_feature", "//cc/toolchains/args/link_flags:feature", "//cc/toolchains/args/linkstamp_flags:feature", "//cc/toolchains/args/library_search_directories:feature", @@ -40,14 +48,3 @@ cc_feature_set( "//cc/toolchains/args/compile_flags:feature", # NOTE: This should come below default flags so user flags can override them ], ) - -cc_feature( - name = "backfill_legacy_args", - feature_name = "experimental_replace_legacy_action_config_features", - # TODO: Convert remaining items in this list into their actual args. - implies = [ - "//cc/toolchains/features/legacy:build_interface_libraries", - "//cc/toolchains/features/legacy:dynamic_library_linker_tool", - ], - visibility = ["//visibility:private"], -) diff --git a/cc/toolchains/args/archiver_flags/BUILD b/cc/toolchains/args/archiver_flags/BUILD index 8a73c4db1..cfd967bb3 100644 --- a/cc/toolchains/args/archiver_flags/BUILD +++ b/cc/toolchains/args/archiver_flags/BUILD @@ -3,6 +3,7 @@ load("//cc/toolchains:args.bzl", "cc_args") load("//cc/toolchains:args_list.bzl", "cc_args_list") load("//cc/toolchains:feature.bzl", "cc_feature") load("//cc/toolchains:nested_args.bzl", "cc_nested_args") +load("//cc/toolchains/impl:args_for_compiler.bzl", "cc_args_for_compiler") package(default_visibility = ["//visibility:private"]) @@ -30,13 +31,15 @@ cc_args_list( name = "archiver_flags", args = [ ":create_static_archive", + ":create_static_archive_windows", ":output_execpath", + ":output_execpath_windows", ":libraries_to_link", ], visibility = ["//visibility:public"], ) -cc_args( +cc_args_for_compiler( name = "create_static_archive", actions = ["//cc/toolchains/actions:ar_actions"], args = select({ @@ -47,16 +50,47 @@ cc_args( ], "//conditions:default": ["rcsD"], }), + exclude_compilers = [ + "msvc-cl", + "clang-cl", + ], requires_not_none = "//cc/toolchains/variables:output_execpath", ) -cc_args( +cc_args_for_compiler( + name = "create_static_archive_windows", + actions = ["//cc/toolchains/actions:ar_actions"], + args = [], + compilers = [ + "msvc-cl", + "clang-cl", + ], + requires_not_none = "//cc/toolchains/variables:output_execpath", +) + +cc_args_for_compiler( name = "output_execpath", actions = ["//cc/toolchains/actions:ar_actions"], args = select({ ":use_libtool_on_macos_setting": ["-o"], "//conditions:default": [], }) + ["{output_execpath}"], + exclude_compilers = [ + "msvc-cl", + "clang-cl", + ], + format = {"output_execpath": "//cc/toolchains/variables:output_execpath"}, + requires_not_none = "//cc/toolchains/variables:output_execpath", +) + +cc_args_for_compiler( + name = "output_execpath_windows", + actions = ["//cc/toolchains/actions:ar_actions"], + args = ["/OUT:{output_execpath}"], + compilers = [ + "msvc-cl", + "clang-cl", + ], format = {"output_execpath": "//cc/toolchains/variables:output_execpath"}, requires_not_none = "//cc/toolchains/variables:output_execpath", ) diff --git a/cc/toolchains/args/compiler_input_flags/BUILD b/cc/toolchains/args/compiler_input_flags/BUILD index 592f1e748..afa468442 100644 --- a/cc/toolchains/args/compiler_input_flags/BUILD +++ b/cc/toolchains/args/compiler_input_flags/BUILD @@ -1,20 +1,54 @@ -load("//cc/toolchains:args.bzl", "cc_args") load("//cc/toolchains:feature.bzl", "cc_feature") +load("//cc/toolchains/impl:args_for_compiler.bzl", "cc_args_for_compiler") cc_feature( name = "feature", - args = [":flags"], + args = [ + ":flags", + ":flags_windows", + ], overrides = "//cc/toolchains/features/legacy:compiler_input_flags", visibility = ["//visibility:public"], ) -cc_args( +cc_args_for_compiler( name = "flags", actions = ["//cc/toolchains/actions:compile_actions"], args = [ "-c", "{source_file}", ], + exclude_compilers = [ + "msvc-cl", + "clang-cl", + ], + format = {"source_file": "//cc/toolchains/variables:source_file"}, + requires_not_none = "//cc/toolchains/variables:source_file", +) + +cc_args_for_compiler( + name = "flags_windows", + actions = [ + "//cc/toolchains/actions:assemble", + "//cc/toolchains/actions:preprocess_assemble", + "//cc/toolchains/actions:c_compile", + "//cc/toolchains/actions:linkstamp_compile", + "//cc/toolchains/actions:cpp_compile", + "//cc/toolchains/actions:cpp_header_parsing", + "//cc/toolchains/actions:cpp_module_compile", + "//cc/toolchains/actions:cpp_module_codegen", + "//cc/toolchains/actions:cpp_module_deps_scanning", + "//cc/toolchains/actions:cpp20_module_compile", + "//cc/toolchains/actions:cpp20_module_codegen", + ], + args = [ + "/c", + "{source_file}", + ], + compilers = [ + "msvc-cl", + "clang-cl", + ], format = {"source_file": "//cc/toolchains/variables:source_file"}, requires_not_none = "//cc/toolchains/variables:source_file", ) diff --git a/cc/toolchains/args/compiler_output_flags/BUILD b/cc/toolchains/args/compiler_output_flags/BUILD index dc73c4966..8a28296b2 100644 --- a/cc/toolchains/args/compiler_output_flags/BUILD +++ b/cc/toolchains/args/compiler_output_flags/BUILD @@ -1,6 +1,7 @@ -load("//cc/toolchains:args.bzl", "cc_args") load("//cc/toolchains:args_list.bzl", "cc_args_list") load("//cc/toolchains:feature.bzl", "cc_feature") +load("//cc/toolchains:nested_args.bzl", "cc_nested_args") +load("//cc/toolchains/impl:args_for_compiler.bzl", "cc_args_for_compiler") cc_feature( name = "feature", @@ -15,30 +16,170 @@ cc_args_list( ":output_assembly_flags", ":output_preprocess_flags", ":output_flags", + ":windows_output_preprocess_flags", + ":windows_output_assembly_flags", + ":windows_output_object_flags", + ":windows_output_object_flags_preprocess_assemble", + ":windows_output_object_flags_asm", ], ) -cc_args( +cc_nested_args( + name = "windows_output_object_file_no_preprocess", + nested = [":windows_output_object_file_no_preprocess_or_assembly"], + requires_none = "//cc/toolchains/variables:output_preprocess_file", +) + +cc_nested_args( + name = "windows_output_object_file_no_preprocess_or_assembly", + args = ["/Fo{output_file}"], + format = {"output_file": "//cc/toolchains/variables:output_file"}, + requires_none = "//cc/toolchains/variables:output_assembly_file", +) + +cc_nested_args( + name = "windows_output_object_file_asm_no_preprocess", + nested = [":windows_output_object_file_asm_no_preprocess_or_assembly"], + requires_none = "//cc/toolchains/variables:output_preprocess_file", +) + +cc_nested_args( + name = "windows_output_object_file_asm_no_preprocess_or_assembly", + args = [ + "/Fo{output_file}", + "/Zi", + ], + format = {"output_file": "//cc/toolchains/variables:output_file"}, + requires_none = "//cc/toolchains/variables:output_assembly_file", +) + +cc_args_for_compiler( name = "output_assembly_flags", actions = ["//cc/toolchains/actions:compile_actions"], args = ["-S"], + exclude_compilers = [ + "msvc-cl", + "clang-cl", + ], requires_not_none = "//cc/toolchains/variables:output_assembly_file", ) -cc_args( +cc_args_for_compiler( name = "output_preprocess_flags", actions = ["//cc/toolchains/actions:compile_actions"], args = ["-E"], + exclude_compilers = [ + "msvc-cl", + "clang-cl", + ], requires_not_none = "//cc/toolchains/variables:output_preprocess_file", ) -cc_args( +cc_args_for_compiler( name = "output_flags", actions = ["//cc/toolchains/actions:compile_actions"], args = [ "-o", "{output_file}", ], + exclude_compilers = [ + "msvc-cl", + "clang-cl", + ], format = {"output_file": "//cc/toolchains/variables:output_file"}, requires_not_none = "//cc/toolchains/variables:output_file", ) + +cc_args_for_compiler( + name = "windows_output_preprocess_flags", + actions = [ + "//cc/toolchains/actions:preprocess_assemble", + "//cc/toolchains/actions:c_compile", + "//cc/toolchains/actions:linkstamp_compile", + "//cc/toolchains/actions:cpp_compile", + "//cc/toolchains/actions:cpp_header_parsing", + "//cc/toolchains/actions:cpp_module_compile", + "//cc/toolchains/actions:cpp_module_codegen", + "//cc/toolchains/actions:cpp_module_deps_scanning", + "//cc/toolchains/actions:cpp20_module_compile", + "//cc/toolchains/actions:cpp20_module_codegen", + ], + args = [ + "/P", + "/Fi{output_file}", + ], + compilers = [ + "msvc-cl", + "clang-cl", + ], + format = {"output_file": "//cc/toolchains/variables:output_preprocess_file"}, + requires_not_none = "//cc/toolchains/variables:output_preprocess_file", +) + +cc_args_for_compiler( + name = "windows_output_assembly_flags", + actions = [ + "//cc/toolchains/actions:preprocess_assemble", + "//cc/toolchains/actions:c_compile", + "//cc/toolchains/actions:linkstamp_compile", + "//cc/toolchains/actions:cpp_compile", + "//cc/toolchains/actions:cpp_header_parsing", + "//cc/toolchains/actions:cpp_module_compile", + "//cc/toolchains/actions:cpp_module_codegen", + "//cc/toolchains/actions:cpp_module_deps_scanning", + "//cc/toolchains/actions:cpp20_module_compile", + "//cc/toolchains/actions:cpp20_module_codegen", + ], + args = [ + "/Fa{output_file}", + ], + compilers = [ + "msvc-cl", + "clang-cl", + ], + format = {"output_file": "//cc/toolchains/variables:output_assembly_file"}, + requires_not_none = "//cc/toolchains/variables:output_assembly_file", +) + +cc_args_for_compiler( + name = "windows_output_object_flags", + actions = [ + "//cc/toolchains/actions:c_compile", + "//cc/toolchains/actions:linkstamp_compile", + "//cc/toolchains/actions:cpp_compile", + "//cc/toolchains/actions:cpp_header_parsing", + "//cc/toolchains/actions:cpp_module_compile", + "//cc/toolchains/actions:cpp_module_codegen", + "//cc/toolchains/actions:cpp_module_deps_scanning", + "//cc/toolchains/actions:cpp20_module_compile", + "//cc/toolchains/actions:cpp20_module_codegen", + ], + compilers = [ + "msvc-cl", + "clang-cl", + ], + nested = [":windows_output_object_file_no_preprocess"], + requires_not_none = "//cc/toolchains/variables:output_file", +) + +cc_args_for_compiler( + name = "windows_output_object_flags_preprocess_assemble", + actions = ["//cc/toolchains/actions:preprocess_assemble"], + compilers = [ + "msvc-cl", + "clang-cl", + ], + nested = [":windows_output_object_file_no_preprocess"], + requires_not_none = "//cc/toolchains/variables:output_file", +) + +cc_args_for_compiler( + name = "windows_output_object_flags_asm", + actions = ["//cc/toolchains/actions:assemble"], + compilers = [ + "msvc-cl", + "clang-cl", + ], + nested = [":windows_output_object_file_asm_no_preprocess"], + requires_not_none = "//cc/toolchains/variables:output_file", +) diff --git a/cc/toolchains/args/dependency_file/BUILD b/cc/toolchains/args/dependency_file/BUILD index 3c80781ea..11128dac8 100644 --- a/cc/toolchains/args/dependency_file/BUILD +++ b/cc/toolchains/args/dependency_file/BUILD @@ -1,14 +1,20 @@ -load("//cc/toolchains:args.bzl", "cc_args") load("//cc/toolchains:feature.bzl", "cc_feature") +load("//cc/toolchains/impl:args_for_compiler.bzl", "cc_args_for_compiler") cc_feature( name = "feature", args = [":flags"], overrides = "//cc/toolchains/features/legacy:dependency_file", + requires_any_of = [ + "//cc/toolchains/compiler:clang", + "//cc/toolchains/compiler:gcc", + "//cc/toolchains/compiler:mingw-gcc", + "//cc/toolchains/compiler:emscripten", + ], visibility = ["//visibility:public"], ) -cc_args( +cc_args_for_compiler( name = "flags", actions = ["//cc/toolchains/actions:compile_actions"], args = [ @@ -16,6 +22,10 @@ cc_args( "-MF", "{dependency_file}", ], + exclude_compilers = [ + "msvc-cl", + "clang-cl", + ], format = {"dependency_file": "//cc/toolchains/variables:dependency_file"}, requires_not_none = "//cc/toolchains/variables:dependency_file", ) diff --git a/cc/toolchains/args/external_include_paths/BUILD b/cc/toolchains/args/external_include_paths/BUILD index de2214f3d..cf1e08fdc 100644 --- a/cc/toolchains/args/external_include_paths/BUILD +++ b/cc/toolchains/args/external_include_paths/BUILD @@ -1,14 +1,42 @@ -load("//cc/toolchains:args.bzl", "cc_args") load("//cc/toolchains:nested_args.bzl", "cc_nested_args") +load("//cc/toolchains/impl:args_for_compiler.bzl", "cc_args_for_compiler") -cc_args( +cc_args_for_compiler( name = "external_include_paths", actions = ["//cc/toolchains/actions:compile_actions"], + exclude_compilers = [ + "msvc-cl", + "clang-cl", + ], nested = [":search_dir_args"], requires_not_none = "//cc/toolchains/variables:external_include_paths", visibility = ["//visibility:public"], ) +cc_args_for_compiler( + name = "external_include_paths_windows", + actions = [ + "//cc/toolchains/actions:preprocess_assemble", + "//cc/toolchains/actions:linkstamp_compile", + "//cc/toolchains/actions:c_compile", + "//cc/toolchains/actions:cpp_compile", + "//cc/toolchains/actions:cpp_header_parsing", + "//cc/toolchains/actions:cpp_module_compile", + "//cc/toolchains/actions:cpp_module_deps_scanning", + "//cc/toolchains/actions:cpp20_module_compile", + "//cc/toolchains/actions:clif_match", + "//cc/toolchains/actions:objc_compile", + "//cc/toolchains/actions:objcpp_compile", + ], + compilers = [ + "msvc-cl", + "clang-cl", + ], + nested = [":search_dir_args_windows"], + requires_not_none = "//cc/toolchains/variables:external_include_paths", + visibility = ["//visibility:public"], +) + cc_nested_args( name = "search_dir_args", args = [ @@ -20,3 +48,14 @@ cc_nested_args( }, iterate_over = "//cc/toolchains/variables:external_include_paths", ) + +cc_nested_args( + name = "search_dir_args_windows", + args = [ + "/external:I{search_dir}", + ], + format = { + "search_dir": "//cc/toolchains/variables:external_include_paths", + }, + iterate_over = "//cc/toolchains/variables:external_include_paths", +) diff --git a/cc/toolchains/args/generate_linkmap/BUILD b/cc/toolchains/args/generate_linkmap/BUILD index eb73dc5b5..3f67fae32 100644 --- a/cc/toolchains/args/generate_linkmap/BUILD +++ b/cc/toolchains/args/generate_linkmap/BUILD @@ -1,21 +1,41 @@ -load("//cc/toolchains:args.bzl", "cc_args") load("//cc/toolchains:feature.bzl", "cc_feature") +load("//cc/toolchains/impl:args_for_compiler.bzl", "cc_args_for_compiler") package(default_visibility = ["//visibility:public"]) cc_feature( name = "feature", - args = [":generate_linkmap"], + args = [ + ":generate_linkmap_windows", + ":generate_linkmap_generic", + ], feature_name = "generate_linkmap", ) -cc_args( - name = "generate_linkmap", +cc_args_for_compiler( + name = "generate_linkmap_windows", + actions = ["//cc/toolchains/actions:link_actions"], + args = ["/MAP:{output_execpath}.map"], + compilers = [ + "msvc-cl", + "clang-cl", + ], + format = {"output_execpath": "//cc/toolchains/variables:output_execpath"}, + requires_not_none = "//cc/toolchains/variables:output_execpath", + visibility = ["//visibility:private"], +) + +cc_args_for_compiler( + name = "generate_linkmap_generic", actions = ["//cc/toolchains/actions:link_actions"], args = select({ "@platforms//os:macos": ["-Wl,-map,{output_execpath}.map"], "//conditions:default": ["-Wl,-Map={output_execpath}.map"], }), + exclude_compilers = [ + "msvc-cl", + "clang-cl", + ], format = {"output_execpath": "//cc/toolchains/variables:output_execpath"}, requires_not_none = "//cc/toolchains/variables:output_execpath", visibility = ["//visibility:private"], diff --git a/cc/toolchains/args/include_flags/BUILD b/cc/toolchains/args/include_flags/BUILD index 1ee790e26..8126f1ca5 100644 --- a/cc/toolchains/args/include_flags/BUILD +++ b/cc/toolchains/args/include_flags/BUILD @@ -1,6 +1,16 @@ -load("//cc/toolchains:args.bzl", "cc_args") load("//cc/toolchains:feature.bzl", "cc_feature") load("//cc/toolchains:feature_set.bzl", "cc_feature_set") +load("//cc/toolchains/impl:args_for_compiler.bzl", "cc_args_for_compiler") + +WINDOWS_INCLUDE_PATH_ACTIONS = [ + "//cc/toolchains/actions:assemble", + "//cc/toolchains/actions:preprocess_assemble", + "//cc/toolchains/actions:c_compile", + "//cc/toolchains/actions:linkstamp_compile", + "//cc/toolchains/actions:cpp_compile", + "//cc/toolchains/actions:cpp_header_parsing", + "//cc/toolchains/actions:cpp_module_compile", +] cc_feature_set( name = "feature", @@ -13,17 +23,23 @@ cc_feature_set( cc_feature( name = "includes_feature", - args = [":includes_flags"], + args = [ + ":includes_flags", + ], overrides = "//cc/toolchains/features/legacy:includes", ) -cc_args( +cc_args_for_compiler( name = "includes_flags", actions = ["//cc/toolchains/actions:source_compile_actions"], args = [ "-include", "{includes}", ], + exclude_compilers = [ + "msvc-cl", + "clang-cl", + ], format = {"includes": "//cc/toolchains/variables:includes"}, iterate_over = "//cc/toolchains/variables:includes", requires_not_none = "//cc/toolchains/variables:includes", @@ -33,51 +49,117 @@ cc_feature( name = "include_paths_feature", args = [ ":quote_include_paths_flags", + ":quote_include_paths_flags_windows", ":include_paths_flags", + ":include_paths_flags_windows", ":system_include_paths_flags", + ":system_include_paths_flags_windows", ":framework_include_paths_flags", + ":framework_include_paths_flags_windows", ], overrides = "//cc/toolchains/features/legacy:include_paths", ) -cc_args( +cc_args_for_compiler( name = "quote_include_paths_flags", actions = ["//cc/toolchains/actions:source_compile_actions"], args = [ "-iquote", "{quote_include_paths}", ], + exclude_compilers = [ + "msvc-cl", + "clang-cl", + ], format = {"quote_include_paths": "//cc/toolchains/variables:quote_include_paths"}, iterate_over = "//cc/toolchains/variables:quote_include_paths", ) -cc_args( +cc_args_for_compiler( + name = "quote_include_paths_flags_windows", + actions = WINDOWS_INCLUDE_PATH_ACTIONS, + args = ["/I{quote_include_paths}"], + compilers = [ + "msvc-cl", + "clang-cl", + ], + format = {"quote_include_paths": "//cc/toolchains/variables:quote_include_paths"}, + iterate_over = "//cc/toolchains/variables:quote_include_paths", +) + +cc_args_for_compiler( name = "include_paths_flags", actions = ["//cc/toolchains/actions:source_compile_actions"], args = [ "-I{include_paths}", ], + exclude_compilers = [ + "msvc-cl", + "clang-cl", + ], + format = {"include_paths": "//cc/toolchains/variables:include_paths"}, + iterate_over = "//cc/toolchains/variables:include_paths", +) + +cc_args_for_compiler( + name = "include_paths_flags_windows", + actions = WINDOWS_INCLUDE_PATH_ACTIONS, + args = ["/I{include_paths}"], + compilers = [ + "msvc-cl", + "clang-cl", + ], format = {"include_paths": "//cc/toolchains/variables:include_paths"}, iterate_over = "//cc/toolchains/variables:include_paths", ) -cc_args( +cc_args_for_compiler( name = "system_include_paths_flags", actions = ["//cc/toolchains/actions:source_compile_actions"], args = [ "-isystem", "{system_include_paths}", ], + exclude_compilers = [ + "msvc-cl", + "clang-cl", + ], format = {"system_include_paths": "//cc/toolchains/variables:system_include_paths"}, iterate_over = "//cc/toolchains/variables:system_include_paths", ) -cc_args( +cc_args_for_compiler( + name = "system_include_paths_flags_windows", + actions = WINDOWS_INCLUDE_PATH_ACTIONS, + args = ["/I{system_include_paths}"], + compilers = [ + "msvc-cl", + "clang-cl", + ], + format = {"system_include_paths": "//cc/toolchains/variables:system_include_paths"}, + iterate_over = "//cc/toolchains/variables:system_include_paths", +) + +cc_args_for_compiler( name = "framework_include_paths_flags", actions = ["//cc/toolchains/actions:source_compile_actions"], args = [ "-F{framework_include_paths}", ], + exclude_compilers = [ + "msvc-cl", + "clang-cl", + ], format = {"framework_include_paths": "//cc/toolchains/variables:framework_include_paths"}, iterate_over = "//cc/toolchains/variables:framework_include_paths", ) + +cc_args_for_compiler( + name = "framework_include_paths_flags_windows", + actions = WINDOWS_INCLUDE_PATH_ACTIONS, + args = [], + compilers = [ + "msvc-cl", + "clang-cl", + ], +) diff --git a/cc/toolchains/args/libraries_to_link/BUILD b/cc/toolchains/args/libraries_to_link/BUILD index 6271856a5..102ce9007 100644 --- a/cc/toolchains/args/libraries_to_link/BUILD +++ b/cc/toolchains/args/libraries_to_link/BUILD @@ -1,7 +1,8 @@ -load("//cc/toolchains:args.bzl", "cc_args") +load("//cc/toolchains:args_list.bzl", "cc_args_list") load("//cc/toolchains:feature.bzl", "cc_feature") load("//cc/toolchains:nested_args.bzl", "cc_nested_args") load("//cc/toolchains/args/libraries_to_link/private:library_link_args.bzl", "library_link_args") +load("//cc/toolchains/impl:args_for_compiler.bzl", "cc_args_for_compiler") package(default_visibility = ["//visibility:private"]) @@ -12,14 +13,39 @@ cc_feature( visibility = ["//visibility:public"], ) -cc_args( +cc_args_list( name = "libraries_to_link", + args = [ + ":libraries_to_link_generic", + ":libraries_to_link_windows", + ], + visibility = ["//visibility:public"], +) + +cc_args_for_compiler( + name = "libraries_to_link_generic", actions = ["//cc/toolchains/actions:link_actions"], + exclude_compilers = [ + "msvc-cl", + "clang-cl", + ], nested = [ ":thinlto_param_file", - ":libraries_to_link_args", + ":libraries_to_link_args_generic", + ], +) + +cc_args_for_compiler( + name = "libraries_to_link_windows", + actions = ["//cc/toolchains/actions:link_actions"], + compilers = [ + "msvc-cl", + "clang-cl", + ], + nested = [ + ":thinlto_param_file", + ":libraries_to_link_args_windows", ], - visibility = ["//visibility:public"], ) cc_nested_args( @@ -32,21 +58,35 @@ cc_nested_args( ) cc_nested_args( - name = "libraries_to_link_args", - nested = [":iterate_over_libraries_to_link"], + name = "libraries_to_link_args_generic", + nested = [":iterate_over_libraries_to_link_generic"], requires_not_none = "//cc/toolchains/variables:libraries_to_link", ) cc_nested_args( - name = "iterate_over_libraries_to_link", + name = "iterate_over_libraries_to_link_generic", iterate_over = "//cc/toolchains/variables:libraries_to_link", nested = [ ":optional_object_file_group_start", - ":single_library_args", + ":single_library_args_generic", ":optional_object_file_group_end", ], ) +cc_nested_args( + name = "libraries_to_link_args_windows", + nested = [":iterate_over_libraries_to_link_windows"], + requires_not_none = "//cc/toolchains/variables:libraries_to_link", +) + +cc_nested_args( + name = "iterate_over_libraries_to_link_windows", + iterate_over = "//cc/toolchains/variables:libraries_to_link", + nested = [ + ":single_library_args_windows", + ], +) + cc_nested_args( name = "optional_object_file_group_start", nested = [":start_lib_arg"], @@ -74,7 +114,7 @@ cc_nested_args( ) cc_nested_args( - name = "single_library_args", + name = "single_library_args_generic", nested = select({ "@platforms//os:macos": [], "//conditions:default": [":optional_whole_archive_start"], @@ -85,7 +125,7 @@ cc_nested_args( ":optional_static_library", ":optional_dynamic_library", ] + select({ - # maOS has a minor nuance where it uses the path to the library instead of `-l:{library_name}`. + # macOS has a minor nuance where it uses the path to the library instead of `-l:{library_name}`. "@platforms//os:macos": [":macos_optional_versioned_dynamic_library"], "//conditions:default": [":generic_optional_versioned_dynamic_library"], }) + select({ @@ -94,6 +134,75 @@ cc_nested_args( }), ) +cc_nested_args( + name = "single_library_args_windows", + nested = [ + ":windows_optional_object_file_group", + ":windows_optional_object_file", + ":windows_optional_interface_library", + ":windows_optional_static_library", + ], +) + +cc_nested_args( + name = "windows_optional_object_file_group", + args = ["{library}"], + format = { + "library": "//cc/toolchains/variables:libraries_to_link.object_files", + }, + iterate_over = "//cc/toolchains/variables:libraries_to_link.object_files", + requires_equal = "//cc/toolchains/variables:libraries_to_link.type", + requires_equal_value = "object_file_group", +) + +cc_nested_args( + name = "windows_optional_object_file", + args = ["{library}"], + format = { + "library": "//cc/toolchains/variables:libraries_to_link.name", + }, + requires_equal = "//cc/toolchains/variables:libraries_to_link.type", + requires_equal_value = "object_file", +) + +cc_nested_args( + name = "windows_optional_interface_library", + args = ["{library}"], + format = { + "library": "//cc/toolchains/variables:libraries_to_link.name", + }, + requires_equal = "//cc/toolchains/variables:libraries_to_link.type", + requires_equal_value = "interface_library", +) + +cc_nested_args( + name = "windows_optional_static_library", + nested = [ + ":windows_static_library_whole_archive", + ":windows_static_library_no_whole_archive", + ], + requires_equal = "//cc/toolchains/variables:libraries_to_link.type", + requires_equal_value = "static_library", +) + +cc_nested_args( + name = "windows_static_library_no_whole_archive", + args = ["{library}"], + format = { + "library": "//cc/toolchains/variables:libraries_to_link.name", + }, + requires_false = "//cc/toolchains/variables:libraries_to_link.is_whole_archive", +) + +cc_nested_args( + name = "windows_static_library_whole_archive", + args = ["/WHOLEARCHIVE:{library}"], + format = { + "library": "//cc/toolchains/variables:libraries_to_link.name", + }, + requires_true = "//cc/toolchains/variables:libraries_to_link.is_whole_archive", +) + cc_nested_args( name = "optional_whole_archive_start", nested = [":whole_archive_start_arg"], diff --git a/cc/toolchains/args/library_search_directories/BUILD b/cc/toolchains/args/library_search_directories/BUILD index 1ed8b1487..b1e9fdee6 100644 --- a/cc/toolchains/args/library_search_directories/BUILD +++ b/cc/toolchains/args/library_search_directories/BUILD @@ -1,5 +1,5 @@ -load("//cc/toolchains:args.bzl", "cc_args") load("//cc/toolchains:feature.bzl", "cc_feature") +load("//cc/toolchains/impl:args_for_compiler.bzl", "cc_args_for_compiler") cc_feature( name = "feature", @@ -8,10 +8,14 @@ cc_feature( visibility = ["//visibility:public"], ) -cc_args( +cc_args_for_compiler( name = "flags", actions = ["//cc/toolchains/actions:link_actions"], args = ["-L{library_search_directories}"], + exclude_compilers = [ + "msvc-cl", + "clang-cl", + ], format = {"library_search_directories": "//cc/toolchains/variables:library_search_directories"}, iterate_over = "//cc/toolchains/variables:library_search_directories", requires_not_none = "//cc/toolchains/variables:library_search_directories", diff --git a/cc/toolchains/args/link_flags/BUILD b/cc/toolchains/args/link_flags/BUILD index 58d38a31f..c6a83b40e 100644 --- a/cc/toolchains/args/link_flags/BUILD +++ b/cc/toolchains/args/link_flags/BUILD @@ -1,6 +1,9 @@ load("//cc/toolchains:args.bzl", "cc_args") +load("//cc/toolchains:args_list.bzl", "cc_args_list") load("//cc/toolchains:feature.bzl", "cc_feature") load("//cc/toolchains:feature_set.bzl", "cc_feature_set") +load("//cc/toolchains:nested_args.bzl", "cc_nested_args") +load("//cc/toolchains/impl:args_for_compiler.bzl", "cc_args_for_compiler") cc_feature_set( name = "feature", @@ -8,6 +11,9 @@ cc_feature_set( ":user_link_flags_feature", ":legacy_link_flags_feature", ":output_link_flags_feature", + ":linker_subsystem_flag_feature", + ":build_interface_libraries_feature", + ":dynamic_library_linker_tool_feature", ], visibility = ["//visibility:public"], ) @@ -44,17 +50,178 @@ cc_args( cc_feature( name = "output_link_flags_feature", - args = [":output_link_flags"], + args = [ + ":output_link_flags", + ":output_link_flags_windows", + ], overrides = "//cc/toolchains/features/legacy:output_execpath_flags", ) -cc_args( +cc_feature( + name = "linker_subsystem_flag_feature", + args = [":linker_subsystem_flag_windows"], + feature_name = "linker_subsystem_flag", + visibility = ["//visibility:private"], +) + +cc_args_for_compiler( + name = "linker_subsystem_flag_windows", + actions = ["//cc/toolchains/actions:link_actions"], + args = ["/SUBSYSTEM:CONSOLE"], + compilers = [ + "msvc-cl", + "clang-cl", + ], +) + +cc_args_for_compiler( name = "output_link_flags", actions = ["//cc/toolchains/actions:link_actions"], args = [ "-o", "{output_execpath}", ], + exclude_compilers = [ + "msvc-cl", + "clang-cl", + ], format = {"output_execpath": "//cc/toolchains/variables:output_execpath"}, requires_not_none = "//cc/toolchains/variables:output_execpath", ) + +cc_args_for_compiler( + name = "output_link_flags_windows", + actions = ["//cc/toolchains/actions:link_actions"], + args = ["/OUT:{output_execpath}"], + compilers = [ + "msvc-cl", + "clang-cl", + ], + format = {"output_execpath": "//cc/toolchains/variables:output_execpath"}, + requires_not_none = "//cc/toolchains/variables:output_execpath", +) + +cc_feature( + name = "build_interface_libraries_feature", + args = ["build_interface_libraries"], + overrides = "//cc/toolchains/features/legacy:build_interface_libraries", +) + +cc_args_list( + name = "build_interface_libraries", + args = [ + ":build_interface_libraries_generic", + ":build_interface_libraries_windows", + ], +) + +cc_nested_args( + name = "build_interface_libraries_check_generic", + nested = [":generic_build_interface_libraries"], + requires_equal = "//cc/toolchains/variables:generate_interface_library", + requires_equal_value = "yes", +) + +cc_nested_args( + name = "build_interface_libraries_check_windows", + nested = [":windows_build_interface_libraries"], + requires_equal = "//cc/toolchains/variables:generate_interface_library", + requires_equal_value = "yes", +) + +cc_args_for_compiler( + name = "build_interface_libraries_generic", + actions = ["//cc/toolchains/actions:dynamic_library_link_actions"], + exclude_compilers = [ + "msvc-cl", + "clang-cl", + ], + nested = [":build_interface_libraries_check_generic"], + requires_not_none = "//cc/toolchains/variables:generate_interface_library", +) + +cc_args_for_compiler( + name = "build_interface_libraries_windows", + actions = ["//cc/toolchains/actions:dynamic_library_link_actions"], + compilers = [ + "msvc-cl", + "clang-cl", + ], + nested = [":build_interface_libraries_check_windows"], + requires_not_none = "//cc/toolchains/variables:generate_interface_library", +) + +cc_nested_args( + name = "windows_build_interface_libraries", + args = ["/IMPLIB:{interface_library_output_path}"], + format = {"interface_library_output_path": "//cc/toolchains/variables:interface_library_output_path"}, + requires_not_none = "//cc/toolchains/variables:interface_library_output_path", +) + +cc_nested_args( + name = "generic_build_interface_libraries", + nested = [ + ":generate_interface_library", + ":interface_library_builder_path", + ":interface_library_input_path", + ":interface_library_output_path", + ], +) + +cc_nested_args( + name = "generate_interface_library", + args = ["{generate_interface_library}"], + format = {"generate_interface_library": "//cc/toolchains/variables:generate_interface_library"}, +) + +cc_nested_args( + name = "interface_library_builder_path", + args = ["{interface_library_builder_path}"], + format = {"interface_library_builder_path": "//cc/toolchains/variables:interface_library_builder_path"}, + requires_not_none = "//cc/toolchains/variables:interface_library_builder_path", +) + +cc_nested_args( + name = "interface_library_input_path", + args = ["{interface_library_input_path}"], + format = {"interface_library_input_path": "//cc/toolchains/variables:interface_library_input_path"}, + requires_not_none = "//cc/toolchains/variables:interface_library_input_path", +) + +cc_nested_args( + name = "interface_library_output_path", + args = ["{interface_library_output_path}"], + format = {"interface_library_output_path": "//cc/toolchains/variables:interface_library_output_path"}, + requires_not_none = "//cc/toolchains/variables:interface_library_output_path", +) + +cc_feature( + name = "dynamic_library_linker_tool_feature", + args = [":dynamic_library_linker_tool_generic"], + overrides = "//cc/toolchains/features/legacy:dynamic_library_linker_tool", +) + +cc_args_for_compiler( + name = "dynamic_library_linker_tool_generic", + actions = ["//cc/toolchains/actions:dynamic_library_link_actions"], + exclude_compilers = [ + "msvc-cl", + "clang-cl", + ], + nested = [":dynamic_library_linker_tool_check"], + requires_not_none = "//cc/toolchains/variables:generate_interface_library", +) + +cc_args( + name = "dynamic_library_linker_tool", + actions = ["//cc/toolchains/actions:dynamic_library_link_actions"], + nested = [":dynamic_library_linker_tool_check"], + requires_not_none = "//cc/toolchains/variables:generate_interface_library", +) + +cc_nested_args( + name = "dynamic_library_linker_tool_check", + args = [" + cppLinkDynamicLibraryToolPath + "], + requires_equal = "//cc/toolchains/variables:generate_interface_library", + requires_equal_value = "yes", +) diff --git a/cc/toolchains/args/preprocessor_defines/BUILD b/cc/toolchains/args/preprocessor_defines/BUILD index d7c3267dd..3c11e9a18 100644 --- a/cc/toolchains/args/preprocessor_defines/BUILD +++ b/cc/toolchains/args/preprocessor_defines/BUILD @@ -1,18 +1,47 @@ -load("//cc/toolchains:args.bzl", "cc_args") load("//cc/toolchains:feature.bzl", "cc_feature") +load("//cc/toolchains/impl:args_for_compiler.bzl", "cc_args_for_compiler") package(default_visibility = ["//visibility:public"]) cc_feature( name = "feature", - args = [":preprocessor_defines"], + args = [ + ":preprocessor_defines", + ":preprocessor_defines_windows", + ], overrides = "//cc/toolchains/features/legacy:preprocessor_defines", ) -cc_args( +cc_args_for_compiler( name = "preprocessor_defines", actions = ["//cc/toolchains/actions:source_compile_actions"], args = ["-D{preprocessor_defines}"], + exclude_compilers = [ + "msvc-cl", + "clang-cl", + ], + format = {"preprocessor_defines": "//cc/toolchains/variables:preprocessor_defines"}, + iterate_over = "//cc/toolchains/variables:preprocessor_defines", +) + +cc_args_for_compiler( + name = "preprocessor_defines_windows", + actions = [ + "//cc/toolchains/actions:assemble", + "//cc/toolchains/actions:preprocess_assemble", + "//cc/toolchains/actions:c_compile", + "//cc/toolchains/actions:linkstamp_compile", + "//cc/toolchains/actions:cpp_compile", + "//cc/toolchains/actions:cpp_header_parsing", + "//cc/toolchains/actions:cpp_module_compile", + "//cc/toolchains/actions:cpp_module_deps_scanning", + "//cc/toolchains/actions:cpp20_module_compile", + ], + args = ["/D{preprocessor_defines}"], + compilers = [ + "msvc-cl", + "clang-cl", + ], format = {"preprocessor_defines": "//cc/toolchains/variables:preprocessor_defines"}, iterate_over = "//cc/toolchains/variables:preprocessor_defines", ) diff --git a/cc/toolchains/args/random_seed/BUILD b/cc/toolchains/args/random_seed/BUILD index 97f56a33f..9ffef3075 100644 --- a/cc/toolchains/args/random_seed/BUILD +++ b/cc/toolchains/args/random_seed/BUILD @@ -1,5 +1,5 @@ -load("//cc/toolchains:args.bzl", "cc_args") load("//cc/toolchains:feature.bzl", "cc_feature") +load("//cc/toolchains/impl:args_for_compiler.bzl", "cc_args_for_compiler") package(default_visibility = ["//visibility:public"]) @@ -9,10 +9,14 @@ cc_feature( overrides = "//cc/toolchains/features/legacy:random_seed", ) -cc_args( +cc_args_for_compiler( name = "random_seed", actions = ["//cc/toolchains/actions:compile_actions"], args = ["-frandom-seed={output_file}"], + exclude_compilers = [ + "msvc-cl", + "clang-cl", + ], format = {"output_file": "//cc/toolchains/variables:output_file"}, requires_not_none = "//cc/toolchains/variables:output_file", ) diff --git a/cc/toolchains/args/runtime_library_search_directories/BUILD b/cc/toolchains/args/runtime_library_search_directories/BUILD index 51b83ea6b..54ef11bdd 100644 --- a/cc/toolchains/args/runtime_library_search_directories/BUILD +++ b/cc/toolchains/args/runtime_library_search_directories/BUILD @@ -1,8 +1,8 @@ -load("//cc/toolchains:args.bzl", "cc_args") load("//cc/toolchains:args_list.bzl", "cc_args_list") load("//cc/toolchains:feature.bzl", "cc_feature") load("//cc/toolchains:feature_constraint.bzl", "cc_feature_constraint") load("//cc/toolchains:nested_args.bzl", "cc_nested_args") +load("//cc/toolchains/impl:args_for_compiler.bzl", "cc_args_for_compiler") package(default_visibility = ["//visibility:private"]) @@ -38,9 +38,13 @@ cc_args_list( visibility = ["//visibility:public"], ) -cc_args( +cc_args_for_compiler( name = "runtime_library_search_directories_static_runtimes_args", actions = ["//cc/toolchains/actions:link_actions"], + exclude_compilers = [ + "msvc-cl", + "clang-cl", + ], nested = [":iterate_over_search_dirs"], requires_any_of = [":static_link_cpp_runtimes_enabled"], requires_not_none = "//cc/toolchains/variables:runtime_library_search_directories", @@ -89,9 +93,13 @@ cc_nested_args( # TODO: b/27153401 - runtime_library_search_directories_args and # search_dir_args are all we need to keep if the workaround is no # longer required. -cc_args( +cc_args_for_compiler( name = "runtime_library_search_directories_args", actions = ["//cc/toolchains/actions:link_actions"], + exclude_compilers = [ + "msvc-cl", + "clang-cl", + ], nested = [":search_dir_args"], # Remove the requires_any_of here if the workaround for b/27153401 is no # longer required. diff --git a/cc/toolchains/args/shared_flag/BUILD b/cc/toolchains/args/shared_flag/BUILD index b01c9eafe..bd318b36a 100644 --- a/cc/toolchains/args/shared_flag/BUILD +++ b/cc/toolchains/args/shared_flag/BUILD @@ -1,18 +1,36 @@ -load("//cc/toolchains:args.bzl", "cc_args") load("//cc/toolchains:feature.bzl", "cc_feature") +load("//cc/toolchains/impl:args_for_compiler.bzl", "cc_args_for_compiler") package(default_visibility = ["//visibility:private"]) cc_feature( name = "feature", - args = [":shared_flag"], + args = [ + ":shared_flag", + ":shared_flag_windows", + ], overrides = "//cc/toolchains/features/legacy:shared_flag", visibility = ["//visibility:public"], ) -cc_args( +cc_args_for_compiler( name = "shared_flag", actions = ["//cc/toolchains/actions:dynamic_library_link_actions"], args = ["-shared"], + exclude_compilers = [ + "msvc-cl", + "clang-cl", + ], + visibility = ["//visibility:public"], +) + +cc_args_for_compiler( + name = "shared_flag_windows", + actions = ["//cc/toolchains/actions:dynamic_library_link_actions"], + args = ["/DLL"], + compilers = [ + "msvc-cl", + "clang-cl", + ], visibility = ["//visibility:public"], ) diff --git a/cc/toolchains/args/strip_debug_symbols/BUILD b/cc/toolchains/args/strip_debug_symbols/BUILD index b94c5b19f..b7aa018d9 100644 --- a/cc/toolchains/args/strip_debug_symbols/BUILD +++ b/cc/toolchains/args/strip_debug_symbols/BUILD @@ -1,5 +1,6 @@ -load("//cc/toolchains:args.bzl", "cc_args") +load("//cc/toolchains:args_list.bzl", "cc_args_list") load("//cc/toolchains:feature.bzl", "cc_feature") +load("//cc/toolchains/impl:args_for_compiler.bzl", "cc_args_for_compiler") package(default_visibility = ["//visibility:public"]) @@ -9,9 +10,20 @@ cc_feature( overrides = "//cc/toolchains/features/legacy:strip_debug_symbols", ) -cc_args( - name = "strip_debug_symbols", +cc_args_for_compiler( + name = "strip_debug_symbols_generic", actions = ["//cc/toolchains/actions:link_actions"], args = ["-Wl,-S"], + exclude_compilers = [ + "msvc-cl", + "clang-cl", + ], requires_not_none = "//cc/toolchains/variables:strip_debug_symbols", ) + +cc_args_list( + name = "strip_debug_symbols", + args = [ + "strip_debug_symbols_generic", + ], +) diff --git a/cc/toolchains/args/strip_flags/BUILD b/cc/toolchains/args/strip_flags/BUILD index 3594a2172..47c8f6300 100644 --- a/cc/toolchains/args/strip_flags/BUILD +++ b/cc/toolchains/args/strip_flags/BUILD @@ -1,6 +1,7 @@ load("//cc/toolchains:args.bzl", "cc_args") load("//cc/toolchains:args_list.bzl", "cc_args_list") load("//cc/toolchains:feature.bzl", "cc_feature") +load("//cc/toolchains/impl:args_for_compiler.bzl", "cc_args_for_compiler") package(default_visibility = ["//visibility:public"]) @@ -38,14 +39,17 @@ cc_args( iterate_over = "//cc/toolchains/variables:stripopts", ) -cc_args( +cc_args_for_compiler( name = "preserve_dates", actions = ["//cc/toolchains/actions:strip"], args = select({ "@platforms//os:macos": [], - "@platforms//os:windows": [], "//conditions:default": ["-p"], }), + exclude_compilers = [ + "msvc-cl", + "clang-cl", + ], ) cc_args( diff --git a/cc/toolchains/args/windows/BUILD b/cc/toolchains/args/windows/BUILD new file mode 100644 index 000000000..baab361fe --- /dev/null +++ b/cc/toolchains/args/windows/BUILD @@ -0,0 +1,757 @@ +load("//cc/toolchains:args.bzl", "cc_args") +load("//cc/toolchains:args_list.bzl", "cc_args_list") +load("//cc/toolchains:feature.bzl", "cc_feature") +load("//cc/toolchains:feature_constraint.bzl", "cc_feature_constraint") +load("//cc/toolchains:mutually_exclusive_category.bzl", "cc_mutually_exclusive_category") +load("//cc/toolchains:nested_args.bzl", "cc_nested_args") +load("//cc/toolchains/impl:args_for_compiler.bzl", "cc_args_for_compiler") + +package(default_visibility = ["//visibility:public"]) + +cc_feature( + name = "def_file_feature", + args = [":def_file_flags"], + feature_name = "def_file", +) + +cc_args_for_compiler( + name = "def_file_flags", + actions = [ + "//cc/toolchains/actions:cpp_link_executable", + "//cc/toolchains/actions:cpp_link_dynamic_library", + "//cc/toolchains/actions:cpp_link_nodeps_dynamic_library", + ], + args = [ + "/DEF:{def_file_path}", + "/ignore:4070", + ], + compilers = [ + "msvc-cl", + "clang-cl", + ], + format = {"def_file_path": "//cc/toolchains/variables:def_file_path"}, + requires_not_none = "//cc/toolchains/variables:def_file_path", +) + +cc_feature( + name = "parse_showincludes_feature", + args = [ + ":parse_showincludes_flags", + ":parse_showincludes_env", + ], + feature_name = "parse_showincludes", +) + +cc_args_for_compiler( + name = "parse_showincludes_flags", + actions = [ + "//cc/toolchains/actions:preprocess_assemble", + "//cc/toolchains/actions:c_compile", + "//cc/toolchains/actions:linkstamp_compile", + "//cc/toolchains/actions:cpp_compile", + "//cc/toolchains/actions:cpp_module_compile", + "//cc/toolchains/actions:cpp_header_parsing", + "//cc/toolchains/actions:cpp_module_deps_scanning", + "//cc/toolchains/actions:cpp20_module_compile", + ], + args = ["/showIncludes"], + compilers = [ + "msvc-cl", + "clang-cl", + ], +) + +cc_args_for_compiler( + name = "parse_showincludes_env", + actions = [ + "//cc/toolchains/actions:preprocess_assemble", + "//cc/toolchains/actions:c_compile", + "//cc/toolchains/actions:linkstamp_compile", + "//cc/toolchains/actions:cpp_compile", + "//cc/toolchains/actions:cpp_module_compile", + "//cc/toolchains/actions:cpp_header_parsing", + ], + compilers = [ + "msvc-cl", + "clang-cl", + ], + env = {"VSLANG": "1033"}, +) + +cc_feature( + name = "default_compile_flags_feature", + args = [":default_compile_flags"], + overrides = "//cc/toolchains/features/legacy:default_compile_flags", +) + +cc_args_for_compiler( + name = "default_compile_flags", + actions = [ + "//cc/toolchains/actions:assemble", + "//cc/toolchains/actions:preprocess_assemble", + "//cc/toolchains/actions:linkstamp_compile", + "//cc/toolchains/actions:c_compile", + "//cc/toolchains/actions:cpp_compile", + "//cc/toolchains/actions:cpp_header_parsing", + "//cc/toolchains/actions:cpp_module_compile", + "//cc/toolchains/actions:cpp_module_codegen", + "//cc/toolchains/actions:cpp_module_deps_scanning", + "//cc/toolchains/actions:cpp20_module_compile", + "//cc/toolchains/actions:cpp20_module_codegen", + "//cc/toolchains/actions:lto_backend", + "//cc/toolchains/actions:clif_match", + ], + args = [ + "/DNOMINMAX", + "/D_CRT_SECURE_NO_DEPRECATE", + "/D_CRT_SECURE_NO_WARNINGS", + # TODO: Add BAZEL_WIN32_WINNT override. + "/D_WIN32_WINNT=0x0601", + "/bigobj", + "/Zm500", + "/EHsc", + "/wd4351", + "/wd4291", + "/wd4250", + "/wd4996", + ], + compilers = [ + "msvc-cl", + "clang-cl", + ], +) + +cc_feature( + name = "default_cpp_std_feature", + args = [":default_cpp_std_flags"], + feature_name = "default_cpp_std", +) + +cc_args_for_compiler( + name = "default_cpp_std_flags", + actions = [ + "//cc/toolchains/actions:cpp_compile", + "//cc/toolchains/actions:linkstamp_compile", + "//cc/toolchains/actions:cpp_header_parsing", + "//cc/toolchains/actions:cpp_module_compile", + "//cc/toolchains/actions:cpp_module_codegen", + "//cc/toolchains/actions:cpp_module_deps_scanning", + "//cc/toolchains/actions:cpp20_module_compile", + "//cc/toolchains/actions:cpp20_module_codegen", + "//cc/toolchains/actions:clif_match", + ], + args = ["/std:c++17"], + compilers = [ + "msvc-cl", + "clang-cl", + ], +) + +cc_feature( + name = "cpp20_module_compile_flags_feature", + args = [ + ":cpp20_module_compile_flags", + ":cpp20_module_output_flags", + ], + feature_name = "cpp20_module_compile_flags", +) + +cc_args_for_compiler( + name = "cpp20_module_compile_flags", + actions = ["//cc/toolchains/actions:cpp20_module_compile"], + args = [ + "/TP", + "/interface", + ], + compilers = [ + "msvc-cl", + "clang-cl", + ], +) + +cc_args_for_compiler( + name = "cpp20_module_output_flags", + actions = ["//cc/toolchains/actions:cpp20_module_compile"], + args = ["/ifcOutput{cpp_module_output_file}"], + compilers = [ + "msvc-cl", + "clang-cl", + ], + format = {"cpp_module_output_file": "//cc/toolchains/variables:cpp_module_output_file"}, + requires_not_none = "//cc/toolchains/variables:cpp_module_output_file", +) + +cc_feature( + name = "determinism_feature", + args = [ + ":determinism_flags", + ":determinism_flags_clang_cl", + ], + feature_name = "determinism", +) + +cc_args_for_compiler( + name = "determinism_flags", + actions = [ + "//cc/toolchains/actions:c_compile", + "//cc/toolchains/actions:cpp_compile", + ], + args = [ + "/wd4117", + "-D__DATE__=\"redacted\"", + "-D__TIMESTAMP__=\"redacted\"", + "-D__TIME__=\"redacted\"", + ], + compilers = [ + "msvc-cl", + "clang-cl", + ], +) + +cc_args_for_compiler( + name = "determinism_flags_clang_cl", + actions = [ + "//cc/toolchains/actions:c_compile", + "//cc/toolchains/actions:cpp_compile", + ], + args = ["-Wno-builtin-macro-redefined"], + compilers = ["clang-cl"], +) + +cc_feature( + name = "nologo_feature", + args = [":nologo_flags"], + feature_name = "nologo", +) + +cc_args_for_compiler( + name = "nologo_flags", + actions = [ + "//cc/toolchains/actions:c_compile", + "//cc/toolchains/actions:linkstamp_compile", + "//cc/toolchains/actions:cpp_compile", + "//cc/toolchains/actions:cpp_module_compile", + "//cc/toolchains/actions:cpp_module_codegen", + "//cc/toolchains/actions:cpp_header_parsing", + "//cc/toolchains/actions:cpp_module_deps_scanning", + "//cc/toolchains/actions:cpp20_module_compile", + "//cc/toolchains/actions:cpp20_module_codegen", + "//cc/toolchains/actions:assemble", + "//cc/toolchains/actions:preprocess_assemble", + "//cc/toolchains/actions:cpp_link_executable", + "//cc/toolchains/actions:cpp_link_dynamic_library", + "//cc/toolchains/actions:cpp_link_nodeps_dynamic_library", + "//cc/toolchains/actions:cpp_link_static_library", + ], + args = ["/nologo"], + compilers = [ + "msvc-cl", + "clang-cl", + ], +) + +cc_feature( + name = "remove_unreferenced_code_feature", + args = [":remove_unreferenced_code_flags"], + feature_name = "remove_unreferenced_code", +) + +cc_args_for_compiler( + name = "remove_unreferenced_code_flags", + actions = [ + "//cc/toolchains/actions:c_compile", + "//cc/toolchains/actions:cpp_compile", + ], + args = ["/Zc:inline"], + compilers = [ + "msvc-cl", + "clang-cl", + ], +) + +cc_feature( + name = "ignore_noisy_warnings_feature", + args = [":ignore_noisy_warnings_flags"], + feature_name = "ignore_noisy_warnings", +) + +cc_args_for_compiler( + name = "ignore_noisy_warnings_flags", + actions = ["//cc/toolchains/actions:cpp_link_static_library"], + args = ["/ignore:4221"], + compilers = [ + "msvc-cl", + "clang-cl", + ], +) + +cc_feature( + name = "symbol_check_feature", + args = [":symbol_check_flags"], + feature_name = "symbol_check", +) + +cc_args_for_compiler( + name = "symbol_check_flags", + actions = ["//cc/toolchains/actions:cpp_link_static_library"], + args = ["/WX:4006"], + compilers = [ + "msvc-cl", + "clang-cl", + ], +) + +cc_feature( + name = "treat_warnings_as_errors_feature", + args = [":treat_warnings_as_errors_flags"], + feature_name = "treat_warnings_as_errors", +) + +cc_args_for_compiler( + name = "treat_warnings_as_errors_flags", + actions = [ + "//cc/toolchains/actions:c_compile", + "//cc/toolchains/actions:cpp_compile", + "//cc/toolchains/actions:cpp_link_executable", + "//cc/toolchains/actions:cpp_link_dynamic_library", + "//cc/toolchains/actions:cpp_link_nodeps_dynamic_library", + ], + args = ["/WX"], + compilers = [ + "msvc-cl", + "clang-cl", + ], +) + +cc_feature( + name = "opt_feature", + args = [ + ":opt_compile_flags", + ], + implies = [ + ":frame_pointer_feature", + ":smaller_binary_feature", + ":disable_assertions_feature", + ], + overrides = "//cc/toolchains/features:opt", +) + +cc_args_for_compiler( + name = "opt_compile_flags", + actions = [ + "//cc/toolchains/actions:c_compile", + "//cc/toolchains/actions:cpp_compile", + ], + args = [ + "/O2", + ], + compilers = [ + "msvc-cl", + "clang-cl", + ], +) + +cc_feature_constraint( + name = "opt_enabled", + all_of = ["//cc/toolchains/features:opt"], +) + +cc_feature( + name = "frame_pointer_feature", + args = [":frame_pointer_flags"], + feature_name = "frame_pointer", +) + +cc_args_for_compiler( + name = "frame_pointer_flags", + actions = [ + "//cc/toolchains/actions:c_compile", + "//cc/toolchains/actions:cpp_compile", + ], + args = ["/Oy-"], + compilers = [ + "msvc-cl", + "clang-cl", + ], + requires_any_of = [":opt_enabled"], +) + +cc_feature( + name = "smaller_binary_feature", + args = [ + ":smaller_binary_compile_flags", + ":smaller_binary_link_flags", + ], + feature_name = "smaller_binary", +) + +cc_args_for_compiler( + name = "smaller_binary_compile_flags", + actions = [ + "//cc/toolchains/actions:c_compile", + "//cc/toolchains/actions:cpp_compile", + ], + args = [ + "/Gy", + "/Gw", + ], + compilers = [ + "msvc-cl", + "clang-cl", + ], + requires_any_of = [":opt_enabled"], +) + +cc_args_for_compiler( + name = "smaller_binary_link_flags", + actions = [ + "//cc/toolchains/actions:cpp_link_executable", + "//cc/toolchains/actions:cpp_link_dynamic_library", + "//cc/toolchains/actions:cpp_link_nodeps_dynamic_library", + ], + args = [ + "/OPT:ICF", + "/OPT:REF", + ], + compilers = [ + "msvc-cl", + "clang-cl", + ], + requires_any_of = [":opt_enabled"], +) + +cc_feature( + name = "disable_assertions_feature", + args = [":disable_assertions_flags"], + feature_name = "disable_assertions", +) + +cc_args_for_compiler( + name = "disable_assertions_flags", + actions = [ + "//cc/toolchains/actions:c_compile", + "//cc/toolchains/actions:cpp_compile", + ], + args = ["/DNDEBUG"], + compilers = [ + "msvc-cl", + "clang-cl", + ], + requires_any_of = [":opt_enabled"], +) + +cc_feature( + name = "dbg_feature", + args = [ + ":dbg_compile_flags", + ":dbg_link_flags", + ], + overrides = "//cc/toolchains/features:dbg", +) + +cc_nested_args( + name = "dbg_link_flag_from_variable", + args = ["{dbg_mode_debug_flag}"], + format = {"dbg_mode_debug_flag": "//cc/toolchains/variables:dbg_mode_debug_flag"}, + requires_not_none = "//cc/toolchains/variables:dbg_mode_debug_flag", +) + +cc_nested_args( + name = "dbg_link_flag_default", + args = ["/DEBUG"], + requires_none = "//cc/toolchains/variables:dbg_mode_debug_flag", +) + +cc_nested_args( + name = "dbg_incremental_no", + args = ["/INCREMENTAL:NO"], +) + +cc_args_for_compiler( + name = "dbg_compile_flags", + actions = [ + "//cc/toolchains/actions:c_compile", + "//cc/toolchains/actions:cpp_compile", + ], + args = [ + "/Od", + "/Z7", + ], + compilers = [ + "msvc-cl", + "clang-cl", + ], +) + +cc_args_for_compiler( + name = "dbg_link_flags", + actions = [ + "//cc/toolchains/actions:cpp_link_executable", + "//cc/toolchains/actions:cpp_link_dynamic_library", + "//cc/toolchains/actions:cpp_link_nodeps_dynamic_library", + ], + compilers = [ + "msvc-cl", + "clang-cl", + ], + nested = [ + ":dbg_link_flag_from_variable", + ":dbg_link_flag_default", + ":dbg_incremental_no", + ], +) + +cc_feature( + name = "fastbuild_feature", + args = [ + ":fastbuild_compile_flags", + ":fastbuild_link_flags", + ], + overrides = "//cc/toolchains/features:fastbuild", +) + +cc_nested_args( + name = "fastbuild_link_flag_from_variable", + args = ["{fastbuild_mode_debug_flag}"], + format = {"fastbuild_mode_debug_flag": "//cc/toolchains/variables:fastbuild_mode_debug_flag"}, + requires_not_none = "//cc/toolchains/variables:fastbuild_mode_debug_flag", +) + +cc_nested_args( + name = "fastbuild_link_flag_default", + args = ["/DEBUG"], + requires_none = "//cc/toolchains/variables:fastbuild_mode_debug_flag", +) + +cc_nested_args( + name = "fastbuild_incremental_no", + args = ["/INCREMENTAL:NO"], +) + +cc_args_for_compiler( + name = "fastbuild_compile_flags", + actions = [ + "//cc/toolchains/actions:c_compile", + "//cc/toolchains/actions:cpp_compile", + ], + args = [ + "/Od", + "/Z7", + ], + compilers = [ + "msvc-cl", + "clang-cl", + ], +) + +cc_args_for_compiler( + name = "fastbuild_link_flags", + actions = [ + "//cc/toolchains/actions:cpp_link_executable", + "//cc/toolchains/actions:cpp_link_dynamic_library", + "//cc/toolchains/actions:cpp_link_nodeps_dynamic_library", + ], + compilers = [ + "msvc-cl", + "clang-cl", + ], + nested = [ + ":fastbuild_link_flag_from_variable", + ":fastbuild_link_flag_default", + ":fastbuild_incremental_no", + ], +) + +cc_mutually_exclusive_category( + name = "msvcrt_link_mode", +) + +cc_feature( + name = "dynamic_link_msvcrt_feature", + args = [":dynamic_link_msvcrt_flags"], + feature_name = "dynamic_link_msvcrt", +) + +cc_feature( + name = "static_link_msvcrt_feature", + args = [":static_link_msvcrt_flags"], + feature_name = "static_link_msvcrt", +) + +cc_feature_constraint( + name = "msvcrt_dbg", + all_of = ["//cc/toolchains/features:dbg"], +) + +cc_feature_constraint( + name = "msvcrt_no_dbg", + none_of = ["//cc/toolchains/features:dbg"], +) + +cc_feature_constraint( + name = "msvcrt_no_static_link", + none_of = [":static_link_msvcrt_feature"], +) + +cc_feature_constraint( + name = "msvcrt_dbg_no_static_link", + all_of = [":msvcrt_dbg"], + none_of = [":static_link_msvcrt_feature"], +) + +cc_feature_constraint( + name = "msvcrt_no_dbg_no_static_link", + all_of = [":msvcrt_no_dbg"], + none_of = [":static_link_msvcrt_feature"], +) + +cc_args_list( + name = "dynamic_link_msvcrt_flags", + args = [ + ":dynamic_link_msvcrt_compile_flags", + ":dynamic_link_msvcrt_compile_flags_dbg", + ":dynamic_link_msvcrt_link_flags", + ":dynamic_link_msvcrt_link_flags_dbg", + ], +) + +cc_args_list( + name = "static_link_msvcrt_flags", + args = [ + ":static_link_msvcrt_compile_flags", + ":static_link_msvcrt_compile_flags_dbg", + ":static_link_msvcrt_link_flags", + ":static_link_msvcrt_link_flags_dbg", + ], +) + +cc_args_for_compiler( + name = "dynamic_link_msvcrt_compile_flags", + actions = [ + "//cc/toolchains/actions:c_compile", + "//cc/toolchains/actions:cpp_compile", + ], + args = ["/MD"], + compilers = [ + "msvc-cl", + "clang-cl", + ], + requires_any_of = [":msvcrt_no_dbg_no_static_link"], +) + +cc_args_for_compiler( + name = "dynamic_link_msvcrt_compile_flags_dbg", + actions = [ + "//cc/toolchains/actions:c_compile", + "//cc/toolchains/actions:cpp_compile", + ], + args = ["/MDd"], + compilers = [ + "msvc-cl", + "clang-cl", + ], + requires_any_of = [":msvcrt_dbg_no_static_link"], +) + +cc_args_for_compiler( + name = "dynamic_link_msvcrt_link_flags", + actions = ["//cc/toolchains/actions:link_actions"], + args = [ + "/DEFAULTLIB:msvcrt.lib", + ], + compilers = [ + "msvc-cl", + "clang-cl", + ], + requires_any_of = [":msvcrt_no_dbg_no_static_link"], +) + +cc_args_for_compiler( + name = "dynamic_link_msvcrt_link_flags_dbg", + actions = ["//cc/toolchains/actions:link_actions"], + args = [ + "/DEFAULTLIB:msvcrtd.lib", + ], + compilers = [ + "msvc-cl", + "clang-cl", + ], + requires_any_of = [":msvcrt_dbg_no_static_link"], +) + +cc_args_for_compiler( + name = "static_link_msvcrt_compile_flags", + actions = [ + "//cc/toolchains/actions:c_compile", + "//cc/toolchains/actions:cpp_compile", + ], + args = ["/MT"], + compilers = [ + "msvc-cl", + "clang-cl", + ], + requires_any_of = [":msvcrt_no_dbg"], +) + +cc_args_for_compiler( + name = "static_link_msvcrt_compile_flags_dbg", + actions = [ + "//cc/toolchains/actions:c_compile", + "//cc/toolchains/actions:cpp_compile", + ], + args = ["/MTd"], + compilers = [ + "msvc-cl", + "clang-cl", + ], + requires_any_of = [":msvcrt_dbg"], +) + +cc_args_for_compiler( + name = "static_link_msvcrt_link_flags", + actions = ["//cc/toolchains/actions:link_actions"], + args = [ + "/DEFAULTLIB:libcmt.lib", + ], + compilers = [ + "msvc-cl", + "clang-cl", + ], + requires_any_of = [":msvcrt_no_dbg"], +) + +cc_args_for_compiler( + name = "static_link_msvcrt_link_flags_dbg", + actions = ["//cc/toolchains/actions:link_actions"], + args = [ + "/DEFAULTLIB:libcmtd.lib", + ], + compilers = [ + "msvc-cl", + "clang-cl", + ], + requires_any_of = [":msvcrt_dbg"], +) + +cc_feature( + name = "windows_machine_flags_feature", + args = [":windows_machine_flags"], + feature_name = "windows_machine_flags", + visibility = ["//visibility:public"], +) + +cc_args( + name = "windows_machine_flags", + actions = [ + "//cc/toolchains/actions:ar_actions", + "//cc/toolchains/actions:cpp_link_executable", + "//cc/toolchains/actions:cpp_link_dynamic_library", + "//cc/toolchains/actions:cpp_link_nodeps_dynamic_library", + ], + args = select({ + "//cc/toolchains/impl:windows_x86_64": ["/MACHINE:X64"], + "//cc/toolchains/impl:windows_x86_32": ["/MACHINE:X86"], + "//cc/toolchains/impl:windows_aarch64": ["/MACHINE:ARM64"], + "//conditions:default": [], + }), +) diff --git a/cc/toolchains/capabilities/BUILD b/cc/toolchains/capabilities/BUILD index 8c55804c6..bbae01a8b 100644 --- a/cc/toolchains/capabilities/BUILD +++ b/cc/toolchains/capabilities/BUILD @@ -17,3 +17,15 @@ cc_tool_capability( cc_tool_capability( name = "supports_pic", ) + +cc_tool_capability( + name = "has_configured_linker_path", +) + +cc_tool_capability( + name = "targets_windows", +) + +cc_tool_capability( + name = "copy_dynamic_libraries_to_binary", +) diff --git a/cc/toolchains/cc_toolchain_info.bzl b/cc/toolchains/cc_toolchain_info.bzl index 38148b463..e44b9375a 100644 --- a/cc/toolchains/cc_toolchain_info.bzl +++ b/cc/toolchains/cc_toolchain_info.bzl @@ -229,6 +229,7 @@ ToolchainConfigInfo = provider( "features": "(Sequence[FeatureInfo]) The features available for this toolchain", "enabled_features": "(Sequence[FeatureInfo]) The features That are enabled by default for this toolchain", "tool_map": "(ToolConfigInfo) A provider mapping toolchain action types to tools.", + "compiler_feature": "(FeatureInfo) The feature indicating the type of compiler", "args": "(Sequence[ArgsInfo]) A list of arguments to be unconditionally applied to the toolchain.", "artifact_name_patterns": "Sequence[ArtifactNamePatternInfo] A artifact name patterns for this toolchain", "make_variables": "Sequence[MakeVariableInfo] Make variable substitutions for this toolchain", diff --git a/cc/toolchains/compiler/BUILD b/cc/toolchains/compiler/BUILD new file mode 100644 index 000000000..c7a97eae0 --- /dev/null +++ b/cc/toolchains/compiler/BUILD @@ -0,0 +1,33 @@ +load("//cc/toolchains:feature.bzl", "cc_feature") + +package(default_visibility = ["//visibility:public"]) + +cc_feature( + name = "clang", + feature_name = "compiler_clang", +) + +cc_feature( + name = "clang-cl", + feature_name = "compiler_clang_cl", +) + +cc_feature( + name = "gcc", + feature_name = "compiler_gcc", +) + +cc_feature( + name = "mingw-gcc", + feature_name = "compiler_mingw_gcc", +) + +cc_feature( + name = "msvc-cl", + feature_name = "compiler_msvc_cl", +) + +cc_feature( + name = "emscripten", + feature_name = "compiler_emscripten", +) diff --git a/cc/toolchains/features/legacy/BUILD b/cc/toolchains/features/legacy/BUILD index 7bfe592e4..c2d3cb284 100644 --- a/cc/toolchains/features/legacy/BUILD +++ b/cc/toolchains/features/legacy/BUILD @@ -101,6 +101,18 @@ cc_external_feature( overridable = True, ) +cc_external_feature( + name = "windows_export_all_symbols", + feature_name = "windows_export_all_symbols", + overridable = True, +) + +cc_external_feature( + name = "no_windows_export_all_symbols", + feature_name = "no_windows_export_all_symbols", + overridable = True, +) + cc_external_feature( name = "linkstamps", feature_name = "linkstamps", diff --git a/cc/toolchains/impl/BUILD b/cc/toolchains/impl/BUILD index 7ee93fdbe..c1f057a6c 100644 --- a/cc/toolchains/impl/BUILD +++ b/cc/toolchains/impl/BUILD @@ -72,3 +72,12 @@ config_setting( ], visibility = ["//visibility:public"], ) + +config_setting( + name = "windows_aarch64", + constraint_values = [ + "@platforms//os:windows", + "@platforms//cpu:aarch64", + ], + visibility = ["//visibility:public"], +) diff --git a/cc/toolchains/impl/args_for_compiler.bzl b/cc/toolchains/impl/args_for_compiler.bzl new file mode 100644 index 000000000..cebf06c14 --- /dev/null +++ b/cc/toolchains/impl/args_for_compiler.bzl @@ -0,0 +1,104 @@ +# Copyright 2026 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. +"""Macros for applying compiler constraints to cc_args.""" + +load("//cc/toolchains:args.bzl", "cc_args") +load("//cc/toolchains:feature_constraint.bzl", "cc_feature_constraint") + +visibility([ + "//cc/toolchains/...", + "//tests/rule_based_toolchain/...", +]) + +_ALL_COMPILERS = [ + "clang", + "clang-cl", + "gcc", + "mingw-gcc", + "msvc-cl", + "emscripten", +] + +def _compiler_label(name): + return "//cc/toolchains/compiler:{}".format(name) + +def _normalize_compilers(compilers, exclude_compilers, fail): + if compilers and exclude_compilers: + fail("Only one of 'compilers' or 'exclude_compilers' may be set.") + + if compilers: + for name in compilers: + if name not in _ALL_COMPILERS: + fail("Unknown compiler name {} in compilers.".format(name)) + return [_compiler_label(name) for name in compilers] + + if exclude_compilers: + exclude = {name: True for name in exclude_compilers} + for name in exclude: + if name not in _ALL_COMPILERS: + fail("Unknown compiler name {} in exclude_compilers.".format(name)) + return [_compiler_label(name) for name in _ALL_COMPILERS if name not in exclude] + + return [] + +def _merge_requires_any_of(name, base_requires_any_of, compiler_constraints): + if not compiler_constraints: + return list(base_requires_any_of or []) + + if not base_requires_any_of: + return list(compiler_constraints) + + merged = [] + for base_idx, base in enumerate(base_requires_any_of): + for compiler_idx, compiler in enumerate(compiler_constraints): + constraint_name = "{}_compiler_constraint_{}_{}".format( + name, + base_idx, + compiler_idx, + ) + cc_feature_constraint( + name = constraint_name, + all_of = [base, compiler], + ) + merged.append(":{}".format(constraint_name)) + + return merged + +def cc_args_for_compiler( + *, + name, + compilers = None, + exclude_compilers = None, + requires_any_of = None, + args_rule = cc_args, + **kwargs): + """cc_args wrapper that limits args to specific compiler(s). + + Use either `compiler` or `exclude_compiler` + """ + compiler_constraints = _normalize_compilers( + compilers, + exclude_compilers, + fail, + ) + merged_requires = _merge_requires_any_of( + name, + requires_any_of or [], + compiler_constraints, + ) + args_rule( + name = name, + requires_any_of = merged_requires, + **kwargs + ) diff --git a/cc/toolchains/impl/legacy_converter.bzl b/cc/toolchains/impl/legacy_converter.bzl index 5e8a14481..62d06a3d4 100644 --- a/cc/toolchains/impl/legacy_converter.bzl +++ b/cc/toolchains/impl/legacy_converter.bzl @@ -157,7 +157,7 @@ def _convert_tool_map(tool_map, args_by_action): caps[cap] = None cap_features = [ - legacy_feature(name = cap.feature.name, enabled = False) + legacy_feature(name = cap.feature.name, enabled = True) for cap in caps ] return action_configs, cap_features diff --git a/cc/toolchains/impl/toolchain_config.bzl b/cc/toolchains/impl/toolchain_config.bzl index 1f6efefe5..a0f935076 100644 --- a/cc/toolchains/impl/toolchain_config.bzl +++ b/cc/toolchains/impl/toolchain_config.bzl @@ -50,18 +50,27 @@ cc_legacy_file_group = rule( }, ) +def _compiler_feature(ctx): + features = { + t.label.name: t + for t in ctx.attr._compiler_known_features + } + return features.get(ctx.attr.compiler, features.get("gcc")) + def _cc_toolchain_config_impl(ctx): if ctx.attr.features: fail("Features is a reserved attribute in bazel. Did you mean 'known_features' or 'enabled_features'?") + compiler_feature = _compiler_feature(ctx) toolchain_config = toolchain_config_info( label = ctx.label, - known_features = ctx.attr.known_features + [ctx.attr._builtin_features], - enabled_features = ctx.attr.enabled_features, + known_features = ctx.attr.known_features + [ctx.attr._builtin_features] + ctx.attr._compiler_known_features, + enabled_features = ctx.attr.enabled_features + [compiler_feature], tool_map = ctx.attr.tool_map, args = ctx.attr.args, artifact_name_patterns = ctx.attr.artifact_name_patterns, make_variables = ctx.attr.make_variables, + compiler_feature = compiler_feature, ) legacy = convert_toolchain(toolchain_config) @@ -112,6 +121,14 @@ cc_toolchain_config = rule( "artifact_name_patterns": attr.label_list(providers = [ArtifactNamePatternInfo]), "make_variables": attr.label_list(providers = [MakeVariableInfo]), "_builtin_features": attr.label(default = "//cc/toolchains/features:all_builtin_features"), + "_compiler_known_features": attr.label_list(default = [ + "//cc/toolchains/compiler:clang", + "//cc/toolchains/compiler:clang-cl", + "//cc/toolchains/compiler:gcc", + "//cc/toolchains/compiler:mingw-gcc", + "//cc/toolchains/compiler:msvc-cl", + "//cc/toolchains/compiler:emscripten", + ]), }, provides = [ToolchainConfigInfo], ) diff --git a/cc/toolchains/impl/toolchain_config_info.bzl b/cc/toolchains/impl/toolchain_config_info.bzl index a0c3cbac0..4f2a35dd0 100644 --- a/cc/toolchains/impl/toolchain_config_info.bzl +++ b/cc/toolchains/impl/toolchain_config_info.bzl @@ -13,7 +13,7 @@ # limitations under the License. """Helper functions to create and validate a ToolchainConfigInfo.""" -load("//cc/toolchains:cc_toolchain_info.bzl", "ArtifactNamePatternInfo", "MakeVariableInfo", "ToolConfigInfo", "ToolchainConfigInfo") +load("//cc/toolchains:cc_toolchain_info.bzl", "ArtifactNamePatternInfo", "FeatureInfo", "MakeVariableInfo", "ToolConfigInfo", "ToolchainConfigInfo") load(":args_utils.bzl", "get_action_type") load(":collect.bzl", "collect_args_lists", "collect_features") @@ -70,7 +70,6 @@ def _get_known_features(features, capability_features, fail): rhs = other.label, )) feature_names[ft.name] = ft - return {_feature_key(feature): None for feature in features} def _can_theoretically_be_enabled(requirement, known_features): @@ -162,7 +161,7 @@ def _collect_make_variables(targets, fail): return make_variables.values() -def toolchain_config_info(label, known_features = [], enabled_features = [], args = [], artifact_name_patterns = [], make_variables = [], tool_map = None, fail = fail): +def toolchain_config_info(label, known_features = [], enabled_features = [], args = [], artifact_name_patterns = [], make_variables = [], tool_map = None, compiler_feature = None, fail = fail): """Generates and validates a ToolchainConfigInfo from lists of labels. Args: @@ -174,6 +173,7 @@ def toolchain_config_info(label, known_features = [], enabled_features = [], arg artifact_name_patterns: (List[Target]) A list of targets providing ArtifactNamePatternInfo. make_variables: (List[Target]) A list of targets providing MakeVariableInfo. tool_map: (Target) A target providing ToolMapInfo. + compiler_feature: (Target) A target providing FeatureInfo. fail: A fail function. Use only during tests. Returns: A validated ToolchainConfigInfo @@ -222,6 +222,7 @@ def toolchain_config_info(label, known_features = [], enabled_features = [], arg allowlist_absolute_include_directories = allowlist_absolute_include_directories, artifact_name_patterns = _collect_artifact_name_patterns(artifact_name_patterns, fail), make_variables = _collect_make_variables(make_variables, fail), + compiler_feature = compiler_feature[FeatureInfo], ) _validate_toolchain(toolchain_config, fail = fail) return toolchain_config diff --git a/cc/toolchains/variables/BUILD b/cc/toolchains/variables/BUILD index 2e9c0a358..ccb02bdb2 100644 --- a/cc/toolchains/variables/BUILD +++ b/cc/toolchains/variables/BUILD @@ -23,6 +23,12 @@ cc_variable( type = types.option(types.file), ) +cc_variable( + name = "dbg_mode_debug_flag", + actions = ["//cc/toolchains/actions:link_actions"], + type = types.option(types.string), +) + cc_variable( name = "dependent_module_map_files", actions = ["//cc/toolchains/actions:compile_actions"], @@ -31,7 +37,13 @@ cc_variable( cc_variable( name = "external_include_paths", - actions = ["//cc/toolchains/actions:compile_actions"], + actions = [ + "//cc/toolchains/actions:compile_actions", + "//cc/toolchains/actions:cpp_module_deps_scanning", + "//cc/toolchains/actions:cpp20_module_compile", + "//cc/toolchains/actions:clif_match", + "//cc/toolchains/actions:objcpp_compile", + ], type = types.option(types.list(types.directory)), ) @@ -56,6 +68,12 @@ cc_variable( type = types.option(types.file), ) +cc_variable( + name = "fastbuild_mode_debug_flag", + actions = ["//cc/toolchains/actions:link_actions"], + type = types.option(types.string), +) + cc_variable( name = "force_pic", actions = ["//cc/toolchains/actions:link_actions"], @@ -298,6 +316,15 @@ cc_variable( type = types.option(types.file), ) +cc_variable( + name = "cpp_module_output_file", + actions = [ + "//cc/toolchains/actions:compile_actions", + "//cc/toolchains/actions:cpp20_module_compile", + ], + type = types.option(types.file), +) + cc_variable( name = "module_name", actions = ["//cc/toolchains/actions:compile_actions"], @@ -318,7 +345,12 @@ cc_variable( cc_variable( name = "output_assembly_file", - actions = ["//cc/toolchains/actions:compile_actions"], + actions = [ + "//cc/toolchains/actions:compile_actions", + "//cc/toolchains/actions:cpp_module_deps_scanning", + "//cc/toolchains/actions:cpp20_module_compile", + "//cc/toolchains/actions:cpp20_module_codegen", + ], type = types.option(types.file), ) @@ -341,6 +373,9 @@ cc_variable( name = "output_file", actions = [ "//cc/toolchains/actions:compile_actions", + "//cc/toolchains/actions:cpp_module_deps_scanning", + "//cc/toolchains/actions:cpp20_module_compile", + "//cc/toolchains/actions:cpp20_module_codegen", "//cc/toolchains/actions:strip", ], type = types.option(types.file), @@ -348,7 +383,12 @@ cc_variable( cc_variable( name = "output_preprocess_file", - actions = ["//cc/toolchains/actions:compile_actions"], + actions = [ + "//cc/toolchains/actions:compile_actions", + "//cc/toolchains/actions:cpp_module_deps_scanning", + "//cc/toolchains/actions:cpp20_module_compile", + "//cc/toolchains/actions:cpp20_module_codegen", + ], type = types.option(types.file), ) @@ -366,7 +406,21 @@ cc_variable( cc_variable( name = "preprocessor_defines", - actions = ["//cc/toolchains/actions:source_compile_actions"], + actions = [ + "//cc/toolchains/actions:assemble", + "//cc/toolchains/actions:preprocess_assemble", + "//cc/toolchains/actions:c_compile", + "//cc/toolchains/actions:linkstamp_compile", + "//cc/toolchains/actions:cpp_compile", + "//cc/toolchains/actions:cpp_header_parsing", + "//cc/toolchains/actions:cpp_module_compile", + "//cc/toolchains/actions:cpp_module_codegen", + "//cc/toolchains/actions:cpp_module_deps_scanning", + "//cc/toolchains/actions:cpp20_module_compile", + "//cc/toolchains/actions:clif_match", + "//cc/toolchains/actions:objc_compile", + "//cc/toolchains/actions:objcpp_compile", + ], type = types.list(types.string), ) @@ -402,7 +456,12 @@ cc_variable( cc_variable( name = "source_file", - actions = ["//cc/toolchains/actions:compile_actions"], + actions = [ + "//cc/toolchains/actions:compile_actions", + "//cc/toolchains/actions:cpp_module_deps_scanning", + "//cc/toolchains/actions:cpp20_module_compile", + "//cc/toolchains/actions:cpp20_module_codegen", + ], type = types.option(types.file), ) @@ -496,12 +555,15 @@ cc_variable( cc_builtin_variables( name = "variables", srcs = [ + ":cpp_module_output_file", ":cs_fdo_instrument_path", + ":dbg_mode_debug_flag", ":def_file_path", ":dependency_file", ":dependent_module_map_files", ":dsym_path", ":external_include_paths", + ":fastbuild_mode_debug_flag", ":fdo_instrument_path", ":fdo_prefetch_hints_path", ":fdo_profile_path", diff --git a/examples/rule_based_toolchain/.bazelrc b/examples/rule_based_toolchain/.bazelrc index 12496e78e..150b8c396 100644 --- a/examples/rule_based_toolchain/.bazelrc +++ b/examples/rule_based_toolchain/.bazelrc @@ -5,4 +5,10 @@ build --repo_env=BAZEL_DO_NOT_DETECT_CPP_TOOLCHAIN=1 build --enable_platform_specific_config build:macos --platforms=//:linux_x86_64 +build:linux --deleted_packages=toolchains/clang_cl,toolchains/clang_cl/tools,toolchains/msvc,toolchains/msvc/tools +build:macos --deleted_packages=toolchains/clang_cl,toolchains/clang_cl/tools,toolchains/msvc,toolchains/msvc/tools,toolchains/gcc,toolchains/gcc/tools,toolchains/gcc/args +build:windows --deleted_packages=toolchains/clang,toolchains/clang/tools,toolchains/clang/args,toolchains/gcc,toolchains/gcc/tools,toolchains/gcc/args + build:gcc --//toolchains:compiler_kind=gcc +build:msvc-cl --//toolchains:compiler_kind=msvc-cl +build:clang-cl --//toolchains:compiler_kind=clang-cl diff --git a/examples/rule_based_toolchain/MODULE.bazel b/examples/rule_based_toolchain/MODULE.bazel index cdd551733..48d7f0292 100644 --- a/examples/rule_based_toolchain/MODULE.bazel +++ b/examples/rule_based_toolchain/MODULE.bazel @@ -46,6 +46,28 @@ http_archive( url = "https://chrome-infra-packages.appspot.com/dl/fuchsia/third_party/clang/mac-amd64/+/git_revision:0cfd03ac0d3f9713090a581bda07584754c73a49", ) +http_archive( + name = "clang-windows-x86_64", + build_file = "//toolchains/clang_cl:clang.BUILD", + type = "zip", + url = "https://chrome-infra-packages.appspot.com/dl/fuchsia/third_party/clang/windows-amd64/+/git_revision%3A0cfd03ac0d3f9713090a581bda07584754c73a49", +) + +http_archive( + name = "clang-windows-aarch64", + build_file = "//toolchains/clang_cl:clang.BUILD", + type = "zip", + url = "https://chrome-infra-packages.appspot.com/dl/fuchsia/third_party/clang/windows-arm64/+/git_revision%3A0cfd03ac0d3f9713090a581bda07584754c73a49", +) + +windows_msvc = use_repo_rule("//toolchains/windows:msvc_repo.bzl", "windows_msvc") + +windows_msvc( + name = "windows_msvc", + clang_resource_header_aarch64 = "@clang-windows-aarch64//:lib/clang/19/include/stddef.h", + clang_resource_header_x86_64 = "@clang-windows-x86_64//:lib/clang/19/include/stddef.h", +) + http_archive( name = "linux_sysroot", build_file = "//toolchains/clang:linux_sysroot.BUILD", diff --git a/examples/rule_based_toolchain/README.md b/examples/rule_based_toolchain/README.md index 9d03fff3e..43ae2396f 100644 --- a/examples/rule_based_toolchain/README.md +++ b/examples/rule_based_toolchain/README.md @@ -1,6 +1,6 @@ # Rule-based toolchains -This example showcases a fully working rule-based toolchain for Linux. This also -serves as an integration test to ensure rule-based toolchains continue to work +This example showcases a fully working rule-based toolchain for Linux and Windows. +This also serves as an integration test to ensure rule-based toolchains continue to work as intended. The complete toolchain configuration lives [here](https://github.com/bazelbuild/rules_cc/tree/main/examples/rule_based_toolchain/toolchains). @@ -12,10 +12,15 @@ with the following command: $ bazel test //... ``` -This example currently only supports Linux. - By default, it will build with `clang`. To use `gcc`, try the following command: ``` $ bazel test --config=gcc //... ``` + +On Windows, you can use MSVC's `cl.exe` or `clang-cl.exe`: + +``` +$ bazel test --config=msvc-cl //... +$ bazel test --config=clang-cl //... +``` diff --git a/examples/rule_based_toolchain/asm_answer/BUILD.bazel b/examples/rule_based_toolchain/asm_answer/BUILD.bazel index 7c47fe233..ed3045047 100644 --- a/examples/rule_based_toolchain/asm_answer/BUILD.bazel +++ b/examples/rule_based_toolchain/asm_answer/BUILD.bazel @@ -18,7 +18,11 @@ licenses(["notice"]) cc_library( name = "asm_answer", - srcs = ["asm_answer.S"], + srcs = select({ + "//constraint:msvc_cl_windows_x86_64": ["asm_answer_win.asm"], + "//constraint:msvc_cl_windows_aarch64": ["asm_answer_win.asm"], + "//conditions:default": ["asm_answer.S"], + }), hdrs = ["public/asm_answer.h"], strip_include_prefix = "public", visibility = ["//visibility:public"], diff --git a/examples/rule_based_toolchain/asm_answer/asm_answer_win.asm b/examples/rule_based_toolchain/asm_answer/asm_answer_win.asm new file mode 100644 index 000000000..7d4ad4b29 --- /dev/null +++ b/examples/rule_based_toolchain/asm_answer/asm_answer_win.asm @@ -0,0 +1,21 @@ +; Copyright 2026 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. + +OPTION casemap:none + +.data +PUBLIC asm_answer +asm_answer DWORD 0FE4B67C4h + +END diff --git a/examples/rule_based_toolchain/constraint/BUILD.bazel b/examples/rule_based_toolchain/constraint/BUILD.bazel index ae342c5c2..2450f7ced 100644 --- a/examples/rule_based_toolchain/constraint/BUILD.bazel +++ b/examples/rule_based_toolchain/constraint/BUILD.bazel @@ -73,3 +73,45 @@ config_setting( name = "gcc", flag_values = {"//toolchains:compiler_kind": "gcc"}, ) + +config_setting( + name = "msvc-cl", + flag_values = {"//toolchains:compiler_kind": "msvc-cl"}, +) + +config_setting( + name = "clang-cl", + flag_values = {"//toolchains:compiler_kind": "clang-cl"}, +) + +selects.config_setting_group( + name = "msvc_cl_windows_x86_64", + match_all = [ + ":windows_x86_64", + ":msvc-cl", + ], +) + +selects.config_setting_group( + name = "msvc_cl_windows_aarch64", + match_all = [ + ":windows_aarch64", + ":msvc-cl", + ], +) + +selects.config_setting_group( + name = "clang_cl_windows_x86_64", + match_all = [ + ":windows_x86_64", + ":clang-cl", + ], +) + +selects.config_setting_group( + name = "clang_cl_windows_aarch64", + match_all = [ + ":windows_aarch64", + ":clang-cl", + ], +) diff --git a/examples/rule_based_toolchain/dynamic_answer/BUILD.bazel b/examples/rule_based_toolchain/dynamic_answer/BUILD.bazel index 3085cdfb4..3e84be054 100644 --- a/examples/rule_based_toolchain/dynamic_answer/BUILD.bazel +++ b/examples/rule_based_toolchain/dynamic_answer/BUILD.bazel @@ -12,6 +12,7 @@ # See the License for the specific language governing permissions and # limitations under the License. +load("@rules_cc//cc:cc_import.bzl", "cc_import") load("@rules_cc//cc:cc_library.bzl", "cc_library") load("@rules_cc//cc:cc_shared_library.bzl", "cc_shared_library") @@ -34,13 +35,36 @@ cc_library( cc_shared_library( name = "shared_library", visibility = ["//visibility:private"], + win_def_file = "dynamic_answer.def", deps = [":answer"], ) +filegroup( + name = "shared_library_interface", + srcs = [":shared_library"], + output_group = "interface_library", + visibility = ["//visibility:private"], +) + +cc_import( + name = "dynamic_answer_import", + interface_library = ":shared_library_interface", + shared_library = ":shared_library", + visibility = ["//visibility:private"], +) + # Forces linkage as a shared library. cc_library( name = "dynamic_answer", - srcs = [":shared_library"], + srcs = select({ + "//constraint:windows_x86_64": [], + "//constraint:windows_aarch64": [], + "//conditions:default": [":shared_library"], + }), visibility = ["//visibility:public"], - deps = [":headers"], + deps = [":headers"] + select({ + "//constraint:windows_x86_64": [":dynamic_answer_import"], + "//constraint:windows_aarch64": [":dynamic_answer_import"], + "//conditions:default": [], + }), ) diff --git a/examples/rule_based_toolchain/dynamic_answer/dynamic_answer.def b/examples/rule_based_toolchain/dynamic_answer/dynamic_answer.def new file mode 100644 index 000000000..09aed3f26 --- /dev/null +++ b/examples/rule_based_toolchain/dynamic_answer/dynamic_answer.def @@ -0,0 +1,3 @@ +LIBRARY shared_library +EXPORTS + dynamic_answer diff --git a/examples/rule_based_toolchain/toolchains/BUILD.bazel b/examples/rule_based_toolchain/toolchains/BUILD.bazel index 2ab49c216..4ce5eb7ec 100644 --- a/examples/rule_based_toolchain/toolchains/BUILD.bazel +++ b/examples/rule_based_toolchain/toolchains/BUILD.bazel @@ -19,6 +19,10 @@ alias( actual = select({ "//constraint:clang": "//toolchains/clang:host_clang_toolchain", "//constraint:gcc": "//toolchains/gcc:host_gcc_toolchain", + "//constraint:msvc_cl_windows_x86_64": "//toolchains/msvc:host_msvc_cl_toolchain", + "//constraint:msvc_cl_windows_aarch64": "//toolchains/msvc:host_msvc_cl_toolchain", + "//constraint:clang_cl_windows_x86_64": "//toolchains/clang_cl:host_clang_cl_toolchain", + "//constraint:clang_cl_windows_aarch64": "//toolchains/clang_cl:host_clang_cl_toolchain", }), visibility = ["//visibility:public"], ) @@ -29,6 +33,8 @@ string_flag( values = [ "clang", "gcc", + "msvc-cl", + "clang-cl", ], visibility = ["//visibility:public"], ) diff --git a/examples/rule_based_toolchain/toolchains/args/BUILD.bazel b/examples/rule_based_toolchain/toolchains/args/BUILD.bazel index 6e72428b6..62d6b58e3 100644 --- a/examples/rule_based_toolchain/toolchains/args/BUILD.bazel +++ b/examples/rule_based_toolchain/toolchains/args/BUILD.bazel @@ -13,9 +13,21 @@ # limitations under the License. load("@rules_cc//cc/toolchains:args.bzl", "cc_args") +load("@rules_cc//cc/toolchains:args_list.bzl", "cc_args_list") +load("@windows_msvc//:paths.bzl", "INCLUDE_PATHS", "LIB_PATHS", "MSVC_PATH") -cc_args( +cc_args_list( name = "warnings", + args = select({ + "//constraint:windows_x86_64": [":warnings_windows"], + "//constraint:windows_aarch64": [":warnings_windows"], + "//conditions:default": [":warnings_generic"], + }), + visibility = ["//visibility:public"], +) + +cc_args( + name = "warnings_generic", actions = [ "@rules_cc//cc/toolchains/actions:c_compile", "@rules_cc//cc/toolchains/actions:cpp_compile_actions", @@ -26,11 +38,29 @@ cc_args( "-Wextra", "-Wpedantic", ], - visibility = ["//visibility:public"], ) cc_args( + name = "warnings_windows", + actions = [ + "@rules_cc//cc/toolchains/actions:c_compile", + "@rules_cc//cc/toolchains/actions:cpp_compile_actions", + ], + args = [], +) + +cc_args_list( name = "no_absolute_paths_for_builtins", + args = select({ + "//constraint:windows_x86_64": [":no_absolute_paths_for_builtins_windows"], + "//constraint:windows_aarch64": [":no_absolute_paths_for_builtins_windows"], + "//conditions:default": [":no_absolute_paths_for_builtins_generic"], + }), + visibility = ["//visibility:public"], +) + +cc_args( + name = "no_absolute_paths_for_builtins_generic", actions = [ "@rules_cc//cc/toolchains/actions:compile_actions", ], @@ -38,11 +68,76 @@ cc_args( "//constraint:gcc": ["-fno-canonical-system-headers"], "//conditions:default": [], }), +) + +cc_args( + name = "no_absolute_paths_for_builtins_windows", + actions = [ + "@rules_cc//cc/toolchains/actions:compile_actions", + ], + args = [], +) + +cc_args( + name = "windows_builtin_includes", + actions = [ + "@rules_cc//cc/toolchains/actions:compile_actions", + ], + allowlist_absolute_include_directories = INCLUDE_PATHS, + allowlist_include_directories = select({ + "//constraint:windows_x86_64": ["@clang-windows-x86_64//:lib-clang-include"], + "//constraint:windows_aarch64": ["@clang-windows-aarch64//:lib-clang-include"], + "//conditions:default": [], + }), + args = [ + "/I\"{}\"".format(path) + for path in INCLUDE_PATHS + ], + env = { + "INCLUDE": ";".join(INCLUDE_PATHS), + }, + visibility = ["//visibility:public"], +) + +cc_args( + name = "windows_builtin_libs", + actions = [ + "@rules_cc//cc/toolchains/actions:link_actions", + ], + args = [], + env = { + "LIB": ";".join(LIB_PATHS), + }, visibility = ["//visibility:public"], ) cc_args( + name = "windows_msvc_path", + actions = [ + "@rules_cc//cc/toolchains/actions:ar_actions", + "@rules_cc//cc/toolchains/actions:assembly_actions", + "@rules_cc//cc/toolchains/actions:compile_actions", + "@rules_cc//cc/toolchains/actions:link_actions", + ], + args = [], + env = { + "PATH": MSVC_PATH, + }, + visibility = ["//visibility:public"], +) + +cc_args_list( name = "target", + args = select({ + "//constraint:windows_x86_64": [":target_windows"], + "//constraint:windows_aarch64": [":target_windows"], + "//conditions:default": [":target_generic"], + }), + visibility = ["//visibility:public"], +) + +cc_args( + name = "target_generic", actions = [ "@rules_cc//cc/toolchains/actions:compile_actions", "@rules_cc//cc/toolchains/actions:link_actions", @@ -50,6 +145,15 @@ cc_args( args = select({ "//constraint:linux_aarch64": ["--target=aarch64-unknown-linux-gnu"], "//constraint:linux_x86_64": ["--target=x86_64-unknown-linux-gnu"], + "//conditions:default": [], }), - visibility = ["//visibility:public"], +) + +cc_args( + name = "target_windows", + actions = [ + "@rules_cc//cc/toolchains/actions:compile_actions", + "@rules_cc//cc/toolchains/actions:link_actions", + ], + args = [], ) diff --git a/examples/rule_based_toolchain/toolchains/clang_cl/BUILD.bazel b/examples/rule_based_toolchain/toolchains/clang_cl/BUILD.bazel new file mode 100644 index 000000000..e68670b9f --- /dev/null +++ b/examples/rule_based_toolchain/toolchains/clang_cl/BUILD.bazel @@ -0,0 +1,89 @@ +# Copyright 2026 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. + +load("@rules_cc//cc/toolchains:make_variable.bzl", "cc_make_variable") +load("@rules_cc//cc/toolchains:toolchain.bzl", "cc_toolchain") + +licenses(["notice"]) + +cc_make_variable( + name = "example_variable", + value = "-DEXAMPLE=1", + variable_name = "EXAMPLE_SUBSTITUTION", +) + +cc_toolchain( + name = "host_clang_cl", + args = [ + "//toolchains/args:no_absolute_paths_for_builtins", + "//toolchains/args:windows_builtin_includes", + "//toolchains/args:windows_builtin_libs", + "@rules_cc//cc/toolchains/args/windows:windows_machine_flags", + "//toolchains/args:warnings", + ], + artifact_name_patterns = [ + "//toolchains/windows:object_file", + "//toolchains/windows:static_library", + "//toolchains/windows:alwayslink_static_library", + "//toolchains/windows:executable", + "//toolchains/windows:dynamic_library", + "//toolchains/windows:interface_library", + ], + compiler = "clang-cl", + enabled_features = [ + "@rules_cc//cc/toolchains/args:experimental_replace_legacy_action_config_features", + "@rules_cc//cc/toolchains/args/windows:parse_showincludes_feature", + "@rules_cc//cc/toolchains/args/windows:default_cpp_std_feature", + "@rules_cc//cc/toolchains/args/windows:cpp20_module_compile_flags_feature", + "@rules_cc//cc/toolchains/args/windows:determinism_feature", + "@rules_cc//cc/toolchains/args/windows:nologo_feature", + "@rules_cc//cc/toolchains/args/windows:remove_unreferenced_code_feature", + "@rules_cc//cc/toolchains/args/windows:ignore_noisy_warnings_feature", + "@rules_cc//cc/toolchains/features/legacy:windows_export_all_symbols", + ], + known_features = [ + "@rules_cc//cc/toolchains/args:experimental_replace_legacy_action_config_features", + "@rules_cc//cc/toolchains/args/windows:parse_showincludes_feature", + "@rules_cc//cc/toolchains/args/windows:dynamic_link_msvcrt_feature", + "@rules_cc//cc/toolchains/args/windows:static_link_msvcrt_feature", + "@rules_cc//cc/toolchains/args/windows:default_cpp_std_feature", + "@rules_cc//cc/toolchains/args/windows:cpp20_module_compile_flags_feature", + "@rules_cc//cc/toolchains/args/windows:determinism_feature", + "@rules_cc//cc/toolchains/args/windows:nologo_feature", + "@rules_cc//cc/toolchains/args/windows:remove_unreferenced_code_feature", + "@rules_cc//cc/toolchains/args/windows:ignore_noisy_warnings_feature", + "@rules_cc//cc/toolchains/args/windows:treat_warnings_as_errors_feature", + "@rules_cc//cc/toolchains/args/windows:symbol_check_feature", + "@rules_cc//cc/toolchains/args/windows:opt_feature", + "@rules_cc//cc/toolchains/args/windows:frame_pointer_feature", + "@rules_cc//cc/toolchains/args/windows:smaller_binary_feature", + "@rules_cc//cc/toolchains/args/windows:disable_assertions_feature", + "@rules_cc//cc/toolchains/args/windows:dbg_feature", + "@rules_cc//cc/toolchains/args/windows:fastbuild_feature", + "@rules_cc//cc/toolchains/features/legacy:windows_export_all_symbols", + "@rules_cc//cc/toolchains/features/legacy:no_windows_export_all_symbols", + ], + make_variables = [":example_variable"], + tool_map = "//toolchains/clang_cl/tools:all_tools", +) + +toolchain( + name = "host_clang_cl_toolchain", + exec_compatible_with = [ + "@platforms//os:windows", + ], + toolchain = ":host_clang_cl", + toolchain_type = "@bazel_tools//tools/cpp:toolchain_type", + visibility = ["//visibility:public"], +) diff --git a/examples/rule_based_toolchain/toolchains/clang_cl/clang.BUILD b/examples/rule_based_toolchain/toolchains/clang_cl/clang.BUILD new file mode 100644 index 000000000..cf21ce941 --- /dev/null +++ b/examples/rule_based_toolchain/toolchains/clang_cl/clang.BUILD @@ -0,0 +1,73 @@ +# Copyright 2026 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. + +load("@bazel_skylib//rules/directory:directory.bzl", "directory") +load("@bazel_skylib//rules/directory:subdirectory.bzl", "subdirectory") + +package(default_visibility = ["//visibility:public"]) + +licenses(["notice"]) + +exports_files(glob(["bin/**"])) + +directory( + name = "toolchain_root", + srcs = glob([ + "lib/**", + "include/**", + ]), +) + +subdirectory( + name = "include-c++-v1", + parent = ":toolchain_root", + path = "include/c++/v1", +) + +subdirectory( + name = "lib-clang-include", + parent = ":toolchain_root", + path = "lib/clang/19/include", +) + +filegroup( + name = "builtin_headers", + srcs = [ + ":include-c++-v1", + ":lib-clang-include", + ], +) + +filegroup( + name = "linker_builtins", + data = glob([ + "bin/lld*", + "bin/ld*", + "bin/*.dll", + "lib/**/*.a", + "lib/**/*.lib", + "lib/**/*.o", + "lib/**/*.obj", + ]) + [ + ":multicall_support_files", + ], +) + +filegroup( + name = "multicall_support_files", + srcs = glob( + ["bin/llvm"], + allow_empty = True, + ), +) diff --git a/examples/rule_based_toolchain/toolchains/clang_cl/tools/BUILD.bazel b/examples/rule_based_toolchain/toolchains/clang_cl/tools/BUILD.bazel new file mode 100644 index 000000000..0874a8992 --- /dev/null +++ b/examples/rule_based_toolchain/toolchains/clang_cl/tools/BUILD.bazel @@ -0,0 +1,133 @@ +# Copyright 2026 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. + +load("@rules_cc//cc/toolchains:tool.bzl", "cc_tool") +load("@rules_cc//cc/toolchains:tool_map.bzl", "cc_tool_map") + +licenses(["notice"]) + +COMMON_TOOLS = { + "@rules_cc//cc/toolchains/actions:assembly_actions": ":clang-cl", + "@rules_cc//cc/toolchains/actions:c_compile": ":clang-cl", + "@rules_cc//cc/toolchains/actions:cpp_compile_actions": ":clang-cl", + "@rules_cc//cc/toolchains/actions:link_actions": ":lld-link", + "@rules_cc//cc/toolchains/actions:objcopy_embed_data": ":llvm-objcopy", + "@rules_cc//cc/toolchains/actions:strip": ":llvm-strip", +} + +cc_tool_map( + name = "all_tools", + tools = COMMON_TOOLS | { + "@rules_cc//cc/toolchains/actions:ar_actions": ":llvm-lib", + }, + visibility = ["//visibility:public"], +) + +cc_tool( + name = "clang-cl", + src = select({ + "//constraint:windows_x86_64": "@clang-windows-x86_64//:bin/clang-cl.exe", + "//constraint:windows_aarch64": "@clang-windows-aarch64//:bin/clang-cl.exe", + }), + capabilities = [ + "@rules_cc//cc/toolchains/capabilities:targets_windows", + ], + data = [ + ":exec_platform_builtin_headers", + ":exec_platform_multicall_support_files", + ], + tags = ["manual"], +) + +cc_tool( + name = "lld-link", + src = select({ + "//constraint:windows_x86_64": "@clang-windows-x86_64//:bin/lld-link.exe", + "//constraint:windows_aarch64": "@clang-windows-aarch64//:bin/lld-link.exe", + }), + capabilities = [ + "@rules_cc//cc/toolchains/capabilities:targets_windows", + "@rules_cc//cc/toolchains/capabilities:supports_interface_shared_libraries", + "@rules_cc//cc/toolchains/capabilities:has_configured_linker_path", + "@rules_cc//cc/toolchains/capabilities:copy_dynamic_libraries_to_binary", + ], + data = [ + ":exec_platform_linker_builtins", + ":exec_platform_multicall_support_files", + ], + tags = ["manual"], +) + +cc_tool( + name = "llvm-lib", + src = select({ + "//constraint:windows_x86_64": "@clang-windows-x86_64//:bin/llvm-lib.exe", + "//constraint:windows_aarch64": "@clang-windows-aarch64//:bin/llvm-lib.exe", + }), + capabilities = [ + "@rules_cc//cc/toolchains/capabilities:targets_windows", + ], + data = [":exec_platform_multicall_support_files"], + tags = ["manual"], +) + +cc_tool( + name = "llvm-objcopy", + src = select({ + "//constraint:windows_x86_64": "@clang-windows-x86_64//:bin/llvm-objcopy.exe", + "//constraint:windows_aarch64": "@clang-windows-aarch64//:bin/llvm-objcopy.exe", + }), + data = [":exec_platform_multicall_support_files"], + tags = ["manual"], +) + +cc_tool( + name = "llvm-strip", + src = select({ + "//constraint:windows_x86_64": "@clang-windows-x86_64//:bin/llvm-strip.exe", + "//constraint:windows_aarch64": "@clang-windows-aarch64//:bin/llvm-strip.exe", + }), + data = [":exec_platform_multicall_support_files"], + tags = ["manual"], +) + +alias( + name = "exec_platform_builtin_headers", + actual = select({ + "//constraint:windows_x86_64": "@clang-windows-x86_64//:builtin_headers", + "//constraint:windows_aarch64": "@clang-windows-aarch64//:builtin_headers", + }), + tags = ["manual"], + visibility = ["//visibility:private"], +) + +alias( + name = "exec_platform_multicall_support_files", + actual = select({ + "//constraint:windows_x86_64": "@clang-windows-x86_64//:multicall_support_files", + "//constraint:windows_aarch64": "@clang-windows-aarch64//:multicall_support_files", + }), + tags = ["manual"], + visibility = ["//visibility:private"], +) + +alias( + name = "exec_platform_linker_builtins", + actual = select({ + "//constraint:windows_x86_64": "@clang-windows-x86_64//:linker_builtins", + "//constraint:windows_aarch64": "@clang-windows-aarch64//:linker_builtins", + }), + tags = ["manual"], + visibility = ["//visibility:private"], +) diff --git a/examples/rule_based_toolchain/toolchains/msvc/BUILD.bazel b/examples/rule_based_toolchain/toolchains/msvc/BUILD.bazel new file mode 100644 index 000000000..f945c44f3 --- /dev/null +++ b/examples/rule_based_toolchain/toolchains/msvc/BUILD.bazel @@ -0,0 +1,88 @@ +# Copyright 2026 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. + +load("@rules_cc//cc/toolchains:make_variable.bzl", "cc_make_variable") +load("@rules_cc//cc/toolchains:toolchain.bzl", "cc_toolchain") + +licenses(["notice"]) + +cc_make_variable( + name = "example_variable", + value = "-DEXAMPLE=1", + variable_name = "EXAMPLE_SUBSTITUTION", +) + +cc_toolchain( + name = "host_msvc_cl", + args = [ + "//toolchains/args:windows_builtin_includes", + "//toolchains/args:windows_builtin_libs", + "@rules_cc//cc/toolchains/args/windows:windows_machine_flags", + "//toolchains/args:windows_msvc_path", + ], + artifact_name_patterns = [ + "//toolchains/windows:object_file", + "//toolchains/windows:static_library", + "//toolchains/windows:alwayslink_static_library", + "//toolchains/windows:executable", + "//toolchains/windows:dynamic_library", + "//toolchains/windows:interface_library", + ], + compiler = "msvc-cl", + enabled_features = [ + "@rules_cc//cc/toolchains/args:experimental_replace_legacy_action_config_features", + "@rules_cc//cc/toolchains/args/windows:parse_showincludes_feature", + "@rules_cc//cc/toolchains/args/windows:default_cpp_std_feature", + "@rules_cc//cc/toolchains/args/windows:cpp20_module_compile_flags_feature", + "@rules_cc//cc/toolchains/args/windows:determinism_feature", + "@rules_cc//cc/toolchains/args/windows:nologo_feature", + "@rules_cc//cc/toolchains/args/windows:remove_unreferenced_code_feature", + "@rules_cc//cc/toolchains/args/windows:ignore_noisy_warnings_feature", + "@rules_cc//cc/toolchains/features/legacy:windows_export_all_symbols", + ], + known_features = [ + "@rules_cc//cc/toolchains/args:experimental_replace_legacy_action_config_features", + "@rules_cc//cc/toolchains/args/windows:parse_showincludes_feature", + "@rules_cc//cc/toolchains/args/windows:dynamic_link_msvcrt_feature", + "@rules_cc//cc/toolchains/args/windows:static_link_msvcrt_feature", + "@rules_cc//cc/toolchains/args/windows:default_cpp_std_feature", + "@rules_cc//cc/toolchains/args/windows:cpp20_module_compile_flags_feature", + "@rules_cc//cc/toolchains/args/windows:determinism_feature", + "@rules_cc//cc/toolchains/args/windows:nologo_feature", + "@rules_cc//cc/toolchains/args/windows:remove_unreferenced_code_feature", + "@rules_cc//cc/toolchains/args/windows:ignore_noisy_warnings_feature", + "@rules_cc//cc/toolchains/args/windows:treat_warnings_as_errors_feature", + "@rules_cc//cc/toolchains/args/windows:symbol_check_feature", + "@rules_cc//cc/toolchains/args/windows:opt_feature", + "@rules_cc//cc/toolchains/args/windows:frame_pointer_feature", + "@rules_cc//cc/toolchains/args/windows:smaller_binary_feature", + "@rules_cc//cc/toolchains/args/windows:disable_assertions_feature", + "@rules_cc//cc/toolchains/args/windows:dbg_feature", + "@rules_cc//cc/toolchains/args/windows:fastbuild_feature", + "@rules_cc//cc/toolchains/features/legacy:windows_export_all_symbols", + "@rules_cc//cc/toolchains/features/legacy:no_windows_export_all_symbols", + ], + make_variables = [":example_variable"], + tool_map = "//toolchains/msvc/tools:msvc_tools", +) + +toolchain( + name = "host_msvc_cl_toolchain", + exec_compatible_with = [ + "@platforms//os:windows", + ], + toolchain = ":host_msvc_cl", + toolchain_type = "@bazel_tools//tools/cpp:toolchain_type", + visibility = ["//visibility:public"], +) diff --git a/examples/rule_based_toolchain/toolchains/msvc/tools/BUILD.bazel b/examples/rule_based_toolchain/toolchains/msvc/tools/BUILD.bazel new file mode 100644 index 000000000..306871321 --- /dev/null +++ b/examples/rule_based_toolchain/toolchains/msvc/tools/BUILD.bazel @@ -0,0 +1,80 @@ +# Copyright 2026 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. + +load("@rules_cc//cc/toolchains:tool.bzl", "cc_tool") +load("@rules_cc//cc/toolchains:tool_map.bzl", "cc_tool_map") + +licenses(["notice"]) + +COMMON_TOOLS = { + "@rules_cc//cc/toolchains/actions:assembly_actions": ":msvc_ml", + "@rules_cc//cc/toolchains/actions:c_compile": ":msvc_cl", + "@rules_cc//cc/toolchains/actions:cpp_compile_actions": ":msvc_cl", + "@rules_cc//cc/toolchains/actions:link_actions": ":msvc_link", + "@rules_cc//cc/toolchains/actions:objcopy_embed_data": ":msvc_nop", + "@rules_cc//cc/toolchains/actions:strip": ":msvc_nop", +} + +cc_tool_map( + name = "msvc_tools", + tools = COMMON_TOOLS | { + "@rules_cc//cc/toolchains/actions:ar_actions": ":msvc_lib", + }, + visibility = ["//visibility:public"], +) + +cc_tool( + name = "msvc_cl", + src = "@windows_msvc//:cl", + capabilities = [ + "@rules_cc//cc/toolchains/capabilities:targets_windows", + ], + tags = ["manual"], +) + +cc_tool( + name = "msvc_link", + src = "@windows_msvc//:link", + capabilities = [ + "@rules_cc//cc/toolchains/capabilities:targets_windows", + "@rules_cc//cc/toolchains/capabilities:supports_interface_shared_libraries", + "@rules_cc//cc/toolchains/capabilities:has_configured_linker_path", + "@rules_cc//cc/toolchains/capabilities:copy_dynamic_libraries_to_binary", + ], + tags = ["manual"], +) + +cc_tool( + name = "msvc_lib", + src = "@windows_msvc//:lib", + capabilities = [ + "@rules_cc//cc/toolchains/capabilities:targets_windows", + ], + tags = ["manual"], +) + +cc_tool( + name = "msvc_ml", + src = "@windows_msvc//:ml", + capabilities = [ + "@rules_cc//cc/toolchains/capabilities:targets_windows", + ], + tags = ["manual"], +) + +cc_tool( + name = "msvc_nop", + src = ":msvc_nop.bat", + tags = ["manual"], +) diff --git a/examples/rule_based_toolchain/toolchains/msvc/tools/msvc_nop.bat b/examples/rule_based_toolchain/toolchains/msvc/tools/msvc_nop.bat new file mode 100644 index 000000000..c63996048 --- /dev/null +++ b/examples/rule_based_toolchain/toolchains/msvc/tools/msvc_nop.bat @@ -0,0 +1,2 @@ +@echo off +exit /b 0 diff --git a/examples/rule_based_toolchain/toolchains/windows/BUILD.bazel b/examples/rule_based_toolchain/toolchains/windows/BUILD.bazel new file mode 100644 index 000000000..2b66ec671 --- /dev/null +++ b/examples/rule_based_toolchain/toolchains/windows/BUILD.bazel @@ -0,0 +1,59 @@ +# Copyright 2025 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. + +load("@rules_cc//cc/toolchains:artifacts.bzl", "cc_artifact_name_pattern") + +package(default_visibility = ["//visibility:public"]) + +cc_artifact_name_pattern( + name = "object_file", + category = "@rules_cc//cc/toolchains/artifacts:object_file", + extension = ".obj", + prefix = "", +) + +cc_artifact_name_pattern( + name = "static_library", + category = "@rules_cc//cc/toolchains/artifacts:static_library", + extension = ".lib", + prefix = "", +) + +cc_artifact_name_pattern( + name = "alwayslink_static_library", + category = "@rules_cc//cc/toolchains/artifacts:alwayslink_static_library", + extension = ".lo.lib", + prefix = "", +) + +cc_artifact_name_pattern( + name = "executable", + category = "@rules_cc//cc/toolchains/artifacts:executable", + extension = ".exe", + prefix = "", +) + +cc_artifact_name_pattern( + name = "dynamic_library", + category = "@rules_cc//cc/toolchains/artifacts:dynamic_library", + extension = ".dll", + prefix = "", +) + +cc_artifact_name_pattern( + name = "interface_library", + category = "@rules_cc//cc/toolchains/artifacts:interface_library", + extension = ".lib", + prefix = "", +) diff --git a/examples/rule_based_toolchain/toolchains/windows/msvc_repo.bzl b/examples/rule_based_toolchain/toolchains/windows/msvc_repo.bzl new file mode 100644 index 000000000..064977501 --- /dev/null +++ b/examples/rule_based_toolchain/toolchains/windows/msvc_repo.bzl @@ -0,0 +1,269 @@ +"""Repository rule to read MSVC from the host.""" + +def _normalize_windows_path(path): + return path.replace("\\", "/").rstrip("/") + +def _detect_vc_root(repository_ctx): + env = repository_ctx.os.environ + bazel_vc = env.get("BAZEL_VC", "") + if bazel_vc: + return _normalize_windows_path(bazel_vc) + + bazel_vs = env.get("BAZEL_VS", "") + if bazel_vs: + return _normalize_windows_path(bazel_vs + "/VC") + + vcinstall_dir = env.get("VCINSTALLDIR", "") + if vcinstall_dir: + return _normalize_windows_path(vcinstall_dir) + + vsinstall_dir = env.get("VSINSTALLDIR", "") + if vsinstall_dir: + return _normalize_windows_path(vsinstall_dir + "/VC") + + return None + +def _is_vs_2017_or_newer(repository_ctx, vc_root): + return repository_ctx.path(vc_root).get_child("Tools").exists + +def _find_vcvars_bat_script(repository_ctx, vc_root): + if _is_vs_2017_or_newer(repository_ctx, vc_root): + vcvars_script = vc_root + "/Auxiliary/Build/VCVARSALL.BAT" + else: + vcvars_script = vc_root + "/VCVARSALL.BAT" + + if not repository_ctx.path(vcvars_script).exists: + return None + + return vcvars_script + +def _latest_msvc_root(repository_ctx, msvc_root): + msvc_root = _normalize_windows_path(msvc_root) + if repository_ctx.path(msvc_root + "/bin").exists: + return msvc_root + + tools_root = msvc_root + "/Tools/MSVC" + tools_path = repository_ctx.path(tools_root) + if not tools_path.exists: + return None + + versions = [path.basename for path in tools_path.readdir()] + version_list = [] + for version in versions: + parts = version.split(".") + if not all([part.isdigit() for part in parts]): + continue + version_list.append(([int(part) for part in parts], version)) + if not version_list: + return None + + version_list = sorted(version_list) + return tools_root + "/" + version_list[-1][1] + +def _detect_target_arch(repository_ctx): + arch = repository_ctx.os.environ.get("PROCESSOR_ARCHITECTURE", "").lower() + if arch in ["amd64", "x86_64"]: + return struct( + target_arch = "x64", + ml_name = "ml64.exe", + host_order = ["HostX64", "HostX86"], + ) + elif arch in ["x86"]: + return struct( + target_arch = "x86", + ml_name = "ml.exe", + host_order = ["HostX86", "HostX64"], + ) + elif arch in ["arm64", "aarch64"]: + return struct( + target_arch = "arm64", + ml_name = "ml64.exe", + host_order = ["HostX64", "HostX86"], + ) + else: + return struct( + target_arch = "", + ml_name = "ml.exe", + host_order = ["HostX64", "HostX86"], + ) + +def _msvc_bin_dirs(repository_ctx, msvc_root, target_arch, host_order): + if not target_arch: + return [] + + if (repository_ctx.path(msvc_root + "/bin/HostX64").exists or + repository_ctx.path(msvc_root + "/bin/HostX86").exists): + return [msvc_root + "/bin/" + host + "/" + target_arch for host in host_order] + return [] + +def _read_vcvars_env(repository_ctx, arch_info): + vc_root = _detect_vc_root(repository_ctx) + if not vc_root: + fail("Failed to locate VC root for arch '{}'.".format(arch_info.target_arch)) + + msvc_root = _latest_msvc_root(repository_ctx, vc_root) + if not msvc_root: + fail("Failed to locate MSVC root for arch '{}'.".format(arch_info.target_arch)) + + vcvars_script = _find_vcvars_bat_script(repository_ctx, vc_root) + if not vcvars_script: + fail("Failed to locate VCVARSALL.BAT under '{}'.".format(vc_root)) + + if arch_info.target_arch == "x64": + arch_arg = "amd64" + elif arch_info.target_arch == "x86": + arch_arg = "x86" + elif arch_info.target_arch == "arm64": + arch_arg = "amd64_arm64" + else: + arch_arg = "amd64" + + repository_ctx.file( + "get_env.bat", + "@echo off\n" + + "call \"" + vcvars_script.replace("/", "\\") + "\" " + arch_arg + " > NUL\n" + + "echo INCLUDE=%INCLUDE%\n" + + "echo LIB=%LIB%\n" + + "echo PATH=%PATH%\n", + True, + ) + output = repository_ctx.execute(["./get_env.bat"]) + + if output.return_code: + fail("Failed to run VCVARSALL.BAT (exit {}).".format(output.return_code)) + + env = {} + for raw in output.stdout.splitlines(): + if "=" not in raw: + continue + key, value = raw.split("=", 1) + env[key] = value + return struct(env = env, vc_root = vc_root, msvc_root = msvc_root) + +def _write_build(repository_ctx, ml_name, has_bins): + cl_srcs = ["bin/cl.exe"] if has_bins else [] + link_srcs = ["bin/link.exe"] if has_bins else [] + lib_srcs = ["bin/lib.exe"] if has_bins else [] + ml_srcs = ["bin/{}".format(ml_name)] if has_bins else [] + exports = cl_srcs + link_srcs + lib_srcs + ml_srcs + + repository_ctx.file( + "BUILD.bazel", + "\n".join([ + "package(default_visibility = [\"//visibility:public\"])", + "", + "filegroup(", + " name = \"cl\",", + " srcs = {},".format(repr(cl_srcs)), + ")", + "filegroup(", + " name = \"link\",", + " srcs = {},".format(repr(link_srcs)), + ")", + "filegroup(", + " name = \"lib\",", + " srcs = {},".format(repr(lib_srcs)), + ")", + "filegroup(", + " name = \"ml\",", + " srcs = {},".format(repr(ml_srcs)), + ")", + "exports_files({})".format(repr(exports)), + "", + ]), + ) + +def _write_paths(repository_ctx, include_paths, lib_paths, msvc_path): + repository_ctx.file( + "paths.bzl", + "INCLUDE_PATHS = {}\nLIB_PATHS = {}\nMSVC_PATH = {}\n".format( + repr(include_paths), + repr(lib_paths), + repr(msvc_path), + ), + ) + +def _windows_msvc_impl(repository_ctx): + if not repository_ctx.os.name.startswith("windows"): + _write_paths(repository_ctx, [], [], "") + _write_build(repository_ctx, "ml.exe", False) + return + + arch_info = _detect_target_arch(repository_ctx) + vcvars = _read_vcvars_env(repository_ctx, arch_info) + env = vcvars.env + + include_paths = [] + include_env = env.get("INCLUDE", "") + if include_env: + for raw in include_env.split(";"): + raw = raw.strip() + if not raw: + continue + include_paths.append(raw.replace("\\", "/")) + if not include_paths: + fail("Failed to locate INCLUDE paths via VCVARSALL.BAT.") + + resource_label = None + if arch_info.target_arch == "x64": + resource_label = repository_ctx.attr.clang_resource_header_x86_64 + elif arch_info.target_arch == "arm64": + resource_label = repository_ctx.attr.clang_resource_header_aarch64 + if resource_label: + resource_path = repository_ctx.path(Label(resource_label)) + if resource_path.exists: + include_paths.append(str(resource_path.dirname).replace("\\", "/")) + + lib_paths = [] + lib_env = env.get("LIB", "") + if lib_env: + for raw in lib_env.split(";"): + raw = raw.strip() + if not raw: + continue + lib_paths.append(raw.replace("\\", "/")) + if not lib_paths: + fail("Failed to locate LIB paths via VCVARSALL.BAT.") + + msvc_root = vcvars.msvc_root + + bin_dirs = _msvc_bin_dirs(repository_ctx, msvc_root, arch_info.target_arch, arch_info.host_order) + bin_dir = None + for candidate in bin_dirs: + if repository_ctx.path(candidate).exists: + bin_dir = candidate + break + if not bin_dir: + fail("Failed to locate MSVC bin directory for arch '{}'.".format(arch_info.target_arch)) + + repository_ctx.symlink(bin_dir, "bin") + + msvc_bins = [] + for path in bin_dirs: + if repository_ctx.path(path).exists: + msvc_bins.append(path.replace("\\", "/")) + if not msvc_bins: + fail("Failed to locate MSVC bin directories under '{}'.".format(msvc_root)) + + path_env = repository_ctx.os.environ.get("PATH", "") + vc_path_env = env.get("PATH", "") + joined = ";".join(msvc_bins + ([vc_path_env] if vc_path_env else []) + ([path_env] if path_env else [])) + + _write_paths(repository_ctx, include_paths, lib_paths, joined) + _write_build(repository_ctx, arch_info.ml_name, True) + +windows_msvc = repository_rule( + implementation = _windows_msvc_impl, + attrs = { + "clang_resource_header_x86_64": attr.string(), + "clang_resource_header_aarch64": attr.string(), + }, + environ = [ + "BAZEL_VC", + "BAZEL_VS", + "PATH", + "PROCESSOR_ARCHITECTURE", + "VCINSTALLDIR", + "VSINSTALLDIR", + ], +) diff --git a/tests/rule_based_toolchain/legacy_features_as_args/BUILD b/tests/rule_based_toolchain/legacy_features_as_args/BUILD index 510f0c483..a28dab1b5 100644 --- a/tests/rule_based_toolchain/legacy_features_as_args/BUILD +++ b/tests/rule_based_toolchain/legacy_features_as_args/BUILD @@ -14,6 +14,7 @@ compare_feature_implementation( actual_implementation = "//cc/toolchains/args/archiver_flags", expected = select({ "@platforms//os:macos": "//tests/rule_based_toolchain/legacy_features_as_args:goldens/macos/archiver_flags.textproto", + "@platforms//os:windows": "//tests/rule_based_toolchain/legacy_features_as_args:goldens/windows/archiver_flags.textproto", "//conditions:default": "//tests/rule_based_toolchain/legacy_features_as_args:goldens/unix/archiver_flags.textproto", }), ) diff --git a/tests/rule_based_toolchain/legacy_features_as_args/goldens/macos/archiver_flags.textproto b/tests/rule_based_toolchain/legacy_features_as_args/goldens/macos/archiver_flags.textproto index 566fbdfbd..1bbd57ab8 100644 --- a/tests/rule_based_toolchain/legacy_features_as_args/goldens/macos/archiver_flags.textproto +++ b/tests/rule_based_toolchain/legacy_features_as_args/goldens/macos/archiver_flags.textproto @@ -7,6 +7,18 @@ flag_sets { flags: "-no_warning_for_no_symbols" flags: "-static" } + with_features { + features: "compiler_clang" + } + with_features { + features: "compiler_gcc" + } + with_features { + features: "compiler_mingw_gcc" + } + with_features { + features: "compiler_emscripten" + } } flag_sets { actions: "c++-link-static-library" @@ -15,6 +27,31 @@ flag_sets { flags: "-o" flags: "%{output_execpath}" } + with_features { + features: "compiler_clang" + } + with_features { + features: "compiler_gcc" + } + with_features { + features: "compiler_mingw_gcc" + } + with_features { + features: "compiler_emscripten" + } +} +flag_sets { + actions: "c++-link-static-library" + flag_groups { + expand_if_available: "output_execpath" + flags: "/OUT:%{output_execpath}" + } + with_features { + features: "compiler_msvc_cl" + } + with_features { + features: "compiler_clang_cl" + } } flag_sets { actions: "c++-link-static-library" diff --git a/tests/rule_based_toolchain/legacy_features_as_args/goldens/macos/libraries_to_link.textproto b/tests/rule_based_toolchain/legacy_features_as_args/goldens/macos/libraries_to_link.textproto index 95e44738d..21187502f 100644 --- a/tests/rule_based_toolchain/legacy_features_as_args/goldens/macos/libraries_to_link.textproto +++ b/tests/rule_based_toolchain/legacy_features_as_args/goldens/macos/libraries_to_link.textproto @@ -120,5 +120,82 @@ flag_sets { } } } + with_features { + features: "compiler_clang" + } + with_features { + features: "compiler_gcc" + } + with_features { + features: "compiler_mingw_gcc" + } + with_features { + features: "compiler_emscripten" + } +} +flag_sets { + actions: "c++-link-dynamic-library" + actions: "c++-link-executable" + actions: "c++-link-nodeps-dynamic-library" + actions: "lto-index-for-dynamic-library" + actions: "lto-index-for-executable" + actions: "lto-index-for-nodeps-dynamic-library" + actions: "objc-executable" + flag_groups { + flag_groups { + expand_if_available: "thinlto_param_file" + flags: "-Wl,@%{thinlto_param_file}" + } + flag_groups { + expand_if_available: "libraries_to_link" + flag_groups { + flag_groups { + flag_groups { + expand_if_equal { + name: "libraries_to_link.type" + value: "object_file_group" + } + flags: "%{libraries_to_link.object_files}" + iterate_over: "libraries_to_link.object_files" + } + flag_groups { + expand_if_equal { + name: "libraries_to_link.type" + value: "object_file" + } + flags: "%{libraries_to_link.name}" + } + flag_groups { + expand_if_equal { + name: "libraries_to_link.type" + value: "interface_library" + } + flags: "%{libraries_to_link.name}" + } + flag_groups { + expand_if_equal { + name: "libraries_to_link.type" + value: "static_library" + } + flag_groups { + expand_if_true: "libraries_to_link.is_whole_archive" + flags: "/WHOLEARCHIVE:%{libraries_to_link.name}" + } + flag_groups { + expand_if_false: "libraries_to_link.is_whole_archive" + flags: "%{libraries_to_link.name}" + } + } + } + iterate_over: "libraries_to_link" + } + } + } + with_features { + features: "compiler_msvc_cl" + } + with_features { + features: "compiler_clang_cl" + } } name: "libraries_to_link_test" diff --git a/tests/rule_based_toolchain/legacy_features_as_args/goldens/macos/runtime_library_search_directories.textproto b/tests/rule_based_toolchain/legacy_features_as_args/goldens/macos/runtime_library_search_directories.textproto index 4cfcc009b..ceceb6e6e 100644 --- a/tests/rule_based_toolchain/legacy_features_as_args/goldens/macos/runtime_library_search_directories.textproto +++ b/tests/rule_based_toolchain/legacy_features_as_args/goldens/macos/runtime_library_search_directories.textproto @@ -28,6 +28,19 @@ flag_sets { } } with_features { + features: "compiler_clang" + features: "static_link_cpp_runtimes" + } + with_features { + features: "compiler_gcc" + features: "static_link_cpp_runtimes" + } + with_features { + features: "compiler_mingw_gcc" + features: "static_link_cpp_runtimes" + } + with_features { + features: "compiler_emscripten" features: "static_link_cpp_runtimes" } } @@ -50,6 +63,19 @@ flag_sets { } } with_features { + features: "compiler_clang" + not_features: "static_link_cpp_runtimes" + } + with_features { + features: "compiler_gcc" + not_features: "static_link_cpp_runtimes" + } + with_features { + features: "compiler_mingw_gcc" + not_features: "static_link_cpp_runtimes" + } + with_features { + features: "compiler_emscripten" not_features: "static_link_cpp_runtimes" } } diff --git a/tests/rule_based_toolchain/legacy_features_as_args/goldens/unix/archiver_flags.textproto b/tests/rule_based_toolchain/legacy_features_as_args/goldens/unix/archiver_flags.textproto index 49f45624e..972fcadfb 100644 --- a/tests/rule_based_toolchain/legacy_features_as_args/goldens/unix/archiver_flags.textproto +++ b/tests/rule_based_toolchain/legacy_features_as_args/goldens/unix/archiver_flags.textproto @@ -5,6 +5,18 @@ flag_sets { expand_if_available: "output_execpath" flags: "rcsD" } + with_features { + features: "compiler_clang" + } + with_features { + features: "compiler_gcc" + } + with_features { + features: "compiler_mingw_gcc" + } + with_features { + features: "compiler_emscripten" + } } flag_sets { actions: "c++-link-static-library" @@ -12,6 +24,31 @@ flag_sets { expand_if_available: "output_execpath" flags: "%{output_execpath}" } + with_features { + features: "compiler_clang" + } + with_features { + features: "compiler_gcc" + } + with_features { + features: "compiler_mingw_gcc" + } + with_features { + features: "compiler_emscripten" + } +} +flag_sets { + actions: "c++-link-static-library" + flag_groups { + expand_if_available: "output_execpath" + flags: "/OUT:%{output_execpath}" + } + with_features { + features: "compiler_msvc_cl" + } + with_features { + features: "compiler_clang_cl" + } } flag_sets { actions: "c++-link-static-library" diff --git a/tests/rule_based_toolchain/legacy_features_as_args/goldens/unix/libraries_to_link.textproto b/tests/rule_based_toolchain/legacy_features_as_args/goldens/unix/libraries_to_link.textproto index 0661a24f7..723abc386 100644 --- a/tests/rule_based_toolchain/legacy_features_as_args/goldens/unix/libraries_to_link.textproto +++ b/tests/rule_based_toolchain/legacy_features_as_args/goldens/unix/libraries_to_link.textproto @@ -104,5 +104,82 @@ flag_sets { } } } + with_features { + features: "compiler_clang" + } + with_features { + features: "compiler_gcc" + } + with_features { + features: "compiler_mingw_gcc" + } + with_features { + features: "compiler_emscripten" + } +} +flag_sets { + actions: "c++-link-dynamic-library" + actions: "c++-link-executable" + actions: "c++-link-nodeps-dynamic-library" + actions: "lto-index-for-dynamic-library" + actions: "lto-index-for-executable" + actions: "lto-index-for-nodeps-dynamic-library" + actions: "objc-executable" + flag_groups { + flag_groups { + expand_if_available: "thinlto_param_file" + flags: "-Wl,@%{thinlto_param_file}" + } + flag_groups { + expand_if_available: "libraries_to_link" + flag_groups { + flag_groups { + flag_groups { + expand_if_equal { + name: "libraries_to_link.type" + value: "object_file_group" + } + flags: "%{libraries_to_link.object_files}" + iterate_over: "libraries_to_link.object_files" + } + flag_groups { + expand_if_equal { + name: "libraries_to_link.type" + value: "object_file" + } + flags: "%{libraries_to_link.name}" + } + flag_groups { + expand_if_equal { + name: "libraries_to_link.type" + value: "interface_library" + } + flags: "%{libraries_to_link.name}" + } + flag_groups { + expand_if_equal { + name: "libraries_to_link.type" + value: "static_library" + } + flag_groups { + expand_if_true: "libraries_to_link.is_whole_archive" + flags: "/WHOLEARCHIVE:%{libraries_to_link.name}" + } + flag_groups { + expand_if_false: "libraries_to_link.is_whole_archive" + flags: "%{libraries_to_link.name}" + } + } + } + iterate_over: "libraries_to_link" + } + } + } + with_features { + features: "compiler_msvc_cl" + } + with_features { + features: "compiler_clang_cl" + } } name: "libraries_to_link_test" diff --git a/tests/rule_based_toolchain/legacy_features_as_args/goldens/unix/runtime_library_search_directories.textproto b/tests/rule_based_toolchain/legacy_features_as_args/goldens/unix/runtime_library_search_directories.textproto index fdaf296bd..7d24188b9 100644 --- a/tests/rule_based_toolchain/legacy_features_as_args/goldens/unix/runtime_library_search_directories.textproto +++ b/tests/rule_based_toolchain/legacy_features_as_args/goldens/unix/runtime_library_search_directories.textproto @@ -28,6 +28,19 @@ flag_sets { } } with_features { + features: "compiler_clang" + features: "static_link_cpp_runtimes" + } + with_features { + features: "compiler_gcc" + features: "static_link_cpp_runtimes" + } + with_features { + features: "compiler_mingw_gcc" + features: "static_link_cpp_runtimes" + } + with_features { + features: "compiler_emscripten" features: "static_link_cpp_runtimes" } } @@ -50,6 +63,19 @@ flag_sets { } } with_features { + features: "compiler_clang" + not_features: "static_link_cpp_runtimes" + } + with_features { + features: "compiler_gcc" + not_features: "static_link_cpp_runtimes" + } + with_features { + features: "compiler_mingw_gcc" + not_features: "static_link_cpp_runtimes" + } + with_features { + features: "compiler_emscripten" not_features: "static_link_cpp_runtimes" } } diff --git a/tests/rule_based_toolchain/legacy_features_as_args/goldens/unix/shared_flag.textproto b/tests/rule_based_toolchain/legacy_features_as_args/goldens/unix/shared_flag.textproto index 51b686882..79a04e085 100644 --- a/tests/rule_based_toolchain/legacy_features_as_args/goldens/unix/shared_flag.textproto +++ b/tests/rule_based_toolchain/legacy_features_as_args/goldens/unix/shared_flag.textproto @@ -7,5 +7,17 @@ flag_sets { flag_groups { flags: "-shared" } + with_features { + features: "compiler_clang" + } + with_features { + features: "compiler_gcc" + } + with_features { + features: "compiler_mingw_gcc" + } + with_features { + features: "compiler_emscripten" + } } name: "shared_flag_test" diff --git a/tests/rule_based_toolchain/legacy_features_as_args/goldens/windows/archiver_flags.textproto b/tests/rule_based_toolchain/legacy_features_as_args/goldens/windows/archiver_flags.textproto new file mode 100755 index 000000000..972fcadfb --- /dev/null +++ b/tests/rule_based_toolchain/legacy_features_as_args/goldens/windows/archiver_flags.textproto @@ -0,0 +1,77 @@ +enabled: false +flag_sets { + actions: "c++-link-static-library" + flag_groups { + expand_if_available: "output_execpath" + flags: "rcsD" + } + with_features { + features: "compiler_clang" + } + with_features { + features: "compiler_gcc" + } + with_features { + features: "compiler_mingw_gcc" + } + with_features { + features: "compiler_emscripten" + } +} +flag_sets { + actions: "c++-link-static-library" + flag_groups { + expand_if_available: "output_execpath" + flags: "%{output_execpath}" + } + with_features { + features: "compiler_clang" + } + with_features { + features: "compiler_gcc" + } + with_features { + features: "compiler_mingw_gcc" + } + with_features { + features: "compiler_emscripten" + } +} +flag_sets { + actions: "c++-link-static-library" + flag_groups { + expand_if_available: "output_execpath" + flags: "/OUT:%{output_execpath}" + } + with_features { + features: "compiler_msvc_cl" + } + with_features { + features: "compiler_clang_cl" + } +} +flag_sets { + actions: "c++-link-static-library" + flag_groups { + expand_if_available: "libraries_to_link" + flag_groups { + flag_groups { + expand_if_equal { + name: "libraries_to_link.type" + value: "object_file" + } + flags: "%{libraries_to_link.name}" + } + flag_groups { + expand_if_equal { + name: "libraries_to_link.type" + value: "object_file_group" + } + flags: "%{libraries_to_link.object_files}" + iterate_over: "libraries_to_link.object_files" + } + iterate_over: "libraries_to_link" + } + } +} +name: "archiver_flags_test" diff --git a/tests/rule_based_toolchain/subjects.bzl b/tests/rule_based_toolchain/subjects.bzl index 79e6f1b50..c47940f32 100644 --- a/tests/rule_based_toolchain/subjects.bzl +++ b/tests/rule_based_toolchain/subjects.bzl @@ -232,6 +232,7 @@ _ToolchainConfigFactory = generate_factory( features = ProviderDepset(_FeatureFactory), enabled_features = _subjects.collection, tool_map = optional_subject(_ToolConfigFactory.factory), + compiler_feature = optional_subject(_FeatureFactory.factory), args = ProviderSequence(_ArgsFactory), files = dict_key_subject(_subjects.depset_file), allowlist_include_directories = _FakeDirectoryDepset, diff --git a/tests/rule_based_toolchain/toolchain_config/toolchain_config_test.bzl b/tests/rule_based_toolchain/toolchain_config/toolchain_config_test.bzl index 1047203c4..98b1683c9 100644 --- a/tests/rule_based_toolchain/toolchain_config/toolchain_config_test.bzl +++ b/tests/rule_based_toolchain/toolchain_config/toolchain_config_test.bzl @@ -49,7 +49,9 @@ _COLLECTED_C_COMPILE_FILES = _COLLECTED_CPP_COMPILE_FILES + [ "tests/rule_based_toolchain/testdata/subdir3/file_baz", ] -def _expect_that_toolchain(env, expr = None, **kwargs): +def _expect_that_toolchain(env, targets, expr = None, **kwargs): + if "compiler_feature" not in kwargs: + kwargs["compiler_feature"] = targets.gcc return env.expect.that_value( value = toolchain_config_info(label = Label("//:toolchain"), **kwargs), expr = expr, @@ -59,6 +61,7 @@ def _expect_that_toolchain(env, expr = None, **kwargs): def _missing_tool_map_invalid_test(env, _targets): _expect_that_toolchain( env, + _targets, tool_map = None, expr = "missing_tool_map", ).err().contains( @@ -68,12 +71,14 @@ def _missing_tool_map_invalid_test(env, _targets): def _empty_toolchain_valid_test(env, targets): _expect_that_toolchain( env, + targets, tool_map = targets.empty_tool_map, # tool_map is always required. ).ok() def _duplicate_feature_names_invalid_test(env, targets): _expect_that_toolchain( env, + targets, known_features = [targets.simple_feature, targets.same_feature_name], tool_map = targets.empty_tool_map, expr = "duplicate_feature_name", @@ -86,6 +91,7 @@ def _duplicate_feature_names_invalid_test(env, targets): # Overriding a feature gives it the same name. Ensure this isn't blocked. _expect_that_toolchain( env, + targets, known_features = [targets.builtin_feature, targets.overrides_feature], tool_map = targets.empty_tool_map, expr = "override_feature", @@ -94,6 +100,7 @@ def _duplicate_feature_names_invalid_test(env, targets): def _feature_config_implies_missing_feature_invalid_test(env, targets): _expect_that_toolchain( env, + targets, expr = "feature_with_implies", known_features = [targets.simple_feature, targets.implies_simple_feature], tool_map = targets.empty_tool_map, @@ -101,6 +108,7 @@ def _feature_config_implies_missing_feature_invalid_test(env, targets): _expect_that_toolchain( env, + targets, known_features = [targets.implies_simple_feature], tool_map = targets.empty_tool_map, expr = "feature_missing_implies", @@ -111,18 +119,21 @@ def _feature_config_implies_missing_feature_invalid_test(env, targets): def _feature_missing_requirements_invalid_test(env, targets): _expect_that_toolchain( env, + targets, known_features = [targets.requires_any_simple_feature, targets.simple_feature], tool_map = targets.empty_tool_map, expr = "requires_any_simple_has_simple", ).ok() _expect_that_toolchain( env, + targets, known_features = [targets.requires_any_simple_feature, targets.simple_feature2], tool_map = targets.empty_tool_map, expr = "requires_any_simple_has_simple2", ).ok() _expect_that_toolchain( env, + targets, known_features = [targets.requires_any_simple_feature], tool_map = targets.empty_tool_map, expr = "requires_any_simple_has_none", @@ -132,12 +143,14 @@ def _feature_missing_requirements_invalid_test(env, targets): _expect_that_toolchain( env, + targets, known_features = [targets.requires_all_simple_feature, targets.simple_feature, targets.simple_feature2], tool_map = targets.empty_tool_map, expr = "requires_all_simple_has_both", ).ok() _expect_that_toolchain( env, + targets, known_features = [targets.requires_all_simple_feature, targets.simple_feature], tool_map = targets.empty_tool_map, expr = "requires_all_simple_has_simple", @@ -146,6 +159,7 @@ def _feature_missing_requirements_invalid_test(env, targets): ) _expect_that_toolchain( env, + targets, known_features = [targets.requires_all_simple_feature, targets.simple_feature2], tool_map = targets.empty_tool_map, expr = "requires_all_simple_has_simple2", @@ -156,6 +170,7 @@ def _feature_missing_requirements_invalid_test(env, targets): def _args_missing_requirements_invalid_test(env, targets): _expect_that_toolchain( env, + targets, args = [targets.requires_all_simple_args], known_features = [targets.simple_feature, targets.simple_feature2], tool_map = targets.empty_tool_map, @@ -163,6 +178,7 @@ def _args_missing_requirements_invalid_test(env, targets): ).ok() _expect_that_toolchain( env, + targets, args = [targets.requires_all_simple_args], known_features = [targets.simple_feature], tool_map = targets.empty_tool_map, @@ -197,6 +213,10 @@ def _toolchain_collects_files_test(env, targets): ], )], ), + legacy_feature( + name = "compiler_gcc", + enabled = True, + ), legacy_feature( name = "compile_feature", enabled = False, @@ -208,9 +228,29 @@ def _toolchain_collects_files_test(env, targets): )], ), legacy_feature( - name = "supports_pic", + name = "compiler_clang", + enabled = False, + ), + legacy_feature( + name = "compiler_clang_cl", + enabled = False, + ), + legacy_feature( + name = "compiler_mingw_gcc", enabled = False, ), + legacy_feature( + name = "compiler_msvc_cl", + enabled = False, + ), + legacy_feature( + name = "compiler_emscripten", + enabled = False, + ), + legacy_feature( + name = "supports_pic", + enabled = True, + ), legacy_feature( name = "implied_by_always_enabled_env_sets", enabled = True, @@ -259,6 +299,7 @@ def _toolchain_collects_files_test(env, targets): ]).in_order() TARGETS = [ + "//cc/toolchains/compiler:gcc", "//tests/rule_based_toolchain/actions:c_compile", "//tests/rule_based_toolchain/actions:cpp_compile", ":builtin_feature",