Skip to content

Commit 187f4db

Browse files
authored
[Build] Add option to alter default location of module cache in Linux (swiftlang#35711)
This is meant to support parallel CI runs on the same Linux bot, so that they don't share the module cache and reduce (hopefully) the likelihood of issues related to invalid signatures. When the option is enabled, the environment variable `XDG_CACHE_HOME` is explicitly set in `build-script` and should be inherited by all the child processes. Currently we don't check the operating system before setting the variable (since it should be a noop for other OSes). Addresses rdar://73887745
1 parent bc81608 commit 187f4db

File tree

6 files changed

+53
-0
lines changed

6 files changed

+53
-0
lines changed

utils/build-script

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -336,6 +336,13 @@ def apply_default_arguments(toolchain, args):
336336
args.build_subdir = \
337337
workspace.compute_build_subdir(args)
338338

339+
if args.relocate_xdg_cache_home_under_build_subdir:
340+
cache_under_build_subdir = os.path.join(
341+
SWIFT_BUILD_ROOT, args.build_subdir, '.cache')
342+
if args.verbose_build:
343+
print("Relocating XDG_CACHE_HOME to {}".format(cache_under_build_subdir))
344+
workspace.relocate_xdg_cache_home_under(cache_under_build_subdir)
345+
339346
if args.install_destdir is None:
340347
args.install_destdir = os.path.join(
341348
SWIFT_BUILD_ROOT, args.build_subdir,

utils/build_swift/build_swift/driver_arguments.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,12 @@ def create_argument_parser():
342342
metavar='PATH',
343343
help='name of the directory under $SWIFT_BUILD_ROOT where the '
344344
'build products will be placed')
345+
option('--relocate-xdg-cache-home-under-build-subdir',
346+
store_true,
347+
help='relocate $XDG_CACHE_HOME to the same location '
348+
'where build products will be placed; '
349+
'this supports having multiple runs for different branches '
350+
'in CI bots for Linux')
345351
option('--install-prefix', store_path,
346352
default=targets.install_prefix(),
347353
help='The installation prefix. This is where built Swift products '

utils/build_swift/tests/expected_options.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,7 @@
193193
'native_llvm_tools_path': None,
194194
'native_swift_tools_path': None,
195195
'dump_config': False,
196+
'relocate_xdg_cache_home_under_build_subdir': False,
196197
'show_sdks': False,
197198
'skip_build': False,
198199
'skip_local_build': False,
@@ -647,6 +648,7 @@ class BuildScriptImplOption(_BaseOption):
647648
PathOption('--android-icu-data'),
648649
PathOption('--android-ndk'),
649650
PathOption('--build-subdir'),
651+
SetTrueOption('--relocate-xdg-cache-home-under-build-subdir'),
650652
PathOption('--clang-profile-instr-use'),
651653
PathOption('--cmake'),
652654
PathOption('--coverage-db'),

utils/swift_build_support/swift_build_support/workspace.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,3 +102,14 @@ def compute_build_subdir(args):
102102
if args.enable_tsan:
103103
build_subdir += "+tsan"
104104
return build_subdir
105+
106+
107+
def relocate_xdg_cache_home_under(new_cache_location):
108+
"""
109+
This allows under Linux to relocate the default location
110+
of the module cache -- this can be useful when there are
111+
are lot of invocations to touch or when some invocations
112+
can't be easily configured (as is the case for Swift
113+
compiler detection in CMake)
114+
"""
115+
os.environ['XDG_CACHE_HOME'] = new_cache_location
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# RUN: %empty-directory(%t)
2+
# RUN: mkdir -p %t
3+
# RUN: SWIFT_BUILD_ROOT=%t %swift_src_root/utils/build-script --dry-run --install-all --llbuild --swiftpm --foundation --libdispatch --relocate-xdg-cache-home-under-build-subdir --verbose-build --cmake %cmake 2>&1 | %FileCheck %s
4+
5+
# REQUIRES: standalone_build
6+
# REQUIRES: OS=linux-gnu
7+
8+
# CHECK: Relocating XDG_CACHE_HOME
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// REQUIRES: OS=linux-gnu
2+
3+
// RUN: %empty-directory(%t)
4+
// RUN: mkdir -p %t
5+
// RUN: split-file %s %t
6+
//
7+
// RUN: PYTHONPATH=%utils %{python} %t/run_swiftc_with_relocated_xdg_cache_home.py %t/.cache %swiftc_driver_plain %t/hello.swift
8+
// RUN: ls %t/.cache/clang/ModuleCache
9+
10+
//--- run_swiftc_with_relocated_xdg_cache_home.py
11+
import sys
12+
import subprocess
13+
from swift_build_support.swift_build_support import workspace
14+
15+
workspace.relocate_xdg_cache_home_under(sys.argv[1])
16+
subprocess.run([sys.argv[2], sys.argv[3]])
17+
18+
//--- hello.swift
19+
print("hello")

0 commit comments

Comments
 (0)