Skip to content

Commit 161899d

Browse files
authored
Merge pull request swiftlang#34133 from porglezomp-misc/twist-it-bop-it-sccache-it
[build-script] Add a flag for sccache
2 parents 23dc001 + b5247d2 commit 161899d

File tree

11 files changed

+67
-12
lines changed

11 files changed

+67
-12
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,8 @@ following (non-exhaustive) set of useful options::
111111
- ``--test``: Test the toolchain after it has been compiled. This is off by default.
112112
- ``--distcc``: Use distcc to speed up the build by distributing the c++ part of
113113
the swift build. This is off by default.
114+
- ``--sccache``: Use sccache to speed up subsequent builds of the compiler by
115+
caching more c++ build artifacts. This is off by default.
114116

115117
More options may be added over time. Please pass ``--help`` to
116118
``build-toolchain`` to see the full set of options.

docs/DevelopmentTips.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,11 @@ Compilation times for the compiler and the standard library can be agonizing, es
3333
```
3434
$ brew install sccache
3535
$ sccache --start-server
36-
$ ./swift/utils/build-script MY_ARGS --cmake-c-launcher $(which sccache) --cmake-cxx-launcher $(which sccache)
36+
$ ./swift/utils/build-script MY_ARGS --sccache
3737
```
3838

39+
If you want to always use sccache, you can `export SWIFT_USE_SCCACHE=1` and the build script will pick it up.
40+
3941
Given the size of artifacts generated, you might also want to bump the cache size from the default 10GB to something larger, say by putting `export SCCACHE_CACHE_SIZE="50G"` in your dotfile(s).
4042

4143
You can run some compiles to see if it is actually doing something by running `sccache --show-stats`. Depending on the exact compilation task you're running, you might see very different cache hit rates. For example, `sccache` is particularly effective if you're rebuilding LLVM, which doesn't change so frequently from the Swift compiler's perspective. On the other hand, if you're changing the compiler's AST, the cache hit rate is likely to be much lower.

docs/HowToGuides/GettingStarted.md

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -246,15 +246,13 @@ Phew, that's a lot to digest! Now let's proceed to the actual build itself!
246246
```sh
247247
utils/build-script --skip-build-benchmarks \
248248
--skip-ios --skip-watchos --skip-tvos --swift-darwin-supported-archs "x86_64" \
249-
--cmake-c-launcher="$(which sccache)" --cmake-cxx-launcher="$(which sccache)" \
250-
--release-debuginfo --test
249+
--sccache --release-debuginfo --test
251250
```
252251
- Via Xcode:
253252
```sh
254253
utils/build-script --skip-build-benchmarks \
255254
--skip-ios --skip-watchos --skip-tvos --swift-darwin-supported-archs "x86_64" \
256-
--cmake-c-launcher="$(which sccache)" --cmake-cxx-launcher="$(which sccache)" \
257-
--release-debuginfo --test \
255+
--sccache --release-debuginfo --test \
258256
--xcode
259257
```
260258
This will create a directory

utils/build-script

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,11 @@ def validate_arguments(toolchain, args):
187187
fatal_error(
188188
"can't find distcc-pump (please install distcc-pump)")
189189

190+
if args.sccache:
191+
if toolchain.sccache is None:
192+
fatal_error(
193+
"can't find sccache (please install sccache)")
194+
190195
if args.host_target is None or args.stdlib_deployment_targets is None:
191196
fatal_error("unknown operating system")
192197

@@ -523,6 +528,9 @@ class BuildScriptInvocation(object):
523528
"--distcc",
524529
"--distcc-pump=%s" % toolchain.distcc_pump
525530
]
531+
if args.sccache:
532+
args.cmake_c_launcher = toolchain.sccache
533+
args.cmake_cxx_launcher = toolchain.sccache
526534

