]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/compile: require -p flag
authorRuss Cox <rsc@golang.org>
Tue, 8 Mar 2022 23:16:35 +0000 (18:16 -0500)
committerRuss Cox <rsc@golang.org>
Wed, 9 Mar 2022 21:31:58 +0000 (21:31 +0000)
The -p flag specifies the import path of the package being compiled.
This CL makes it required when invoking the compiler and
adjusts tests that invoke the compiler directly to conform to this
new requirement. The go command already passes the flag, so it
is unmodified in this CL. It is expected that any other Go build systems
also already pass -p, or else they will need to arrange to do so before
updating to Go 1.19. Of particular note, Bazel already does for rules
with an importpath= attribute, which includes all Gazelle-generated rules.

There is more cleanup possible now in cmd/compile, cmd/link,
and other consumers of Go object files, but that is left to future CLs.

Additional historical background follows but can be ignored.

Long ago, before the go command, or modules, or any kind of
versioning, symbols in Go archive files were named using just the
package name, so that for example func F in math/rand and func F in
crypto/rand would both be the object file symbol 'rand.F'. This led to
collisions even in small source trees, which made certain packages
unusable in the presence of other packages and generally was a problem
for Go's goal of scaling to very large source trees.

Fixing this problem required changing from package names to import
paths in symbol names, which was mostly straightforward. One wrinkle,
though, is that the compiler did not know the import path of the
package being compiled; it only knew the package name. At the time,
there was no go command, just Makefiles that people had invoking 6g
(now “go tool compile”) and then copying the resulting object file to
an importable location. That is, everyone had a custom build setup for
Go, because there was no standard one. So it was not particularly
attractive to change how the compiler was invoked, since that would
break approximately every Go user at the time. Instead, we arranged
for the compiler to emit, and other tools reading object files to
recognize, a special import path (the empty string, it turned out)
denoting “the import path of this object file”. This worked well
enough at the time and maintained complete command-line compatibility
with existing Go usage.

The changes implementing this transition can be found by searching
the Git history for “package global name space”, which is what they
eliminated. In particular, CL 190076 (a6736fa4), CL 186263 (758f2bc5),
CL 193080 (1cecac81), CL 194053 (19126320), and CL 194071 (531e6b77)
did the bulk of this transformation in January 2010.

Later, in September 2011, we added the -p flag to the compiler for
diagnostic purposes. The problem was that it was easy to create import
cycles, especially in tests, and these could not be diagnosed until
link time. You'd really want the compiler to diagnose these, for
example if the compilation of package sort noticed it was importing a
package that itself imported "sort". But the compilation of package
sort didn't know its own import path, and so it could not tell whether
it had found itself as a transitive dependency. Adding the -p flag
solved this problem, and its use was optional, since the linker would
still diagnose the import cycle in builds that had not updated to
start passing -p. This was CL 4972057 (1e480cd1).

There was still no go command at this point, but when we introduced
the go command we made it pass -p, which it has for many years at this
point.

Over time, parts of the compiler began to depend on the presence of
the -p flag for various reasonable purposes. For example:

In CL 6497074 (041fc8bf; Oct 2012), the race detector used -p to
detect packages that should not have race annotations, such as
runtime/race and sync/atomic.

In CL 13367052 (7276c02b; Sep 2013), a bug fix used -p to detect the
compilation of package reflect.

In CL 30539 (8aadcc55; Oct 2016), the compiler started using -p to
identify package math, to be able to intrinsify calls to Sqrt inside
that package.

In CL 61019 (9daee931; Sep 2017), CL 71430 (2c1d2e06; Oct 2017), and
later related CLs, the compiler started using the -p value when
creating various DWARF debugging information.

In CL 174657 (cc5eaf93; May 2019), the compiler started writing
symbols without the magic empty string whenever -p was used, to reduce
the amount of work required in the linker.

In CL 179861 (dde7c770; Jun 2019), the compiler made the second
argument to //go:linkname optional when -p is used, because in that
case the compiler can derive an appropriate default.

There are more examples. Today it is impossible to compile the Go
standard library without using -p, and DWARF debug information is
incomplete without using -p.

All known Go build systems pass -p. In particular, the go command
does, which is what nearly all Go developers invoke to build Go code.
And Bazel does, for go_library rules that set the importpath
attribute, which is all rules generated by Gazelle.

Gccgo has an equivalent of -p and has required its use in order to
disambiguate packages with the same name but different import paths
since 2010.

On top of all this, various parts of code generation for generics
are made more complicated by needing to cope with the case where -p
is not specified, even though it's essentially always specified.

In summary, the current state is:

 - Use of the -p flag with cmd/compile is required for building
   the standard library, and for complete DWARF information,
   and to enable certain linker speedups.

 - The go command and Bazel, which we expect account for just
   about 100% of Go builds, both invoke cmd/compile with -p.

 - The code in cmd/compile to support builds without -p is
   complex and has become more complex with generics, but it is
   almost always dead code and therefore not worth maintaining.

 - Gccgo already requires its equivalent of -p in any build
   where two packages have the same name.

All this supports the change in this CL, which makes -p required
and adjusts tests that invoke cmd/compile to add -p appropriately.

Future CLs will be able to remove all the code dealing with the
possibility of -p not having been specified.

