]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/go: add go build -i
authorDavid Crawshaw <david.crawshaw@zentus.com>
Tue, 6 May 2014 13:12:15 +0000 (09:12 -0400)
committerDavid Crawshaw <david.crawshaw@zentus.com>
Tue, 6 May 2014 13:12:15 +0000 (09:12 -0400)
Fixes #7071.

LGTM=iant
R=golang-codereviews, iant
CC=golang-codereviews
https://golang.org/cl/93770044

src/cmd/go/build.go
src/cmd/go/test.bash
src/cmd/go/test.go
src/cmd/go/testflag.go

index 1e4571b2c71ce390d9e5b389633f0e379094b9f3..530f5a379670e630f3dd9f4a479db3c7a6d38517 100644 (file)
@@ -28,7 +28,7 @@ import (
 )
 
 var cmdBuild = &Command{
-       UsageLine: "build [-o output] [build flags] [packages]",
+       UsageLine: "build [-o output] [-i] [build flags] [packages]",
        Short:     "compile packages and dependencies",
        Long: `
 Build compiles the packages named by the import paths,
@@ -50,6 +50,8 @@ derives from the first file name mentioned, such as f1 for 'go build
 f1.go f2.go'; with no files provided ('go build'), the output file
 name is the base name of the containing directory.
 
+The -i flag installs the packages that are dependencies of the target.
+
 The build flags are shared by the build, install, run, and test commands:
 
        -a
@@ -107,6 +109,8 @@ func init() {
        cmdBuild.Run = runBuild
        cmdInstall.Run = runInstall
 
+       cmdBuild.Flag.BoolVar(&buildI, "i", false, "")
+
        addBuildFlags(cmdBuild)
        addBuildFlags(cmdInstall)
 }
@@ -117,6 +121,7 @@ var buildN bool               // -n flag
 var buildP = runtime.NumCPU() // -p flag
 var buildV bool               // -v flag
 var buildX bool               // -x flag
+var buildI bool               // -i flag
 var buildO = cmdBuild.Flag.String("o", "", "output file")
 var buildWork bool           // -work flag
 var buildGcflags []string    // -gcflags flag
@@ -290,8 +295,12 @@ func runBuild(cmd *Command, args []string) {
        }
 
        a := &action{}
+       depMode := modeBuild
+       if buildI {
+               depMode = modeInstall
+       }
        for _, p := range packages(args) {
-               a.deps = append(a.deps, b.action(modeBuild, modeBuild, p))
+               a.deps = append(a.deps, b.action(modeBuild, depMode, p))
        }
        b.do(a)
 }
index 4bde166110421b558a3ac815917410dafb18d8a3..b6da37bd3ac62731c1cf89d46993092f3a58a472 100755 (executable)
@@ -708,6 +708,46 @@ if ./testgo test notest >/dev/null 2>&1; then
 fi
 unset GOPATH
 
+TEST list template can use context function
+if ! ./testgo list -f "GOARCH: {{context.GOARCH}}"; then 
+       echo unable to use context in list template
+       ok=false
+fi
+
+TEST build -i installs dependencies
+d=$(TMPDIR=/var/tmp mktemp -d -t testgoXXX)
+export GOPATH=$d
+mkdir -p $d/src/x/y/foo $d/src/x/y/bar
+echo '
+package foo
+func F() {}
+' >$d/src/x/y/foo/foo.go
+echo '
+package bar
+import "x/y/foo"
+func F() { foo.F() }
+' >$d/src/x/y/bar/bar.go
+if ! ./testgo build -v -i x/y/bar &> $d/err; then
+       echo build -i failed
+       cat $d/err
+       ok=false
+elif ! grep x/y/foo $d/err >/dev/null; then
+       echo first build -i did not build x/y/foo
+       cat $d/err
+       ok=false
+fi
+if ! ./testgo build -v -i x/y/bar &> $d/err; then
+       echo second build -i failed
+       cat $d/err
+       ok=false
+elif grep x/y/foo $d/err >/dev/null; then
+       echo second build -i built x/y/foo
+       cat $d/err
+       ok=false
+fi
+rm -rf $d
+unset GOPATH
+
 # clean up
 if $started; then stop; fi
 rm -rf testdata/bin testdata/bin1
index a5497e71a3d724138bdc2bd9b782816b86674ef9..d206da8dcd5ade96ad388732fe0b4f16c2c9a9c0 100644 (file)
@@ -276,7 +276,6 @@ var (
        testCoverPkgs    []*Package // -coverpkg flag
        testProfile      bool       // some profiling flag
        testNeedBinary   bool       // profile needs to keep binary around
-       testI            bool       // -i flag
        testV            bool       // -v flag
        testFiles        []string   // -file flag(s)  TODO: not respected
        testTimeout      string     // -timeout flag
@@ -339,7 +338,7 @@ func runTest(cmd *Command, args []string) {
        var b builder
        b.init()
 
-       if testI {
+       if buildI {
                buildV = testV
 
                deps := make(map[string]bool)
index 8c45e5c1b259064622f49a5a6fd44fd377ce423b..73f311e5f69e6e3a99c610e40b066ea353315f91 100644 (file)
@@ -66,7 +66,6 @@ var testFlagDefn = []*testFlagSpec{
        // local.
        {name: "c", boolVar: &testC},
        {name: "file", multiOK: true},
-       {name: "i", boolVar: &testI},
        {name: "cover", boolVar: &testCover},
        {name: "coverpkg"},
 
@@ -75,6 +74,7 @@ var testFlagDefn = []*testFlagSpec{
        {name: "n", boolVar: &buildN},
        {name: "p"},
        {name: "x", boolVar: &buildX},
+       {name: "i", boolVar: &buildI},
        {name: "work", boolVar: &buildWork},
        {name: "ccflags"},
        {name: "gcflags"},