1
0
mirror of https://github.com/golang/go synced 2024-11-17 21:54:49 -07:00

cmd/go: reject -buildmode=plugin if package is not main

Fixes #17625

Change-Id: I09319b888b547c631a50dbfab9255cc08e8a2426
Reviewed-on: https://go-review.googlesource.com/40691
Reviewed-by: Ian Lance Taylor <iant@golang.org>
Run-TryBot: Ian Lance Taylor <iant@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
This commit is contained in:
Alexander Menzhinsky 2017-04-13 16:15:26 -05:00 committed by Ian Lance Taylor
parent c99483feb8
commit b225051f1d
2 changed files with 26 additions and 9 deletions

View File

@ -2214,6 +2214,20 @@ func TestCoverageImportMainLoop(t *testing.T) {
tg.grepStderr("not an importable package", "did not detect import main")
}
func TestPluginNonMain(t *testing.T) {
wd, err := os.Getwd()
if err != nil {
t.Fatal(err)
}
pkg := filepath.Join(wd, "testdata", "testdep", "p2")
tg := testgo(t)
defer tg.cleanup()
tg.runFail("build", "-buildmode=plugin", pkg)
}
func TestTestEmpty(t *testing.T) {
if !canRace {
t.Skip("no race detector")

View File

@ -255,6 +255,13 @@ func pkgsNotMain(pkgs []*load.Package) (res []*load.Package) {
return res
}
func oneMainPkg(pkgs []*load.Package) []*load.Package {
if len(pkgs) != 1 || pkgs[0].Name != "main" {
base.Fatalf("-buildmode=%s requires exactly one main package", cfg.BuildBuildmode)
}
return pkgs
}
var pkgsFilter = func(pkgs []*load.Package) []*load.Package { return pkgs }
func BuildModeInit() {
@ -265,12 +272,7 @@ func BuildModeInit() {
case "archive":
pkgsFilter = pkgsNotMain
case "c-archive":
pkgsFilter = func(p []*load.Package) []*load.Package {
if len(p) != 1 || p[0].Name != "main" {
base.Fatalf("-buildmode=c-archive requires exactly one main package")
}
return p
}
pkgsFilter = oneMainPkg
switch platform {
case "darwin/arm", "darwin/arm64":
codegenArg = "-shared"
@ -347,7 +349,7 @@ func BuildModeInit() {
}
ldBuildmode = "shared"
case "plugin":
pkgsFilter = pkgsMain
pkgsFilter = oneMainPkg
if gccgo {
codegenArg = "-fPIC"
} else {
@ -454,9 +456,10 @@ func runBuild(cmd *base.Command, args []string) {
return
}
pkgs = pkgsFilter(load.Packages(args))
var a *Action
if cfg.BuildBuildmode == "shared" {
pkgs := pkgsFilter(load.Packages(args))
if libName, err := libname(args, pkgs); err != nil {
base.Fatalf("%s", err.Error())
} else {
@ -464,7 +467,7 @@ func runBuild(cmd *base.Command, args []string) {
}
} else {
a = &Action{}
for _, p := range pkgsFilter(load.Packages(args)) {
for _, p := range pkgs {
a.Deps = append(a.Deps, b.Action(ModeBuild, depMode, p))
}
}