]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/go: add -pkgdir build flag
authorRuss Cox <rsc@golang.org>
Tue, 14 Jul 2015 04:27:59 +0000 (00:27 -0400)
committerRuss Cox <rsc@golang.org>
Wed, 15 Jul 2015 04:19:08 +0000 (04:19 +0000)
Fixes #10210.

Change-Id: I82ddd665bca31773b1fb1b056338c04818ef68f5
Reviewed-on: https://go-review.googlesource.com/12171
Reviewed-by: Rob Pike <r@golang.org>
src/cmd/go/alldocs.go
src/cmd/go/build.go
src/cmd/go/go_test.go
src/cmd/go/pkg.go

index 33ea685bf541434f3088129e49c48653ea51b4ee..b85e924486cc6403018de193db5625b696e081c4 100644 (file)
@@ -96,11 +96,10 @@ and test commands:
        -x
                print the commands.
 
+       -asmflags 'flag list'
+               arguments to pass on each go tool asm invocation.
        -buildmode mode
                build mode to use. See 'go help buildmode' for more.
-       -linkshared
-               link against shared libraries previously created with
-               -buildmode=shared
        -compiler name
                name of compiler to use, as in runtime.Compiler (gccgo or gc).
        -gccgoflags 'arg list'
@@ -115,8 +114,13 @@ and test commands:
                option that requires non-default compile flags has a similar effect.
        -ldflags 'flag list'
                arguments to pass on each go tool link invocation.
-       -asmflags 'flag list'
-               arguments to pass on each go tool asm invocation.
+       -linkshared
+               link against shared libraries previously created with
+               -buildmode=shared
+       -pkgdir dir
+               install and load all packages from dir instead of the usual locations.
+               For example, when building with a non-standard configuration,
+               use -pkgdir to keep generated packages in a separate location.
        -tags 'tag list'
                a list of build tags to consider satisfied during the build.
                For more information about build tags, see the description of
index 93aa4546660027b08743060842faac455061a182..54944a6340c3199d5ed45ae6c1eebdfbc165c784 100644 (file)
@@ -76,11 +76,10 @@ and test commands:
        -x
                print the commands.
 
+       -asmflags 'flag list'
+               arguments to pass on each go tool asm invocation.
        -buildmode mode
                build mode to use. See 'go help buildmode' for more.
-       -linkshared
-               link against shared libraries previously created with
-               -buildmode=shared
        -compiler name
                name of compiler to use, as in runtime.Compiler (gccgo or gc).
        -gccgoflags 'arg list'
@@ -95,8 +94,13 @@ and test commands:
                option that requires non-default compile flags has a similar effect.
        -ldflags 'flag list'
                arguments to pass on each go tool link invocation.
-       -asmflags 'flag list'
-               arguments to pass on each go tool asm invocation.
+       -linkshared
+               link against shared libraries previously created with
+               -buildmode=shared
+       -pkgdir dir
+               install and load all packages from dir instead of the usual locations.
+               For example, when building with a non-standard configuration,
+               use -pkgdir to keep generated packages in a separate location.
        -tags 'tag list'
                a list of build tags to consider satisfied during the build.
                For more information about build tags, see the description of
@@ -157,6 +161,7 @@ var buildRace bool           // -race flag
 var buildToolExec []string   // -toolexec flag
 var buildBuildmode string    // -buildmode flag
 var buildLinkshared bool     // -linkshared flag
+var buildPkgdir string       // -pkgdir flag
 
 var buildContext = build.Default
 var buildToolchain toolchain = noToolchain{}
@@ -196,21 +201,22 @@ func init() {
 // addBuildFlags adds the flags common to the build, clean, get,
 // install, list, run, and test commands.
 func addBuildFlags(cmd *Command) {
-       // NOTE: If you add flags here, also add them to testflag.go.
        cmd.Flag.BoolVar(&buildA, "a", false, "")
        cmd.Flag.BoolVar(&buildN, "n", false, "")
        cmd.Flag.IntVar(&buildP, "p", buildP, "")
        cmd.Flag.BoolVar(&buildV, "v", false, "")
        cmd.Flag.BoolVar(&buildX, "x", false, "")
+
        cmd.Flag.Var((*stringsFlag)(&buildAsmflags), "asmflags", "")
+       cmd.Flag.Var(buildCompiler{}, "compiler", "")
+       cmd.Flag.StringVar(&buildBuildmode, "buildmode", "default", "")
        cmd.Flag.Var((*stringsFlag)(&buildGcflags), "gcflags", "")
-       cmd.Flag.Var((*stringsFlag)(&buildLdflags), "ldflags", "")
        cmd.Flag.Var((*stringsFlag)(&buildGccgoflags), "gccgoflags", "")
        cmd.Flag.StringVar(&buildContext.InstallSuffix, "installsuffix", "", "")
-       cmd.Flag.Var(buildCompiler{}, "compiler", "")
-       cmd.Flag.BoolVar(&buildRace, "race", false, "")
-       cmd.Flag.StringVar(&buildBuildmode, "buildmode", "default", "")
+       cmd.Flag.Var((*stringsFlag)(&buildLdflags), "ldflags", "")
        cmd.Flag.BoolVar(&buildLinkshared, "linkshared", false, "")
+       cmd.Flag.StringVar(&buildPkgdir, "pkgdir", "", "")
+       cmd.Flag.BoolVar(&buildRace, "race", false, "")
        cmd.Flag.Var((*stringsFlag)(&buildContext.BuildTags), "tags", "")
        cmd.Flag.Var((*stringsFlag)(&buildToolExec), "toolexec", "")
        cmd.Flag.BoolVar(&buildWork, "work", false, "")
index 768cf025a8af7b5d70065a2c01a3f81565c0ed85..61eecbebad9ed092e6181e0ca7ef9a7d6a5cb1d7 100644 (file)
@@ -2065,3 +2065,15 @@ func TestGoRunDirs(t *testing.T) {
        tg.runFail("run", "sub/sub.go", "x.go")
        tg.grepStderr("named files must all be in one directory; have sub/ and .", "wrong output")
 }
+
+func TestGoInstallPkgdir(t *testing.T) {
+       tg := testgo(t)
+       defer tg.cleanup()
+       tg.makeTempdir()
+       pkg := tg.path(".")
+       tg.run("install", "-pkgdir", pkg, "errors")
+       _, err := os.Stat(filepath.Join(pkg, "errors.a"))
+       tg.must(err)
+       _, err = os.Stat(filepath.Join(pkg, "runtime.a"))
+       tg.must(err)
+}
index 432a98ba994549bb52f4787604652dd9b5e1da04..03858d9b4c1a4a4f718f0d698b802d5483cbe258 100644 (file)
@@ -110,6 +110,13 @@ type CoverVar struct {
 func (p *Package) copyBuild(pp *build.Package) {
        p.build = pp
 
+       if pp.PkgTargetRoot != "" && buildPkgdir != "" {
+               old := pp.PkgTargetRoot
+               pp.PkgRoot = buildPkgdir
+               pp.PkgTargetRoot = buildPkgdir
+               pp.PkgObj = filepath.Join(buildPkgdir, strings.TrimPrefix(pp.PkgObj, old))
+       }
+
        p.Dir = pp.Dir
        p.ImportPath = pp.ImportPath
        p.ImportComment = pp.ImportComment