527535
# *NOTE* We use normal cmake to pass through tsan/ubsan options. We do
528536
# NOT pass them to build-script-impl.
@@ -1064,6 +1072,12 @@ def parse_preset_args():
10641072
action=argparse.actions.StoreTrueAction,
10651073
nargs=argparse.Nargs.OPTIONAL,
10661074
default=os.environ.get('USE_DISTCC') == '1')
1075+
parser.add_argument(
1076+
"--sccache",
1077+
help="use sccache",
1078+
action=argparse.actions.StoreTrueAction,
1079+
nargs=argparse.Nargs.OPTIONAL,
1080+
default=os.environ.get('SWIFT_USE_SCCACHE') == '1')
10671081
parser.add_argument(
10681082
"--cmake-c-launcher",
10691083
help="the absolute path to set CMAKE_C_COMPILER_LAUNCHER",
@@ -1157,6 +1171,10 @@ def main_preset():
11571171
fatal_error(
11581172
'--distcc can not be used with' +
11591173
' --cmake-c-launcher or --cmake-cxx-launcher')
1174+
if args.sccache and (args.cmake_c_launcher or args.cmake_cxx_launcher):
1175+
fatal_error(
1176+
'--sccache can not be used with' +
1177+
' --cmake-c-launcher or --cmake-cxx-launcher')
11601178

11611179
build_script_args = [sys.argv[0]]
11621180
if args.dry_run:
@@ -1167,6 +1185,8 @@ def main_preset():
11671185
build_script_args += preset_args
11681186
if args.distcc:
11691187
build_script_args += ["--distcc"]
1188+
if args.sccache:
1189+
build_script_args += ["--sccache"]
11701190
if args.build_jobs:
11711191
build_script_args += ["--jobs", str(args.build_jobs)]
11721192
if args.swiftsyntax_install_prefix:

utils/build-toolchain

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -85,13 +85,7 @@ while [ $# -ne 0 ]; do
8585
DISTCC_FLAG="--distcc"
8686
;;
8787
--sccache)
88-
SCCACHE=$(which sccache)
89-
if [[ -z "${SCCACHE}" ]]; then
90-
echo "Error! Asked to use sccache, but could not find sccache in PATH?!"
91-
usage
92-
exit 1
93-
fi
94-
SCCACHE_FLAG="--cmake-c-launcher=${SCCACHE} --cmake-cxx-launcher=${SCCACHE}"
88+
SCCACHE_FLAG="--sccache"
9589
;;
9690
--preset-file)
9791
shift

