Source file
test/linkmain_run.go
1
2
3
4
5
6
7
8
9
10 package main
11
12 import (
13 "fmt"
14 "io/ioutil"
15 "os"
16 "os/exec"
17 "path/filepath"
18 "strings"
19 )
20
21 var tmpDir string
22
23 func cleanup() {
24 os.RemoveAll(tmpDir)
25 }
26
27 func run(cmdline ...string) {
28 args := strings.Fields(strings.Join(cmdline, " "))
29 cmd := exec.Command(args[0], args[1:]...)
30 out, err := cmd.CombinedOutput()
31 if err != nil {
32 fmt.Printf("$ %s\n", cmdline)
33 fmt.Println(string(out))
34 fmt.Println(err)
35 cleanup()
36 os.Exit(1)
37 }
38 }
39
40 func runFail(cmdline ...string) {
41 args := strings.Fields(strings.Join(cmdline, " "))
42 cmd := exec.Command(args[0], args[1:]...)
43 out, err := cmd.CombinedOutput()
44 if err == nil {
45 fmt.Printf("$ %s\n", cmdline)
46 fmt.Println(string(out))
47 fmt.Println("SHOULD HAVE FAILED!")
48 cleanup()
49 os.Exit(1)
50 }
51 }
52
53 func main() {
54 var err error
55 tmpDir, err = ioutil.TempDir("", "")
56 if err != nil {
57 fmt.Println(err)
58 os.Exit(1)
59 }
60 tmp := func(name string) string {
61 return filepath.Join(tmpDir, name)
62 }
63
64 importcfg, err := exec.Command("go", "list", "-export", "-f", "{{if .Export}}packagefile {{.ImportPath}}={{.Export}}{{end}}", "std").Output()
65 if err != nil {
66 fmt.Println(err)
67 os.Exit(1)
68 }
69 os.WriteFile(tmp("importcfg"), importcfg, 0644)
70
71
72 run("go tool compile -p=main -importcfg", tmp("importcfg"), "-o", tmp("linkmain.o"), "helloworld.go")
73 run("go tool compile -p=main -importcfg", tmp("importcfg"), " -pack -o", tmp("linkmain.a"), "helloworld.go")
74 run("go tool link -importcfg", tmp("importcfg"), "-o", tmp("linkmain.exe"), tmp("linkmain.o"))
75 run("go tool link -importcfg", tmp("importcfg"), "-o", tmp("linkmain.exe"), tmp("linkmain.a"))
76
77
78 run("go tool compile -importcfg", tmp("importcfg"), "-p=notmain -o", tmp("linkmain1.o"), "linkmain.go")
79 run("go tool compile -importcfg", tmp("importcfg"), "-p=notmain -pack -o", tmp("linkmain1.a"), "linkmain.go")
80 runFail("go tool link -importcfg", tmp("importcfg"), "-o", tmp("linkmain.exe"), tmp("linkmain1.o"))
81 runFail("go tool link -importcfg", tmp("importcfg"), "-o", tmp("linkmain.exe"), tmp("linkmain1.a"))
82 cleanup()
83 }
84
View as plain text