aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRuss Cox <rsc@golang.org>2012-03-12 16:49:12 -0400
committerRuss Cox <rsc@golang.org>2012-03-12 16:49:12 -0400
commitbccafa72107a60c1443bd405849df94349d3302e (patch)
treed812658d2947f85b1d9ee2dda77838e851466ade
parent2d3cc97c9c7ced9d13d99611c3f3a69b4c81fbba (diff)
downloadgo-bccafa72107a60c1443bd405849df94349d3302e.tar.gz
go-bccafa72107a60c1443bd405849df94349d3302e.zip
cmd/go: respect $GOBIN always
Before, we only consulted $GOBIN for source code found in $GOROOT, but that's confusing to explain and less useful. The new behavior lets users set GOBIN=$HOME/bin and have all go-compiled binaries installed there. Fixes #3269. R=golang-dev, bradfitz CC=golang-dev https://golang.org/cl/5754088
-rw-r--r--doc/install-source.html5
-rw-r--r--src/cmd/go/build.go35
-rw-r--r--src/cmd/go/doc.go4
-rw-r--r--src/cmd/go/help.go4
-rw-r--r--src/cmd/go/pkg.go4
5 files changed, 30 insertions, 22 deletions
diff --git a/doc/install-source.html b/doc/install-source.html
index 82ff8e740d..4673850f42 100644
--- a/doc/install-source.html
+++ b/doc/install-source.html
@@ -393,11 +393,12 @@ For example, you should not set <code>$GOHOSTARCH</code> to
<p><code>$GOBIN</code>
<p>
-The location where binaries from the main repository will be installed.
-XXX THIS MAY CHANGE TO BE AN OVERRIDE EVEN FOR GOPATH ENTRIES XXX
+The location where Go binaries will be installed.
The default is <code>$GOROOT/bin</code>.
After installing, you will want to arrange to add this
directory to your <code>$PATH</code>, so you can use the tools.
+If <code>$GOBIN</code> is set, the <a href="/cmd/go">go command</a>
+installs all commands there.
</p>
<p><code>$GOARM</code> (arm, default=6)</p>
diff --git a/src/cmd/go/build.go b/src/cmd/go/build.go
index e62de32e16..7929a3a54d 100644
--- a/src/cmd/go/build.go
+++ b/src/cmd/go/build.go
@@ -199,6 +199,8 @@ along with their dependencies.
For more about the build flags, see 'go help build'.
For more about specifying packages, see 'go help packages'.
+For more about where packages and binaries are installed,
+see 'go help gopath'.
See also: go build, go get, go clean.
`,
@@ -302,20 +304,13 @@ const (
)
var (
+ gobin = os.Getenv("GOBIN")
goroot = filepath.Clean(runtime.GOROOT())
- gobin = defaultGobin()
gorootSrcPkg = filepath.Join(goroot, "src/pkg")
gorootPkg = filepath.Join(goroot, "pkg")
gorootSrc = filepath.Join(goroot, "src")
)
-func defaultGobin() string {
- if s := os.Getenv("GOBIN"); s != "" {
- return s
- }
- return filepath.Join(goroot, "bin")
-}
-
func (b *builder) init() {
var err error
b.print = fmt.Print
@@ -387,18 +382,24 @@ func goFilesPackage(gofiles []string) *Package {
pkg.load(&stk, bp, err)
pkg.localPrefix = dirToImportPath(dir)
pkg.ImportPath = "command-line-arguments"
+ pkg.target = ""
- if *buildO == "" {
- if pkg.Name == "main" {
- _, elem := filepath.Split(gofiles[0])
- *buildO = elem[:len(elem)-len(".go")] + exeSuffix
- } else {
+ if pkg.Name == "main" {
+ _, elem := filepath.Split(gofiles[0])
+ exe := elem[:len(elem)-len(".go")] + exeSuffix
+ if *buildO == "" {
+ *buildO = exe
+ }
+ if gobin != "" {
+ pkg.target = filepath.Join(gobin, exe)
+ }
+ } else {
+ if *buildO == "" {
*buildO = pkg.Name + ".a"
}
}
- pkg.target = ""
- pkg.Target = ""
pkg.Stale = true
+ pkg.Target = pkg.target
computeStale(pkg)
return pkg
@@ -462,13 +463,13 @@ func (b *builder) action(mode buildMode, depMode buildMode, p *Package) *action
return a
}
- if p.local {
+ a.link = p.Name == "main"
+ if p.local && (!a.link || p.target == "") {
// Imported via local path. No permanent target.
mode = modeBuild
}
a.objdir = filepath.Join(b.work, a.p.ImportPath, "_obj") + string(filepath.Separator)
a.objpkg = buildToolchain.pkgpath(b.work, a.p)
- a.link = p.Name == "main"
switch mode {
case modeInstall:
diff --git a/src/cmd/go/doc.go b/src/cmd/go/doc.go
index eb9c38b639..aacd7269e6 100644
--- a/src/cmd/go/doc.go
+++ b/src/cmd/go/doc.go
@@ -453,7 +453,9 @@ the final element, not the entire path. That is, the
command with source in DIR/src/foo/quux is installed into
DIR/bin/quux, not DIR/bin/foo/quux. The foo/ is stripped
so that you can add DIR/bin to your PATH to get at the
-installed commands.
+installed commands. If the GOBIN environment variable is
+set, commands are installed to the directory it names instead
+of DIR/bin.
Here's an example directory layout:
diff --git a/src/cmd/go/help.go b/src/cmd/go/help.go
index 26640d833c..47ea0c7110 100644
--- a/src/cmd/go/help.go
+++ b/src/cmd/go/help.go
@@ -209,7 +209,9 @@ the final element, not the entire path. That is, the
command with source in DIR/src/foo/quux is installed into
DIR/bin/quux, not DIR/bin/foo/quux. The foo/ is stripped
so that you can add DIR/bin to your PATH to get at the
-installed commands.
+installed commands. If the GOBIN environment variable is
+set, commands are installed to the directory it names instead
+of DIR/bin.
Here's an example directory layout:
diff --git a/src/cmd/go/pkg.go b/src/cmd/go/pkg.go
index 1b6a8c5124..9a72bb1e2e 100644
--- a/src/cmd/go/pkg.go
+++ b/src/cmd/go/pkg.go
@@ -276,6 +276,9 @@ func expandScanner(err error) error {
// load populates p using information from bp, err, which should
// be the result of calling build.Context.Import.
func (p *Package) load(stk *importStack, bp *build.Package, err error) *Package {
+ if gobin != "" {
+ bp.BinDir = gobin
+ }
p.copyBuild(bp)
// The localPrefix is the path we interpret ./ imports relative to.
@@ -538,7 +541,6 @@ func loadPackage(arg string, stk *importStack) *Package {
bp, err := build.ImportDir(filepath.Join(gorootSrc, arg), 0)
bp.ImportPath = arg
bp.Goroot = true
- bp.BinDir = gobin
bp.Root = goroot
bp.SrcRoot = gorootSrc
p := new(Package)