]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/go: accept relative -pkgdir argument
authorRuss Cox <rsc@golang.org>
Mon, 6 Nov 2017 20:43:47 +0000 (15:43 -0500)
committerRuss Cox <rsc@golang.org>
Tue, 7 Nov 2017 01:50:16 +0000 (01:50 +0000)
Fixes #21309.

Change-Id: I8ff1b0f37e34a3a4e9f8448d66a64fe3863d081f
Reviewed-on: https://go-review.googlesource.com/76250
Run-TryBot: Russ Cox <rsc@golang.org>
Reviewed-by: Rob Pike <r@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>

src/cmd/go/go_test.go
src/cmd/go/internal/get/get.go
src/cmd/go/internal/list/list.go
src/cmd/go/internal/run/run.go
src/cmd/go/internal/test/test.go
src/cmd/go/internal/vet/vet.go
src/cmd/go/internal/work/build.go
src/cmd/go/internal/work/init.go [new file with mode: 0644]

index c56cd94732d39e19678abb13b2fcca8b1c328165..ca1e392833a0e8d4bb1794a42ca62be3f33293ed 100644 (file)
@@ -4982,3 +4982,13 @@ func TestFmtLoadErrors(t *testing.T) {
        tg.runFail("fmt", "does-not-exist")
        tg.run("fmt", "-n", "exclude")
 }
