Skip to content

Commit 1666670

Browse files
allevatoswiple-rules-gardener
authored andcommitted
Remove VFS functionality (which we never used) and replace it with the explicit Swift module JSON file supported by Swift 5.6 (but not enabled on any toolchain yet).
This file is actually supported in earlier versions of Swift, but 5.6 is the first version where it doesn't require *all* modules (i.e., system modules) to be specified explicitly as well. PiperOrigin-RevId: 456822150
1 parent 0bb2ada commit 1666670

File tree

6 files changed

+97
-120
lines changed

6 files changed

+97
-120
lines changed

swift/internal/BUILD

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,12 @@ bzl_library(
4343
deps = [
4444
":action_names",
4545
":actions",
46+
":explicit_module_map_file",
4647
":feature_names",
4748
":features",
4849
":module_maps",
4950
":providers",
5051
":utils",
51-
":vfsoverlay",
5252
":wmo",
5353
"@bazel_skylib//lib:paths",
5454
"@bazel_skylib//lib:sets",
@@ -67,6 +67,11 @@ bzl_library(
6767
],
6868
)
6969

70+
bzl_library(
71+
name = "explicit_module_map_file",
72+
srcs = ["explicit_module_map_file.bzl"],
73+
)
74+
7075
bzl_library(
7176
name = "feature_names",
7277
srcs = ["feature_names.bzl"],
@@ -186,11 +191,6 @@ bzl_library(
186191
],
187192
)
188193

189-
bzl_library(
190-
name = "vfsoverlay",
191-
srcs = ["vfsoverlay.bzl"],
192-
)
193-
194194
bzl_library(
195195
name = "wmo",
196196
srcs = ["wmo.bzl"],

swift/internal/compiling.bzl

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ load(
2323
"SWIFT_ACTION_PRECOMPILE_C_MODULE",
2424
)
2525
load(":actions.bzl", "is_action_enabled", "run_toolchain_action")
26+
load(":explicit_module_map_file.bzl", "write_explicit_swift_module_map_file")
2627
load(
2728
":feature_names.bzl",
2829
"SWIFT_FEATURE_EMIT_C_MODULE",
@@ -31,7 +32,7 @@ load(
3132
"SWIFT_FEATURE_OPT",
3233
"SWIFT_FEATURE_OPT_USES_WMO",
3334
"SWIFT_FEATURE_SUPPORTS_LIBRARY_EVOLUTION",
34-
"SWIFT_FEATURE_VFSOVERLAY",
35+
"SWIFT_FEATURE_USE_EXPLICIT_SWIFT_MODULE_MAP",
3536
"SWIFT_FEATURE__NUM_THREADS_1_IN_SWIFTCOPTS",
3637
"SWIFT_FEATURE__WMO_IN_SWIFTCOPTS",
3738
)
@@ -57,13 +58,8 @@ load(
5758
"owner_relative_path",
5859
"struct_fields",
5960
)
60-
load(":vfsoverlay.bzl", "write_vfsoverlay")
6161
load(":wmo.bzl", "find_num_threads_flag_value", "is_wmo_manually_requested")
6262

63-
# VFS root where all .swiftmodule files will be placed when
64-
# SWIFT_FEATURE_VFSOVERLAY is enabled.
65-
_SWIFTMODULES_VFS_ROOT = "/__build_bazel_rules_swift/swiftmodules"
66-
6763
def _module_name_safe(string):
6864
"""Returns a transformation of `string` that is safe for module names."""
6965
result = ""
@@ -275,30 +271,29 @@ def compile(
275271
sets.make(swift_module.defines),
276272
)
277273

278-
# We need this when generating the VFS overlay file and also when
279-
# configuring inputs for the compile action, so it's best to precompute it
280-
# here.
281274
if is_feature_enabled(
282275
feature_configuration = feature_configuration,
283-
feature_name = SWIFT_FEATURE_VFSOVERLAY,
276+
feature_name = SWIFT_FEATURE_USE_EXPLICIT_SWIFT_MODULE_MAP,
284277
):
285-
vfsoverlay_file = actions.declare_file(
286-
"{}.vfsoverlay.yaml".format(target_name),
278+
# Generate the JSON file that contains the manifest of Swift
279+
# dependencies.
280+
explicit_swift_module_map_file = actions.declare_file(
281+
"{}.swift-explicit-module-map.json".format(target_name),
287282
)
288-
write_vfsoverlay(
283+
write_explicit_swift_module_map_file(
289284
actions = actions,
290-
swiftmodules = transitive_swiftmodules,
291-
vfsoverlay_file = vfsoverlay_file,
292-
virtual_swiftmodule_root = _SWIFTMODULES_VFS_ROOT,
285+
explicit_swift_module_map_file = explicit_swift_module_map_file,
286+
module_contexts = transitive_modules,
293287
)
294288
else:
295-
vfsoverlay_file = None
289+
explicit_swift_module_map_file = None
296290

297291
prerequisites = struct(
298292
additional_inputs = additional_inputs,
299293
bin_dir = feature_configuration._bin_dir,
300294
cc_compilation_context = merged_compilation_context,
301295
defines = sets.to_list(defines_set),
296+
explicit_swift_module_map_file = explicit_swift_module_map_file,
302297
genfiles_dir = feature_configuration._genfiles_dir,
303298
is_swift = True,
304299
module_name = module_name,
@@ -307,8 +302,6 @@ def compile(
307302
transitive_modules = transitive_modules,
308303
transitive_swiftmodules = transitive_swiftmodules,
309304
user_compile_flags = copts,
310-
vfsoverlay_file = vfsoverlay_file,
311-
vfsoverlay_search_path = _SWIFTMODULES_VFS_ROOT,
312305
# Merge the compile outputs into the prerequisites.
313306
**struct_fields(compile_outputs)
314307
)
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 & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,10 @@ SWIFT_FEATURE_REWRITE_GENERATED_HEADER = "swift.rewrite_generated_header"
129129
# them.
130130
SWIFT_FEATURE_USE_C_MODULES = "swift.use_c_modules"
131131

132+
# If enabled, Swift modules for dependencies will be passed to the compiler
133+
# using a JSON file instead of `-I` search paths.
134+
SWIFT_FEATURE_USE_EXPLICIT_SWIFT_MODULE_MAP = "swift.use_explicit_swift_module_map"
135+
132136
# If enabled, Swift compilation actions will use the same global Clang module
133137
# cache used by Objective-C compilation actions. This is disabled by default
134138
# because under some circumstances Clang module cache corruption can cause the
@@ -149,13 +153,6 @@ SWIFT_FEATURE_USE_OLD_DRIVER = "swift.use_old_driver"
149153
# using a sufficiently recent version of Swift (4.2 or higher).
150154
SWIFT_FEATURE_USE_RESPONSE_FILES = "swift.use_response_files"
151155

152-
# If enabled, Swift compilation actions will create a virtual file system
153-
# overlay containing all its dependencies' `.swiftmodule` files and use that
154-
# overlay as its sole search path. This improves build performance by avoiding
155-
# worst-case O(N^2) search (N modules, each in its own subdirectory), especially
156-
# when access to those paths involves traversing a networked file system.
157-
SWIFT_FEATURE_VFSOVERLAY = "swift.vfsoverlay"
158-
159156
# If enabled, builds using the "dbg" compilation mode will explicitly disable
160157
# swiftc from producing swiftmodules containing embedded file paths, which are
161158
# inherently non-portable across machines.

swift/internal/vfsoverlay.bzl

Lines changed: 0 additions & 64 deletions
This file was deleted.

swift/toolchains/config/compile_config.bzl

Lines changed: 25 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,9 @@ load(
4545
"SWIFT_FEATURE_SUPPORTS_SYSTEM_MODULE_FLAG",
4646
"SWIFT_FEATURE_SYSTEM_MODULE",
4747
"SWIFT_FEATURE_USE_C_MODULES",
48+
"SWIFT_FEATURE_USE_EXPLICIT_SWIFT_MODULE_MAP",
4849
"SWIFT_FEATURE_USE_GLOBAL_MODULE_CACHE",
4950
"SWIFT_FEATURE_USE_OLD_DRIVER",
50-
"SWIFT_FEATURE_VFSOVERLAY",
5151
"SWIFT_FEATURE__NUM_THREADS_1_IN_SWIFTCOPTS",
5252
"SWIFT_FEATURE__WMO_IN_SWIFTCOPTS",
5353
)
@@ -451,22 +451,24 @@ def compile_action_configs(
451451
),
452452
]
453453

454-
#### Search paths for Swift module dependencies
454+
#### Search paths/explicit module map for Swift module dependencies
455455
action_configs.extend([
456456
ActionConfigInfo(
457-
actions = [
458-
SWIFT_ACTION_COMPILE,
459-
SWIFT_ACTION_SYMBOL_GRAPH_EXTRACT,
460-
],
461-
configurators = [_dependencies_swiftmodules_configurator],
462-
not_features = [SWIFT_FEATURE_VFSOVERLAY],
457+
actions = [SWIFT_ACTION_COMPILE],
458+
configurators = [_explicit_swift_module_map_configurator],
459+
features = [SWIFT_FEATURE_USE_EXPLICIT_SWIFT_MODULE_MAP],
463460
),
464461
ActionConfigInfo(
465462
actions = [SWIFT_ACTION_COMPILE],
466-
configurators = [
467-
_dependencies_swiftmodules_vfsoverlay_configurator,
468-
],
469-
features = [SWIFT_FEATURE_VFSOVERLAY],
463+
configurators = [_dependencies_swiftmodules_configurator],
464+
not_features = [SWIFT_FEATURE_USE_EXPLICIT_SWIFT_MODULE_MAP],
465+
),
466+
467+
# swift-symbolgraph-extract doesn't yet support explicit Swift module
468+
# maps.
469+
ActionConfigInfo(
470+
actions = [SWIFT_ACTION_SYMBOL_GRAPH_EXTRACT],
471+
configurators = [_dependencies_swiftmodules_configurator],
470472
),
471473
])
472474

@@ -1050,20 +1052,19 @@ def _dependencies_swiftmodules_configurator(prerequisites, args):
10501052
inputs = prerequisites.transitive_swiftmodules,
10511053
)
10521054

1053-
def _dependencies_swiftmodules_vfsoverlay_configurator(prerequisites, args):
1054-
"""Provides a single `.swiftmodule` search path using a VFS overlay."""
1055-
swiftmodules = prerequisites.transitive_swiftmodules
1056-
1057-
# Bug: `swiftc` doesn't pass its `-vfsoverlay` arg to the frontend.
1058-
# Workaround: Pass `-vfsoverlay` directly via `-Xfrontend`.
1059-
args.add(
1060-
"-Xfrontend",
1061-
"-vfsoverlay{}".format(prerequisites.vfsoverlay_file.path),
1055+
def _explicit_swift_module_map_configurator(prerequisites, args):
1056+
"""Adds the explicit Swift module map file to the command line."""
1057+
args.add_all(
1058+
[
1059+
"-explicit-swift-module-map-file",
1060+
prerequisites.explicit_swift_module_map_file,
1061+
],
1062+
before_each = "-Xfrontend",
10621063
)
1063-
args.add("-I{}".format(prerequisites.vfsoverlay_search_path))
1064-
10651064
return ConfigResultInfo(
1066-
inputs = swiftmodules + [prerequisites.vfsoverlay_file],
1065+
inputs = prerequisites.transitive_swiftmodules + [
1066+
prerequisites.explicit_swift_module_map_file,
1067+
],
10671068
)
10681069

10691070
def _module_name_configurator(prerequisites, args):

0 commit comments

Comments
 (0)