Skip to content

Commit 0fa9632

Browse files
committed
redo with upstream, leaving vfs too
1 parent b6a111e commit 0fa9632

File tree

4 files changed

+121
-0
lines changed

4 files changed

+121
-0
lines changed

swift/internal/BUILD

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ bzl_library(
4545
":debugging",
4646
":derived_files",
4747
":developer_dirs",
48+
":explicit_module_map_file",
4849
":feature_names",
4950
":features",
5051
":providers",
@@ -388,6 +389,11 @@ bzl_library(
388389
],
389390
)
390391

392+
bzl_library(
393+
name = "explicit_module_map_file",
394+
srcs = ["explicit_module_map_file.bzl"],
395+
)
396+
391397
bzl_library(
392398
name = "feature_names",
393399
srcs = ["feature_names.bzl"],

swift/internal/compiling.bzl

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ load(
2424
"run_toolchain_action",
2525
"swift_action_names",
2626
)
27+
load(":explicit_module_map_file.bzl", "write_explicit_swift_module_map_file")
2728
load(":derived_files.bzl", "derived_files")
2829
load(
2930
":feature_names.bzl",
@@ -62,6 +63,7 @@ load(
6263
"SWIFT_FEATURE_SUPPORTS_SYSTEM_MODULE_FLAG",
6364
"SWIFT_FEATURE_SYSTEM_MODULE",
6465
"SWIFT_FEATURE_USE_C_MODULES",
66+
"SWIFT_FEATURE_USE_EXPLICIT_SWIFT_MODULE_MAP",
6567
"SWIFT_FEATURE_USE_GLOBAL_INDEX_STORE",
6668
"SWIFT_FEATURE_USE_GLOBAL_MODULE_CACHE",
6769
"SWIFT_FEATURE_USE_OLD_DRIVER",
@@ -890,6 +892,29 @@ def compile_action_configs(
890892
),
891893
])
892894

895+
action_configs.extend([
896+
swift_toolchain_config.action_config(
897+
actions = [
898+
swift_action_names.COMPILE,
899+
swift_action_names.DERIVE_FILES,
900+
swift_action_names.DUMP_AST,
901+
],
902+
configurators = [_dependencies_swiftmodules_configurator],
903+
not_features = [SWIFT_FEATURE_USE_EXPLICIT_SWIFT_MODULE_MAP],
904+
),
905+
swift_toolchain_config.action_config(
906+
actions = [
907+
swift_action_names.COMPILE,
908+
swift_action_names.DERIVE_FILES,
909+
swift_action_names.DUMP_AST,
910+
],
911+
configurators = [
912+
_explicit_swift_module_map_configurator,
913+
],
914+
features = [SWIFT_FEATURE_USE_EXPLICIT_SWIFT_MODULE_MAP],
915+
),
916+
])
917+
893918
#### Search paths for framework dependencies
894919
action_configs.extend([
895920
swift_toolchain_config.action_config(
@@ -1620,6 +1645,21 @@ def _dependencies_swiftmodules_vfsoverlay_configurator(prerequisites, args):
16201645
inputs = swiftmodules + [prerequisites.vfsoverlay_file],
16211646
)
16221647

1648+
def _explicit_swift_module_map_configurator(prerequisites, args):
1649+
"""Adds the explicit Swift module map file to the command line."""
1650+
args.add_all(
1651+
[
1652+
"-explicit-swift-module-map-file",
1653+
prerequisites.explicit_swift_module_map_file,
1654+
],
1655+
before_each = "-Xfrontend",
1656+
)
1657+
return swift_toolchain_config.config_result(
1658+
inputs = prerequisites.transitive_swiftmodules + [
1659+
prerequisites.explicit_swift_module_map_file,
1660+
],
1661+
)
1662+
16231663
def _module_name_configurator(prerequisites, args):
16241664
"""Adds the module name flag to the command line."""
16251665
args.add("-module-name", prerequisites.module_name)
@@ -2081,11 +2121,32 @@ def compile(
20812121
else:
20822122
vfsoverlay_file = None
20832123

2124+
if is_feature_enabled(
2125+
feature_configuration = feature_configuration,
2126+
feature_name = SWIFT_FEATURE_USE_EXPLICIT_SWIFT_MODULE_MAP,
2127+
):
2128+
if vfsoverlay_file:
2129+
fail("Cannot use both `swift.vfsoverlay` and `swift.use_explicit_swift_module_map` features at the same time.")
2130+
2131+
# Generate the JSON file that contains the manifest of Swift
2132+
# dependencies.
2133+
explicit_swift_module_map_file = actions.declare_file(
2134+
"{}.swift-explicit-module-map.json".format(target_name),
2135+
)
2136+
write_explicit_swift_module_map_file(
2137+
actions = actions,
2138+
explicit_swift_module_map_file = explicit_swift_module_map_file,
2139+
module_contexts = transitive_modules,
2140+
)
2141+
else:
2142+
explicit_swift_module_map_file = None
2143+
20842144
prerequisites = struct(
20852145
additional_inputs = additional_inputs,
20862146
bin_dir = feature_configuration._bin_dir,
20872147
cc_compilation_context = merged_providers.cc_info.compilation_context,
20882148
defines = sets.to_list(defines_set),
2149+
explicit_swift_module_map_file = explicit_swift_module_map_file,
20892150
developer_dirs = swift_toolchain.developer_dirs,
20902151
genfiles_dir = feature_configuration._genfiles_dir,
20912152
is_swift = True,
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# Copyright 2022 The Bazel Authors. All rights reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
"""Generates the JSON manifest used to pass Swift modules to the compiler."""
16+
17+
def write_explicit_swift_module_map_file(
18+
*,
19+
actions,
20+
explicit_swift_module_map_file,
21+
module_contexts):
22+
"""Generates the JSON-formatted explicit module map file.
23+
24+
This file is a manifest that contains the path information for all the
25+
Swift modules from dependencies that are needed to compile a particular
26+
module.
27+
28+
Args:
29+
actions: The object used to register actions.
30+
explicit_swift_module_map_file: A `File` to which the generated JSON
31+
will be written.
32+
module_contexts: A list of module contexts that provide the Swift
33+
dependencies for the compilation.
34+
"""
35+
module_descriptions = []
36+
37+
for module_context in module_contexts:
38+
if not module_context.swift:
39+
continue
40+
41+
swift_context = module_context.swift
42+
module_description = {"moduleName": module_context.name}
43+
if swift_context.swiftmodule:
44+
module_description["modulePath"] = swift_context.swiftmodule.path
45+
module_descriptions.append(module_description)
46+
47+
actions.write(
48+
content = json.encode(module_descriptions),
49+
output = explicit_swift_module_map_file,
50+
)

swift/internal/feature_names.bzl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,10 @@ SWIFT_FEATURE_REWRITE_GENERATED_HEADER = "swift.rewrite_generated_header"
166166
# them.
167167
SWIFT_FEATURE_USE_C_MODULES = "swift.use_c_modules"
168168

169+
# If enabled, Swift modules for dependencies will be passed to the compiler
170+
# using a JSON file instead of `-I` search paths.
171+
SWIFT_FEATURE_USE_EXPLICIT_SWIFT_MODULE_MAP = "swift.use_explicit_swift_module_map"
172+
169173
# If enabled, Swift compilation actions will use the same global Clang module
170174
# cache used by Objective-C compilation actions. This is disabled by default
171175
# because under some circumstances Clang module cache corruption can cause the

0 commit comments

Comments
 (0)