Commit b225051f authored by Alexander Menzhinsky's avatar Alexander Menzhinsky Committed by Ian Lance Taylor

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

Fixes #17625

Change-Id: I09319b888b547c631a50dbfab9255cc08e8a2426
Reviewed-on: https://go-review.googlesource.com/40691Reviewed-by: default avatarIan Lance Taylor <iant@golang.org>
Run-TryBot: Ian Lance Taylor <iant@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
parent c99483fe
...@@ -2214,6 +2214,20 @@ func TestCoverageImportMainLoop(t *testing.T) { ...@@ -2214,6 +2214,20 @@ func TestCoverageImportMainLoop(t *testing.T) {
tg.grepStderr("not an importable package", "did not detect import main") 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) { func TestTestEmpty(t *testing.T) {
if !canRace { if !canRace {
t.Skip("no race detector") t.Skip("no race detector")
......
...@@ -255,6 +255,13 @@ func pkgsNotMain(pkgs []*load.Package) (res []*load.Package) { ...@@ -255,6 +255,13 @@ func pkgsNotMain(pkgs []*load.Package) (res []*load.Package) {
return res 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 } var pkgsFilter = func(pkgs []*load.Package) []*load.Package { return pkgs }
func BuildModeInit() { func BuildModeInit() {
...@@ -265,12 +272,7 @@ func BuildModeInit() { ...@@ -265,12 +272,7 @@ func BuildModeInit() {
case "archive": case "archive":
pkgsFilter = pkgsNotMain pkgsFilter = pkgsNotMain
case "c-archive": case "c-archive":
pkgsFilter = func(p []*load.Package) []*load.Package { pkgsFilter = oneMainPkg
if len(p) != 1 || p[0].Name != "main" {
base.Fatalf("-buildmode=c-archive requires exactly one main package")
}
return p
}
switch platform { switch platform {
case "darwin/arm", "darwin/arm64": case "darwin/arm", "darwin/arm64":
codegenArg = "-shared" codegenArg = "-shared"
...@@ -347,7 +349,7 @@ func BuildModeInit() { ...@@ -347,7 +349,7 @@ func BuildModeInit() {
} }
ldBuildmode = "shared" ldBuildmode = "shared"
case "plugin": case "plugin":
pkgsFilter = pkgsMain pkgsFilter = oneMainPkg
if gccgo { if gccgo {
codegenArg = "-fPIC" codegenArg = "-fPIC"
} else { } else {
...@@ -454,9 +456,10 @@ func runBuild(cmd *base.Command, args []string) { ...@@ -454,9 +456,10 @@ func runBuild(cmd *base.Command, args []string) {
return return
} }
pkgs = pkgsFilter(load.Packages(args))
var a *Action var a *Action
if cfg.BuildBuildmode == "shared" { if cfg.BuildBuildmode == "shared" {
pkgs := pkgsFilter(load.Packages(args))
if libName, err := libname(args, pkgs); err != nil { if libName, err := libname(args, pkgs); err != nil {
base.Fatalf("%s", err.Error()) base.Fatalf("%s", err.Error())
} else { } else {
...@@ -464,7 +467,7 @@ func runBuild(cmd *base.Command, args []string) { ...@@ -464,7 +467,7 @@ func runBuild(cmd *base.Command, args []string) {
} }
} else { } else {
a = &Action{} a = &Action{}
for _, p := range pkgsFilter(load.Packages(args)) { for _, p := range pkgs {
a.Deps = append(a.Deps, b.Action(ModeBuild, depMode, p)) a.Deps = append(a.Deps, b.Action(ModeBuild, depMode, p))
} }
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment