From c9dda0e919906f5f534e0f05458e7ee74ea13f1e Mon Sep 17 00:00:00 2001 From: Joshua Kurland Date: Fri, 9 Aug 2024 15:07:07 +0100 Subject: [PATCH 1/7] Allow user provided platform constraints --- toolchain/extensions/llvm.bzl | 36 ++++++++++++++++++++++++++++++++ toolchain/internal/configure.bzl | 12 +++++++++-- toolchain/internal/repo.bzl | 8 +++++++ 3 files changed, 54 insertions(+), 2 deletions(-) diff --git a/toolchain/extensions/llvm.bzl b/toolchain/extensions/llvm.bzl index e38b783a..28148719 100644 --- a/toolchain/extensions/llvm.bzl +++ b/toolchain/extensions/llvm.bzl @@ -34,6 +34,20 @@ def _root_dict(roots, cls, name, strip_target): return res +def _constraint_dict(tags, name): + constraints = {} + + # Gather all the additional constraints for each target + for tag in tags: + targets = list(tag.targets) + if not targets: + targets = [""] + for target in targets: + constraints_for_target = constraints.setdefault(target, []) + constraints_for_target.extend([str(c) for c in tag.constraints]) + + return constraints + def _llvm_impl_(module_ctx): for mod in module_ctx.modules: if not mod.is_root: @@ -49,6 +63,14 @@ def _llvm_impl_(module_ctx): } attrs["toolchain_roots"] = _root_dict([root for root in mod.tags.toolchain_root if root.name == name], "toolchain_root", name, True) attrs["sysroot"] = _root_dict([sysroot for sysroot in mod.tags.sysroot if sysroot.name == name], "sysroot", name, False) + attrs["extra_exec_compatible_with"] = _constraint_dict( + [tag for tag in mod.tags.extra_exec_compatible_with if tag.name == name], + name, + ) + attrs["extra_target_compatible_with"] = _constraint_dict( + [tag for tag in mod.tags.extra_target_compatible_with if tag.name == name], + name, + ) llvm_toolchain( **attrs @@ -95,5 +117,19 @@ llvm = module_extension( "path": attr.string(doc = "Absolute path to the sysroot."), }, ), + "extra_exec_compatible_with": tag_class( + attrs = { + "name": attr.string(doc = "Same name as the toolchain tag.", default = "llvm_toolchain"), + "targets": attr.string_list(doc = "Specific targets, if any; empty list means this applies to all."), + "constraints": attr.label_list(doc = "List of extra constraints to add to exec_compatible_with for the generated toolchains."), + }, + ), + "extra_target_compatible_with": tag_class( + attrs = { + "name": attr.string(doc = "Same name as the toolchain tag.", default = "llvm_toolchain"), + "targets": attr.string_list(doc = "Specific targets, if any; empty list means this applies to all."), + "constraints": attr.label_list(doc = "List of extra constraints to add to target_compatible_with for the generated toolchains."), + }, + ), }, ) diff --git a/toolchain/internal/configure.bzl b/toolchain/internal/configure.bzl index 6e40059c..8731af75 100644 --- a/toolchain/internal/configure.bzl +++ b/toolchain/internal/configure.bzl @@ -59,6 +59,8 @@ def _join(path1, path2): def llvm_config_impl(rctx): _check_os_arch_keys(rctx.attr.sysroot) _check_os_arch_keys(rctx.attr.cxx_builtin_include_directories) + _check_os_arch_keys(rctx.attr.extra_exec_compatible_with) + _check_os_arch_keys(rctx.attr.extra_target_compatible_with) os = _os(rctx) if os == "windows": @@ -166,6 +168,8 @@ def llvm_config_impl(rctx): unfiltered_compile_flags_dict = rctx.attr.unfiltered_compile_flags, llvm_version = llvm_version, extra_compiler_files = rctx.attr.extra_compiler_files, + extra_exec_compatible_with = rctx.attr.extra_exec_compatible_with, + extra_target_compatible_with = rctx.attr.extra_target_compatible_with, ) exec_dl_ext = "dylib" if os == "darwin" else "so" cc_toolchains_str, toolchain_labels_str = _cc_toolchains_str( @@ -380,11 +384,11 @@ toolchain( exec_compatible_with = [ "@platforms//cpu:{exec_arch}", "@platforms//os:{exec_os_bzl}", - ], + ] + {extra_exec_compatible_with_specific} + {extra_exec_compatible_with_all_targets}, target_compatible_with = [ "@platforms//cpu:{target_arch}", "@platforms//os:{target_os_bzl}", - ], + ] + {extra_target_compatible_with_specific} + {extra_target_compatible_with_all_targets}, target_settings = {target_settings}, toolchain = ":cc-clang-{suffix}", toolchain_type = "@bazel_tools//tools/cpp:toolchain_type", @@ -543,6 +547,10 @@ cc_toolchain( ]), extra_compiler_files = ("\"%s\"," % str(toolchain_info.extra_compiler_files)) if toolchain_info.extra_compiler_files else "", major_llvm_version = major_llvm_version, + extra_exec_compatible_with_specific = toolchain_info.extra_exec_compatible_with.get(target_pair, []), + extra_target_compatible_with_specific = toolchain_info.extra_target_compatible_with.get(target_pair, []), + extra_exec_compatible_with_all_targets = toolchain_info.extra_exec_compatible_with.get("", []), + extra_target_compatible_with_all_targets = toolchain_info.extra_target_compatible_with.get("", []), ) def _convenience_targets_str(rctx, use_absolute_paths, llvm_dist_rel_path, llvm_dist_label_prefix, exec_dl_ext): diff --git a/toolchain/internal/repo.bzl b/toolchain/internal/repo.bzl index 27012db6..df89c4c1 100644 --- a/toolchain/internal/repo.bzl +++ b/toolchain/internal/repo.bzl @@ -271,6 +271,14 @@ llvm_config_attrs.update({ default = False, doc = "Use absolute paths in the toolchain. Avoids sandbox overhead.", ), + "extra_exec_compatible_with": attr.string_list_dict( + mandatory = False, + doc = "Extra constraints to be added to exec_compatible_with for each target", + ), + "extra_target_compatible_with": attr.string_list_dict( + mandatory = False, + doc = "Extra constraints to be added to target_compatible_with for each target", + ), "_cc_toolchain_config_bzl": attr.label( default = "//toolchain:cc_toolchain_config.bzl", ), From 034caf2f5e921d93e73d470815f2e07e54057c67 Mon Sep 17 00:00:00 2001 From: Joshua Kurland Date: Fri, 16 Aug 2024 12:36:20 +0100 Subject: [PATCH 2/7] Add a test for extra_target_compatible_with --- tests/BUILD.bazel | 52 ++++++++++++++++++++++++++++++++++++++++++- tests/MODULE.bazel | 18 ++++++++++++++- tests/test_define.cc | 24 ++++++++++++++++++++ tests/transitions.bzl | 21 +++++++++++++++++ 4 files changed, 113 insertions(+), 2 deletions(-) create mode 100644 tests/test_define.cc diff --git a/tests/BUILD.bazel b/tests/BUILD.bazel index 940c9577..daefc26f 100644 --- a/tests/BUILD.bazel +++ b/tests/BUILD.bazel @@ -14,7 +14,7 @@ load("@bazel_skylib//rules:build_test.bzl", "build_test") load("@rules_cc//cc:defs.bzl", "cc_binary", "cc_library", "cc_test") -load(":transitions.bzl", "dwp_file") +load(":transitions.bzl", "dwp_file", "transition_library_to_test_define") cc_library( name = "stdlib", @@ -139,3 +139,53 @@ toolchain( toolchain = "@@rules_foreign_cc~~tools~ninja_1.11.1_mac//:ninja_tool", toolchain_type = "@rules_foreign_cc//toolchains:ninja_toolchain", ) + +# Testing extra_target_compatible_with +constraint_setting( + name = "with_test_define", + default_constraint_value = ":no_test_define", + visibility = ["//visibility:public"], +) + +constraint_value( + name = "no_test_define", + constraint_setting = ":with_test_define", + visibility = ["//visibility:public"], +) + +constraint_value( + name = "yes_test_define", + constraint_setting = ":with_test_define", + visibility = ["//visibility:public"], +) + +platform( + name = "platform_with_test_define", + constraint_values = [ + ":yes_test_define", + ], + parents = ["@bazel_tools//tools:host_platform"], + visibility = ["//visibility:public"], +) + +cc_library( + name = "test_define_lib", + srcs = ["test_define.cc"], +) + +cc_test( + name = "test_define_no_define", + args = ["no"], + deps = [":test_define_lib"], +) + +transition_library_to_test_define( + name = "test_define_lib_with_define", + lib = ":test_define_lib", +) + +cc_test( + name = "test_define_yes_define", + args = ["yes"], + deps = [":test_define_lib_with_define"], +) diff --git a/tests/MODULE.bazel b/tests/MODULE.bazel index 126b4dd3..d733db24 100644 --- a/tests/MODULE.bazel +++ b/tests/MODULE.bazel @@ -66,10 +66,26 @@ llvm.toolchain( name = "llvm_toolchain", llvm_versions = LLVM_VERSIONS, ) +llvm.extra_target_compatible_with( + name = "llvm_toolchain", + constraints = ["@//:no_test_define"], +) use_repo(llvm, "llvm_toolchain", "llvm_toolchain_llvm") - register_toolchains("@llvm_toolchain//:all") +llvm.toolchain( + name = "llvm_toolchain_with_define", + llvm_versions = LLVM_VERSIONS, + cxx_flags = {"": ["-DTEST_DEFINE", "-stdlib=libc++"]}, +) +llvm.extra_target_compatible_with( + name = "llvm_toolchain_with_define", + constraints = ["//:yes_test_define"], +) +use_repo(llvm, "llvm_toolchain_with_define") +register_toolchains("@llvm_toolchain_with_define//:all") + + # Example toolchain with user provided URLs. # TODO(siddharthab): Add test. llvm.toolchain( diff --git a/tests/test_define.cc b/tests/test_define.cc new file mode 100644 index 00000000..450e95dd --- /dev/null +++ b/tests/test_define.cc @@ -0,0 +1,24 @@ +#include +#include + +int main(int argc, char** argv) { + if (argc != 2) { + std::cout << "Not enough arguments" << std::endl; + return 1; + } + + std::string arg = argv[1]; + +#ifdef TEST_DEFINE + if (arg != "yes") { + std::cout << "TEST_DEFINE is defined but it was expected to be not defined" << std::endl; + return 1; + } +#else + if (arg != "no") { + std::cout << "TEST_DEFINE is not defined but it was expected to be defined" << std::endl; + return 1; + } +#endif + return 0; +} \ No newline at end of file diff --git a/tests/transitions.bzl b/tests/transitions.bzl index 23db8d31..fb5bb969 100644 --- a/tests/transitions.bzl +++ b/tests/transitions.bzl @@ -96,3 +96,24 @@ dwp_file = rule( ), }, ) + +def _transition_to_test_define_impl(_, _attr): + return {"//command_line_option:platforms": "//:platform_with_test_define"} + +_transition_to_test_define = transition( + implementation = _transition_to_test_define_impl, + inputs = [], + outputs = ["//command_line_option:platforms"], +) + +def _transition_library_to_test_define_impl(ctx): + return [ + ctx.attr.lib[0][CcInfo], + ] + +transition_library_to_test_define = rule( + implementation = _transition_library_to_test_define_impl, + attrs = { + "lib": attr.label(cfg = _transition_to_test_define), + }, +) From 187bb3ddce174a648a29dfdfe9756756efc4e708 Mon Sep 17 00:00:00 2001 From: Joshua Kurland Date: Mon, 19 Aug 2024 10:50:01 +0100 Subject: [PATCH 3/7] Use cxx_standard instead of cxxflags to detect which toolchain is used and support workspace --- tests/BUILD.bazel | 41 +++++++++++++++++++------------------- tests/MODULE.bazel | 15 +++++++------- tests/WORKSPACE | 17 ++++++++++++++++ tests/test_cxx_standard.cc | 22 ++++++++++++++++++++ tests/test_define.cc | 24 ---------------------- tests/transitions.bzl | 17 ++++++++-------- 6 files changed, 77 insertions(+), 59 deletions(-) create mode 100644 tests/test_cxx_standard.cc delete mode 100644 tests/test_define.cc diff --git a/tests/BUILD.bazel b/tests/BUILD.bazel index daefc26f..a350f976 100644 --- a/tests/BUILD.bazel +++ b/tests/BUILD.bazel @@ -14,7 +14,7 @@ load("@bazel_skylib//rules:build_test.bzl", "build_test") load("@rules_cc//cc:defs.bzl", "cc_binary", "cc_library", "cc_test") -load(":transitions.bzl", "dwp_file", "transition_library_to_test_define") +load(":transitions.bzl", "dwp_file", "transition_library_to_platform") cc_library( name = "stdlib", @@ -142,50 +142,51 @@ toolchain( # Testing extra_target_compatible_with constraint_setting( - name = "with_test_define", - default_constraint_value = ":no_test_define", + name = "cxx_standard", + default_constraint_value = ":cxx17", visibility = ["//visibility:public"], ) constraint_value( - name = "no_test_define", - constraint_setting = ":with_test_define", + name = "cxx20", + constraint_setting = ":cxx_standard", visibility = ["//visibility:public"], ) constraint_value( - name = "yes_test_define", - constraint_setting = ":with_test_define", + name = "cxx17", + constraint_setting = ":cxx_standard", visibility = ["//visibility:public"], ) platform( - name = "platform_with_test_define", + name = "cxx20_platform", constraint_values = [ - ":yes_test_define", + ":cxx20", ], parents = ["@bazel_tools//tools:host_platform"], visibility = ["//visibility:public"], ) cc_library( - name = "test_define_lib", - srcs = ["test_define.cc"], + name = "test_cxx_standard_lib", + srcs = ["test_cxx_standard.cc"], ) cc_test( - name = "test_define_no_define", - args = ["no"], - deps = [":test_define_lib"], + name = "test_cxx_standard_is_17", + args = ["201703"], + deps = [":test_cxx_standard_lib"], ) -transition_library_to_test_define( - name = "test_define_lib_with_define", - lib = ":test_define_lib", +transition_library_to_platform( + name = "test_cxx_standard_lib_transitioned", + lib = ":test_cxx_standard_lib", + platform = ":cxx20_platform", ) cc_test( - name = "test_define_yes_define", - args = ["yes"], - deps = [":test_define_lib_with_define"], + name = "test_cxx_standard_is_20", + args = ["202002"], + deps = [":test_cxx_standard_lib_transitioned"], ) diff --git a/tests/MODULE.bazel b/tests/MODULE.bazel index d733db24..1145b32c 100644 --- a/tests/MODULE.bazel +++ b/tests/MODULE.bazel @@ -65,25 +65,26 @@ LLVM_VERSIONS = { llvm.toolchain( name = "llvm_toolchain", llvm_versions = LLVM_VERSIONS, + cxx_standard = {"": "c++17"}, ) llvm.extra_target_compatible_with( name = "llvm_toolchain", - constraints = ["@//:no_test_define"], + constraints = ["@//:cxx17"], ) use_repo(llvm, "llvm_toolchain", "llvm_toolchain_llvm") register_toolchains("@llvm_toolchain//:all") llvm.toolchain( - name = "llvm_toolchain_with_define", + name = "llvm_toolchain_cxx20", llvm_versions = LLVM_VERSIONS, - cxx_flags = {"": ["-DTEST_DEFINE", "-stdlib=libc++"]}, + cxx_standard = {"": "c++20"}, ) llvm.extra_target_compatible_with( - name = "llvm_toolchain_with_define", - constraints = ["//:yes_test_define"], + name = "llvm_toolchain_cxx20", + constraints = ["//:cxx20"], ) -use_repo(llvm, "llvm_toolchain_with_define") -register_toolchains("@llvm_toolchain_with_define//:all") +use_repo(llvm, "llvm_toolchain_cxx20") +register_toolchains("@llvm_toolchain_cxx20//:all") # Example toolchain with user provided URLs. diff --git a/tests/WORKSPACE b/tests/WORKSPACE index 04129246..ab9181dd 100644 --- a/tests/WORKSPACE +++ b/tests/WORKSPACE @@ -37,6 +37,19 @@ LLVM_VERSIONS = { llvm_toolchain( name = "llvm_toolchain", + cxx_standard = {"": "c++17"}, + extra_target_compatible_with = { + "": ["@//:cxx17"], + }, + llvm_versions = LLVM_VERSIONS, +) + +llvm_toolchain( + name = "llvm_toolchain_cxx20", + cxx_standard = {"": "c++20"}, + extra_target_compatible_with = { + "": ["@//:cxx20"], + }, llvm_versions = LLVM_VERSIONS, ) @@ -75,6 +88,10 @@ load("@llvm_toolchain//:toolchains.bzl", "llvm_register_toolchains") llvm_register_toolchains() +load("@llvm_toolchain_cxx20//:toolchains.bzl", llvm_register_toolchains_cxx20 = "llvm_register_toolchains") + +llvm_register_toolchains_cxx20() + ## Toolchain example with absolute paths; tested in GitHub CI. llvm_toolchain( name = "llvm_toolchain_with_absolute_paths", diff --git a/tests/test_cxx_standard.cc b/tests/test_cxx_standard.cc new file mode 100644 index 00000000..7d383aee --- /dev/null +++ b/tests/test_cxx_standard.cc @@ -0,0 +1,22 @@ +#include +#include + +int main(int argc, char** argv) { + if (argc != 2) { + std::cout << "Not enough arguments" << std::endl; + return 1; + } + + long expected_version = std::atol(argv[1]); + + if (expected_version == 0) { + std::cout << "Invalid version argument, must be an integer" << std::endl; + return 1; + } + + if (expected_version != __cplusplus) { + std::cout << "Expected version to be " << argv[1] << " but got " << __cplusplus << std::endl; + return 1; + } + return 0; +} \ No newline at end of file diff --git a/tests/test_define.cc b/tests/test_define.cc deleted file mode 100644 index 450e95dd..00000000 --- a/tests/test_define.cc +++ /dev/null @@ -1,24 +0,0 @@ -#include -#include - -int main(int argc, char** argv) { - if (argc != 2) { - std::cout << "Not enough arguments" << std::endl; - return 1; - } - - std::string arg = argv[1]; - -#ifdef TEST_DEFINE - if (arg != "yes") { - std::cout << "TEST_DEFINE is defined but it was expected to be not defined" << std::endl; - return 1; - } -#else - if (arg != "no") { - std::cout << "TEST_DEFINE is not defined but it was expected to be defined" << std::endl; - return 1; - } -#endif - return 0; -} \ No newline at end of file diff --git a/tests/transitions.bzl b/tests/transitions.bzl index fb5bb969..33938337 100644 --- a/tests/transitions.bzl +++ b/tests/transitions.bzl @@ -97,23 +97,24 @@ dwp_file = rule( }, ) -def _transition_to_test_define_impl(_, _attr): - return {"//command_line_option:platforms": "//:platform_with_test_define"} +def _transition_library_to_platform_transition_impl(_, attr): + return {"//command_line_option:platforms": str(attr.platform)} -_transition_to_test_define = transition( - implementation = _transition_to_test_define_impl, +_transition_library_to_platform_transition = transition( + implementation = _transition_library_to_platform_transition_impl, inputs = [], outputs = ["//command_line_option:platforms"], ) -def _transition_library_to_test_define_impl(ctx): +def _transition_library_to_platform_impl(ctx): return [ ctx.attr.lib[0][CcInfo], ] -transition_library_to_test_define = rule( - implementation = _transition_library_to_test_define_impl, +transition_library_to_platform = rule( + implementation = _transition_library_to_platform_impl, attrs = { - "lib": attr.label(cfg = _transition_to_test_define), + "lib": attr.label(mandatory = True, cfg = _transition_library_to_platform_transition), + "platform": attr.label(mandatory = True), }, ) From 7849f229142416abfc95a5eb7e136c676872aa02 Mon Sep 17 00:00:00 2001 From: Joshua Kurland Date: Tue, 20 Aug 2024 10:46:42 +0100 Subject: [PATCH 4/7] Use old host platform label, add function_transition_allowlist to platform transition rule --- tests/BUILD.bazel | 4 +++- tests/transitions.bzl | 3 +++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/tests/BUILD.bazel b/tests/BUILD.bazel index a350f976..0548b7f4 100644 --- a/tests/BUILD.bazel +++ b/tests/BUILD.bazel @@ -164,7 +164,7 @@ platform( constraint_values = [ ":cxx20", ], - parents = ["@bazel_tools//tools:host_platform"], + parents = ["@platforms//host"], visibility = ["//visibility:public"], ) @@ -175,6 +175,7 @@ cc_library( cc_test( name = "test_cxx_standard_is_17", + size = "small", args = ["201703"], deps = [":test_cxx_standard_lib"], ) @@ -187,6 +188,7 @@ transition_library_to_platform( cc_test( name = "test_cxx_standard_is_20", + size = "small", args = ["202002"], deps = [":test_cxx_standard_lib_transitioned"], ) diff --git a/tests/transitions.bzl b/tests/transitions.bzl index 33938337..ac7316ed 100644 --- a/tests/transitions.bzl +++ b/tests/transitions.bzl @@ -116,5 +116,8 @@ transition_library_to_platform = rule( attrs = { "lib": attr.label(mandatory = True, cfg = _transition_library_to_platform_transition), "platform": attr.label(mandatory = True), + "_allowlist_function_transition": attr.label( + default = "@bazel_tools//tools/allowlists/function_transition_allowlist", + ), }, ) From 7f73ce490b80fbe454ddc23206bdf384a7800ea2 Mon Sep 17 00:00:00 2001 From: Joshua Kurland Date: Tue, 20 Aug 2024 10:50:26 +0100 Subject: [PATCH 5/7] Try adding a src file to platform constraint tests to see if that fixes macos --- tests/BUILD.bazel | 2 ++ tests/test_cxx_standard.cc | 4 ++-- tests/test_cxx_standard_main.cc | 5 +++++ 3 files changed, 9 insertions(+), 2 deletions(-) create mode 100644 tests/test_cxx_standard_main.cc diff --git a/tests/BUILD.bazel b/tests/BUILD.bazel index 0548b7f4..c1c9f8fe 100644 --- a/tests/BUILD.bazel +++ b/tests/BUILD.bazel @@ -176,6 +176,7 @@ cc_library( cc_test( name = "test_cxx_standard_is_17", size = "small", + srcs = ["test_cxx_standard_main.cc"], args = ["201703"], deps = [":test_cxx_standard_lib"], ) @@ -189,6 +190,7 @@ transition_library_to_platform( cc_test( name = "test_cxx_standard_is_20", size = "small", + srcs = ["test_cxx_standard_main.cc"], args = ["202002"], deps = [":test_cxx_standard_lib_transitioned"], ) diff --git a/tests/test_cxx_standard.cc b/tests/test_cxx_standard.cc index 7d383aee..26892880 100644 --- a/tests/test_cxx_standard.cc +++ b/tests/test_cxx_standard.cc @@ -1,7 +1,7 @@ #include #include -int main(int argc, char** argv) { +int run_test(int argc, char** argv) { if (argc != 2) { std::cout << "Not enough arguments" << std::endl; return 1; @@ -19,4 +19,4 @@ int main(int argc, char** argv) { return 1; } return 0; -} \ No newline at end of file +} diff --git a/tests/test_cxx_standard_main.cc b/tests/test_cxx_standard_main.cc new file mode 100644 index 00000000..9a388226 --- /dev/null +++ b/tests/test_cxx_standard_main.cc @@ -0,0 +1,5 @@ +int run_test(int argc, char** argv); + +int main(int argc, char** argv) { + return run_test(argc, argv); +} \ No newline at end of file From 49aa18ed95cfca84e73fece55fb9bb715e99fa64 Mon Sep 17 00:00:00 2001 From: Joshua Kurland Date: Thu, 22 Aug 2024 10:44:11 +0100 Subject: [PATCH 6/7] Add c++17 constraint to other toolchains which get registered in different tests, add host_platform repo in non bzlmod tests --- tests/MODULE.bazel | 30 ++++++++++++++++++++++++++++++ tests/WORKSPACE | 37 +++++++++++++++++++++++++++++++++++++ 2 files changed, 67 insertions(+) diff --git a/tests/MODULE.bazel b/tests/MODULE.bazel index 1145b32c..7eb07566 100644 --- a/tests/MODULE.bazel +++ b/tests/MODULE.bazel @@ -111,6 +111,11 @@ llvm.toolchain( "darwin-aarch64": ["https://github.com/llvm/llvm-project/releases/download/llvmorg-15.0.7/clang+llvm-15.0.7-arm64-apple-darwin22.0.tar.xz"], "darwin-x86_64": ["https://github.com/llvm/llvm-project/releases/download/llvmorg-15.0.7/clang+llvm-15.0.7-x86_64-apple-darwin21.0.tar.xz"], }, + cxx_standard = {"": "c++17"}, +) +llvm.extra_target_compatible_with( + name = "llvm_toolchain_with_urls", + constraints = ["@//:cxx17"], ) use_repo(llvm, "llvm_toolchain_with_urls") @@ -121,6 +126,11 @@ use_repo(llvm, "llvm_toolchain_with_urls") llvm.toolchain( name = "llvm_toolchain_13_0_0", llvm_version = "13.0.0", + cxx_standard = {"": "c++17"}, +) +llvm.extra_target_compatible_with( + name = "llvm_toolchain_13_0_0", + constraints = ["@//:cxx17"], ) use_repo(llvm, "llvm_toolchain_13_0_0") @@ -129,6 +139,11 @@ llvm.toolchain( name = "llvm_toolchain_with_absolute_paths", absolute_paths = True, llvm_versions = LLVM_VERSIONS, + cxx_standard = {"": "c++17"}, +) +llvm.extra_target_compatible_with( + name = "llvm_toolchain_with_absolute_paths", + constraints = ["@//:cxx17"], ) # We can share the downloaded LLVM distribution with the first configuration. llvm.toolchain_root( @@ -141,6 +156,11 @@ use_repo(llvm, "llvm_toolchain_with_absolute_paths") llvm.toolchain( name = "llvm_toolchain_with_system_llvm", llvm_versions = LLVM_VERSIONS, + cxx_standard = {"": "c++17"}, +) +llvm.extra_target_compatible_with( + name = "llvm_toolchain_with_system_llvm", + constraints = ["@//:cxx17"], ) # For this toolchain to work, the LLVM distribution archive would need to be unpacked here. llvm.toolchain_root( @@ -153,6 +173,11 @@ use_repo(llvm, "llvm_toolchain_with_system_llvm") llvm.toolchain( name = "llvm_toolchain_with_sysroot", llvm_versions = LLVM_VERSIONS, + cxx_standard = {"": "c++17"}, +) +llvm.extra_target_compatible_with( + name = "llvm_toolchain_with_sysroot", + constraints = ["@//:cxx17"], ) # We can share the downloaded LLVM distribution with the first configuration. llvm.toolchain_root( @@ -177,5 +202,10 @@ llvm.toolchain( # distribution = "clang+llvm-17.0.6-x86_64-linux-gnu-ubuntu-22.04.tar.xz", exec_os = "linux", exec_arch = "amd64", + cxx_standard = {"": "c++17"}, +) +llvm.extra_target_compatible_with( + name = "llvm_toolchain_linux_exec", + constraints = ["@//:cxx17"], ) use_repo(llvm, "llvm_toolchain_linux_exec") diff --git a/tests/WORKSPACE b/tests/WORKSPACE index ab9181dd..f2a8af6f 100644 --- a/tests/WORKSPACE +++ b/tests/WORKSPACE @@ -57,6 +57,10 @@ llvm_toolchain( # TODO(siddharthab): Add test. llvm_toolchain( name = "llvm_toolchain_with_urls", + cxx_standard = {"": "c++17"}, + extra_target_compatible_with = { + "": ["@//:cxx17"], + }, llvm_versions = LLVM_VERSIONS, sha256 = { "": "38bc7f5563642e73e69ac5626724e206d6d539fbef653541b34cae0ba9c3f036", @@ -81,6 +85,10 @@ llvm_toolchain( # image base-devel as of the time of this writing (23 May 2022). llvm_toolchain( name = "llvm_toolchain_13_0_0", + cxx_standard = {"": "c++17"}, + extra_target_compatible_with = { + "": ["@//:cxx17"], + }, llvm_version = "13.0.0", ) @@ -96,6 +104,10 @@ llvm_register_toolchains_cxx20() llvm_toolchain( name = "llvm_toolchain_with_absolute_paths", absolute_paths = True, + cxx_standard = {"": "c++17"}, + extra_target_compatible_with = { + "": ["@//:cxx17"], + }, llvm_versions = LLVM_VERSIONS, # We can share the downloaded LLVM distribution with the first configuration. toolchain_roots = { @@ -106,6 +118,10 @@ llvm_toolchain( ## Toolchain example with system LLVM; tested in GitHub CI. llvm_toolchain( name = "llvm_toolchain_with_system_llvm", + cxx_standard = {"": "c++17"}, + extra_target_compatible_with = { + "": ["@//:cxx17"], + }, llvm_versions = LLVM_VERSIONS, # For this toolchain to work, the LLVM distribution archive would need to be unpacked here. toolchain_roots = {"": "/opt/llvm-16"}, @@ -129,6 +145,10 @@ filegroup( llvm_toolchain( name = "llvm_toolchain_with_sysroot", + cxx_standard = {"": "c++17"}, + extra_target_compatible_with = { + "": ["@//:cxx17"], + }, llvm_versions = LLVM_VERSIONS, sysroot = { "linux-x86_64": "@org_chromium_sysroot_linux_x64//:sysroot", @@ -145,10 +165,14 @@ bazel_skylib_workspace() llvm_toolchain( name = "llvm_toolchain_linux_exec", + cxx_standard = {"": "c++17"}, exec_arch = "amd64", # Option 2: # distribution = "clang+llvm-17.0.6-x86_64-linux-gnu-ubuntu-22.04.tar.xz", exec_os = "linux", + extra_target_compatible_with = { + "": ["@//:cxx17"], + }, llvm_version = "17.0.6", # Option 1: sha256 = {"": "884ee67d647d77e58740c1e645649e29ae9e8a6fe87c1376be0f3a30f3cc9ab3"}, @@ -247,3 +271,16 @@ http_archive( "https://ftp.pcre.org/pub/pcre/pcre-8.43.tar.gz", ], ) + +http_archive( + name = "platforms", + sha256 = "218efe8ee736d26a3572663b374a253c012b716d8af0c07e842e82f238a0a7ee", + urls = [ + "https://mirror.bazel.build/github.com/bazelbuild/platforms/releases/download/0.0.10/platforms-0.0.10.tar.gz", + "https://github.com/bazelbuild/platforms/releases/download/0.0.10/platforms-0.0.10.tar.gz", + ], +) + +load("@platforms//host:extension.bzl", "host_platform_repo") + +host_platform_repo(name = "host_platform") From 659db85963eb8099f383fad8c014026210803929 Mon Sep 17 00:00:00 2001 From: Joshua Kurland Date: Fri, 30 Aug 2024 11:02:37 +0100 Subject: [PATCH 7/7] Only build the c++20 test when the default toolchain is being used --- tests/BUILD.bazel | 6 ++++++ tests/MODULE.bazel | 30 ------------------------------ tests/WORKSPACE | 24 ------------------------ tests/scripts/run_tests.sh | 12 +++++++++++- 4 files changed, 17 insertions(+), 55 deletions(-) diff --git a/tests/BUILD.bazel b/tests/BUILD.bazel index c1c9f8fe..02ab2032 100644 --- a/tests/BUILD.bazel +++ b/tests/BUILD.bazel @@ -192,5 +192,11 @@ cc_test( size = "small", srcs = ["test_cxx_standard_main.cc"], args = ["202002"], + + # Since some platforms require special toolchains (e.g. llvm 13.0.0) this + # target won't build on those platforms unless we create a new toolchain per + # platform with c++20. So instead just only run this test on platforms that + # can use the default toolchain + tags = ["manual"], deps = [":test_cxx_standard_lib_transitioned"], ) diff --git a/tests/MODULE.bazel b/tests/MODULE.bazel index 7eb07566..1145b32c 100644 --- a/tests/MODULE.bazel +++ b/tests/MODULE.bazel @@ -111,11 +111,6 @@ llvm.toolchain( "darwin-aarch64": ["https://github.com/llvm/llvm-project/releases/download/llvmorg-15.0.7/clang+llvm-15.0.7-arm64-apple-darwin22.0.tar.xz"], "darwin-x86_64": ["https://github.com/llvm/llvm-project/releases/download/llvmorg-15.0.7/clang+llvm-15.0.7-x86_64-apple-darwin21.0.tar.xz"], }, - cxx_standard = {"": "c++17"}, -) -llvm.extra_target_compatible_with( - name = "llvm_toolchain_with_urls", - constraints = ["@//:cxx17"], ) use_repo(llvm, "llvm_toolchain_with_urls") @@ -126,11 +121,6 @@ use_repo(llvm, "llvm_toolchain_with_urls") llvm.toolchain( name = "llvm_toolchain_13_0_0", llvm_version = "13.0.0", - cxx_standard = {"": "c++17"}, -) -llvm.extra_target_compatible_with( - name = "llvm_toolchain_13_0_0", - constraints = ["@//:cxx17"], ) use_repo(llvm, "llvm_toolchain_13_0_0") @@ -139,11 +129,6 @@ llvm.toolchain( name = "llvm_toolchain_with_absolute_paths", absolute_paths = True, llvm_versions = LLVM_VERSIONS, - cxx_standard = {"": "c++17"}, -) -llvm.extra_target_compatible_with( - name = "llvm_toolchain_with_absolute_paths", - constraints = ["@//:cxx17"], ) # We can share the downloaded LLVM distribution with the first configuration. llvm.toolchain_root( @@ -156,11 +141,6 @@ use_repo(llvm, "llvm_toolchain_with_absolute_paths") llvm.toolchain( name = "llvm_toolchain_with_system_llvm", llvm_versions = LLVM_VERSIONS, - cxx_standard = {"": "c++17"}, -) -llvm.extra_target_compatible_with( - name = "llvm_toolchain_with_system_llvm", - constraints = ["@//:cxx17"], ) # For this toolchain to work, the LLVM distribution archive would need to be unpacked here. llvm.toolchain_root( @@ -173,11 +153,6 @@ use_repo(llvm, "llvm_toolchain_with_system_llvm") llvm.toolchain( name = "llvm_toolchain_with_sysroot", llvm_versions = LLVM_VERSIONS, - cxx_standard = {"": "c++17"}, -) -llvm.extra_target_compatible_with( - name = "llvm_toolchain_with_sysroot", - constraints = ["@//:cxx17"], ) # We can share the downloaded LLVM distribution with the first configuration. llvm.toolchain_root( @@ -202,10 +177,5 @@ llvm.toolchain( # distribution = "clang+llvm-17.0.6-x86_64-linux-gnu-ubuntu-22.04.tar.xz", exec_os = "linux", exec_arch = "amd64", - cxx_standard = {"": "c++17"}, -) -llvm.extra_target_compatible_with( - name = "llvm_toolchain_linux_exec", - constraints = ["@//:cxx17"], ) use_repo(llvm, "llvm_toolchain_linux_exec") diff --git a/tests/WORKSPACE b/tests/WORKSPACE index f2a8af6f..65c8ee52 100644 --- a/tests/WORKSPACE +++ b/tests/WORKSPACE @@ -57,10 +57,6 @@ llvm_toolchain( # TODO(siddharthab): Add test. llvm_toolchain( name = "llvm_toolchain_with_urls", - cxx_standard = {"": "c++17"}, - extra_target_compatible_with = { - "": ["@//:cxx17"], - }, llvm_versions = LLVM_VERSIONS, sha256 = { "": "38bc7f5563642e73e69ac5626724e206d6d539fbef653541b34cae0ba9c3f036", @@ -85,10 +81,6 @@ llvm_toolchain( # image base-devel as of the time of this writing (23 May 2022). llvm_toolchain( name = "llvm_toolchain_13_0_0", - cxx_standard = {"": "c++17"}, - extra_target_compatible_with = { - "": ["@//:cxx17"], - }, llvm_version = "13.0.0", ) @@ -104,10 +96,6 @@ llvm_register_toolchains_cxx20() llvm_toolchain( name = "llvm_toolchain_with_absolute_paths", absolute_paths = True, - cxx_standard = {"": "c++17"}, - extra_target_compatible_with = { - "": ["@//:cxx17"], - }, llvm_versions = LLVM_VERSIONS, # We can share the downloaded LLVM distribution with the first configuration. toolchain_roots = { @@ -118,10 +106,6 @@ llvm_toolchain( ## Toolchain example with system LLVM; tested in GitHub CI. llvm_toolchain( name = "llvm_toolchain_with_system_llvm", - cxx_standard = {"": "c++17"}, - extra_target_compatible_with = { - "": ["@//:cxx17"], - }, llvm_versions = LLVM_VERSIONS, # For this toolchain to work, the LLVM distribution archive would need to be unpacked here. toolchain_roots = {"": "/opt/llvm-16"}, @@ -145,10 +129,6 @@ filegroup( llvm_toolchain( name = "llvm_toolchain_with_sysroot", - cxx_standard = {"": "c++17"}, - extra_target_compatible_with = { - "": ["@//:cxx17"], - }, llvm_versions = LLVM_VERSIONS, sysroot = { "linux-x86_64": "@org_chromium_sysroot_linux_x64//:sysroot", @@ -165,14 +145,10 @@ bazel_skylib_workspace() llvm_toolchain( name = "llvm_toolchain_linux_exec", - cxx_standard = {"": "c++17"}, exec_arch = "amd64", # Option 2: # distribution = "clang+llvm-17.0.6-x86_64-linux-gnu-ubuntu-22.04.tar.xz", exec_os = "linux", - extra_target_compatible_with = { - "": ["@//:cxx17"], - }, llvm_version = "17.0.6", # Option 1: sha256 = {"": "884ee67d647d77e58740c1e645649e29ae9e8a6fe87c1376be0f3a30f3cc9ab3"}, diff --git a/tests/scripts/run_tests.sh b/tests/scripts/run_tests.sh index 694e5533..c50a346a 100755 --- a/tests/scripts/run_tests.sh +++ b/tests/scripts/run_tests.sh @@ -46,8 +46,18 @@ test_args=( "--linkopt=-Wl,-t" ) +targets=( + "//:all" +) +# :test_cxx_standard_is_20 builds with a version of the default toolchain, if +# we're trying to build with a different toolchain then it's likely the default +# toolchain won't work so :test_cxx_standard_is_20 won't build. +if [[ -z ${toolchain_name} ]]; then + targets+=("//:test_cxx_standard_is_20") +fi + "${bazel}" ${TEST_MIGRATION:+"--strict"} --bazelrc=/dev/null test \ - "${common_test_args[@]}" "${test_args[@]}" //:all + "${common_test_args[@]}" "${test_args[@]}" "${targets[@]}" # Note that the following flags are currently known to cause issues in migration tests: # --incompatible_disallow_struct_provider_syntax # https://github.com/bazelbuild/bazel/issues/7347