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

Commit

Permalink
Include the headers from direct dependencies of a Swift target when c…
Browse files Browse the repository at this point in the history
…ompiling the explicit module for its generated header.

PiperOrigin-RevId: 372240650
  • Loading branch information
allevato authored and swiple-rules-gardener committed May 6, 2021
1 parent d8a381c commit cc15b6e
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 32 deletions.
13 changes: 10 additions & 3 deletions swift/internal/compiling.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ load(
":utils.bzl",
"collect_cc_libraries",
"compact",
"compilation_context_for_explicit_module_compilation",
"get_providers",
"struct_fields",
)
Expand Down Expand Up @@ -1494,12 +1495,18 @@ def compile(
feature_configuration = feature_configuration,
feature_name = SWIFT_FEATURE_NO_GENERATED_MODULE_MAP,
):
compilation_context_to_compile = (
compilation_context_for_explicit_module_compilation(
compilation_contexts = [cc_common.create_compilation_context(
headers = depset([compile_outputs.generated_header_file]),
)],
deps = deps,
)
)
precompiled_module = _precompile_clang_module(
actions = actions,
bin_dir = bin_dir,
cc_compilation_context = cc_common.create_compilation_context(
headers = depset([compile_outputs.generated_header_file]),
),
cc_compilation_context = compilation_context_to_compile,
feature_configuration = feature_configuration,
genfiles_dir = genfiles_dir,
is_swift_generated_header = True,
Expand Down
40 changes: 11 additions & 29 deletions swift/internal/swift_clang_module_aspect.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,11 @@ load(
"create_module",
"create_swift_info",
)
load(":utils.bzl", "get_providers")
load(
":utils.bzl",
"compilation_context_for_explicit_module_compilation",
"get_providers",
)

_MULTIPLE_TARGET_ASPECT_ATTRS = [
"deps",
Expand Down Expand Up @@ -456,34 +460,12 @@ def _handle_module(
# a framework import rule. For now, we won't support compiling those as
# explicit modules; fix this.
if module_name:
# We only need to propagate the information from the compilation
# contexts, but we can't merge those directly; we can only merge
# `CcInfo` objects. So we "unwrap" the compilation context from each
# provider and then "rewrap" it in a new provider that lacks the linking
# context so that our merge operation does less work.
target_and_deps_cc_infos = [
CcInfo(compilation_context = compilation_context),
]
for dep in getattr(attr, "deps", []):
if CcInfo in dep:
target_and_deps_cc_infos.append(
CcInfo(
compilation_context = dep[CcInfo].compilation_context,
),
)
if apple_common.Objc in dep:
target_and_deps_cc_infos.append(
CcInfo(
compilation_context = cc_common.create_compilation_context(
includes = dep[apple_common.Objc].strict_include,
),
),
)

compilation_context_to_compile = cc_common.merge_cc_infos(
direct_cc_infos = target_and_deps_cc_infos,
).compilation_context

compilation_context_to_compile = (
compilation_context_for_explicit_module_compilation(
compilation_contexts = [compilation_context],
deps = getattr(attr, "deps", []),
)
)
precompiled_module = precompile_clang_module(
actions = aspect_ctx.actions,
bin_dir = aspect_ctx.bin_dir,
Expand Down
45 changes: 45 additions & 0 deletions swift/internal/utils.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,51 @@ def create_cc_info(
direct_cc_infos = local_cc_infos,
)

def compilation_context_for_explicit_module_compilation(
compilation_contexts,
deps):
"""Returns a compilation context suitable for compiling an explicit module.
Args:
compilation_contexts: `CcCompilationContext`s that provide information
about headers and include paths for the target being compiled.
deps: Direct dependencies of the target being compiled.
Returns:
A `CcCompilationContext` containing information needed when compiling an
explicit module, such as the headers and search paths of direct
dependencies (since Clang needs to find those on the file system in
order to map them to a module).
"""
cc_infos = [
CcInfo(compilation_context = compilation_context)
for compilation_context in compilation_contexts
]

for dep in deps:
if CcInfo in dep:
# TODO(b/179692096): We only need to propagate the information from
# the compilation contexts, but we can't merge those directly; we
# can only merge `CcInfo` objects. So we "unwrap" the compilation
# context from each provider and then "rewrap" it in a new provider
# that lacks the linking context so that our merge operation does
# less work.
cc_infos.append(
CcInfo(compilation_context = dep[CcInfo].compilation_context),
)
if apple_common.Objc in dep:
cc_infos.append(
CcInfo(
compilation_context = cc_common.create_compilation_context(
includes = dep[apple_common.Objc].strict_include,
),
),
)

return cc_common.merge_cc_infos(
direct_cc_infos = cc_infos,
).compilation_context

def expand_locations(ctx, values, targets = []):
"""Expands the `$(location)` placeholders in each of the given values.
Expand Down

0 comments on commit cc15b6e

Please sign in to comment.