aboutsummaryrefslogtreecommitdiff
path: root/src/cmd/go/testdata/script/toolexec.txt
blob: 526234196b599262cb64bf632319744a63db83f1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
[short] skip

# Build our simple toolexec program.
go build ./cmd/mytool

# Build the main package with our toolexec program. For each action, it will
# print the tool's name and the TOOLEXEC_IMPORTPATH value. We expect to compile
# each package once, and link the main package once.
# Don't check the entire output at once, because the order in which the tools
# are run is irrelevant here.
# Finally, note that asm and cgo are run twice.

go build -toolexec=$PWD/mytool
[amd64] stderr -count=2 '^asm'${GOEXE}' TOOLEXEC_IMPORTPATH=test/main/withasm$'
stderr -count=1 '^compile'${GOEXE}' TOOLEXEC_IMPORTPATH=test/main/withasm$'
[cgo] stderr -count=2 '^cgo'${GOEXE}' TOOLEXEC_IMPORTPATH=test/main/withcgo$'
[cgo] stderr -count=1 '^compile'${GOEXE}' TOOLEXEC_IMPORTPATH=test/main/withcgo$'
stderr -count=1 '^compile'${GOEXE}' TOOLEXEC_IMPORTPATH=test/main$'
stderr -count=1 '^link'${GOEXE}' TOOLEXEC_IMPORTPATH=test/main$'

-- go.mod --
module test/main
-- foo.go --
// Simple package so we can test a program build with -toolexec.
// With a dummy import, to test different TOOLEXEC_IMPORTPATH values.
// Includes dummy uses of cgo and asm, to cover those tools as well.
package main

import (
	_ "test/main/withasm"
	_ "test/main/withcgo"
)

func main() {}
-- withcgo/withcgo.go --
package withcgo

// int fortytwo()
// {
//     return 42;
// }
import "C"
-- withcgo/stub.go --
package withcgo

// Stub file to ensure we build without cgo too.
-- withasm/withasm.go --
package withasm

// Note that we don't need to declare the Add func at all.
-- withasm/withasm_amd64.s --
TEXT ·Add(SB),$0-24
	MOVQ a+0(FP), AX
	ADDQ b+8(FP), AX
	MOVQ AX, ret+16(FP)
	RET
-- cmd/mytool/main.go --
package main

import (
	"fmt"
	"os"
	"os/exec"
	"path/filepath"
)

func main() {
	tool, args := os.Args[1], os.Args[2:]
	toolName := filepath.Base(tool)
	if len(args) > 0 && args[0] == "-V=full" {
		// We can't alter the version output.
	} else {
		// Print which tool we're running, and on what package.
		fmt.Fprintf(os.Stdout, "%s TOOLEXEC_IMPORTPATH=%s\n", toolName, os.Getenv("TOOLEXEC_IMPORTPATH"))
	}

	// Simply run the tool.
	cmd := exec.Command(tool, args...)
	cmd.Stdout = os.Stdout
	cmd.Stderr = os.Stderr
	if err := cmd.Run(); err != nil {
		fmt.Fprintln(os.Stderr, err)
		os.Exit(1)
	}
}