Skip to content
This repository has been archived by the owner on Mar 6, 2020. It is now read-only.

cmd/gb: wire up -race flag #487

Merged
merged 1 commit into from
Dec 15, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions cmd/gb/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ var (
// skip caching of packages
FF bool

// enable race runtime
race bool

ldflags, gcflags []string

P int // number of executors to run in parallel
Expand All @@ -45,6 +48,7 @@ func addBuildFlags(fs *flag.FlagSet) {
fs.BoolVar(&R, "r", false, "perform a release build")
fs.BoolVar(&F, "f", false, "rebuild up-to-date packages")
fs.BoolVar(&FF, "F", false, "do not cache built packages")
fs.BoolVar(&race, "race", false, "enable race detector")
fs.IntVar(&P, "P", runtime.NumCPU(), "number of parallel jobs")
fs.Var((*stringsFlag)(&ldflags), "ldflags", "flags passed to the linker")
fs.Var((*stringsFlag)(&gcflags), "gcflags", "flags passed to the compiler")
Expand Down
43 changes: 43 additions & 0 deletions cmd/gb/gb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1223,3 +1223,46 @@ func TestA(t *testing.T) {
gb.run("test")
gb.grepStdout("^a$", "expected 'a'")
}

// test -race flag is wired up correctly
func TestBuildRaceFlag(t *testing.T) {
gb := T{T: t}
defer gb.cleanup()

gb.tempDir("src/x")
gb.tempFile("src/x/x_race.go", "package x\nconst A = 1\n")
gb.tempFile("src/x/y.go", "// +build race\n\npackage x\nconst B = 2\n")
gb.tempFile("src/x/z.go", "package x\n const C = A +B\n")
gb.cd(gb.tempdir)
tmpdir := gb.tempDir("tmp")
gb.setenv("TMP", tmpdir)
gb.run("build", "-race", "x")
gb.mustBeEmpty(tmpdir)
gb.wantArchive(filepath.Join(gb.tempdir, "pkg", runtime.GOOS+"-"+runtime.GOARCH+"-race", "x.a"))
}

func TestTestRaceFlag(t *testing.T) {
gb := T{T: t}
defer gb.cleanup()

gb.tempDir("src/x")
gb.tempFile("src/x/x_race.go", "package x\nconst A = 1\n")
gb.tempFile("src/x/y.go", "// +build race\n\npackage x\nconst B = 2\n")
gb.tempFile("src/x/q.go", "// +build !race\n\npackage x\nconst B = 7\n")
gb.tempFile("src/x/x_test.go", `package x
import "testing"

func TestRaceFlag(t *testing.T) {
if A != 1 || B != 2 {
t.Fatal("expected", 1, 2,"got", A, B)
}
}
`)
gb.cd(gb.tempdir)
tmpdir := gb.tempDir("tmp")
gb.setenv("TMP", tmpdir)
gb.run("test", "-race", "x")
gb.grepStdout("^x$", "expected x") // output from gb test
gb.mustBeEmpty(tmpdir)
gb.mustNotExist(filepath.Join(gb.tempdir, "pkg")) // ensure no pkg directory is created
}
10 changes: 10 additions & 0 deletions cmd/gb/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,9 @@ func main() {
}

args = fs.Args() // reset args to the leftovers from fs.Parse

debug.Debugf("args: %v", args)

if command == commands["plugin"] {
args = append([]string{name}, args...)
}
Expand All @@ -125,7 +128,14 @@ func main() {
gb.Gcflags(gcflags...),
gb.Ldflags(ldflags...),
gb.Tags(buildtags...),
func(c *gb.Context) error {
if race {
return gb.WithRace(c)
}
return nil
},
)

if err != nil {
fatalf("unable to construct context: %v", err)
}
Expand Down
1 change: 1 addition & 0 deletions cmd/gb/testflag.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ var testFlagDefn = map[string]*testFlagSpec{
"gcflags": {},
"dotfile": {},
"tags": {},
"race": {},

// Passed to the test binary
"q": {boolVar: true, passToTest: true},
Expand Down
4 changes: 4 additions & 0 deletions cmd/gb/testflag_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,10 @@ func TestTestFlagsPreParse(t *testing.T) {
args: []string{"-cover", "-name=jardin"},
pargs: []string{"-cover"},
eargs: []string{"-name=jardin"},
}, {
args: []string{"-race", "-name=jardin"},
pargs: []string{"-race"},
eargs: []string{"-name=jardin"},
}}

for _, tt := range tests {
Expand Down