xref: /aosp_15_r20/external/toolchain-utils/compiler_wrapper/ccache_flag_test.go (revision 760c253c1ed00ce9abd48f8546f08516e57485fe)
1*760c253cSXin Li// Copyright 2019 The ChromiumOS Authors
2*760c253cSXin Li// Use of this source code is governed by a BSD-style license that can be
3*760c253cSXin Li// found in the LICENSE file.
4*760c253cSXin Li
5*760c253cSXin Lipackage main
6*760c253cSXin Li
7*760c253cSXin Liimport (
8*760c253cSXin Li	"path/filepath"
9*760c253cSXin Li	"testing"
10*760c253cSXin Li)
11*760c253cSXin Li
12*760c253cSXin Lifunc TestCallCCacheGivenConfig(t *testing.T) {
13*760c253cSXin Li	withCCacheEnabledTestContext(t, func(ctx *testContext) {
14*760c253cSXin Li		cmd := ctx.must(callCompiler(ctx, ctx.cfg,
15*760c253cSXin Li			ctx.newCommand(gccX86_64, mainCc)))
16*760c253cSXin Li		if err := verifyPath(cmd, "/usr/bin/ccache"); err != nil {
17*760c253cSXin Li			t.Error(err)
18*760c253cSXin Li		}
19*760c253cSXin Li		if err := verifyArgOrder(cmd, gccX86_64+".real", mainCc); err != nil {
20*760c253cSXin Li			t.Error(err)
21*760c253cSXin Li		}
22*760c253cSXin Li	})
23*760c253cSXin Li}
24*760c253cSXin Li
25*760c253cSXin Lifunc TestNotCallCCacheGivenConfig(t *testing.T) {
26*760c253cSXin Li	withTestContext(t, func(ctx *testContext) {
27*760c253cSXin Li		cmd := ctx.must(callCompiler(ctx, ctx.cfg,
28*760c253cSXin Li			ctx.newCommand(gccX86_64, mainCc)))
29*760c253cSXin Li		if err := verifyPath(cmd, gccX86_64+".real"); err != nil {
30*760c253cSXin Li			t.Error(err)
31*760c253cSXin Li		}
32*760c253cSXin Li	})
33*760c253cSXin Li}
34*760c253cSXin Li
35*760c253cSXin Lifunc TestCallCCacheGivenEnviron(t *testing.T) {
36*760c253cSXin Li	withCCacheEnabledTestContext(t, func(ctx *testContext) {
37*760c253cSXin Li		ctx.env = append(ctx.env, "COMPILER_WRAPPER_FORCE_CCACHE=1")
38*760c253cSXin Li
39*760c253cSXin Li		cmd := ctx.must(callCompiler(ctx, ctx.cfg,
40*760c253cSXin Li			ctx.newCommand(gccX86_64, mainCc)))
41*760c253cSXin Li		if err := verifyPath(cmd, "/usr/bin/ccache"); err != nil {
42*760c253cSXin Li			t.Error(err)
43*760c253cSXin Li		}
44*760c253cSXin Li		if err := verifyArgOrder(cmd, gccX86_64+".real", mainCc); err != nil {
45*760c253cSXin Li			t.Error(err)
46*760c253cSXin Li		}
47*760c253cSXin Li	})
48*760c253cSXin Li}
49*760c253cSXin Li
50*760c253cSXin Lifunc TestNotCallCCacheGivenEnviron(t *testing.T) {
51*760c253cSXin Li	withCCacheEnabledTestContext(t, func(ctx *testContext) {
52*760c253cSXin Li		ctx.env = append(ctx.env, "COMPILER_WRAPPER_FORCE_CCACHE=0")
53*760c253cSXin Li
54*760c253cSXin Li		cmd := ctx.must(callCompiler(ctx, ctx.cfg,
55*760c253cSXin Li			ctx.newCommand(gccX86_64, mainCc)))
56*760c253cSXin Li		if err := verifyPath(cmd, gccX86_64+".real"); err != nil {
57*760c253cSXin Li			t.Error(err)
58*760c253cSXin Li		}
59*760c253cSXin Li	})
60*760c253cSXin Li}
61*760c253cSXin Li
62*760c253cSXin Lifunc TestNotCallCCacheGivenConfigAndNoCCacheArg(t *testing.T) {
63*760c253cSXin Li	withTestContext(t, func(ctx *testContext) {
64*760c253cSXin Li		cmd := ctx.must(callCompiler(ctx, ctx.cfg,
65*760c253cSXin Li			ctx.newCommand(gccX86_64, "-noccache", mainCc)))
66*760c253cSXin Li		if err := verifyPath(cmd, gccX86_64+".real"); err != nil {
67*760c253cSXin Li			t.Error(err)
68*760c253cSXin Li		}
69*760c253cSXin Li		if err := verifyArgCount(cmd, 0, "-noccache"); err != nil {
70*760c253cSXin Li			t.Error(err)
71*760c253cSXin Li		}
72*760c253cSXin Li	})
73*760c253cSXin Li}
74*760c253cSXin Li
75*760c253cSXin Lifunc TestSetCacheDir(t *testing.T) {
76*760c253cSXin Li	withCCacheEnabledTestContext(t, func(ctx *testContext) {
77*760c253cSXin Li		cmd := ctx.must(callCompiler(ctx, ctx.cfg,
78*760c253cSXin Li			ctx.newCommand(gccX86_64, mainCc)))
79*760c253cSXin Li		if err := verifyEnvUpdate(cmd, "CCACHE_DIR=/var/cache/distfiles/ccache"); err != nil {
80*760c253cSXin Li			t.Error(err)
81*760c253cSXin Li		}
82*760c253cSXin Li	})
83*760c253cSXin Li}
84*760c253cSXin Li
85*760c253cSXin Lifunc TestSetCacheUmask(t *testing.T) {
86*760c253cSXin Li	withCCacheEnabledTestContext(t, func(ctx *testContext) {
87*760c253cSXin Li		cmd := ctx.must(callCompiler(ctx, ctx.cfg,
88*760c253cSXin Li			ctx.newCommand(gccX86_64, mainCc)))
89*760c253cSXin Li		if err := verifyEnvUpdate(cmd, "CCACHE_UMASK=002"); err != nil {
90*760c253cSXin Li			t.Error(err)
91*760c253cSXin Li		}
92*760c253cSXin Li	})
93*760c253cSXin Li}
94*760c253cSXin Li
95*760c253cSXin Lifunc TestUpdateSandboxRewriteWithValue(t *testing.T) {
96*760c253cSXin Li	withCCacheEnabledTestContext(t, func(ctx *testContext) {
97*760c253cSXin Li		cmd := ctx.must(callCompiler(ctx, ctx.cfg,
98*760c253cSXin Li			ctx.newCommand(gccX86_64, mainCc)))
99*760c253cSXin Li		if err := verifyNoEnvUpdate(cmd, "SANDBOX_WRITE"); err != nil {
100*760c253cSXin Li			t.Error(err)
101*760c253cSXin Li		}
102*760c253cSXin Li
103*760c253cSXin Li		ctx.env = []string{"SANDBOX_WRITE=xyz"}
104*760c253cSXin Li		cmd = ctx.must(callCompiler(ctx, ctx.cfg,
105*760c253cSXin Li			ctx.newCommand(gccX86_64, mainCc)))
106*760c253cSXin Li		if err := verifyEnvUpdate(cmd,
107*760c253cSXin Li			"SANDBOX_WRITE=xyz:/var/cache/distfiles/ccache"); err != nil {
108*760c253cSXin Li			t.Error(err)
109*760c253cSXin Li		}
110*760c253cSXin Li	})
111*760c253cSXin Li}
112*760c253cSXin Li
113*760c253cSXin Lifunc TestUpdateSandboxRewriteWithoutValue(t *testing.T) {
114*760c253cSXin Li	withCCacheEnabledTestContext(t, func(ctx *testContext) {
115*760c253cSXin Li		cmd := ctx.must(callCompiler(ctx, ctx.cfg,
116*760c253cSXin Li			ctx.newCommand(gccX86_64, mainCc)))
117*760c253cSXin Li		if err := verifyNoEnvUpdate(cmd, "SANDBOX_WRITE"); err != nil {
118*760c253cSXin Li			t.Error(err)
119*760c253cSXin Li		}
120*760c253cSXin Li
121*760c253cSXin Li		ctx.env = []string{"SANDBOX_WRITE="}
122*760c253cSXin Li		cmd = ctx.must(callCompiler(ctx, ctx.cfg,
123*760c253cSXin Li			ctx.newCommand(gccX86_64, mainCc)))
124*760c253cSXin Li		if err := verifyEnvUpdate(cmd,
125*760c253cSXin Li			"SANDBOX_WRITE=:/var/cache/distfiles/ccache"); err != nil {
126*760c253cSXin Li			t.Error(err)
127*760c253cSXin Li		}
128*760c253cSXin Li	})
129*760c253cSXin Li}
130*760c253cSXin Li
131*760c253cSXin Lifunc TestClearCCacheDisableWithValue(t *testing.T) {
132*760c253cSXin Li	withCCacheEnabledTestContext(t, func(ctx *testContext) {
133*760c253cSXin Li		cmd := ctx.must(callCompiler(ctx, ctx.cfg,
134*760c253cSXin Li			ctx.newCommand(gccX86_64, mainCc)))
135*760c253cSXin Li		if err := verifyNoEnvUpdate(cmd, "CCACHE_DISABLE"); err != nil {
136*760c253cSXin Li			t.Error(err)
137*760c253cSXin Li		}
138*760c253cSXin Li
139*760c253cSXin Li		ctx.env = []string{"CCACHE_DISABLE=true"}
140*760c253cSXin Li		cmd = ctx.must(callCompiler(ctx, ctx.cfg,
141*760c253cSXin Li			ctx.newCommand(gccX86_64, mainCc)))
142*760c253cSXin Li		if err := verifyEnvUpdate(cmd, "CCACHE_DISABLE="); err != nil {
143*760c253cSXin Li			t.Error(err)
144*760c253cSXin Li		}
145*760c253cSXin Li	})
146*760c253cSXin Li}
147*760c253cSXin Li
148*760c253cSXin Lifunc TestClearCCacheDisableWithoutValue(t *testing.T) {
149*760c253cSXin Li	withCCacheEnabledTestContext(t, func(ctx *testContext) {
150*760c253cSXin Li		cmd := ctx.must(callCompiler(ctx, ctx.cfg,
151*760c253cSXin Li			ctx.newCommand(gccX86_64, mainCc)))
152*760c253cSXin Li		if err := verifyNoEnvUpdate(cmd, "CCACHE_DISABLE"); err != nil {
153*760c253cSXin Li			t.Error(err)
154*760c253cSXin Li		}
155*760c253cSXin Li
156*760c253cSXin Li		ctx.env = []string{"CCACHE_DISABLE="}
157*760c253cSXin Li		cmd = ctx.must(callCompiler(ctx, ctx.cfg,
158*760c253cSXin Li			ctx.newCommand(gccX86_64, mainCc)))
159*760c253cSXin Li		if err := verifyEnvUpdate(cmd, "CCACHE_DISABLE="); err != nil {
160*760c253cSXin Li			t.Error(err)
161*760c253cSXin Li		}
162*760c253cSXin Li	})
163*760c253cSXin Li}
164*760c253cSXin Li
165*760c253cSXin Lifunc TestAddCCacheCpp2FlagForClang(t *testing.T) {
166*760c253cSXin Li	withCCacheEnabledTestContext(t, func(ctx *testContext) {
167*760c253cSXin Li		cmd := ctx.must(callCompiler(ctx, ctx.cfg,
168*760c253cSXin Li			ctx.newCommand(clangX86_64, mainCc)))
169*760c253cSXin Li		if err := verifyEnvUpdate(cmd, "CCACHE_CPP2=yes"); err != nil {
170*760c253cSXin Li			t.Error(err)
171*760c253cSXin Li		}
172*760c253cSXin Li	})
173*760c253cSXin Li}
174*760c253cSXin Li
175*760c253cSXin Lifunc TestOmitCCacheCpp2FlagForGcc(t *testing.T) {
176*760c253cSXin Li	withCCacheEnabledTestContext(t, func(ctx *testContext) {
177*760c253cSXin Li		cmd := ctx.must(callCompiler(ctx, ctx.cfg,
178*760c253cSXin Li			ctx.newCommand(gccX86_64, mainCc)))
179*760c253cSXin Li		if err := verifyNoEnvUpdate(cmd, "CCACHE_CPP2"); err != nil {
180*760c253cSXin Li			t.Error(err)
181*760c253cSXin Li		}
182*760c253cSXin Li	})
183*760c253cSXin Li}
184*760c253cSXin Li
185*760c253cSXin Lifunc withCCacheEnabledTestContext(t *testing.T, work func(ctx *testContext)) {
186*760c253cSXin Li	withTestContext(t, func(ctx *testContext) {
187*760c253cSXin Li		ctx.cfg.useCCache = true
188*760c253cSXin Li		work(ctx)
189*760c253cSXin Li	})
190*760c253cSXin Li}
191*760c253cSXin Li
192*760c253cSXin Lifunc TestRusagePreventsCCache(t *testing.T) {
193*760c253cSXin Li	withCCacheEnabledTestContext(t, func(ctx *testContext) {
194*760c253cSXin Li		ctx.NoteTestWritesToUmask()
195*760c253cSXin Li
196*760c253cSXin Li		ctx.env = append(ctx.env, "TOOLCHAIN_RUSAGE_OUTPUT="+filepath.Join(ctx.tempDir, "rusage.log"))
197*760c253cSXin Li		cmd := ctx.must(callCompiler(ctx, ctx.cfg,
198*760c253cSXin Li			ctx.newCommand(gccX86_64, mainCc)))
199*760c253cSXin Li		if err := verifyPath(cmd, gccX86_64+".real"); err != nil {
200*760c253cSXin Li			t.Error(err)
201*760c253cSXin Li		}
202*760c253cSXin Li	})
203*760c253cSXin Li}
204*760c253cSXin Li
205*760c253cSXin Lifunc TestCcacheIsDisabledInSrcConfigure(t *testing.T) {
206*760c253cSXin Li	withCCacheEnabledTestContext(t, func(ctx *testContext) {
207*760c253cSXin Li		ctx.NoteTestWritesToUmask()
208*760c253cSXin Li
209*760c253cSXin Li		ctx.env = append(ctx.env, "EBUILD_PHASE=configure")
210*760c253cSXin Li		cmd := ctx.must(callCompiler(ctx, ctx.cfg,
211*760c253cSXin Li			ctx.newCommand(gccX86_64, mainCc)))
212*760c253cSXin Li		if err := verifyPath(cmd, gccX86_64+".real"); err != nil {
213*760c253cSXin Li			t.Error(err)
214*760c253cSXin Li		}
215*760c253cSXin Li	})
216*760c253cSXin Li}
217