xref: /aosp_15_r20/external/toolchain-utils/compiler_wrapper/ccache_flag.go (revision 760c253c1ed00ce9abd48f8546f08516e57485fe)
1// Copyright 2019 The ChromiumOS Authors
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5package main
6
7func isInConfigureStage(env env) bool {
8	val, present := env.getenv("EBUILD_PHASE")
9	return present && val == "configure"
10}
11
12func processCCacheFlag(builder *commandBuilder) {
13	// We should be able to share the objects across compilers as
14	// the pre-processed output will differ.  This allows boards
15	// that share compiler flags (like x86 boards) to share caches.
16	const ccacheDir = "/var/cache/distfiles/ccache"
17
18	useCCache := builder.cfg.useCCache
19	builder.transformArgs(func(arg builderArg) string {
20		if arg.value == "-noccache" {
21			useCCache = false
22			return ""
23		}
24		return arg.value
25	})
26
27	if force, present := builder.env.getenv("COMPILER_WRAPPER_FORCE_CCACHE"); present {
28		switch force {
29		case "0":
30			useCCache = false
31		case "1":
32			useCCache = true
33		}
34	}
35
36	// Disable ccache during portage's src_configure phase. Using ccache here is generally a
37	// waste of time, since these files are very small. Experimentally, this speeds up
38	// configuring by ~13%.
39	if isInConfigureStage(builder.env) {
40		useCCache = false
41	}
42
43	if useCCache {
44		// Note: we used to also set CCACHE_BASEDIR but don't do it
45		// anymore for reasons outlined in crrev.com/c/2103170.
46		if _, present := builder.env.getenv("CCACHE_DISABLE"); present {
47			// Portage likes to set this for us when it has FEATURES=-ccache.
48			// The other vars we need to setup manually because of tools like
49			// scons that scrubs the env before we get executed.
50			builder.updateEnv("CCACHE_DISABLE=")
51		}
52		// If RESTRICT=sandbox is enabled, then sandbox won't be setup,
53		// and the env vars won't be available for appending.
54		if sandboxRewrite, present := builder.env.getenv("SANDBOX_WRITE"); present {
55			builder.updateEnv("SANDBOX_WRITE=" + sandboxRewrite + ":" + ccacheDir)
56		}
57
58		// Make sure we keep the cached files group writable.
59		builder.updateEnv("CCACHE_DIR="+ccacheDir, "CCACHE_UMASK=002")
60
61		// ccache may generate false positive warnings.
62		// Workaround bug https://crbug.com/649740
63		if builder.target.compilerType == clangType {
64			builder.updateEnv("CCACHE_CPP2=yes")
65		}
66
67		builder.wrapPath("/usr/bin/ccache")
68	}
69}
70