diff options
author | Tobias Bosch <tbosch@google.com> | 2019-06-20 17:47:19 -0700 |
---|---|---|
committer | Tobias Bosch <tbosch@google.com> | 2019-06-25 14:47:26 +0000 |
commit | aa31116c1d7c2491245604f0564e0693f22dfd71 (patch) | |
tree | 4fd8fb92a9a459bb718f8d0c943a05632defbd08 /compiler_wrapper/config_test.go | |
parent | b9992bba3b4e752f10410c624a500762d090fa2b (diff) | |
download | toolchain-utils-aa31116c1d7c2491245604f0564e0693f22dfd71.tar.gz |
Allow to pass in the use_ccache via go build.
Removes usage of go build tags in favor of passing in
configuration via -ldflags -X ...
BUG=chromium:773875
TEST=unit test
Change-Id: I4e8a58e1679b2858e9d4620d6b9c7a35ad08a6ee
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/third_party/toolchain-utils/+/1670987
Reviewed-by: George Burgess <gbiv@chromium.org>
Tested-by: Tobias Bosch <tbosch@google.com>
Diffstat (limited to 'compiler_wrapper/config_test.go')
-rw-r--r-- | compiler_wrapper/config_test.go | 101 |
1 files changed, 89 insertions, 12 deletions
diff --git a/compiler_wrapper/config_test.go b/compiler_wrapper/config_test.go index 9a75acbd..0dcd58dc 100644 --- a/compiler_wrapper/config_test.go +++ b/compiler_wrapper/config_test.go @@ -8,12 +8,12 @@ import ( func TestFullHardeningConfigAndGcc(t *testing.T) { withTestContext(t, func(ctx *testContext) { initFullHardeningConfig(ctx) - wrapperCmd := ctx.newCommand(gccX86_64, mainCc) - cmd := ctx.must(calcCompilerCommandAndCompareToOld(ctx, ctx.cfg, wrapperCmd)) + cmd := ctx.must(calcCompilerCommandAndCompareToOld(ctx, ctx.cfg, + ctx.newCommand(gccX86_64, mainCc))) if err := verifyPath(cmd, "/usr/bin/ccache"); err != nil { t.Error(err) } - if err := verifyArgOrder(cmd, wrapperCmd.path+".real", "--sysroot=/usr/x86_64-cros-linux-gnu", "-Wno-unused-local-typedefs", + if err := verifyArgOrder(cmd, gccX86_64+".real", "--sysroot=/usr/x86_64-cros-linux-gnu", "-Wno-unused-local-typedefs", "-Wno-maybe-uninitialized", "-fno-reorder-blocks-and-partition", "-fPIE", "-D_FORTIFY_SOURCE=2", "-fstack-protector-strong", "-pie", "-fno-omit-frame-pointer", "main.cc", "-mno-movbe"); err != nil { t.Error(err) @@ -24,8 +24,8 @@ func TestFullHardeningConfigAndGcc(t *testing.T) { func TestFullHardeningConfigAndClang(t *testing.T) { withTestContext(t, func(ctx *testContext) { initFullHardeningConfig(ctx) - wrapperCmd := ctx.newCommand(clangX86_64, mainCc) - cmd := ctx.must(calcCompilerCommandAndCompareToOld(ctx, ctx.cfg, wrapperCmd)) + cmd := ctx.must(calcCompilerCommandAndCompareToOld(ctx, ctx.cfg, + ctx.newCommand(clangX86_64, mainCc))) if err := verifyPath(cmd, "/usr/bin/ccache"); err != nil { t.Error(err) } @@ -46,12 +46,12 @@ func TestFullHardeningConfigAndClang(t *testing.T) { func TestNonHardeningConfigAndGcc(t *testing.T) { withTestContext(t, func(ctx *testContext) { initNonHardeningConfig(ctx) - wrapperCmd := ctx.newCommand(gccX86_64, mainCc) - cmd := ctx.must(calcCompilerCommandAndCompareToOld(ctx, ctx.cfg, wrapperCmd)) + cmd := ctx.must(calcCompilerCommandAndCompareToOld(ctx, ctx.cfg, + ctx.newCommand(gccX86_64, mainCc))) if err := verifyPath(cmd, "/usr/bin/ccache"); err != nil { t.Error(err) } - if err := verifyArgOrder(cmd, wrapperCmd.path+".real", "--sysroot=/usr/x86_64-cros-linux-gnu", + if err := verifyArgOrder(cmd, gccX86_64+".real", "--sysroot=/usr/x86_64-cros-linux-gnu", "-Wno-unused-local-typedefs", "-Wno-maybe-uninitialized", "-Wtrampolines", "-Wno-deprecated-declarations", "main.cc", "-mno-movbe"); err != nil { t.Error(err) @@ -62,8 +62,8 @@ func TestNonHardeningConfigAndGcc(t *testing.T) { func TestNonHardeningConfigAndClang(t *testing.T) { withTestContext(t, func(ctx *testContext) { initNonHardeningConfig(ctx) - wrapperCmd := ctx.newCommand(clangX86_64, mainCc) - cmd := ctx.must(calcCompilerCommandAndCompareToOld(ctx, ctx.cfg, wrapperCmd)) + cmd := ctx.must(calcCompilerCommandAndCompareToOld(ctx, ctx.cfg, + ctx.newCommand(clangX86_64, mainCc))) if err := verifyPath(cmd, "/usr/bin/ccache"); err != nil { t.Error(err) } @@ -80,12 +80,89 @@ func TestNonHardeningConfigAndClang(t *testing.T) { }) } +func TestRealConfigWithUseCCacheFlag(t *testing.T) { + resetGlobals() + defer resetGlobals() + ConfigName = "cros.hardened" + + UseCCache = "false" + cfg, err := getRealConfig() + if err != nil { + t.Fatal(err) + } + if cfg.useCCache { + t.Fatal("UseCCache: Expected false got true") + } + + UseCCache = "true" + cfg, err = getRealConfig() + if err != nil { + t.Fatal(err) + } + if !cfg.useCCache { + t.Fatal("UseCCache: Expected true got false") + } + + UseCCache = "invalid" + _, err = getRealConfig() + if err == nil { + t.Fatalf("UseCCache: Expected an error, got none") + } +} + +func TestRealConfigWithConfigNameFlag(t *testing.T) { + resetGlobals() + defer resetGlobals() + UseCCache = "false" + + ConfigName = "cros.hardened" + cfg, err := getRealConfig() + if err != nil { + t.Fatal(err) + } + if !isHardened(cfg) { + t.Fatal("ConfigName: Expected hardened config got non hardened") + } + + ConfigName = "cros.nonhardened" + cfg, err = getRealConfig() + if err != nil { + t.Fatal(err) + } + if isHardened(cfg) { + t.Fatal("ConfigName: Expected non hardened config got hardened") + } + + ConfigName = "invalid" + _, err = getRealConfig() + if err == nil { + t.Fatalf("ConfigName: Expected an error, got none") + } +} + +func isHardened(cfg *config) bool { + for _, arg := range cfg.commonFlags { + if arg == "-pie" { + return true + } + } + return false +} + func initFullHardeningConfig(ctx *testContext) { - *ctx.cfg = crosHardenedConfig + useCCache := true + *ctx.cfg = *getCrosHardenedConfig(useCCache) ctx.setOldWrapperPath(oldHardenedWrapperPathForTest) } func initNonHardeningConfig(ctx *testContext) { - *ctx.cfg = crosNonHardenedConfig + useCCache := true + *ctx.cfg = *getCrosNonHardenedConfig(useCCache) ctx.setOldWrapperPath(oldNonHardenedWrapperPathForTest) } + +func resetGlobals() { + // Set all global variables to a defined state. + ConfigName = "unknown" + UseCCache = "unknown" +} |