Skip to content

Commit 9079e72

Browse files
authored
Upstream: Add a feature to allow certain targets to ignore the implicit dependencies from the toolchain. (#1518)
If enabled, the toolchain's implicit dependencies will not be used when compiling Swift or Clang modules. This should only be used when building the toolchain itself. Upstreams: c29e0ab
1 parent 2e812b5 commit 9079e72

File tree

6 files changed

+88
-15
lines changed

6 files changed

+88
-15
lines changed

swift/BUILD

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,10 +161,11 @@ bzl_library(
161161
srcs = ["swift_import.bzl"],
162162
deps = [
163163
":providers",
164+
":swift_clang_module_aspect",
164165
"//swift/internal:attrs",
165166
"//swift/internal:compiling",
166167
"//swift/internal:features",
167-
"//swift/internal:linking",
168+
"//swift/internal:providers",
168169
"//swift/internal:toolchain_utils",
169170
"//swift/internal:utils",
170171
"@bazel_skylib//lib:dicts",

swift/internal/BUILD

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,7 @@ bzl_library(
177177
":developer_dirs",
178178
":feature_names",
179179
":features",
180+
":utils",
180181
"//swift:providers",
181182
],
182183
)

swift/internal/compiling.bzl

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,8 @@ load(
7171
":utils.bzl",
7272
"compact",
7373
"compilation_context_for_explicit_module_compilation",
74+
"get_clang_implicit_deps",
75+
"get_swift_implicit_deps",
7476
"merge_compilation_contexts",
7577
"owner_relative_path",
7678
"struct_fields",
@@ -182,16 +184,18 @@ def compile_module_interface(
182184
"""
183185
swiftmodule_file = actions.declare_file("{}.swiftmodule".format(module_name))
184186

187+
implicit_swift_infos, implicit_cc_infos = get_swift_implicit_deps(
188+
feature_configuration = feature_configuration,
189+
swift_toolchain = swift_toolchain,
190+
)
185191
merged_compilation_context = merge_compilation_contexts(
186192
transitive_compilation_contexts = compilation_contexts + [
187193
cc_info.compilation_context
188-
for cc_info in swift_toolchain.implicit_deps_providers.cc_infos
194+
for cc_info in implicit_cc_infos
189195
],
190196
)
191197
merged_swift_info = SwiftInfo(
192-
swift_infos = (
193-
swift_infos + swift_toolchain.implicit_deps_providers.swift_infos
194-
),
198+
swift_infos = swift_infos + implicit_swift_infos,
195199
)
196200

197201
# Flattening this `depset` is necessary because we need to extract the
@@ -461,10 +465,12 @@ def compile(
461465
user_swift_infos = swift_infos + private_swift_infos,
462466
)
463467

468+
implicit_swift_infos, implicit_cc_infos = get_swift_implicit_deps(
469+
feature_configuration = feature_configuration,
470+
swift_toolchain = swift_toolchain,
471+
)
464472
all_swift_infos = (
465-
swift_infos_to_propagate +
466-
private_swift_infos +
467-
swift_toolchain.implicit_deps_providers.swift_infos
473+
swift_infos_to_propagate + private_swift_infos + implicit_swift_infos
468474
)
469475
merged_swift_info = SwiftInfo(swift_infos = all_swift_infos)
470476

@@ -542,7 +548,7 @@ def compile(
542548
]
543549
merged_cc_info = cc_common.merge_cc_infos(
544550
cc_infos = cc_infos + private_cc_infos +
545-
swift_toolchain.implicit_deps_providers.cc_infos,
551+
implicit_cc_infos,
546552
)
547553

548554
transitive_swiftmodules = []
@@ -932,18 +938,19 @@ def _precompile_clang_module(
932938
)
933939

934940
if not is_swift_generated_header:
935-
implicit_swift_infos = (
936-
swift_toolchain.clang_implicit_deps_providers.swift_infos
941+
implicit_swift_infos, implicit_cc_infos = get_clang_implicit_deps(
942+
feature_configuration = feature_configuration,
943+
swift_toolchain = swift_toolchain,
937944
)
938945
cc_compilation_context = merge_compilation_contexts(
939946
direct_compilation_contexts = [cc_compilation_context],
940947
transitive_compilation_contexts = [
941948
cc_info.compilation_context
942-
for cc_info in swift_toolchain.clang_implicit_deps_providers.cc_infos
949+
for cc_info in implicit_cc_infos
943950
],
944951
)
945952
else:
946-
implicit_swift_infos = []
953+
implicit_swift_infos, _ = [], []
947954

948955
if not is_swift_generated_header and implicit_swift_infos:
949956
swift_infos = list(swift_infos)

swift/internal/feature_names.bzl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,11 @@ SWIFT_FEATURE_OPT = "swift.opt"
3838
# Swift 6 features even before switching to a Swift 6 compiler.
3939
SWIFT_FEATURE_ENABLE_V6 = "swift.enable_v6"
4040

41+
# If enabled, the toolchain's implicit dependencies will not be used when
42+
# compiling Swift or Clang modules. This should only be used when building the
43+
# toolchain itself.
44+
SWIFT_FEATURE_NO_IMPLICIT_DEPS = "swift.no_implicit_deps"
45+
4146
# If True, transitive C headers will be always be passed as inputs to Swift
4247
# compilation actions, even when building with explicit modules.
4348
SWIFT_FEATURE_HEADERS_ALWAYS_ACTION_INPUTS = "swift.headers_always_action_inputs"

swift/internal/linking.bzl

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ load(
3535
"get_cc_feature_configuration",
3636
"is_feature_enabled",
3737
)
38+
load(":utils.bzl", "get_swift_implicit_deps")
3839

3940
def configure_features_for_binary(
4041
*,
@@ -244,9 +245,13 @@ def create_linking_context_from_compilation_outputs(
244245
context to be propagated by the caller's `CcInfo` provider and the
245246
artifact representing the library that was linked, respectively.
246247
"""
248+
_, implicit_cc_infos = get_swift_implicit_deps(
249+
feature_configuration = feature_configuration,
250+
swift_toolchain = swift_toolchain,
251+
)
247252
extra_linking_contexts = [
248253
cc_info.linking_context
249-
for cc_info in swift_toolchain.implicit_deps_providers.cc_infos
254+
for cc_info in implicit_cc_infos
250255
]
251256

252257
debugging_linking_context = _create_embedded_debugging_linking_context(
@@ -481,9 +486,13 @@ def register_link_binary_action(
481486

482487
# Collect linking contexts from any of the toolchain's implicit
483488
# dependencies.
489+
_, implicit_cc_infos = get_swift_implicit_deps(
490+
feature_configuration = feature_configuration,
491+
swift_toolchain = swift_toolchain,
492+
)
484493
linking_contexts.extend([
485494
cc_info.linking_context
486-
for cc_info in swift_toolchain.implicit_deps_providers.cc_infos
495+
for cc_info in implicit_cc_infos
487496
])
488497

489498
return cc_common.link(

swift/internal/utils.bzl

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
load("@bazel_skylib//lib:paths.bzl", "paths")
1818
load("//swift:providers.bzl", "SwiftInfo")
19+
load(":feature_names.bzl", "SWIFT_FEATURE_NO_IMPLICIT_DEPS")
20+
load(":features.bzl", "is_feature_enabled")
1921

2022
def collect_implicit_deps_providers(targets, additional_cc_infos = []):
2123
"""Returns a struct with important providers from a list of implicit deps.
@@ -393,3 +395,51 @@ def include_developer_search_paths(attr):
393395
"always_include_developer_search_paths",
394396
False,
395397
)
398+
399+
def get_swift_implicit_deps(*, feature_configuration, swift_toolchain):
400+
"""Returns the Swift and C++ providers for implicit Swift dependencies.
401+
402+
Args:
403+
feature_configuration: A feature configuration obtained from
404+
`swift_common.configure_features`. If this feature configuration is
405+
such that implicit dependencies should be ignored, this function
406+
returns an empty list for both providers.
407+
swift_toolchain: The `SwiftToolchainInfo` provider of the toolchain.
408+
409+
Returns:
410+
A tuple `(list[SwiftInfo], list[CcInfo])`.
411+
"""
412+
if is_feature_enabled(
413+
feature_configuration = feature_configuration,
414+
feature_name = SWIFT_FEATURE_NO_IMPLICIT_DEPS,
415+
):
416+
return [], []
417+
else:
418+
return (
419+
swift_toolchain.implicit_deps_providers.swift_infos,
420+
swift_toolchain.implicit_deps_providers.cc_infos,
421+
)
422+
423+
def get_clang_implicit_deps(*, feature_configuration, swift_toolchain):
424+
"""Returns the Swift and C++ providers for implicit Clang dependencies.
425+
426+
Args:
427+
feature_configuration: A feature configuration obtained from
428+
`swift_common.configure_features`. If this feature configuration is
429+
such that implicit dependencies should be ignored, this function
430+
returns an empty list for both providers.
431+
swift_toolchain: The `SwiftToolchainInfo` provider of the toolchain.
432+
433+
Returns:
434+
A tuple `(list[SwiftInfo], list[CcInfo])`.
435+
"""
436+
if is_feature_enabled(
437+
feature_configuration = feature_configuration,
438+
feature_name = SWIFT_FEATURE_NO_IMPLICIT_DEPS,
439+
):
440+
return [], []
441+
else:
442+
return (
443+
swift_toolchain.clang_implicit_deps_providers.swift_infos,
444+
swift_toolchain.clang_implicit_deps_providers.cc_infos,
445+
)

0 commit comments

Comments
 (0)