aboutsummaryrefslogtreecommitdiff
path: root/compiler_wrapper/ccache_flag.go
blob: 02fb43ac3005ed44e5fd322726becf93ab2952ca (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
// Copyright 2019 The Chromium OS Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

package main

func processCCacheFlag(builder *commandBuilder) {
	// We should be able to share the objects across compilers as
	// the pre-processed output will differ.  This allows boards
	// that share compiler flags (like x86 boards) to share caches.
	const ccacheDir = "/var/cache/distfiles/ccache"

	useCCache := true
	builder.transformArgs(func(arg builderArg) string {
		if arg.value == "-noccache" {
			useCCache = false
			return ""
		}
		return arg.value
	})

	// Disable ccache during portage's src_configure phase. Using ccache here is generally a
	// waste of time, since these files are very small. Experimentally, this speeds up
	// configuring by ~13%.
	if val, present := builder.env.getenv("EBUILD_PHASE"); present && val == "configure" {
		useCCache = false
	}

	if builder.cfg.useCCache && useCCache {
		// Note: we used to also set CCACHE_BASEDIR but don't do it
		// anymore for reasons outlined in crrev.com/c/2103170.
		if _, present := builder.env.getenv("CCACHE_DISABLE"); present {
			// Portage likes to set this for us when it has FEATURES=-ccache.
			// The other vars we need to setup manually because of tools like
			// scons that scrubs the env before we get executed.
			builder.updateEnv("CCACHE_DISABLE=")
		}
		// If RESTRICT=sandbox is enabled, then sandbox won't be setup,
		// and the env vars won't be available for appending.
		if sandboxRewrite, present := builder.env.getenv("SANDBOX_WRITE"); present {
			builder.updateEnv("SANDBOX_WRITE=" + sandboxRewrite + ":" + ccacheDir)
		}

		// Make sure we keep the cached files group writable.
		builder.updateEnv("CCACHE_DIR="+ccacheDir, "CCACHE_UMASK=002")

		// ccache may generate false positive warnings.
		// Workaround bug https://crbug.com/649740
		if builder.target.compilerType == clangType {
			builder.updateEnv("CCACHE_CPP2=yes")
		}

		builder.wrapPath("/usr/bin/ccache")
	}
}