cmd/compile: default to -p=main for main package

With the previous CL the compiler emits an unlinkable object if
the -p flag is not specified. It is actually okay (and convenient)
to omit the -p flag for (just) the main package. This CL makes it
so.

Change-Id: I978d54d14c45b3bb9ed7471e40a2c47f269b56f7
Reviewed-on: https://go-review.googlesource.com/c/go/+/394834
Trust: Cherry Mui <cherryyz@google.com>
Run-TryBot: Cherry Mui <cherryyz@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Matthew Dempsky <mdempsky@google.com>
This commit is contained in:
Cherry Mui 2022-03-22 17:08:56 -04:00
parent 1e34c00b4c
commit 20ba3f5de5
2 changed files with 37 additions and 6 deletions

View file

@ -188,6 +188,9 @@ func Main(archInit func(*ssagen.ArchInfo)) {
// Parse and typecheck input.
noder.LoadPackage(flag.Args())
if base.Ctxt.Pkgpath == obj.UnlinkablePkg && types.LocalPkg.Name == "main" {
base.Ctxt.Pkgpath = "main"
}
dwarfgen.RecordPackageName()

View file

@ -1067,19 +1067,30 @@ func TestUnlinkableObj(t *testing.T) {
tmpdir := t.TempDir()
src := filepath.Join(tmpdir, "x.go")
obj := filepath.Join(tmpdir, "x.o")
xSrc := filepath.Join(tmpdir, "x.go")
pSrc := filepath.Join(tmpdir, "p.go")
xObj := filepath.Join(tmpdir, "x.o")
pObj := filepath.Join(tmpdir, "p.o")
exe := filepath.Join(tmpdir, "x.exe")
err := ioutil.WriteFile(src, []byte("package main\nfunc main() {}\n"), 0666)
err := ioutil.WriteFile(xSrc, []byte("package main\nimport _ \"p\"\nfunc main() {}\n"), 0666)
if err != nil {
t.Fatalf("failed to write source file: %v", err)
}
cmd := exec.Command(testenv.GoToolPath(t), "tool", "compile", "-o", obj, src) // without -p
err = ioutil.WriteFile(pSrc, []byte("package p\n"), 0666)
if err != nil {
t.Fatalf("failed to write source file: %v", err)
}
cmd := exec.Command(testenv.GoToolPath(t), "tool", "compile", "-o", pObj, pSrc) // without -p
out, err := cmd.CombinedOutput()
if err != nil {
t.Fatalf("compile failed: %v. output:\n%s", err, out)
t.Fatalf("compile p.go failed: %v. output:\n%s", err, out)
}
cmd = exec.Command(testenv.GoToolPath(t), "tool", "link", "-o", exe, obj)
cmd = exec.Command(testenv.GoToolPath(t), "tool", "compile", "-I", tmpdir, "-p=main", "-o", xObj, xSrc)
out, err = cmd.CombinedOutput()
if err != nil {
t.Fatalf("compile x.go failed: %v. output:\n%s", err, out)
}
cmd = exec.Command(testenv.GoToolPath(t), "tool", "link", "-L", tmpdir, "-o", exe, xObj)
out, err = cmd.CombinedOutput()
if err == nil {
t.Fatalf("link did not fail")
@ -1087,4 +1098,21 @@ func TestUnlinkableObj(t *testing.T) {
if !bytes.Contains(out, []byte("unlinkable object")) {
t.Errorf("did not see expected error message. out:\n%s", out)
}
// It is okay to omit -p for (only) main package.
cmd = exec.Command(testenv.GoToolPath(t), "tool", "compile", "-p=p", "-o", pObj, pSrc)
out, err = cmd.CombinedOutput()
if err != nil {
t.Fatalf("compile p.go failed: %v. output:\n%s", err, out)
}
cmd = exec.Command(testenv.GoToolPath(t), "tool", "compile", "-I", tmpdir, "-o", xObj, xSrc) // without -p
out, err = cmd.CombinedOutput()
if err != nil {
t.Fatalf("compile failed: %v. output:\n%s", err, out)
}
cmd = exec.Command(testenv.GoToolPath(t), "tool", "link", "-L", tmpdir, "-o", exe, xObj)
out, err = cmd.CombinedOutput()
if err != nil {
t.Errorf("link failed: %v. output:\n%s", err, out)
}
}