aboutsummaryrefslogtreecommitdiff
path: root/src/cmd/go/testdata/script/build_trimpath.txt
blob: ba414372d347abe986b262c9cc067ad764b02e6d (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
86
87
88
89
90
91
92
93
[short] skip
env GO111MODULE=on

# A binary built without -trimpath should contain the current workspace
# and GOROOT for debugging and stack traces.
cd a
go build -o $WORK/paths-a.exe paths.go
exec $WORK/paths-a.exe $WORK/paths-a.exe
stdout 'binary contains GOPATH: true'
stdout 'binary contains GOROOT: true'

# A binary built with -trimpath should not contain the current workspace
# or GOROOT.
go build -trimpath -o $WORK/paths-a.exe paths.go
exec $WORK/paths-a.exe $WORK/paths-a.exe
stdout 'binary contains GOPATH: false'
stdout 'binary contains GOROOT: false'

# A binary from an external module built with -trimpath should not contain
# the current workspace or GOROOT.
cd $WORK
go get -trimpath rsc.io/fortune
exec $WORK/paths-a.exe $GOPATH/bin/fortune$GOEXE
stdout 'binary contains GOPATH: false'
stdout 'binary contains GOROOT: false'

# Two binaries built from identical packages in different directories
# should be identical.
# TODO(golang.org/issue/35435): at the moment, they are not.
#mkdir $GOPATH/src/b
#cp $GOPATH/src/a/go.mod $GOPATH/src/b/go.mod
#cp $GOPATH/src/a/paths.go $GOPATH/src/b/paths.go
#cd $GOPATH/src/b
#go build -trimpath -o $WORK/paths-b.exe .
#cmp -q $WORK/paths-a.exe $WORK/paths-b.exe

[!exec:gccgo] stop

# A binary built with gccgo without -trimpath should contain the current
# GOPATH and GOROOT.
env GO111MODULE=off # The current released gccgo does not support builds in module mode.
cd $GOPATH/src/a
go build -compiler=gccgo -o $WORK/gccgo-paths-a.exe .
exec $WORK/gccgo-paths-a.exe $WORK/gccgo-paths-b.exe
stdout 'binary contains GOPATH: true'
stdout 'binary contains GOROOT: true'

# A binary built with gccgo with -trimpath should not contain GOPATH or GOROOT.
go build -compiler=gccgo -trimpath -o $WORK/gccgo-paths-a.exe .
exec $WORK/gccgo-paths-a.exe $WORK/gccgo-paths-b.exe
stdout 'binary contains GOPATH: false'
stdout 'binary contains GOROOT: false'

# Two binaries built from identical packages in different directories
# should be identical.
# TODO(golang.org/issue/35435): at the moment, they are not.
#cd ../b
#go build -compiler=gccgo -trimpath -o $WORK/gccgo-paths-b.exe .
#cmp -q $WORK/gccgo-paths-a.exe $WORK/gccgo-paths-b.exe

-- $GOPATH/src/a/paths.go --
package main

import (
	"bytes"
	"fmt"
	"io/ioutil"
	"log"
	"os"
	"path/filepath"
)

func main() {
	exe := os.Args[1]
	data, err := ioutil.ReadFile(exe)
	if err != nil {
		log.Fatal(err)
	}

	gopath := []byte(filepath.ToSlash(os.Getenv("GOPATH")))
	if len(gopath) == 0 {
		log.Fatal("GOPATH not set")
	}
	fmt.Printf("binary contains GOPATH: %v\n", bytes.Contains(data, gopath))

	goroot := []byte(filepath.ToSlash(os.Getenv("GOROOT")))
	if len(goroot) == 0 {
		log.Fatal("GOROOT not set")
	}
	fmt.Printf("binary contains GOROOT: %v\n", bytes.Contains(data, goroot))
}
-- $GOPATH/src/a/go.mod --
module example.com/a