Change-Id: I6b95b9d4cffe59c7bac82eb273ef6c4a67bb0e43
Reviewed-on: https://go-review.googlesource.com/c/go/+/391014
Trust: Russ Cox <rsc@golang.org>
Run-TryBot: Russ Cox <rsc@golang.org>
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Matthew Dempsky <mdempsky@google.com>
29 files changed:
src/cmd/compile/internal/base/flag.go
src/cmd/compile/internal/importer/gcimporter_test.go
src/cmd/compile/internal/logopt/logopt_test.go
src/cmd/compile/internal/test/fixedbugs_test.go
src/cmd/compile/internal/test/lang_test.go
src/cmd/compile/internal/test/reproduciblebuilds_test.go
src/cmd/internal/archive/archive_test.go
src/cmd/internal/obj/objfile_test.go
src/cmd/link/link_test.go
src/cmd/objdump/objdump_test.go
src/cmd/pack/pack_test.go
src/go/internal/gcimporter/gcimporter_test.go
src/internal/abi/abi_test.go
test/const7.go
test/fixedbugs/bug302.go
test/fixedbugs/bug369.go
test/fixedbugs/issue11771.go
test/fixedbugs/issue21317.go
test/fixedbugs/issue22660.go
test/fixedbugs/issue22662b.go
test/fixedbugs/issue26411.go
test/fixedbugs/issue30908.go
test/fixedbugs/issue9355.go
test/interface/embed1.dir/embed0.go
test/linkmain_run.go
test/linkname2.go [deleted file]
test/linkobj.go
test/run.go
test/sinit_run.go

index 6377091ce0b20967eaf3e9ffc0a1676a1ef5c2ff..0b04f62e1c105d147f639c0a7f49d2b08a2f0857 100644 (file)
@@ -201,6 +201,10 @@ func ParseFlags() {
                Exit(2)
        }
 
