4 // Copyright 2016 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.
8 // Test the compiler -linkobj flag.
21 var pwd, tmpdir string
24 dir, err := ioutil.TempDir("", "go-test-linkobj-")
32 if err := os.Chdir(dir); err != nil {
42 println("hello from p1")
52 println("hello from p2")
64 println("hello from main")
68 // two rounds: once using normal objects, again using .a files (compile -pack).
69 for round := 0; round < 2; round++ {
70 pkg := "-pack=" + fmt.Sprint(round)
72 // The compiler expects the files being read to have the right suffix.
78 // inlining is disabled to make sure that the link objects contain needed code.
79 run("go", "tool", "compile", pkg, "-D", ".", "-I", ".", "-l", "-o", "p1."+o, "-linkobj", "p1.lo", "p1.go")
80 run("go", "tool", "compile", pkg, "-D", ".", "-I", ".", "-l", "-o", "p2."+o, "-linkobj", "p2.lo", "p2.go")
81 run("go", "tool", "compile", pkg, "-D", ".", "-I", ".", "-l", "-o", "p3."+o, "-linkobj", "p3.lo", "p3.go")
89 out := runFail("go", "tool", "link", "p2."+o)
90 if !strings.Contains(out, "not package main") {
91 fatalf("link p2.o failed but not for package main:\n%s", out)
94 run("go", "tool", "link", "-L", ".", "-o", "a.out.exe", "p3."+o)
95 out = run("./a.out.exe")
96 if !strings.Contains(out, "hello from p1\nhello from p2\nhello from main\n") {
97 fatalf("running main, incorrect output:\n%s", out)
100 // ensure that mistaken future round can't use these
102 os.Remove("a.out.exe")
108 func run(args ...string) string {
109 out, err := exec.Command(args[0], args[1:]...).CombinedOutput()
111 fatalf("run %v: %s\n%s", args, err, out)
116 func runFail(args ...string) string {
117 out, err := exec.Command(args[0], args[1:]...).CombinedOutput()
119 fatalf("runFail %v: unexpected success!\n%s", args, err, out)
124 func cp(src, dst string) {
125 data, err := ioutil.ReadFile(src)
129 err = ioutil.WriteFile(dst, data, 0666)
135 func writeFile(name, data string) {
136 err := ioutil.WriteFile(name, []byte(data), 0666)
145 println("TMPDIR:", tmpdir)
148 os.Chdir(pwd) // get out of tmpdir before removing it
152 func fatalf(format string, args ...interface{}) {
154 log.Fatalf(format, args...)