From: Russ Cox Date: Tue, 14 Jul 2015 04:27:59 +0000 (-0400) Subject: cmd/go: add -pkgdir build flag X-Git-Tag: go1.5beta2~50 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=b2cb3b416c538406c52e8c83a8f833fae812b9fd;p=gostls13.git cmd/go: add -pkgdir build flag Fixes #10210. Change-Id: I82ddd665bca31773b1fb1b056338c04818ef68f5 Reviewed-on: https://go-review.googlesource.com/12171 Reviewed-by: Rob Pike --- diff --git a/src/cmd/go/alldocs.go b/src/cmd/go/alldocs.go index 33ea685bf5..b85e924486 100644 --- a/src/cmd/go/alldocs.go +++ b/src/cmd/go/alldocs.go @@ -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 diff --git a/src/cmd/go/build.go b/src/cmd/go/build.go index 93aa454666..54944a6340 100644 --- a/src/cmd/go/build.go +++ b/src/cmd/go/build.go @@ -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, "") diff --git a/src/cmd/go/go_test.go b/src/cmd/go/go_test.go index 768cf025a8..61eecbebad 100644 --- a/src/cmd/go/go_test.go +++ b/src/cmd/go/go_test.go @@ -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) +} diff --git a/src/cmd/go/pkg.go b/src/cmd/go/pkg.go index 432a98ba99..03858d9b4c 100644 --- a/src/cmd/go/pkg.go +++ b/src/cmd/go/pkg.go @@ -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