Source file
test/linkmain_run.go
1
2
3
4
5
6
7
8
9
10
11 package main
12
13 import (
14 "fmt"
15 "io/ioutil"
16 "os"
17 "os/exec"
18 "path/filepath"
19 "strings"
20 )
21
22 var tmpDir string
23
24 func cleanup() {
25 os.RemoveAll(tmpDir)
26 }
27
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()
32 if err != nil {
33 fmt.Printf("$ %s\n", cmdline)
34 fmt.Println(string(out))
35 fmt.Println(err)
36 cleanup()
37 os.Exit(1)
38 }
39 }
40
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()
45 if err == nil {
46 fmt.Printf("$ %s\n", cmdline)
47 fmt.Println(string(out))
48 fmt.Println("SHOULD HAVE FAILED!")
49 cleanup()
50 os.Exit(1)
51 }
52 }
53
54 func main() {
55 var err error
56 tmpDir, err = ioutil.TempDir("", "")
57 if err != nil {
58 fmt.Println(err)
59 os.Exit(1)
60 }
61 tmp := func(name string) string {
62 return filepath.Join(tmpDir, name)
63 }
64
65 importcfg, err := exec.Command("go", "list", "-export", "-f", "{{if .Export}}packagefile {{.ImportPath}}={{.Export}}{{end}}", "std").Output()
66 if err != nil {
67 fmt.Println(err)
68 os.Exit(1)
69 }
70 os.WriteFile(tmp("importcfg"), importcfg, 0644)
71
72
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"))
77
78
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"))
83 cleanup()
84 }
85
View as plain text