3 //go:build !nacl && !js && !wasip1
5 // Copyright 2014 The Go Authors. All rights reserved.
6 // Use of this source code is governed by a BSD-style
7 // license that can be found in the LICENSE file.
28 func run(cmdline ...string) {
29 args := strings.Fields(strings.Join(cmdline, " "))
30 cmd := exec.Command(args[0], args[1:]...)
31 out, err := cmd.CombinedOutput()
33 fmt.Printf("$ %s\n", cmdline)
34 fmt.Println(string(out))
41 func runFail(cmdline ...string) {
42 args := strings.Fields(strings.Join(cmdline, " "))
43 cmd := exec.Command(args[0], args[1:]...)
44 out, err := cmd.CombinedOutput()
46 fmt.Printf("$ %s\n", cmdline)
47 fmt.Println(string(out))
48 fmt.Println("SHOULD HAVE FAILED!")
56 tmpDir, err = ioutil.TempDir("", "")
61 tmp := func(name string) string {
62 return filepath.Join(tmpDir, name)
65 importcfg, err := exec.Command("go", "list", "-export", "-f", "{{if .Export}}packagefile {{.ImportPath}}={{.Export}}{{end}}", "std").Output()
70 os.WriteFile(tmp("importcfg"), importcfg, 0644)
72 // helloworld.go is package main
73 run("go tool compile -p=main -importcfg", tmp("importcfg"), "-o", tmp("linkmain.o"), "helloworld.go")
74 run("go tool compile -p=main -importcfg", tmp("importcfg"), " -pack -o", tmp("linkmain.a"), "helloworld.go")
75 run("go tool link -importcfg", tmp("importcfg"), "-o", tmp("linkmain.exe"), tmp("linkmain.o"))
76 run("go tool link -importcfg", tmp("importcfg"), "-o", tmp("linkmain.exe"), tmp("linkmain.a"))
79 run("go tool compile -importcfg", tmp("importcfg"), "-p=notmain -o", tmp("linkmain1.o"), "linkmain.go")
80 run("go tool compile -importcfg", tmp("importcfg"), "-p=notmain -pack -o", tmp("linkmain1.a"), "linkmain.go")
81 runFail("go tool link -importcfg", tmp("importcfg"), "-o", tmp("linkmain.exe"), tmp("linkmain1.o"))
82 runFail("go tool link -importcfg", tmp("importcfg"), "-o", tmp("linkmain.exe"), tmp("linkmain1.a"))