1// run
2
3//go:build !nacl && !js && !wasip1 && gc
4
5// Copyright 2011 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.
8
9// Test that compiling with optimization turned on produces faster code.
10
11package main
12
13import (
14	"fmt"
15	"io/ioutil"
16	"os"
17	"os/exec"
18	"path/filepath"
19)
20
21func main() {
22	err := os.Chdir(filepath.Join(".", "fixedbugs", "bug369.dir"))
23	check(err)
24
25	tmpDir, err := ioutil.TempDir("", "bug369")
26	check(err)
27	defer os.RemoveAll(tmpDir)
28
29	tmp := func(name string) string {
30		return filepath.Join(tmpDir, name)
31	}
32
33	check(os.Mkdir(tmp("test"), 0777))
34
35	stdlibimportcfg, err := os.ReadFile(os.Getenv("STDLIB_IMPORTCFG"))
36	check(err)
37	importcfg := string(stdlibimportcfg) + "\npackagefile test/slow=" + tmp("test/slow.o") + "\npackagefile test/fast=" + tmp("test/fast.o")
38	os.WriteFile(tmp("importcfg"), []byte(importcfg), 0644)
39
40	run("go", "tool", "compile", "-importcfg="+tmp("importcfg"), "-p=test/slow", "-N", "-o", tmp("test/slow.o"), "pkg.go")
41	run("go", "tool", "compile", "-importcfg="+tmp("importcfg"), "-p=test/fast", "-o", tmp("test/fast.o"), "pkg.go")
42	run("go", "tool", "compile", "-importcfg="+tmp("importcfg"), "-p=main", "-D", "test", "-o", tmp("main.o"), "main.go")
43	run("go", "tool", "link", "-importcfg="+tmp("importcfg"), "-o", tmp("a.exe"), tmp("main.o"))
44	run(tmp("a.exe"))
45}
46
47func run(name string, args ...string) {
48	cmd := exec.Command(name, args...)
49	out, err := cmd.CombinedOutput()
50	if err != nil {
51		fmt.Println(string(out))
52		fmt.Println(err)
53		os.Exit(1)
54	}
55}
56
57func check(err error) {
58	if err != nil {
59		fmt.Println(err)
60		os.Exit(1)
61	}
62}
63