+       if *Flag.LowerP == "" {
+               log.Fatalf("-p is required")
+       }
+
        if Flag.LowerO == "" {
                p := flag.Arg(0)
                if i := strings.LastIndex(p, "/"); i >= 0 {
index 5d80db244b9bf3c31ae93ddac34b24f37f3ac901..cc804aabbc5bdcb093e12df0f1c0c0dd579b6100 100644 (file)
@@ -38,7 +38,7 @@ func compile(t *testing.T, dirname, filename, outdirname string) string {
        }
        basename := filepath.Base(filename)
        outname := filepath.Join(outdirname, basename[:len(basename)-2]+"o")
-       cmd := exec.Command(testenv.GoToolPath(t), "tool", "compile", "-o", outname, filename)
+       cmd := exec.Command(testenv.GoToolPath(t), "tool", "compile", "-p=p", "-o", outname, filename)
        cmd.Dir = dirname
        out, err := cmd.CombinedOutput()
        if err != nil {
index 902cbc8091f1eb1e56b1c0507cd25b19b52c78c1..8d07a49cc0fd6041ccf8722193718c0e905d7afa 100644 (file)
@@ -226,7 +226,7 @@ func s15a8(x *[15]int64) [15]int64 {
 }
 
 func testLogOpt(t *testing.T, flag, src, outfile string) (string, error) {
-       run := []string{testenv.GoToolPath(t), "tool", "compile", flag, "-o", outfile, src}
+       run := []string{testenv.GoToolPath(t), "tool", "compile", "-p=p", flag, "-o", outfile, src}
        t.Log(run)
        cmd := exec.Command(run[0], run[1:]...)
        out, err := cmd.CombinedOutput()
@@ -236,7 +236,7 @@ func testLogOpt(t *testing.T, flag, src, outfile string) (string, error) {
 
 func testLogOptDir(t *testing.T, dir, flag, src, outfile string) (string, error) {
        // Notice the specified import path "x"
-       run := []string{testenv.GoToolPath(t), "tool", "compile", "-p", "x", flag, "-o", outfile, src}
+       run := []string{testenv.GoToolPath(t), "tool", "compile", "-p=x", flag, "-o", outfile, src}
        t.Log(run)
        cmd := exec.Command(run[0], run[1:]...)
        cmd.Dir = dir
@@ -247,7 +247,7 @@ func testLogOptDir(t *testing.T, dir, flag, src, outfile string) (string, error)
 
 func testCopy(t *testing.T, dir, goarch, goos, src, outfile string) (string, error) {
        // Notice the specified import path "x"
-       run := []string{testenv.GoToolPath(t), "tool", "compile", "-p", "x", "-json=0,file://log/opt", "-o", outfile, src}
+       run := []string{testenv.GoToolPath(t), "tool", "compile", "-p=x", "-json=0,file://log/opt", "-o", outfile, src}
        t.Log(run)
        cmd := exec.Command(run[0], run[1:]...)
        cmd.Dir = dir
index 376b45edfcf00c9f0bfce4b0668d06fcdd5d43d4..cd0d5fc3536e7136ba2195d665511fb32036c99d 100644 (file)
@@ -72,7 +72,7 @@ func TestIssue16214(t *testing.T) {
                t.Fatalf("could not write file: %v", err)
        }
 
-       cmd := exec.Command(testenv.GoToolPath(t), "tool", "compile", "-S", "-o", filepath.Join(dir, "out.o"), src)
+       cmd := exec.Command(testenv.GoToolPath(t), "tool", "compile", "-p=main", "-S", "-o", filepath.Join(dir, "out.o"), src)
        out, err := cmd.CombinedOutput()
        if err != nil {
                t.Fatalf("go tool compile: %v\n%s", err, out)
index 67c1551292298355ab90969f5d6fa513988f2b12..66ab8401c680fa06a11e6fef937f91870a25444f 100644 (file)
@@ -56,7 +56,7 @@ func TestInvalidLang(t *testing.T) {
 }
 
 func testLang(t *testing.T, lang, src, outfile string) error {
-       run := []string{testenv.GoToolPath(t), "tool", "compile", "-lang", lang, "-o", outfile, src}
+       run := []string{testenv.GoToolPath(t), "tool", "compile", "-p=p", "-lang", lang, "-o", outfile, src}
        t.Log(run)
        out, err := exec.Command(run[0], run[1:]...).CombinedOutput()
        t.Logf("%s", out)
index 4d84f9cdeffc9acc4539753b6ff68cb5f87ab4ea..0a1a5e9b994fe23e5c877e960dc3146147cd7d64 100644 (file)
@@ -41,7 +41,7 @@ func TestReproducibleBuilds(t *testing.T) {
                        for i := 0; i < iters; i++ {
                                // Note: use -c 2 to expose any nondeterminism which is the result
                                // of the runtime scheduler.
-                               out, err := exec.Command(testenv.GoToolPath(t), "tool", "compile", "-c", "2", "-o", tmp.Name(), filepath.Join("testdata", "reproducible", test)).CombinedOutput()
+                               out, err := exec.Command(testenv.GoToolPath(t), "tool", "compile", "-p=p", "-c", "2", "-o", tmp.Name(), filepath.Join("testdata", "reproducible", test)).CombinedOutput()
                                if err != nil {
                                        t.Fatalf("failed to compile: %v\n%s", err, out)
                                }
@@ -89,7 +89,7 @@ func TestIssue38068(t *testing.T) {
                s := &scenarios[i]
                s.libpath = filepath.Join(tmpdir, s.tag+".a")
                // Note: use of "-p" required in order for DWARF to be generated.
-               cmd := exec.Command(testenv.GoToolPath(t), "tool", "compile", "-trimpath", "-p=issue38068", "-buildid=", s.args, "-o", s.libpath, src)
+               cmd := exec.Command(testenv.GoToolPath(t), "tool", "compile", "-p=issue38068", "-buildid=", s.args, "-o", s.libpath, src)
                out, err := cmd.CombinedOutput()
                if err != nil {
                        t.Fatalf("%v: %v:\n%s", cmd.Args, err, out)
index c284a9cf0dcc5331d13b9cc325972bd53ef8f068..9573495dec1bf9f0ae54e667cc99f498e5c63c6f 100644 (file)
@@ -109,11 +109,11 @@ func buildGoobj() error {
        go1src := filepath.Join("testdata", "go1.go")
        go2src := filepath.Join("testdata", "go2.go")
 
-       out, err := exec.Command(gotool, "tool", "compile", "-o", go1obj, go1src).CombinedOutput()
+       out, err := exec.Command(gotool, "tool", "compile", "-p=p", "-o", go1obj, go1src).CombinedOutput()
        if err != nil {
                return fmt.Errorf("go tool compile -o %s %s: %v\n%s", go1obj, go1src, err, out)
        }
-       out, err = exec.Command(gotool, "tool", "compile", "-o", go2obj, go2src).CombinedOutput()
+       out, err = exec.Command(gotool, "tool", "compile", "-p=p", "-o", go2obj, go2src).CombinedOutput()
        if err != nil {
                return fmt.Errorf("go tool compile -o %s %s: %v\n%s", go2obj, go2src, err, out)
        }
index 146627b62b9ff0e889a5fe0eda5dc94df33db974..f5a4016eec367627034006bb242d2135029913ed 100644 (file)
@@ -111,7 +111,7 @@ func TestSymbolTooLarge(t *testing.T) { // Issue 42054
                t.Fatalf("failed to write source file: %v\n", err)
        }
        obj := filepath.Join(tmpdir, "p.o")
-       cmd := exec.Command(testenv.GoToolPath(t), "tool", "compile", "-o", obj, src)
+       cmd := exec.Command(testenv.GoToolPath(t), "tool", "compile", "-p=p", "-o", obj, src)
        out, err := cmd.CombinedOutput()
        if err == nil {
                t.Fatalf("did not fail\noutput: %s", out)
index ad7658bb2577f0fb163b8854d63c47c26c470a7a..0492feaf0d6c74a5933f27b86688d4fa5bc155c6 100644 (file)
@@ -55,7 +55,7 @@ func main() {}
                t.Fatalf("failed to write main.go: %v\n", err)
        }
 
-       cmd := exec.Command(testenv.GoToolPath(t), "tool", "compile", "main.go")
+       cmd := exec.Command(testenv.GoToolPath(t), "tool", "compile", "-p=main", "main.go")
        cmd.Dir = tmpdir
        out, err := cmd.CombinedOutput()
        if err != nil {
@@ -100,7 +100,7 @@ func TestIssue28429(t *testing.T) {
 
        // Compile a main package.
        write("main.go", "package main; func main() {}")
-       runGo("tool", "compile", "-p", "main", "main.go")
+       runGo("tool", "compile", "-p=main", "main.go")
        runGo("tool", "pack", "c", "main.a", "main.o")
 
        // Add an extra section with a short, non-.o name.
@@ -236,7 +236,7 @@ void foo() {
 
        // Compile, assemble and pack the Go and C code.
        runGo("tool", "asm", "-gensymabis", "-o", "symabis", "x.s")
-       runGo("tool", "compile", "-symabis", "symabis", "-p", "main", "-o", "x1.o", "main.go")
+       runGo("tool", "compile", "-symabis", "symabis", "-p=main", "-o", "x1.o", "main.go")
        runGo("tool", "asm", "-o", "x2.o", "x.s")
        run(cc, append(cflags, "-c", "-o", "x3.o", "x.c")...)
        runGo("tool", "pack", "c", "x.a", "x1.o", "x2.o", "x3.o")
@@ -431,7 +431,7 @@ func TestIssue34788Android386TLSSequence(t *testing.T) {
        }
 
        obj := filepath.Join(tmpdir, "blah.o")
-       cmd := exec.Command(testenv.GoToolPath(t), "tool", "compile", "-o", obj, src)
+       cmd := exec.Command(testenv.GoToolPath(t), "tool", "compile", "-p=blah", "-o", obj, src)
        cmd.Env = append(os.Environ(), "GOARCH=386", "GOOS=android")
        if out, err := cmd.CombinedOutput(); err != nil {
                t.Fatalf("failed to compile blah.go: %v, output: %s\n", err, out)
@@ -765,13 +765,13 @@ func TestIndexMismatch(t *testing.T) {
        exe := filepath.Join(tmpdir, "main.exe")
 
        // Build a program with main package importing package a.
-       cmd := exec.Command(testenv.GoToolPath(t), "tool", "compile", "-o", aObj, aSrc)
+       cmd := exec.Command(testenv.GoToolPath(t), "tool", "compile", "-p=a", "-o", aObj, aSrc)
        t.Log(cmd)
        out, err := cmd.CombinedOutput()
        if err != nil {
                t.Fatalf("compiling a.go failed: %v\n%s", err, out)
        }
-       cmd = exec.Command(testenv.GoToolPath(t), "tool", "compile", "-I", tmpdir, "-o", mObj, mSrc)
+       cmd = exec.Command(testenv.GoToolPath(t), "tool", "compile", "-p=main", "-I", tmpdir, "-o", mObj, mSrc)
        t.Log(cmd)
        out, err = cmd.CombinedOutput()
        if err != nil {
@@ -786,7 +786,7 @@ func TestIndexMismatch(t *testing.T) {
 
        // Now, overwrite a.o with the object of b.go. This should
        // result in an index mismatch.
-       cmd = exec.Command(testenv.GoToolPath(t), "tool", "compile", "-o", aObj, bSrc)
+       cmd = exec.Command(testenv.GoToolPath(t), "tool", "compile", "-p=a", "-o", aObj, bSrc)
        t.Log(cmd)
        out, err = cmd.CombinedOutput()
        if err != nil {
index ff4316103083ac6d35f3d47b13c46f43863b849b..313cc7a80933a9ec3a661cbdc1eddfd7eebb4885 100644 (file)
@@ -267,7 +267,7 @@ func TestDisasmGoobj(t *testing.T) {
        mustHaveDisasm(t)
 
        hello := filepath.Join(tmp, "hello.o")
-       args := []string{"tool", "compile", "-o", hello}
+       args := []string{"tool", "compile", "-p=main", "-o", hello}
        args = append(args, "testdata/fmthello.go")
        out, err := exec.Command(testenv.GoToolPath(t), args...).CombinedOutput()
        if err != nil {
index 81e78f53e28d08d4fc461d93c8e1c9d57995f2bb..6eec1f50ef62974940d3aa3e26345f7fa9b9dbb8 100644 (file)
@@ -179,7 +179,7 @@ func TestHello(t *testing.T) {
 
        goBin := testenv.GoToolPath(t)
        run(goBin, "build", "cmd/pack") // writes pack binary to dir
-       run(goBin, "tool", "compile", "hello.go")
+       run(goBin, "tool", "compile", "-p=main", "hello.go")
        run("./pack", "grc", "hello.a", "hello.o")
        run(goBin, "tool", "link", "-o", "a.out", "hello.a")
        out := run("./a.out")
@@ -246,9 +246,9 @@ func TestLargeDefs(t *testing.T) {
 
        goBin := testenv.GoToolPath(t)
        run(goBin, "build", "cmd/pack") // writes pack binary to dir
-       run(goBin, "tool", "compile", "large.go")
+       run(goBin, "tool", "compile", "-p=large", "large.go")
        run("./pack", "grc", "large.a", "large.o")
-       run(goBin, "tool", "compile", "-I", ".", "main.go")
+       run(goBin, "tool", "compile", "-p=main", "-I", ".", "main.go")
        run(goBin, "tool", "link", "-L", ".", "-o", "a.out", "main.o")
        out := run("./a.out")
        if out != "ok\n" {
@@ -281,9 +281,9 @@ func TestIssue21703(t *testing.T) {
 
        goBin := testenv.GoToolPath(t)
        run(goBin, "build", "cmd/pack") // writes pack binary to dir
-       run(goBin, "tool", "compile", "a.go")
+       run(goBin, "tool", "compile", "-p=a", "a.go")
        run("./pack", "c", "a.a", "a.o")
-       run(goBin, "tool", "compile", "-I", ".", "b.go")
+       run(goBin, "tool", "compile", "-p=b", "-I", ".", "b.go")
 }
 
 // Test the "c" command can "see through" the archive generated by the compiler.
@@ -305,7 +305,7 @@ func TestCreateWithCompilerObj(t *testing.T) {
 
        goBin := testenv.GoToolPath(t)
        run(goBin, "build", "cmd/pack") // writes pack binary to dir
-       run(goBin, "tool", "compile", "-pack", "-o", "p.a", "p.go")
+       run(goBin, "tool", "compile", "-pack", "-p=p", "-o", "p.a", "p.go")
        run("./pack", "c", "packed.a", "p.a")
        fi, err := os.Stat(filepath.Join(dir, "p.a"))
        if err != nil {
@@ -323,7 +323,7 @@ func TestCreateWithCompilerObj(t *testing.T) {
        }
 
        // Test -linkobj flag as well.
-       run(goBin, "tool", "compile", "-linkobj", "p2.a", "-o", "p.x", "p.go")
+       run(goBin, "tool", "compile", "-p=p", "-linkobj", "p2.a", "-o", "p.x", "p.go")
        run("./pack", "c", "packed2.a", "p2.a")
        fi, err = os.Stat(filepath.Join(dir, "p2.a"))
        if err != nil {
@@ -369,7 +369,7 @@ func TestRWithNonexistentFile(t *testing.T) {
 
        goBin := testenv.GoToolPath(t)
        run(goBin, "build", "cmd/pack") // writes pack binary to dir
-       run(goBin, "tool", "compile", "-o", "p.o", "p.go")
+       run(goBin, "tool", "compile", "-p=p", "-o", "p.o", "p.go")
        run("./pack", "r", "p.a", "p.o") // should succeed
 }
 
index c9c5946d9fb46c94e149ef578cdae7146af1a41a..51511ea620fd0723b3fc66c536ca47e76c0628f0 100644 (file)
@@ -45,7 +45,7 @@ func compile(t *testing.T, dirname, filename, outdirname string) string {
        }
        basename := filepath.Base(filename)
        outname := filepath.Join(outdirname, basename[:len(basename)-2]+"o")
-       cmd := exec.Command(testenv.GoToolPath(t), "tool", "compile", "-o", outname, filename)
+       cmd := exec.Command(testenv.GoToolPath(t), "tool", "compile", "-p=p", "-o", outname, filename)
        cmd.Dir = dirname
        out, err := cmd.CombinedOutput()
        if err != nil {
index 5a3b6b616d37ef2b19c63683fd3cdbd442d81367..51d26f69aecae940313b34f900471849cb0f63dd 100644 (file)
@@ -50,7 +50,7 @@ func TestFuncPCCompileError(t *testing.T) {
        }
 
        // compile go code.
-       cmd = exec.Command(testenv.GoToolPath(t), "tool", "compile", "-symabis", symabi, "-o", obj, goSrc)
+       cmd = exec.Command(testenv.GoToolPath(t), "tool", "compile", "-p=p", "-symabis", symabi, "-o", obj, goSrc)
        out, err = cmd.CombinedOutput()
        if err == nil {
                t.Fatalf("go tool compile did not fail")
index e625671278190f2bf008ad1e9911529aa989eb0d..6acd7fde3a0decc3ac912db3ce30e24e8c7c41a4 100644 (file)
@@ -37,7 +37,7 @@ func testProg(dir, name string, length int, ok bool) {
                log.Fatal(err)
        }
 
-       cmd := exec.Command("go", "tool", "compile", filename)
+       cmd := exec.Command("go", "tool", "compile", "-p=p", filename)
        cmd.Dir = dir
        output, err := cmd.CombinedOutput()
 
index a2ab661277a38ff40b45c1c73232d25502f91bb5..b3b958c3aa9124ffa2a5f6ea000b27bb2ebdc6e4 100644 (file)
@@ -28,9 +28,9 @@ func main() {
        }
        defer os.RemoveAll(tmpDir)
 
-       run("go", "tool", "compile", filepath.Join(fb, "bug302.dir", "p.go"))
+       run("go", "tool", "compile", "-p=p", filepath.Join(fb, "bug302.dir", "p.go"))
        run("go", "tool", "pack", "grc", "pp.a", "p.o")
-       run("go", "tool", "compile", "-I", ".", filepath.Join(fb, "bug302.dir", "main.go"))
+       run("go", "tool", "compile", "-p=main", "-I", ".", filepath.Join(fb, "bug302.dir", "main.go"))
 }
 
 func run(cmd string, args ...string) {
index 83f638d04632af0dafaff07c4c6324fad14c79b7..8e50678c89915db0910af7d8a041df81b0a347d0 100644 (file)
@@ -29,9 +29,9 @@ func main() {
                return filepath.Join(tmpDir, name)
        }
 
-       run("go", "tool", "compile", "-N", "-o", tmp("slow.o"), "pkg.go")
-       run("go", "tool", "compile", "-o", tmp("fast.o"), "pkg.go")
-       run("go", "tool", "compile", "-D", tmpDir, "-o", tmp("main.o"), "main.go")
+       run("go", "tool", "compile", "-p=pkg", "-N", "-o", tmp("slow.o"), "pkg.go")
+       run("go", "tool", "compile", "-p=pkg", "-o", tmp("fast.o"), "pkg.go")
+       run("go", "tool", "compile", "-p=main", "-D", tmpDir, "-o", tmp("main.o"), "main.go")
        run("go", "tool", "link", "-o", tmp("a.exe"), tmp("main.o"))
        run(tmp("a.exe"))
 }
index c95dd6ba3965849ac2432eaa1494a67d209dc972..e5bed186bbd820e21ec896b4363a38870934055e 100644 (file)
@@ -52,7 +52,7 @@ func x() {
                log.Fatal(err)
        }
 
-       cmd := exec.Command("go", "tool", "compile", "x.go")
+       cmd := exec.Command("go", "tool", "compile", "-p=p", "x.go")
        cmd.Dir = dir
        output, err := cmd.CombinedOutput()
        if err == nil {
index 32b660c1639169262c16826024369d3e04d0a088..fe51ef1738a7fe0ac90354c82b4f5b95dff2d39c 100644 (file)
@@ -38,7 +38,7 @@ func main() {
        defer os.RemoveAll(f.Name())
 
        // compile and test output
-       cmd := exec.Command("go", "tool", "compile", f.Name())
+       cmd := exec.Command("go", "tool", "compile", "-p=main", f.Name())
        out, err := cmd.CombinedOutput()
        if err == nil {
                log.Fatalf("expected cmd/compile to fail")
index 9ce9c4d732e6d4b6cffb4f85e53ed3a005cdcea3..7f542c5153437fa173bdb3a1ffbe2d96e18bb4d2 100644 (file)
@@ -35,7 +35,7 @@ func main() {
                log.Fatal(err)
        }
 
-       out, err := exec.Command("go", "tool", "compile", fmt.Sprintf("-trimpath=%s", path), f.Name()).CombinedOutput()
+       out, err := exec.Command("go", "tool", "compile", "-p=p", fmt.Sprintf("-trimpath=%s", path), f.Name()).CombinedOutput()
        if err == nil {
                log.Fatalf("expected compiling %s to fail", f.Name())
        }
index 8da17679be08e9f58047df4cdd8de94a46542738..df4f28429c7a126590a5490606a04ce1fffd64af 100644 (file)
@@ -48,7 +48,7 @@ func main() {
                        log.Fatal(err)
                }
 
-               out, err := exec.Command("go", "tool", "compile", f.Name()).CombinedOutput()
+               out, err := exec.Command("go", "tool", "compile", "-p=p", f.Name()).CombinedOutput()
                if err == nil {
                        log.Fatalf("expected compiling\n---\n%s\n---\nto fail", test.src)
                }
index 5f40bf25229a1a1ef400dc6e71408fffdbaaa0a3..eb17960c47670d41dcedb7968883d17ab4dc5eb6 100644 (file)
@@ -75,7 +75,7 @@ bar            :
                        log.Printf("#%d: failed to create file %s", i, filename)
                        continue
                }
-               output, _ := exec.Command("go", "tool", "compile", filename).CombinedOutput()
+               output, _ := exec.Command("go", "tool", "compile", "-p=p", filename).CombinedOutput()
 
                // remove each matching error from the output
                for _, err := range test.errors {
index 60fbd11457fb5973c2c543c98b442144d078916b..27f070ececc24f9afcf7c548323edcc6c38fb0ec 100644 (file)
@@ -1,9 +1,10 @@
-// rundir -P -ldflags -strictdups=2 -w=0
+// rundir -ldflags -strictdups=2 -w=0
 
 // Copyright 2019 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.
 
+//go:build !nacl && !js
 // +build !nacl,!js
 
 package ignored
index 319a2a90df9ba26c8725fe179abe6e349262afb8..31376153ac760d0d242d00d1bfc53c99e9de9f7a 100644 (file)
@@ -27,7 +27,7 @@ func main() {
        }
        f.Close()
 
-       out := run("go", "tool", "compile", "-o", f.Name(), "-S", "a.go")
+       out := run("go", "tool", "compile", "-p=p", "-o", f.Name(), "-S", "a.go")
        os.Remove(f.Name())
 
        // 6g/8g print the offset as dec, but 5g/9g print the offset as hex.
index 728bec74e876ca73f2bea8d764b88b03b17189dd..4aed391b634a0069024e686b0af7629bf198a003 100644 (file)
@@ -7,10 +7,11 @@
 package p
 
 type T int
+
 func (t T) m() {}
 
-type I interface { m() }
-type J interface { I }
+type I interface{ m() }
+type J interface{ I }
 
 func main() {
        var i I
index 077f7ee91753c56d23177873274be2ea9208a090..6bc82dfafcd1b9b71129723f92c46cba54524b3b 100644 (file)
@@ -62,14 +62,14 @@ func main() {
        }
 
        // helloworld.go is package main
-       run("go tool compile -o", tmp("linkmain.o"), "helloworld.go")
-       run("go tool compile -pack -o", tmp("linkmain.a"), "helloworld.go")
+       run("go tool compile -p=main -o", tmp("linkmain.o"), "helloworld.go")
+       run("go tool compile -p=main -pack -o", tmp("linkmain.a"), "helloworld.go")
        run("go tool link -o", tmp("linkmain.exe"), tmp("linkmain.o"))
        run("go tool link -o", tmp("linkmain.exe"), tmp("linkmain.a"))
 
        // linkmain.go is not
-       run("go tool compile -o", tmp("linkmain1.o"), "linkmain.go")
-       run("go tool compile -pack -o", tmp("linkmain1.a"), "linkmain.go")
+       run("go tool compile -p=notmain -o", tmp("linkmain1.o"), "linkmain.go")
+       run("go tool compile -p=notmain -pack -o", tmp("linkmain1.a"), "linkmain.go")
        runFail("go tool link -o", tmp("linkmain.exe"), tmp("linkmain1.o"))
        runFail("go tool link -o", tmp("linkmain.exe"), tmp("linkmain1.a"))
        cleanup()
diff --git a/test/linkname2.go b/test/linkname2.go
deleted file mode 100644 (file)
index 5eb250f..0000000
+++ /dev/null
@@ -1,21 +0,0 @@
-// errorcheck
-
-// Copyright 2020 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.
-
-// Tests that errors are reported for misuse of linkname.
-package p
-
-import _ "unsafe"
-
-type t int
-
-var x, y int
-
-//go:linkname x ok
-
-// ERROR "//go:linkname requires linkname argument or -p compiler flag"
-
-//line linkname2.go:18
-//go:linkname y
index 4c9bd24568b112a9bbd6a01d4d02ccaebf64e505..023996aa30952a6453c8e3fbc3575676f60922a8 100644 (file)
@@ -37,28 +37,28 @@ func main() {
 
        writeFile("p1.go", `
                package p1
-               
+
                func F() {
                        println("hello from p1")
                }
        `)
        writeFile("p2.go", `
                package p2
-               
+
                import "./p1"
 
                func F() {
                        p1.F()
                        println("hello from p2")
                }
-               
+
                func main() {}
        `)
        writeFile("p3.go", `
                package main
 
                import "./p2"
-               
+
                func main() {
                        p2.F()
                        println("hello from main")
@@ -76,9 +76,9 @@ func main() {
                }
 
                // inlining is disabled to make sure that the link objects contain needed code.
-               run("go", "tool", "compile", pkg, "-D", ".", "-I", ".", "-l", "-o", "p1."+o, "-linkobj", "p1.lo", "p1.go")
-               run("go", "tool", "compile", pkg, "-D", ".", "-I", ".", "-l", "-o", "p2."+o, "-linkobj", "p2.lo", "p2.go")
-               run("go", "tool", "compile", pkg, "-D", ".", "-I", ".", "-l", "-o", "p3."+o, "-linkobj", "p3.lo", "p3.go")
+               run("go", "tool", "compile", "-p=p1", pkg, "-D", ".", "-I", ".", "-l", "-o", "p1."+o, "-linkobj", "p1.lo", "p1.go")
+               run("go", "tool", "compile", "-p=p2", pkg, "-D", ".", "-I", ".", "-l", "-o", "p2."+o, "-linkobj", "p2.lo", "p2.go")
+               run("go", "tool", "compile", "-p=main", pkg, "-D", ".", "-I", ".", "-l", "-o", "p3."+o, "-linkobj", "p3.lo", "p3.go")
 
                cp("p1."+o, "p1.oo")
                cp("p2."+o, "p2.oo")
index 869911a4265f5c898c82004ceeb337fa52f069b0..e5dd0e443c8945bff26411ab19708fc6bf242776 100644 (file)
@@ -184,7 +184,7 @@ func main() {
                resCount[status]++
                dt := fmt.Sprintf("%.3fs", test.dt.Seconds())
                if status == "FAIL" {
-                       fmt.Printf("# go run run.go %s\n%s\nFAIL\t%s\t%s\n",
+                       fmt.Printf("# go run run.go -- %s\n%s\nFAIL\t%s\t%s\n",
                                path.Join(test.dir, test.gofile),
                                errStr, test.goFileName(), dt)
                        continue
@@ -254,7 +254,7 @@ func goFiles(dir string) []string {
 type runCmd func(...string) ([]byte, error)
 
 func compileFile(runcmd runCmd, longname string, flags []string) (out []byte, err error) {
-       cmd := []string{goTool(), "tool", "compile", "-e"}
+       cmd := []string{goTool(), "tool", "compile", "-e", "-p=p"}
        cmd = append(cmd, flags...)
        if *linkshared {
                cmd = append(cmd, "-dynlink", "-installsuffix=dynlink")
@@ -263,8 +263,11 @@ func compileFile(runcmd runCmd, longname string, flags []string) (out []byte, er
        return runcmd(cmd...)
 }
 
-func compileInDir(runcmd runCmd, dir string, flags []string, localImports bool, names ...string) (out []byte, err error) {
-       cmd := []string{goTool(), "tool", "compile", "-e"}
+func compileInDir(runcmd runCmd, dir string, flags []string, localImports bool, pkgname string, names ...string) (out []byte, err error) {
+       if pkgname != "main" {
+               pkgname = strings.TrimSuffix(names[0], ".go")
+       }
+       cmd := []string{goTool(), "tool", "compile", "-e", "-p=" + pkgname}
        if localImports {
                // Set relative path for local imports and import search path to current dir.
                cmd = append(cmd, "-D", ".", "-I", ".")
@@ -415,28 +418,33 @@ func getPackageNameFromSource(fn string) (string, error) {
        return pkgname[1], nil
 }
 
+type goDirPkg struct {
+       name  string
+       files []string
+}
+
 // If singlefilepkgs is set, each file is considered a separate package
 // even if the package names are the same.
-func goDirPackages(longdir string, singlefilepkgs bool) ([][]string, error) {
+func goDirPackages(longdir string, singlefilepkgs bool) ([]*goDirPkg, error) {
        files, err := goDirFiles(longdir)
        if err != nil {
                return nil, err
        }
-       var pkgs [][]string
-       m := make(map[string]int)
+       var pkgs []*goDirPkg
+       m := make(map[string]*goDirPkg)
        for _, file := range files {
                name := file.Name()
                pkgname, err := getPackageNameFromSource(filepath.Join(longdir, name))
                if err != nil {
                        log.Fatal(err)
                }
-               i, ok := m[pkgname]
+               p, ok := m[pkgname]
                if singlefilepkgs || !ok {
-                       i = len(pkgs)
-                       pkgs = append(pkgs, nil)
-                       m[pkgname] = i
+                       p = &goDirPkg{name: pkgname}
+                       pkgs = append(pkgs, p)
+                       m[pkgname] = p
                }
-               pkgs[i] = append(pkgs[i], name)
+               p.files = append(p.files, name)
        }
        return pkgs, nil
 }
@@ -607,7 +615,6 @@ func (t *test) run() {
        wantError := false
        wantAuto := false
        singlefilepkgs := false
-       setpkgpaths := false
        localImports := true
        f, err := splitQuoted(action)
        if err != nil {
@@ -652,8 +659,6 @@ func (t *test) run() {
                        wantError = false
                case "-s":
                        singlefilepkgs = true
-               case "-P":
-                       setpkgpaths = true
                case "-n":
                        // Do not set relative path for local imports to current dir,
                        // e.g. do not pass -D . -I . to the compiler.
@@ -843,7 +848,7 @@ func (t *test) run() {
                // Fail if wantError is true and compilation was successful and vice versa.
                // Match errors produced by gc against errors in comments.
                // TODO(gri) remove need for -C (disable printing of columns in error messages)
-               cmdline := []string{goTool(), "tool", "compile", "-d=panic", "-C", "-e", "-o", "a.o"}
+               cmdline := []string{goTool(), "tool", "compile", "-p=p", "-d=panic", "-C", "-e", "-o", "a.o"}
                // No need to add -dynlink even if linkshared if we're just checking for errors...
                cmdline = append(cmdline, flags...)
                cmdline = append(cmdline, long)
@@ -880,8 +885,8 @@ func (t *test) run() {
                        t.err = err
                        return
                }
-               for _, gofiles := range pkgs {
-                       _, t.err = compileInDir(runcmd, longdir, flags, localImports, gofiles...)
+               for _, pkg := range pkgs {
+                       _, t.err = compileInDir(runcmd, longdir, flags, localImports, pkg.name, pkg.files...)
                        if t.err != nil {
                                return
                        }
@@ -904,8 +909,8 @@ func (t *test) run() {
                        // Preceding pkg must return an error from compileInDir.
                        errPkg--
                }
-               for i, gofiles := range pkgs {
-                       out, err := compileInDir(runcmd, longdir, flags, localImports, gofiles...)
+               for i, pkg := range pkgs {
+                       out, err := compileInDir(runcmd, longdir, flags, localImports, pkg.name, pkg.files...)
                        if i == errPkg {
                                if wantError && err == nil {
                                        t.err = fmt.Errorf("compilation succeeded unexpectedly\n%s", out)
@@ -919,7 +924,7 @@ func (t *test) run() {
                                return
                        }
                        var fullshort []string
-                       for _, name := range gofiles {
+                       for _, name := range pkg.files {
                                fullshort = append(fullshort, filepath.Join(longdir, name), name)
                        }
                        t.err = t.errorCheck(string(out), wantAuto, fullshort...)
@@ -953,18 +958,8 @@ func (t *test) run() {
                        }
                }
 
-               for i, gofiles := range pkgs {
-                       pflags := []string{}
-                       pflags = append(pflags, flags...)
-                       if setpkgpaths {
-                               fp := filepath.Join(longdir, gofiles[0])
-                               pkgname, err := getPackageNameFromSource(fp)
-                               if err != nil {
-                                       log.Fatal(err)
-                               }
-                               pflags = append(pflags, "-p", pkgname)
-                       }
-                       _, err := compileInDir(runcmd, longdir, pflags, localImports, gofiles...)
+               for i, pkg := range pkgs {
+                       _, err := compileInDir(runcmd, longdir, flags, localImports, pkg.name, pkg.files...)
                        // Allow this package compilation fail based on conditions below;
                        // its errors were checked in previous case.
                        if err != nil && !(wantError && action == "errorcheckandrundir" && i == len(pkgs)-2) {
@@ -972,7 +967,7 @@ func (t *test) run() {
                                return
                        }
                        if i == len(pkgs)-1 {
-                               err = linkFile(runcmd, gofiles[0], ldflags)
+                               err = linkFile(runcmd, pkg.files[0], ldflags)
                                if err != nil {
                                        t.err = err
                                        return
@@ -1071,7 +1066,7 @@ func (t *test) run() {
                        }
                }
                var objs []string
-               cmd := []string{goTool(), "tool", "compile", "-e", "-D", ".", "-I", ".", "-o", "go.o"}
+               cmd := []string{goTool(), "tool", "compile", "-p=main", "-e", "-D", ".", "-I", ".", "-o", "go.o"}
                if len(asms) > 0 {
                        cmd = append(cmd, "-asmhdr", "go_asm.h", "-symabis", "symabis")
                }
@@ -1156,7 +1151,7 @@ func (t *test) run() {
                        // Because we run lots of trivial test programs,
                        // the time adds up.
                        pkg := filepath.Join(t.tempDir, "pkg.a")
-                       if _, err := runcmd(goTool(), "tool", "compile", "-o", pkg, t.goFileName()); err != nil {
+                       if _, err := runcmd(goTool(), "tool", "compile", "-p=main", "-o", pkg, t.goFileName()); err != nil {
                                t.err = err
                                return
                        }
@@ -1238,7 +1233,7 @@ func (t *test) run() {
                        t.err = fmt.Errorf("write tempfile:%s", err)
                        return
                }
-               cmdline := []string{goTool(), "tool", "compile", "-d=panic", "-e", "-o", "a.o"}
+               cmdline := []string{goTool(), "tool", "compile", "-p=p", "-d=panic", "-e", "-o", "a.o"}
                cmdline = append(cmdline, flags...)
                cmdline = append(cmdline, tfile)
                out, err = runcmd(cmdline...)
index dcaf3383312602a524efb566e007ff635fa1dec4..e01502bd56d2c6aed1bafb013426352917910a02 100644 (file)
@@ -25,7 +25,7 @@ func main() {
        }
        f.Close()
 
-       cmd := exec.Command("go", "tool", "compile", "-o", f.Name(), "-S", "sinit.go")
+       cmd := exec.Command("go", "tool", "compile", "-p=sinit", "-o", f.Name(), "-S", "sinit.go")
        out, err := cmd.CombinedOutput()
        os.Remove(f.Name())
        if err != nil {