Skip to content

Commit

Permalink
enable cxx interop in sdk pcm compilation
Browse files Browse the repository at this point in the history
Summary:
Traverse the correct SDK dep tree when cxx interop is enabled, and add handling for the std and Darwin modules.

This adds very basic support for cxx interop with explicit modules, there are remaining issues importing parts of the cxx stdlib.

Reviewed By: drodriguez

Differential Revision: D60546682

fbshipit-source-id: 049c6b8a0679bf5605ad0a3faa58c80645fa7b97
  • Loading branch information
rmaz authored and facebook-github-bot committed Aug 6, 2024
1 parent 273e44d commit caadef5
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 3 deletions.
7 changes: 5 additions & 2 deletions prelude/apple/swift/swift_compilation.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,9 @@ SwiftDebugInfo = record(
shared = list[ArtifactTSet],
)

REQUIRED_SDK_MODULES = ["Swift", "SwiftOnoneSupport", "Darwin", "_Concurrency", "_StringProcessing"]
_REQUIRED_SDK_MODULES = ["Swift", "SwiftOnoneSupport", "Darwin", "_Concurrency", "_StringProcessing"]

_REQUIRED_SDK_CXX_MODULES = _REQUIRED_SDK_MODULES + ["std"]

def get_swift_anonymous_targets(ctx: AnalysisContext, get_apple_library_providers: typing.Callable) -> Promise:
swift_cxx_flags = get_swift_cxx_flags(ctx)
Expand All @@ -141,7 +143,7 @@ def get_swift_anonymous_targets(ctx: AnalysisContext, get_apple_library_provider
# all transitive deps will be compiled recursively.
direct_uncompiled_sdk_deps = get_uncompiled_sdk_deps(
ctx.attrs.sdk_modules,
REQUIRED_SDK_MODULES,
_REQUIRED_SDK_CXX_MODULES if ctx.attrs.enable_cxx_interop else _REQUIRED_SDK_MODULES,
ctx.attrs._apple_toolchain[AppleToolchainInfo].swift_toolchain_info,
)

Expand All @@ -157,6 +159,7 @@ def get_swift_anonymous_targets(ctx: AnalysisContext, get_apple_library_provider
# passing apple_library's cxx flags through that must be used for all downward PCM compilations.
sdk_pcm_targets = get_swift_sdk_pcm_anon_targets(
ctx,
ctx.attrs.enable_cxx_interop,
direct_uncompiled_sdk_deps,
swift_cxx_flags,
)
Expand Down
3 changes: 3 additions & 0 deletions prelude/apple/swift/swift_pcm_compilation.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ def get_swift_pcm_anon_targets(
deps = [
{
"dep": uncompiled_dep,
"enable_cxx_interop": ctx.attrs.enable_cxx_interop,
"name": uncompiled_dep.label,
"swift_cxx_args": swift_cxx_args,
"_apple_toolchain": ctx.attrs._apple_toolchain,
Expand Down Expand Up @@ -196,6 +197,7 @@ def _swift_pcm_compilation_impl(ctx: AnalysisContext) -> [Promise, list[Provider
# Recursively compiling SDK's Clang dependencies
sdk_pcm_deps_anon_targets = get_swift_sdk_pcm_anon_targets(
ctx,
ctx.attrs.enable_cxx_interop,
direct_uncompiled_sdk_deps,
ctx.attrs.swift_cxx_args,
)
Expand All @@ -212,6 +214,7 @@ _swift_pcm_compilation = rule(
impl = _swift_pcm_compilation_impl,
attrs = {
"dep": attrs.dep(),
"enable_cxx_interop": attrs.bool(),
"swift_cxx_args": attrs.list(attrs.string(), default = []),
"_apple_toolchain": attrs.dep(),
},
Expand Down
20 changes: 19 additions & 1 deletion prelude/apple/swift/swift_sdk_pcm_compilation.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -90,13 +90,15 @@ def _add_sdk_module_search_path(cmd, uncompiled_sdk_module_info, apple_toolchain

def get_swift_sdk_pcm_anon_targets(
ctx: AnalysisContext,
enable_cxx_interop: bool,
uncompiled_sdk_deps: list[Dependency],
swift_cxx_args: list[str]):
# We include the Swift deps here too as we need
# to include their transitive clang deps.
return [
(_swift_sdk_pcm_compilation, {
"dep": module_dep,
"enable_cxx_interop": enable_cxx_interop,
"name": module_dep.label,
"swift_cxx_args": swift_cxx_args,
"_apple_toolchain": ctx.attrs._apple_toolchain,
Expand Down Expand Up @@ -172,6 +174,19 @@ def _swift_sdk_pcm_compilation_impl(ctx: AnalysisContext) -> [Promise, list[Prov

cmd.add(ctx.attrs.swift_cxx_args)

if module_name == "Darwin" and ctx.attrs.enable_cxx_interop:
# The Darwin module requires special handling with cxx interop
# to ensure that it does not include the c++ headers. The module
# is marked with [no_undeclared_includes] which will prevent
# including headers declared in other modulemaps. So that the
# cxx modules are visible we need to pass the module map path
# without the corresponding module file, which we cannot build
# until the Darwin module is available.
cmd.add([
"-Xcc",
cmd_args(swift_toolchain.sdk_path, format = "-fmodule-map-file={}/usr/include/c++/v1/module.modulemap"),
])

_add_sdk_module_search_path(cmd, uncompiled_sdk_module_info, apple_toolchain)

ctx.actions.run(
Expand Down Expand Up @@ -226,9 +241,11 @@ def _swift_sdk_pcm_compilation_impl(ctx: AnalysisContext) -> [Promise, list[Prov
]

# Compile the transitive clang module deps of this target.
deps = ctx.attrs.dep[SdkUncompiledModuleInfo].cxx_deps if ctx.attrs.enable_cxx_interop else ctx.attrs.dep[SdkUncompiledModuleInfo].deps
clang_module_deps = get_swift_sdk_pcm_anon_targets(
ctx,
ctx.attrs.dep[SdkUncompiledModuleInfo].deps,
ctx.attrs.enable_cxx_interop,
deps,
ctx.attrs.swift_cxx_args,
)

Expand All @@ -238,6 +255,7 @@ _swift_sdk_pcm_compilation = rule(
impl = _swift_sdk_pcm_compilation_impl,
attrs = {
"dep": attrs.dep(),
"enable_cxx_interop": attrs.bool(),
"swift_cxx_args": attrs.list(attrs.string(), default = []),
"_apple_toolchain": attrs.dep(),
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ def _swift_interface_compilation_impl(ctx: AnalysisContext) -> [Promise, list[Pr
module_info = ctx.attrs.dep[SdkUncompiledModuleInfo]
clang_module_deps = get_swift_sdk_pcm_anon_targets(
ctx,
False,
module_info.deps,
["-target", module_info.target],
)
Expand Down

0 comments on commit caadef5

Please sign in to comment.