utils/build_swift/build_swift/driver_arguments.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -387,6 +387,9 @@ def create_argument_parser():
387387
option('--distcc', toggle_true,
388388
default=os.environ.get('USE_DISTCC') == '1',
389389
help='use distcc in pump mode')
390+
option('--sccache', toggle_true,
391+
default=os.environ.get('SWIFT_USE_SCCACHE') == '1',
392+
help='use sccache')
390393
option('--enable-asan', toggle_true,
391394
help='enable Address Sanitizer')
392395
option('--enable-ubsan', toggle_true,

utils/build_swift/tests/expected_options.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,7 @@
138138
defaults.DARWIN_DEPLOYMENT_VERSION_WATCHOS,
139139
'darwin_xcrun_toolchain': None,
140140
'distcc': False,
141+
'sccache': False,
141142
'dry_run': False,
142143
'enable_asan': False,
143144
'enable_experimental_differentiable_programming': True,
@@ -500,6 +501,7 @@ class BuildScriptImplOption(_BaseOption):
500501
EnableOption('--build-swift-static-stdlib'),
501502
EnableOption('--build-swift-stdlib-unittest-extra'),
502503
EnableOption('--distcc'),
504+
EnableOption('--sccache'),
503505
EnableOption('--enable-asan'),
504506
EnableOption('--enable-experimental-differentiable-programming'),
505507
EnableOption('--enable-experimental-concurrency'),

utils/swift_build_support/swift_build_support/cmake.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,10 @@ def common_options(self):
126126
define("CMAKE_C_COMPILER_LAUNCHER:PATH", toolchain.distcc)
127127
define("CMAKE_CXX_COMPILER_LAUNCHER:PATH", toolchain.distcc)
128128

129+
if args.sccache:
130+
define("CMAKE_C_COMPILER_LAUNCHER:PATH", toolchain.sccache)
131+
define("CMAKE_CXX_COMPILER_LAUNCHER:PATH", toolchain.sccache)
132+
129133
if args.cmake_c_launcher:
130134
define("CMAKE_C_COMPILER_LAUNCHER:PATH", args.cmake_c_launcher)
131135
if args.cmake_cxx_launcher:

utils/swift_build_support/swift_build_support/toolchain.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ def _getter(self):
6262
_register("llvm_cov", "llvm-cov")
6363
_register("lipo", "lipo")
6464
_register("libtool", "libtool")
65+
_register("sccache", "sccache")
6566
_register("swiftc", "swiftc")
6667

6768

utils/swift_build_support/tests/test_cmake.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,15 @@ def mock_distcc_path(self):
3333
executable = 'mock-distcc'
3434
return os.path.join(os.path.dirname(__file__), executable)
3535

36+
def mock_sccache_path(self):
37+
"""Return a path string of a mock sccache executable
38+
"""
39+
if platform.system() == 'Windows':
40+
executable = 'sccache.cmd'
41+
else:
42+
executable = 'sccache'
43+
return os.path.join(os.path.dirname(__file__), executable)
44+
3645
def default_args(self):
3746
"""Return new args object with default values
3847
"""
@@ -46,6 +55,7 @@ def default_args(self):
4655
enable_sanitize_coverage=False,
4756
export_compile_commands=False,
4857
distcc=False,
58+
sccache=False,
4959
cmake_generator="Ninja",
5060
cmake_c_launcher=None,
5161
cmake_cxx_launcher=None,
@@ -73,6 +83,8 @@ def cmake(self, args):
7383
toolchain.libtool = args.host_libtool
7484
if args.distcc:
7585
toolchain.distcc = self.mock_distcc_path()
86+
if args.sccache:
87+
toolchain.sccache = self.mock_sccache_path()
7688
toolchain.ninja = self.which_ninja(args)
7789
return CMake(args=args, toolchain=toolchain)
7890

@@ -222,6 +234,20 @@ def test_common_options_distcc(self):
222234
"-DCMAKE_LIBTOOL:PATH=/path/to/libtool",
223235
"-DCMAKE_MAKE_PROGRAM=" + self.which_ninja(args)])
224236

237+
def test_common_options_sccache(self):
238+
args = self.default_args()
239+
args.sccache = True
240+
cmake = self.cmake(args)
241+
self.assertEqual(
242+
list(cmake.common_options()),
243+
["-G", "Ninja",
244+
"-DCMAKE_C_COMPILER_LAUNCHER:PATH=" + self.mock_sccache_path(),
245+
"-DCMAKE_CXX_COMPILER_LAUNCHER:PATH=" + self.mock_sccache_path(),
246+
"-DCMAKE_C_COMPILER:PATH=/path/to/clang",
247+
"-DCMAKE_CXX_COMPILER:PATH=/path/to/clang++",
248+
"-DCMAKE_LIBTOOL:PATH=/path/to/libtool",
249+
"-DCMAKE_MAKE_PROGRAM=" + self.which_ninja(args)])
250+
225251
def test_common_options_launcher(self):
226252
args = self.default_args()
227253
cmake_c_launcher = "/path/to/c_launcher"

utils/swift_build_support/tests/test_toolchain.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,9 @@ def test_misc_tools(self):
6868
self.assertTrue(tc.distcc_pump is None or
6969
os.path.basename(tc.distcc_pump) == 'pump' or
7070
os.path.basename(tc.distcc_pump) == 'distcc-pump')
71+
# sccache
72+
self.assertTrue(tc.sccache is None or
73+
os.path.basename(tc.sccache) == 'sccache')
7174

7275
def test_find_tool(self):
7376
tc = host_toolchain()

0 commit comments

Comments
 (0)