Skip to content

Commit 418d9f9

Browse files
committed
Add swift.use_tmpdir_for_module_cache feature
This adds a new feature, that can be enabled by passing `--features=swift.use_tmpdir_for_module_cache` to the build command. This feature, when enabled, will makes the Swift compilation actions use the shared Clang module cache path written to `/private/tmp/__build_bazel_rules_swift`. This makes the embedded Clang module breadcrumbs deterministic between Bazel instances, because they are always embedded as absolute paths. Note that the use of this cache is non-hermetic--the cached modules are not wiped between builds, and won't be cleaned when invoking `bazel clean`; the user is responsible for manually cleaning them. Additionally, this can be used as a workaround for a bug in the Swift compiler that causes the module breadcrumbs to be embedded even though the `-no-clang-module-breadcrumbs` flag is passed (https://bugs.swift.org/browse/SR-13275).
1 parent bdb2a7b commit 418d9f9

File tree

2 files changed

+41
-1
lines changed

2 files changed

+41
-1
lines changed

swift/internal/compiling.bzl

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ load(
5959
"SWIFT_FEATURE_SYSTEM_MODULE",
6060
"SWIFT_FEATURE_USE_C_MODULES",
6161
"SWIFT_FEATURE_USE_GLOBAL_MODULE_CACHE",
62+
"SWIFT_FEATURE_USE_TMPDIR_FOR_MODULE_CACHE",
6263
"SWIFT_FEATURE_VFSOVERLAY",
6364
)
6465
load(":features.bzl", "are_all_features_enabled", "is_feature_enabled")
@@ -446,7 +447,19 @@ def compile_action_configs():
446447
],
447448
configurators = [_global_module_cache_configurator],
448449
features = [SWIFT_FEATURE_USE_GLOBAL_MODULE_CACHE],
449-
not_features = [SWIFT_FEATURE_USE_C_MODULES],
450+
not_features = [
451+
SWIFT_FEATURE_USE_C_MODULES,
452+
SWIFT_FEATURE_USE_TMPDIR_FOR_MODULE_CACHE,
453+
],
454+
),
455+
swift_toolchain_config.action_config(
456+
actions = [swift_action_names.COMPILE],
457+
configurators = [_use_tmpdir_for_module_cache_configurator],
458+
features = [
459+
SWIFT_FEATURE_USE_C_MODULES,
460+
SWIFT_FEATURE_USE_TMPDIR_FOR_MODULE_CACHE,
461+
],
462+
not_features = [SWIFT_FEATURE_USE_GLOBAL_MODULE_CACHE],
450463
),
451464
swift_toolchain_config.action_config(
452465
actions = [
@@ -461,6 +474,7 @@ def compile_action_configs():
461474
not_features = [
462475
[SWIFT_FEATURE_USE_C_MODULES],
463476
[SWIFT_FEATURE_USE_GLOBAL_MODULE_CACHE],
477+
[SWIFT_FEATURE_USE_TMPDIR_FOR_MODULE_CACHE],
464478
],
465479
),
466480
swift_toolchain_config.action_config(
@@ -818,6 +832,18 @@ def _global_module_cache_configurator(prerequisites, args):
818832
paths.join(prerequisites.bin_dir.path, "_swift_module_cache"),
819833
)
820834

835+
def _use_tmpdir_for_module_cache_configurator(prerequisites, args):
836+
"""Adds flags to use a pre-defined module cache path."""
837+
if prerequisites.bin_dir:
838+
args.add(
839+
"-module-cache-path",
840+
paths.join(
841+
"/private/tmp/__build_bazel_rules_swift",
842+
prerequisites.bin_dir.path,
843+
"_swift_module_cache",
844+
),
845+
)
846+
821847
def _batch_mode_configurator(prerequisites, args):
822848
"""Adds flags to enable batch compilation mode."""
823849
if not _is_wmo_manually_requested(prerequisites.user_compile_flags):

swift/internal/feature_names.bzl

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,20 @@ SWIFT_FEATURE_USE_C_MODULES = "swift.use_c_modules"
155155
# crashes.
156156
SWIFT_FEATURE_USE_GLOBAL_MODULE_CACHE = "swift.use_global_module_cache"
157157

158+
# If enabled, Swift compilation actions will use the shared Clang module cache
159+
# path written to `/private/tmp/__build_bazel_rules_swift`. This makes the embedded Clang
160+
# module breadcrumbs deterministic between Bazel instances, because they are
161+
# always embedded as absolute paths. Note that the use of this cache is
162+
# non-hermetic--the cached modules are not wiped between builds, and won't be
163+
# cleaned when invoking `bazel clean`; the user is responsible for manually
164+
# cleaning them.
165+
#
166+
# Additionally, this can be used as a workaround for a bug in the Swift
167+
# compiler that causes the module breadcrumbs to be embedded even though the
168+
# `-no-clang-module-breadcrumbs` flag is passed
169+
# (https://bugs.swift.org/browse/SR-13275).
170+
SWIFT_FEATURE_USE_TMPDIR_FOR_MODULE_CACHE = "swift.use_tmpdir_for_module_cache"
171+
158172
# If enabled, actions invoking the Swift driver or frontend may write argument
159173
# lists into response files (i.e., "@args.txt") to avoid passing command lines
160174
# that exceed the system limit. Toolchains typically set this automatically if

0 commit comments

Comments
 (0)