[Build] Add option to alter default location of module cache in Linux (#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
This commit is contained in:
Eric Miotto
2021-02-03 07:32:29 -08:00
committed by GitHub
parent bc816087bb
commit 187f4dbaf8
6 changed files with 53 additions and 0 deletions

View File

@@ -336,6 +336,13 @@ def apply_default_arguments(toolchain, args):
args.build_subdir = \
workspace.compute_build_subdir(args)
if args.relocate_xdg_cache_home_under_build_subdir:
cache_under_build_subdir = os.path.join(
SWIFT_BUILD_ROOT, args.build_subdir, '.cache')
if args.verbose_build:
print("Relocating XDG_CACHE_HOME to {}".format(cache_under_build_subdir))
workspace.relocate_xdg_cache_home_under(cache_under_build_subdir)
if args.install_destdir is None:
args.install_destdir = os.path.join(
SWIFT_BUILD_ROOT, args.build_subdir,

View File

@@ -342,6 +342,12 @@ def create_argument_parser():
metavar='PATH',
help='name of the directory under $SWIFT_BUILD_ROOT where the '
'build products will be placed')
option('--relocate-xdg-cache-home-under-build-subdir',
store_true,
help='relocate $XDG_CACHE_HOME to the same location '
'where build products will be placed; '
'this supports having multiple runs for different branches '
'in CI bots for Linux')
option('--install-prefix', store_path,
default=targets.install_prefix(),
help='The installation prefix. This is where built Swift products '

View File

@@ -193,6 +193,7 @@ EXPECTED_DEFAULTS = {
'native_llvm_tools_path': None,
'native_swift_tools_path': None,
'dump_config': False,
'relocate_xdg_cache_home_under_build_subdir': False,
'show_sdks': False,
'skip_build': False,
'skip_local_build': False,
@@ -647,6 +648,7 @@ EXPECTED_OPTIONS = [
PathOption('--android-icu-data'),
PathOption('--android-ndk'),
PathOption('--build-subdir'),
SetTrueOption('--relocate-xdg-cache-home-under-build-subdir'),
PathOption('--clang-profile-instr-use'),
PathOption('--cmake'),
PathOption('--coverage-db'),

View File

@@ -102,3 +102,14 @@ def compute_build_subdir(args):
if args.enable_tsan:
build_subdir += "+tsan"
return build_subdir
def relocate_xdg_cache_home_under(new_cache_location):
"""
This allows under Linux to relocate the default location
of the module cache -- this can be useful when there are
are lot of invocations to touch or when some invocations
can't be easily configured (as is the case for Swift
compiler detection in CMake)
"""
os.environ['XDG_CACHE_HOME'] = new_cache_location

View File

@@ -0,0 +1,8 @@
# RUN: %empty-directory(%t)
# RUN: mkdir -p %t
# 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
# REQUIRES: standalone_build
# REQUIRES: OS=linux-gnu
# CHECK: Relocating XDG_CACHE_HOME

View File

@@ -0,0 +1,19 @@
// REQUIRES: OS=linux-gnu
// RUN: %empty-directory(%t)
// RUN: mkdir -p %t
// RUN: split-file %s %t
//
// RUN: PYTHONPATH=%utils %{python} %t/run_swiftc_with_relocated_xdg_cache_home.py %t/.cache %swiftc_driver_plain %t/hello.swift
// RUN: ls %t/.cache/clang/ModuleCache
//--- run_swiftc_with_relocated_xdg_cache_home.py
import sys
import subprocess
from swift_build_support.swift_build_support import workspace
workspace.relocate_xdg_cache_home_under(sys.argv[1])
subprocess.run([sys.argv[2], sys.argv[3]])
//--- hello.swift
print("hello")