+
+func TestRelativePkgdir(t *testing.T) {
+       tg := testgo(t)
+       defer tg.cleanup()
+       tg.makeTempdir()
+       tg.setenv("GOCACHE", "off")
+       tg.cd(tg.tempdir)
+
+       tg.run("build", "-i", "-pkgdir=.", "runtime")
+}
index bef1d761c89fa65cbbc53a796054445ca48ea982..d42dae6e61ff510f6123b935a269940f80c7ba25 100644 (file)
@@ -90,8 +90,7 @@ func init() {
 }
 
 func runGet(cmd *base.Command, args []string) {
-       work.InstrumentInit()
-       work.BuildModeInit()
+       work.BuildInit()
 
        if *getF && !*getU {
                base.Fatalf("go get: cannot use -f flag without -u")
index b473397194debaf99a1fa27f75cd6eb44c5f0349..7435273000421cabbe44e4526ebefb2d9e4a9180 100644 (file)
@@ -152,7 +152,7 @@ var listJson = CmdList.Flag.Bool("json", false, "")
 var nl = []byte{'\n'}
 
 func runList(cmd *base.Command, args []string) {
-       work.BuildModeInit()
+       work.BuildInit()
        out := newTrackingWriter(os.Stdout)
        defer out.w.Flush()
 
index d67f02ad10731517167d6c8070c387c38f9c59c1..ce24748f4e33af58498ab15b7de6dbb9c7ff1b50 100644 (file)
@@ -52,8 +52,7 @@ func printStderr(args ...interface{}) (int, error) {
 }
 
 func runRun(cmd *base.Command, args []string) {
-       work.InstrumentInit()
-       work.BuildModeInit()
+       work.BuildInit()
        var b work.Builder
        b.Init()
        b.Print = printStderr
index 0a76d9309d4cde603db20e66b6f9ac32c119cc63..30b5f4a4f4fd1015d126d58cf7988b2bd04e10de 100644 (file)
@@ -505,8 +505,7 @@ func runTest(cmd *base.Command, args []string) {
 
        work.FindExecCmd() // initialize cached result
 
-       work.InstrumentInit()
-       work.BuildModeInit()
+       work.BuildInit()
        work.VetFlags = testVetFlags
 
        pkgs = load.PackagesForBuild(pkgArgs)
index ff129a62f04cbb33edf6d9cccc96c06c8c42c185..db734c9d846d7f726874214da10104bb7c01ae6b 100644 (file)
@@ -36,8 +36,7 @@ See also: go fmt, go fix.
 func runVet(cmd *base.Command, args []string) {
        vetFlags, pkgArgs := vetFlags(args)
 
-       work.InstrumentInit()
-       work.BuildModeInit()
+       work.BuildInit()
        work.VetFlags = vetFlags
 
        pkgs := load.PackagesForBuild(pkgArgs)
index 18b20133a62151d391de256a7e66825362860039..136d0fbac4b999cbab8bbe6a672f2b379041bf29 100644 (file)
@@ -6,7 +6,6 @@ package work
 
 import (
        "errors"
-       "flag"
        "fmt"
        "go/build"
        "os"
@@ -256,153 +255,10 @@ func oneMainPkg(pkgs []*load.Package) []*load.Package {
 
 var pkgsFilter = func(pkgs []*load.Package) []*load.Package { return pkgs }
 
-func BuildModeInit() {
-       gccgo := cfg.BuildToolchainName == "gccgo"
-       var codegenArg string
-       platform := cfg.Goos + "/" + cfg.Goarch
-       switch cfg.BuildBuildmode {
-       case "archive":
-               pkgsFilter = pkgsNotMain
-       case "c-archive":
-               pkgsFilter = oneMainPkg
-               switch platform {
-               case "darwin/arm", "darwin/arm64":
-                       codegenArg = "-shared"
-               default:
-                       switch cfg.Goos {
-                       case "dragonfly", "freebsd", "linux", "netbsd", "openbsd", "solaris":
-                               // Use -shared so that the result is
-                               // suitable for inclusion in a PIE or
-                               // shared library.
-                               codegenArg = "-shared"
-                       }
-               }
-               cfg.ExeSuffix = ".a"
-               ldBuildmode = "c-archive"
-       case "c-shared":
-               pkgsFilter = oneMainPkg
-               if gccgo {
-                       codegenArg = "-fPIC"
-               } else {
-                       switch platform {
-                       case "linux/amd64", "linux/arm", "linux/arm64", "linux/386", "linux/ppc64le",
-                               "android/amd64", "android/arm", "android/arm64", "android/386":
-                               codegenArg = "-shared"
-                       case "darwin/amd64", "darwin/386":
-                       case "windows/amd64", "windows/386":
-                               // Do not add usual .exe suffix to the .dll file.
-                               cfg.ExeSuffix = ""
-                       default:
-                               base.Fatalf("-buildmode=c-shared not supported on %s\n", platform)
-                       }
-               }
-               ldBuildmode = "c-shared"
-       case "default":
-               switch platform {
-               case "android/arm", "android/arm64", "android/amd64", "android/386":
-                       codegenArg = "-shared"
-                       ldBuildmode = "pie"
-               case "darwin/arm", "darwin/arm64":
-                       codegenArg = "-shared"
-                       fallthrough
-               default:
-                       ldBuildmode = "exe"
-               }
-       case "exe":
-               pkgsFilter = pkgsMain
-               ldBuildmode = "exe"
-       case "pie":
-               if cfg.BuildRace {
-                       base.Fatalf("-buildmode=pie not supported when -race is enabled")
-               }
-               if gccgo {
-                       base.Fatalf("-buildmode=pie not supported by gccgo")
-               } else {
-                       switch platform {
-                       case "linux/386", "linux/amd64", "linux/arm", "linux/arm64", "linux/ppc64le", "linux/s390x",
-                               "android/amd64", "android/arm", "android/arm64", "android/386":
-                               codegenArg = "-shared"
-                       case "darwin/amd64":
-                               codegenArg = "-shared"
-                       default:
-                               base.Fatalf("-buildmode=pie not supported on %s\n", platform)
-                       }
-               }
-               ldBuildmode = "pie"
-       case "shared":
-               pkgsFilter = pkgsNotMain
-               if gccgo {
-                       codegenArg = "-fPIC"
-               } else {
-                       switch platform {
-                       case "linux/386", "linux/amd64", "linux/arm", "linux/arm64", "linux/ppc64le", "linux/s390x":
-                       default:
-                               base.Fatalf("-buildmode=shared not supported on %s\n", platform)
-                       }
-                       codegenArg = "-dynlink"
-               }
-               if cfg.BuildO != "" {
-                       base.Fatalf("-buildmode=shared and -o not supported together")
-               }
-               ldBuildmode = "shared"
-       case "plugin":
-               pkgsFilter = oneMainPkg
-               if gccgo {
-                       codegenArg = "-fPIC"
-               } else {
-                       switch platform {
-                       case "linux/amd64", "linux/arm", "linux/arm64", "linux/386", "linux/s390x", "linux/ppc64le",
-                               "android/amd64", "android/arm", "android/arm64", "android/386":
-                       case "darwin/amd64":
-                               // Skip DWARF generation due to #21647
-                               cfg.BuildLdflags = append(cfg.BuildLdflags, "-w")
-                       default:
-                               base.Fatalf("-buildmode=plugin not supported on %s\n", platform)
-                       }
-                       codegenArg = "-dynlink"
-               }
-               cfg.ExeSuffix = ".so"
-               ldBuildmode = "plugin"
-       default:
-               base.Fatalf("buildmode=%s not supported", cfg.BuildBuildmode)
-       }
-       if cfg.BuildLinkshared {
-               if gccgo {
-                       codegenArg = "-fPIC"
-               } else {
-                       switch platform {
-                       case "linux/386", "linux/amd64", "linux/arm", "linux/arm64", "linux/ppc64le", "linux/s390x":
-                               buildAsmflags = append(buildAsmflags, "-D=GOBUILDMODE_shared=1")
-                       default:
-                               base.Fatalf("-linkshared not supported on %s\n", platform)
-                       }
-                       codegenArg = "-dynlink"
-                       // TODO(mwhudson): remove -w when that gets fixed in linker.
-                       cfg.BuildLdflags = append(cfg.BuildLdflags, "-linkshared", "-w")
-               }
-       }
-       if codegenArg != "" {
-               if gccgo {
-                       buildGccgoflags = append([]string{codegenArg}, buildGccgoflags...)
-               } else {
-                       buildAsmflags = append([]string{codegenArg}, buildAsmflags...)
-                       buildGcflags = append([]string{codegenArg}, buildGcflags...)
-               }
-               // Don't alter InstallSuffix when modifying default codegen args.
-               if cfg.BuildBuildmode != "default" || cfg.BuildLinkshared {
-                       if cfg.BuildContext.InstallSuffix != "" {
-                               cfg.BuildContext.InstallSuffix += "_"
-                       }
-                       cfg.BuildContext.InstallSuffix += codegenArg[1:]
-               }
-       }
-}
-
 var runtimeVersion = runtime.Version()
 
 func runBuild(cmd *base.Command, args []string) {
-       InstrumentInit()
-       BuildModeInit()
+       BuildInit()
        var b Builder
        b.Init()
 
@@ -537,8 +393,7 @@ func libname(args []string, pkgs []*load.Package) (string, error) {
 }
 
 func runInstall(cmd *base.Command, args []string) {
-       InstrumentInit()
-       BuildModeInit()
+       BuildInit()
        InstallPackages(args, false)
 }
 
@@ -639,50 +494,6 @@ func InstallPackages(args []string, forGet bool) {
        }
 }
 
-func InstrumentInit() {
-       if !cfg.BuildRace && !cfg.BuildMSan {
-               return
-       }
-       if cfg.BuildRace && cfg.BuildMSan {
-               fmt.Fprintf(os.Stderr, "go %s: may not use -race and -msan simultaneously\n", flag.Args()[0])
-               os.Exit(2)
-       }
-       if cfg.BuildMSan && (cfg.Goos != "linux" || cfg.Goarch != "amd64") {
-               fmt.Fprintf(os.Stderr, "-msan is not supported on %s/%s\n", cfg.Goos, cfg.Goarch)
-               os.Exit(2)
-       }
-       if cfg.Goarch != "amd64" || cfg.Goos != "linux" && cfg.Goos != "freebsd" && cfg.Goos != "darwin" && cfg.Goos != "windows" {
-               fmt.Fprintf(os.Stderr, "go %s: -race and -msan are only supported on linux/amd64, freebsd/amd64, darwin/amd64 and windows/amd64\n", flag.Args()[0])
-               os.Exit(2)
-       }
-       if !cfg.BuildContext.CgoEnabled {
-               instrFlag := "-race"
-               if cfg.BuildMSan {
-                       instrFlag = "-msan"
-               }
-               fmt.Fprintf(os.Stderr, "go %s: %s requires cgo; enable cgo by setting CGO_ENABLED=1\n", flag.Args()[0], instrFlag)
-               os.Exit(2)
-       }
-       if cfg.BuildRace {
-               buildGcflags = append(buildGcflags, "-race")
-               cfg.BuildLdflags = append(cfg.BuildLdflags, "-race")
-       } else {
-               buildGcflags = append(buildGcflags, "-msan")
-               cfg.BuildLdflags = append(cfg.BuildLdflags, "-msan")
-       }
-       if cfg.BuildContext.InstallSuffix != "" {
-               cfg.BuildContext.InstallSuffix += "_"
-       }
-
-       if cfg.BuildRace {
-               cfg.BuildContext.InstallSuffix += "race"
-               cfg.BuildContext.BuildTags = append(cfg.BuildContext.BuildTags, "race")
-       } else {
-               cfg.BuildContext.InstallSuffix += "msan"
-               cfg.BuildContext.BuildTags = append(cfg.BuildContext.BuildTags, "msan")
-       }
-}
-
 // ExecCmd is the command to use to run user binaries.
 // Normally it is empty, meaning run the binaries directly.
 // If cross-compiling and running on a remote system or
diff --git a/src/cmd/go/internal/work/init.go b/src/cmd/go/internal/work/init.go
new file mode 100644 (file)
index 0000000..0f622f5
--- /dev/null
@@ -0,0 +1,218 @@
+// Copyright 2017 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+// Build initialization (after flag parsing).
+
+package work
+
+import (
+       "cmd/go/internal/base"
+       "cmd/go/internal/cfg"
+       "flag"
+       "fmt"
+       "os"
+       "path/filepath"
+)
+
+func BuildInit() {
+       instrumentInit()
+       buildModeInit()
+
+       // Make sure -pkgdir is absolute, because we run commands
+       // in different directories.
+       if cfg.BuildPkgdir != "" && !filepath.IsAbs(cfg.BuildPkgdir) {
+               p, err := filepath.Abs(cfg.BuildPkgdir)
+               if err != nil {
+                       fmt.Fprintf(os.Stderr, "go %s: evaluating -pkgdir: %v\n", flag.Args()[0], err)
+                       os.Exit(2)
+               }
+               cfg.BuildPkgdir = p
+       }
+}
+
+func instrumentInit() {
+       if !cfg.BuildRace && !cfg.BuildMSan {
+               return
+       }
+       if cfg.BuildRace && cfg.BuildMSan {
+               fmt.Fprintf(os.Stderr, "go %s: may not use -race and -msan simultaneously\n", flag.Args()[0])
+               os.Exit(2)
+       }
+       if cfg.BuildMSan && (cfg.Goos != "linux" || cfg.Goarch != "amd64") {
+               fmt.Fprintf(os.Stderr, "-msan is not supported on %s/%s\n", cfg.Goos, cfg.Goarch)
+               os.Exit(2)
+       }
+       if cfg.Goarch != "amd64" || cfg.Goos != "linux" && cfg.Goos != "freebsd" && cfg.Goos != "darwin" && cfg.Goos != "windows" {
+               fmt.Fprintf(os.Stderr, "go %s: -race and -msan are only supported on linux/amd64, freebsd/amd64, darwin/amd64 and windows/amd64\n", flag.Args()[0])
+               os.Exit(2)
+       }
+       if !cfg.BuildContext.CgoEnabled {
+               instrFlag := "-race"
+               if cfg.BuildMSan {
+                       instrFlag = "-msan"
+               }
+               fmt.Fprintf(os.Stderr, "go %s: %s requires cgo; enable cgo by setting CGO_ENABLED=1\n", flag.Args()[0], instrFlag)
+               os.Exit(2)
+       }
+       if cfg.BuildRace {
+               buildGcflags = append(buildGcflags, "-race")
+               cfg.BuildLdflags = append(cfg.BuildLdflags, "-race")
+       } else {
+               buildGcflags = append(buildGcflags, "-msan")
+               cfg.BuildLdflags = append(cfg.BuildLdflags, "-msan")
+       }
+       if cfg.BuildContext.InstallSuffix != "" {
+               cfg.BuildContext.InstallSuffix += "_"
+       }
+
+       if cfg.BuildRace {
+               cfg.BuildContext.InstallSuffix += "race"
+               cfg.BuildContext.BuildTags = append(cfg.BuildContext.BuildTags, "race")
+       } else {
+               cfg.BuildContext.InstallSuffix += "msan"
+               cfg.BuildContext.BuildTags = append(cfg.BuildContext.BuildTags, "msan")
+       }
+}
+
+func buildModeInit() {
+       gccgo := cfg.BuildToolchainName == "gccgo"
+       var codegenArg string
+       platform := cfg.Goos + "/" + cfg.Goarch
+       switch cfg.BuildBuildmode {
+       case "archive":
+               pkgsFilter = pkgsNotMain
+       case "c-archive":
+               pkgsFilter = oneMainPkg
+               switch platform {
+               case "darwin/arm", "darwin/arm64":
+                       codegenArg = "-shared"
+               default:
+                       switch cfg.Goos {
+                       case "dragonfly", "freebsd", "linux", "netbsd", "openbsd", "solaris":
+                               // Use -shared so that the result is
+                               // suitable for inclusion in a PIE or
+                               // shared library.
+                               codegenArg = "-shared"
+                       }
+               }
+               cfg.ExeSuffix = ".a"
+               ldBuildmode = "c-archive"
+       case "c-shared":
+               pkgsFilter = oneMainPkg
+               if gccgo {
+                       codegenArg = "-fPIC"
+               } else {
+                       switch platform {
+                       case "linux/amd64", "linux/arm", "linux/arm64", "linux/386", "linux/ppc64le",
+                               "android/amd64", "android/arm", "android/arm64", "android/386":
+                               codegenArg = "-shared"
+                       case "darwin/amd64", "darwin/386":
+                       case "windows/amd64", "windows/386":
+                               // Do not add usual .exe suffix to the .dll file.
+                               cfg.ExeSuffix = ""
+                       default:
+                               base.Fatalf("-buildmode=c-shared not supported on %s\n", platform)
+                       }
+               }
+               ldBuildmode = "c-shared"
+       case "default":
+               switch platform {
+               case "android/arm", "android/arm64", "android/amd64", "android/386":
+                       codegenArg = "-shared"
+                       ldBuildmode = "pie"
+               case "darwin/arm", "darwin/arm64":
+                       codegenArg = "-shared"
+                       fallthrough
+               default:
+                       ldBuildmode = "exe"
+               }
+       case "exe":
+               pkgsFilter = pkgsMain
+               ldBuildmode = "exe"
+       case "pie":
+               if cfg.BuildRace {
+                       base.Fatalf("-buildmode=pie not supported when -race is enabled")
+               }
+               if gccgo {
+                       base.Fatalf("-buildmode=pie not supported by gccgo")
+               } else {
+                       switch platform {
+                       case "linux/386", "linux/amd64", "linux/arm", "linux/arm64", "linux/ppc64le", "linux/s390x",
+                               "android/amd64", "android/arm", "android/arm64", "android/386":
+                               codegenArg = "-shared"
+                       case "darwin/amd64":
+                               codegenArg = "-shared"
+                       default:
+                               base.Fatalf("-buildmode=pie not supported on %s\n", platform)
+                       }
+               }
+               ldBuildmode = "pie"
+       case "shared":
+               pkgsFilter = pkgsNotMain
+               if gccgo {
+                       codegenArg = "-fPIC"
+               } else {
+                       switch platform {
+                       case "linux/386", "linux/amd64", "linux/arm", "linux/arm64", "linux/ppc64le", "linux/s390x":
+                       default:
+                               base.Fatalf("-buildmode=shared not supported on %s\n", platform)
+                       }
+                       codegenArg = "-dynlink"
+               }
+               if cfg.BuildO != "" {
+                       base.Fatalf("-buildmode=shared and -o not supported together")
+               }
+               ldBuildmode = "shared"
+       case "plugin":
+               pkgsFilter = oneMainPkg
+               if gccgo {
+                       codegenArg = "-fPIC"
+               } else {
+                       switch platform {
+                       case "linux/amd64", "linux/arm", "linux/arm64", "linux/386", "linux/s390x", "linux/ppc64le",
+                               "android/amd64", "android/arm", "android/arm64", "android/386":
+                       case "darwin/amd64":
+                               // Skip DWARF generation due to #21647
+                               cfg.BuildLdflags = append(cfg.BuildLdflags, "-w")
+                       default:
+                               base.Fatalf("-buildmode=plugin not supported on %s\n", platform)
+                       }
+                       codegenArg = "-dynlink"
+               }
+               cfg.ExeSuffix = ".so"
+               ldBuildmode = "plugin"
+       default:
+               base.Fatalf("buildmode=%s not supported", cfg.BuildBuildmode)
+       }
+       if cfg.BuildLinkshared {
+               if gccgo {
+                       codegenArg = "-fPIC"
+               } else {
+                       switch platform {
+                       case "linux/386", "linux/amd64", "linux/arm", "linux/arm64", "linux/ppc64le", "linux/s390x":
+                               buildAsmflags = append(buildAsmflags, "-D=GOBUILDMODE_shared=1")
+                       default:
+                               base.Fatalf("-linkshared not supported on %s\n", platform)
+                       }
+                       codegenArg = "-dynlink"
+                       // TODO(mwhudson): remove -w when that gets fixed in linker.
+                       cfg.BuildLdflags = append(cfg.BuildLdflags, "-linkshared", "-w")
+               }
+       }
+       if codegenArg != "" {
+               if gccgo {
+                       buildGccgoflags = append([]string{codegenArg}, buildGccgoflags...)
+               } else {
+                       buildAsmflags = append([]string{codegenArg}, buildAsmflags...)
+                       buildGcflags = append([]string{codegenArg}, buildGcflags...)
+               }
+               // Don't alter InstallSuffix when modifying default codegen args.
+               if cfg.BuildBuildmode != "default" || cfg.BuildLinkshared {
+                       if cfg.BuildContext.InstallSuffix != "" {
+                               cfg.BuildContext.InstallSuffix += "_"
+                       }
+                       cfg.BuildContext.InstallSuffix += codegenArg[1:]
+               }
+       }
+}