Skip to content
This repository was archived by the owner on Jan 25, 2024. It is now read-only.

Commit cc15b6e

Browse files
allevatoswiple-rules-gardener
authored andcommitted
Include the headers from direct dependencies of a Swift target when compiling the explicit module for its generated header.
PiperOrigin-RevId: 372240650
1 parent d8a381c commit cc15b6e

File tree

3 files changed

+66
-32
lines changed

3 files changed

+66
-32
lines changed

swift/internal/compiling.bzl

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ load(
6666
":utils.bzl",
6767
"collect_cc_libraries",
6868
"compact",
69+
"compilation_context_for_explicit_module_compilation",
6970
"get_providers",
7071
"struct_fields",
7172
)
@@ -1494,12 +1495,18 @@ def compile(
14941495
feature_configuration = feature_configuration,
14951496
feature_name = SWIFT_FEATURE_NO_GENERATED_MODULE_MAP,
14961497
):
1498+
compilation_context_to_compile = (
1499+
compilation_context_for_explicit_module_compilation(
1500+
compilation_contexts = [cc_common.create_compilation_context(
1501+
headers = depset([compile_outputs.generated_header_file]),
1502+
)],
1503+
deps = deps,
1504+
)
1505+
)
14971506
precompiled_module = _precompile_clang_module(
14981507
actions = actions,
14991508
bin_dir = bin_dir,
1500-
cc_compilation_context = cc_common.create_compilation_context(
1501-
headers = depset([compile_outputs.generated_header_file]),
1502-
),
1509+
cc_compilation_context = compilation_context_to_compile,
15031510
feature_configuration = feature_configuration,
15041511
genfiles_dir = genfiles_dir,
15051512
is_swift_generated_header = True,

swift/internal/swift_clang_module_aspect.bzl

Lines changed: 11 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,11 @@ load(
3333
"create_module",
3434
"create_swift_info",
3535
)
36-
load(":utils.bzl", "get_providers")
36+
load(
37+
":utils.bzl",
38+
"compilation_context_for_explicit_module_compilation",
39+
"get_providers",
40+
)
3741

3842
_MULTIPLE_TARGET_ASPECT_ATTRS = [
3943
"deps",
@@ -456,34 +460,12 @@ def _handle_module(
456460
# a framework import rule. For now, we won't support compiling those as
457461
# explicit modules; fix this.
458462
if module_name:
459-
# We only need to propagate the information from the compilation
460-
# contexts, but we can't merge those directly; we can only merge
461-
# `CcInfo` objects. So we "unwrap" the compilation context from each
462-
# provider and then "rewrap" it in a new provider that lacks the linking
463-
# context so that our merge operation does less work.
464-
target_and_deps_cc_infos = [
465-
CcInfo(compilation_context = compilation_context),
466-
]
467-
for dep in getattr(attr, "deps", []):
468-
if CcInfo in dep:
469-
target_and_deps_cc_infos.append(
470-
CcInfo(
471-
compilation_context = dep[CcInfo].compilation_context,
472-
),
473-
)
474-
if apple_common.Objc in dep:
475-
target_and_deps_cc_infos.append(
476-
CcInfo(
477-
compilation_context = cc_common.create_compilation_context(
478-
includes = dep[apple_common.Objc].strict_include,
479-
),
480-
),
481-
)
482-
483-
compilation_context_to_compile = cc_common.merge_cc_infos(
484-
direct_cc_infos = target_and_deps_cc_infos,
485-
).compilation_context
486-
463+
compilation_context_to_compile = (
464+
compilation_context_for_explicit_module_compilation(
465+
compilation_contexts = [compilation_context],
466+
deps = getattr(attr, "deps", []),
467+
)
468+
)
487469
precompiled_module = precompile_clang_module(
488470
actions = aspect_ctx.actions,
489471
bin_dir = aspect_ctx.bin_dir,

swift/internal/utils.bzl

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,51 @@ def create_cc_info(
168168
direct_cc_infos = local_cc_infos,
169169
)
170170

171+
def compilation_context_for_explicit_module_compilation(
172+
compilation_contexts,
173+
deps):
174+
"""Returns a compilation context suitable for compiling an explicit module.
175+
176+
Args:
177+
compilation_contexts: `CcCompilationContext`s that provide information
178+
about headers and include paths for the target being compiled.
179+
deps: Direct dependencies of the target being compiled.
180+
181+
Returns:
182+
A `CcCompilationContext` containing information needed when compiling an
183+
explicit module, such as the headers and search paths of direct
184+
dependencies (since Clang needs to find those on the file system in
185+
order to map them to a module).
186+
"""
187+
cc_infos = [
188+
CcInfo(compilation_context = compilation_context)
189+
for compilation_context in compilation_contexts
190+
]
191+
192+
for dep in deps:
193+
if CcInfo in dep:
194+
# TODO(b/179692096): We only need to propagate the information from
195+
# the compilation contexts, but we can't merge those directly; we
196+
# can only merge `CcInfo` objects. So we "unwrap" the compilation
197+
# context from each provider and then "rewrap" it in a new provider
198+
# that lacks the linking context so that our merge operation does
199+
# less work.
200+
cc_infos.append(
201+
CcInfo(compilation_context = dep[CcInfo].compilation_context),
202+
)
203+
if apple_common.Objc in dep:
204+
cc_infos.append(
205+
CcInfo(
206+
compilation_context = cc_common.create_compilation_context(
207+
includes = dep[apple_common.Objc].strict_include,
208+
),
209+
),
210+
)
211+
212+
return cc_common.merge_cc_infos(
213+
direct_cc_infos = cc_infos,
214+
).compilation_context
215+
171216
def expand_locations(ctx, values, targets = []):
172217
"""Expands the `$(location)` placeholders in each of the given values.
173218

0 commit comments

Comments
 (0)