]> Cypherpunks.ru repositories - gostls13.git/blob - test/linkmain_run.go
cmd/compile: use type hash from itab field instead of type field
[gostls13.git] / test / linkmain_run.go
1 // +build !nacl,!js,!wasip1
2 // run
3
4 // Copyright 2014 The Go Authors. All rights reserved.
5 // Use of this source code is governed by a BSD-style
6 // license that can be found in the LICENSE file.
7
8 // Run the sinit test.
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         // helloworld.go is package main
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         // linkmain.go is not
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 }