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