]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/compile: default to -p=main for main package
authorCherry Mui <cherryyz@google.com>
Tue, 22 Mar 2022 21:08:56 +0000 (17:08 -0400)
committerCherry Mui <cherryyz@google.com>
Wed, 23 Mar 2022 14:57:56 +0000 (14:57 +0000)
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>
src/cmd/compile/internal/gc/main.go
src/cmd/link/link_test.go

index 5a9a889894a89e39f229b43ca3c44bfebc27fd28..1f547130ad5af0e4e6fd718ccba092b17063b648 100644 (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()
 
index 2eefc5c8e7d1b0a260c8ea02a06cebb2605f2e4e..8df31d7fd490e6c1557ef6021664c0b89f0bbe15 100644 (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)
+       }
 }