From 677833e817b23d288009cb5a387af9431c5bbc09 Mon Sep 17 00:00:00 2001 From: Tony Allevato Date: Mon, 5 Jun 2023 13:59:55 -0700 Subject: [PATCH] Only pass test runner deps to `swift_test` test runner compile PiperOrigin-RevId: 537974634 (cherry picked from commit 693b10c31fa6770a5d8170206908c4dd807f6dd9) Cherry-pick notes: Also a slight refactor to align better with future cherry-picks. Signed-off-by: Brentley Jones --- swift/swift_test.bzl | 40 ++++++++++++++++++++++------------------ 1 file changed, 22 insertions(+), 18 deletions(-) diff --git a/swift/swift_test.bzl b/swift/swift_test.bzl index ede3a350b..bc2dd47f6 100644 --- a/swift/swift_test.bzl +++ b/swift/swift_test.bzl @@ -40,6 +40,7 @@ load("//swift/internal:toolchain_utils.bzl", "use_swift_toolchain") load( "//swift/internal:utils.bzl", "expand_locations", + "get_providers", "include_developer_search_paths", ) load( @@ -344,6 +345,18 @@ def _swift_test_impl(ctx): if swizzle_absolute_xcttestsourcelocation: extra_link_deps.append(ctx.attr._swizzle_absolute_xcttestsourcelocation) + deps = list(ctx.attr.deps) + test_runner_deps = list(ctx.attr._test_runner_deps) + + # In test discovery mode (whether manual or by the Obj-C runtime), inject + # the test observer that prints the xUnit-style output for Bazel. Otherwise + # don't link this, because we don't want it to pull in link time + # dependencies on XCTest, which the test binary may not be using. + if discover_tests: + additional_link_deps = test_runner_deps + else: + additional_link_deps = [] + # We also need to collect nested providers from `SwiftCompilerPluginInfo` # since we support testing those. deps_cc_infos = [] @@ -351,7 +364,7 @@ def _swift_test_impl(ctx): deps_objc_infos = [] deps_swift_infos = [] additional_linking_contexts = [] - for dep in ctx.attr.deps: + for dep in deps: if CcInfo in dep: deps_cc_infos.append(dep[CcInfo]) deps_compilation_contexts.append(dep[CcInfo].compilation_context) @@ -365,21 +378,14 @@ def _swift_test_impl(ctx): additional_linking_contexts.append( plugin_info.cc_info.linking_context, ) + additional_linking_contexts.append(malloc_linking_context(ctx)) - test_runner_deps_swift_infos = [ctx.attr._test_observer[SwiftInfo]] + test_runner_deps_cc_infos = get_providers(test_runner_deps, CcInfo) + test_runner_deps_swift_infos = get_providers(test_runner_deps, SwiftInfo) srcs = ctx.files.srcs owner_symbol_graph_dir = None - all_deps = list(ctx.attr.deps) - - # In test discovery mode (whether manual or by the Obj-C runtime), inject - # the test observer that prints the xUnit-style output for Bazel. Otherwise - # don't link this, because we don't want it to pull in link time - # dependencies on XCTest, which the test binary may not be using. - if discover_tests: - all_deps.append(ctx.attr._test_observer) - module_name = ctx.attr.module_name if not module_name: module_name = swift_common.derive_module_name(ctx.label) @@ -463,7 +469,7 @@ def _swift_test_impl(ctx): ctx = ctx, # The generated test runner uses `@main`. additional_copts = ["-parse-as-library"], - cc_infos = deps_cc_infos, + cc_infos = test_runner_deps_cc_infos, feature_configuration = feature_configuration, include_dev_srch_paths = include_dev_srch_paths, module_name = module_name + "__GeneratedTestDiscoveryRunner", @@ -488,8 +494,6 @@ def _swift_test_impl(ctx): discovery_compile_result.supplemental_outputs, ) - additional_linking_contexts.append(malloc_linking_context(ctx)) - # If we need to run the test in an .xctest bundle, the binary must have # Mach-O type `MH_BUNDLE` instead of `MH_EXECUTE`. extra_linkopts = ["-Wl,-bundle"] if is_bundled else [] @@ -519,7 +523,7 @@ def _swift_test_impl(ctx): additional_linking_contexts = additional_linking_contexts, additional_outputs = additional_debug_outputs, compilation_outputs = compilation_outputs, - deps = all_deps, + deps = deps + additional_link_deps, feature_configuration = feature_configuration, module_contexts = module_contexts, name = name, @@ -654,10 +658,10 @@ standard executable binary that is invoked directly. ), executable = True, ), - "_test_observer": attr.label( - default = Label( + "_test_runner_deps": attr.label_list( + default = [ "@build_bazel_rules_swift//tools/test_observer", - ), + ], ), "_xctest_runner_template": attr.label( allow_single_file = True,