Skip to content

Commit 0b119d4

Browse files
committed
Add swift.deterministic_module_breadcrumbs feature
This adds a new feature, that can be enabled by passing `--features=swift.deterministic_module_breadcrumbs` to the build command. This feature, when enabled, will makes the Swift compilation actions use the shared Clang module cache path written to `/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 b1610a1 commit 0b119d4

File tree

2 files changed

+42
-1
lines changed

2 files changed

+42
-1
lines changed

swift/internal/compiling.bzl

+28-1
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ load(
3434
"SWIFT_FEATURE_COVERAGE",
3535
"SWIFT_FEATURE_DBG",
3636
"SWIFT_FEATURE_DEBUG_PREFIX_MAP",
37+
"SWIFT_FEATURE_DETERMINISTIC_MODULE_BREADCRUMBS",
3738
"SWIFT_FEATURE_EMIT_C_MODULE",
3839
"SWIFT_FEATURE_EMIT_SWIFTINTERFACE",
3940
"SWIFT_FEATURE_ENABLE_BATCH_MODE",
@@ -352,6 +353,16 @@ def compile_action_configs():
352353
SWIFT_FEATURE_IMPLICIT_MODULES,
353354
SWIFT_FEATURE_USE_GLOBAL_MODULE_CACHE,
354355
],
356+
not_features = [SWIFT_FEATURE_DETERMINISTIC_MODULE_BREADCRUMBS],
357+
),
358+
swift_toolchain_config.action_config(
359+
actions = [swift_action_names.COMPILE],
360+
configurators = [_deterministic_module_breadcrumbs_configurator],
361+
features = [
362+
SWIFT_FEATURE_IMPLICIT_MODULES,
363+
SWIFT_FEATURE_DETERMINISTIC_MODULE_BREADCRUMBS,
364+
],
365+
not_features = [SWIFT_FEATURE_USE_GLOBAL_MODULE_CACHE],
355366
),
356367
swift_toolchain_config.action_config(
357368
actions = [swift_action_names.COMPILE],
@@ -361,7 +372,10 @@ def compile_action_configs():
361372
),
362373
],
363374
features = [SWIFT_FEATURE_IMPLICIT_MODULES],
364-
not_features = [SWIFT_FEATURE_USE_GLOBAL_MODULE_CACHE],
375+
not_features = [
376+
[SWIFT_FEATURE_USE_GLOBAL_MODULE_CACHE],
377+
[SWIFT_FEATURE_DETERMINISTIC_MODULE_BREADCRUMBS],
378+
],
365379
),
366380
]
367381

@@ -592,6 +606,19 @@ def _global_module_cache_configurator(prerequisites, args):
592606
paths.join(prerequisites.bin_dir.path, "_swift_module_cache"),
593607
)
594608

609+
def _deterministic_module_breadcrumbs_configurator(prerequisites, args):
610+
"""Adds flags to use a hard-coded module cache path. This makes the Clang
611+
module breadcrumbs embedded in the produced object file deterministic."""
612+
if prerequisites.bin_dir:
613+
args.add(
614+
"-module-cache-path",
615+
paths.join(
616+
"/tmp/build_bazel_rules_swift",
617+
prerequisites.bin_dir.path,
618+
"_swift_module_cache",
619+
),
620+
)
621+
595622
def _batch_mode_configurator(prerequisites, args):
596623
"""Adds flags to enable batch compilation mode."""
597624
if not _is_wmo_manually_requested(prerequisites.user_compile_flags):

swift/internal/feature_names.bzl

+14
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,20 @@ SWIFT_FEATURE_USE_C_MODULES = "swift.use_c_modules"
148148
# crashes.
149149
SWIFT_FEATURE_USE_GLOBAL_MODULE_CACHE = "swift.use_global_module_cache"
150150

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

0 commit comments

Comments
 (0)