aboutsummaryrefslogtreecommitdiff
path: root/src/cmd/go/testdata/script/build_overlay.txt
blob: 2932b94e6c42c70cdec8884c1e36dc281e063369 (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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
[short] skip

# Test building in overlays.
# TODO(#39958): add a test case where the destination file in the replace map
#   isn't a go file. Either completely exclude that case in fs.IsDirWithGoFiles
#   if the compiler doesn't allow it, or test that it works all the way.
# TODO(#39958): add a test that both gc and gccgo assembly files can include .h
#   files.

# The main package (m) is contained in an overlay. It imports m/dir2 which has one
# file in an overlay and one file outside the overlay, which in turn imports m/dir,
# which only has source files in the overlay.

cd m

! go build .
go build -overlay overlay.json -o main$GOEXE .
exec ./main$goexe
stdout '^hello$'

go build -overlay overlay.json -o print_abspath$GOEXE ./printpath
exec ./print_abspath$GOEXE
stdout $WORK[/\\]gopath[/\\]src[/\\]m[/\\]printpath[/\\]main.go

go build -overlay overlay.json -o print_trimpath$GOEXE -trimpath ./printpath
exec ./print_trimpath$GOEXE
stdout ^m[/\\]printpath[/\\]main.go

go build -overlay overlay.json -o print_trimpath_two_files$GOEXE printpath/main.go printpath/other.go
exec ./print_trimpath_two_files$GOEXE
stdout $WORK[/\\]gopath[/\\]src[/\\]m[/\\]printpath[/\\]main.go
stdout $WORK[/\\]gopath[/\\]src[/\\]m[/\\]printpath[/\\]other.go

go build -overlay overlay.json -o main_cgo_replace$GOEXE ./cgo_hello_replace
exec ./main_cgo_replace$GOEXE
stdout '^hello cgo\r?\n'

go build -overlay overlay.json -o main_cgo_quote$GOEXE ./cgo_hello_quote
exec ./main_cgo_quote$GOEXE
stdout '^hello cgo\r?\n'

go build -overlay overlay.json -o main_cgo_angle$GOEXE ./cgo_hello_angle
exec ./main_cgo_angle$GOEXE
stdout '^hello cgo\r?\n'

go build -overlay overlay.json -o main_call_asm$GOEXE ./call_asm
exec ./main_call_asm$GOEXE
! stdout .

# Change the contents of a file in the overlay and ensure that makes the target stale
go install -overlay overlay.json ./test_cache
go list -overlay overlay.json -f '{{.Stale}}' ./test_cache
stdout '^false$'
cp overlay/test_cache_different.go overlay/test_cache.go
go list -overlay overlay.json -f '{{.Stale}}' ./test_cache
stdout '^true$'

go list -compiled -overlay overlay.json -f '{{range .CompiledGoFiles}}{{. | printf "%s\n"}}{{end}}' ./cgo_hello_replace
cp stdout compiled_cgo_sources.txt
go run ../print_line_comments.go compiled_cgo_sources.txt
stdout $GOPATH[/\\]src[/\\]m[/\\]cgo_hello_replace[/\\]cgo_hello_replace.go
! stdout $GOPATH[/\\]src[/\\]m[/\\]overlay[/\\]hello.c

# Run same tests but with gccgo.
env GO111MODULE=off
[!exec:gccgo] stop

! go build -compiler=gccgo .
go build -compiler=gccgo -overlay overlay.json -o main_gccgo$GOEXE .
exec ./main_gccgo$goexe
stdout '^hello$'

go build -compiler=gccgo -overlay overlay.json -o print_abspath_gccgo$GOEXE ./printpath
exec ./print_abspath_gccgo$GOEXE
stdout $WORK[/\\]gopath[/\\]src[/\\]m[/\\]printpath[/\\]main.go

go build -compiler=gccgo -overlay overlay.json -o print_trimpath_gccgo$GOEXE -trimpath ./printpath
exec ./print_trimpath_gccgo$GOEXE
stdout ^\.[/\\]printpath[/\\]main.go


go build -compiler=gccgo  -overlay overlay.json -o main_cgo_replace_gccgo$GOEXE ./cgo_hello_replace
exec ./main_cgo_replace_gccgo$GOEXE
stdout '^hello cgo\r?\n'

go build -compiler=gccgo  -overlay overlay.json -o main_cgo_quote_gccgo$GOEXE ./cgo_hello_quote
exec ./main_cgo_quote_gccgo$GOEXE
stdout '^hello cgo\r?\n'

go build -compiler=gccgo  -overlay overlay.json -o main_cgo_angle_gccgo$GOEXE ./cgo_hello_angle
exec ./main_cgo_angle_gccgo$GOEXE
stdout '^hello cgo\r?\n'

go build -compiler=gccgo -overlay overlay.json -o main_call_asm_gccgo$GOEXE ./call_asm
exec ./main_call_asm_gccgo$GOEXE
! stdout .


-- m/go.mod --
// TODO(matloob): how do overlays work with go.mod (especially if mod=readonly)
module m

go 1.16

-- m/dir2/h.go --
package dir2

func PrintMessage() {
	printMessage()
}
-- m/dir/foo.txt --
The build action code currently expects the package directory
to exist, so it can run the compiler in that directory.
TODO(matloob): Remove this requirement.
-- m/printpath/about.txt --
the actual code is in the overlay
-- m/overlay.json --
{
	"Replace": {
		"f.go": "overlay/f.go",
		"dir/g.go": "overlay/dir_g.go",
		"dir2/i.go": "overlay/dir2_i.go",
		"printpath/main.go": "overlay/printpath.go",
		"printpath/other.go": "overlay2/printpath2.go",
		"call_asm/asm_gc.s": "overlay/asm_gc.s",
		"call_asm/asm_gccgo.s": "overlay/asm_gccgo.s",
		"test_cache/main.go": "overlay/test_cache.go",
		"cgo_hello_replace/cgo_header.h": "overlay/cgo_head.h",
		"cgo_hello_replace/hello.c": "overlay/hello.c",
		"cgo_hello_quote/cgo_hello.go": "overlay/cgo_hello_quote.go",
		"cgo_hello_quote/cgo_header.h": "overlay/cgo_head.h",
		"cgo_hello_angle/cgo_hello.go": "overlay/cgo_hello_angle.go",
		"cgo_hello_angle/cgo_header.h": "overlay/cgo_head.h"
	}
}
-- m/cgo_hello_replace/cgo_hello_replace.go --
package main

// #include "cgo_header.h"
import "C"

func main() {
	C.say_hello()
}
-- m/cgo_hello_replace/cgo_header.h --
 // Test that this header is replaced with one that has the proper declaration.
void say_goodbye();

-- m/cgo_hello_replace/hello.c --
#include <stdio.h>

void say_goodbye() { puts("goodbye cgo\n"); fflush(stdout); }

-- m/overlay/f.go --
package main

import "m/dir2"

func main() {
	dir2.PrintMessage()
}
-- m/call_asm/main.go --
package main

func foo() // There will be a "missing function body" error if the assembly file isn't found.

func main() {
	foo()
}
-- m/overlay/dir_g.go --
package dir

import "fmt"

func PrintMessage() {
	fmt.Println("hello")
}
-- m/overlay/printpath.go --
package main

import (
	"fmt"
	"path/filepath"
	"runtime"
)

func main() {
	_, file, _, _ := runtime.Caller(0)

	// Since https://golang.org/cl/214286, the runtime's debug paths are
	// slash-separated regardless of platform, so normalize them to system file
	// paths.
	fmt.Println(filepath.FromSlash(file))
}
-- m/overlay2/printpath2.go --
package main

import (
	"fmt"
	"path/filepath"
	"runtime"
)

func init() {
	_, file, _, _ := runtime.Caller(0)
	fmt.Println(filepath.FromSlash(file))
}
-- m/overlay/dir2_i.go --
package dir2

import "m/dir"

func printMessage() {
	dir.PrintMessage()
}
-- m/overlay/cgo_hello_quote.go --
package main

// #include "cgo_header.h"
import "C"

func main() {
	C.say_hello()
}
-- m/overlay/cgo_hello_angle.go --
package main

// #include <cgo_header.h>
import "C"

func main() {
	C.say_hello()
}
-- m/overlay/cgo_head.h --
void say_hello();
-- m/overlay/hello.c --
#include <stdio.h>

void say_hello() { puts("hello cgo\n"); fflush(stdout); }
-- m/overlay/asm_gc.s --
// +build gc

TEXT ·foo(SB),0,$0
	RET

-- m/overlay/asm_gccgo.s --
// +build gccgo

.globl main.foo
.text
main.foo:
	ret

-- m/overlay/test_cache.go --
package foo

import "fmt"

func bar() {
	fmt.Println("something")
}
-- m/overlay/test_cache_different.go --
package foo

import "fmt"

func bar() {
	fmt.Println("different")
}
-- m/cgo_hello_quote/hello.c --
#include <stdio.h>

void say_hello() { puts("hello cgo\n"); fflush(stdout); }
-- m/cgo_hello_angle/hello.c --
#include <stdio.h>

void say_hello() { puts("hello cgo\n"); fflush(stdout); }

-- print_line_comments.go --
package main

import (
	"fmt"
	"io/ioutil"
	"log"
	"os"
	"strings"
)

func main() {
	compiledGoFilesArg := os.Args[1]
	b, err := ioutil.ReadFile(compiledGoFilesArg)
	if err != nil {
		log.Fatal(err)
	}
	compiledGoFiles := strings.Split(strings.TrimSpace(string(b)), "\n")
	for _, f := range compiledGoFiles {
		b, err := ioutil.ReadFile(f)
		if err != nil {
			log.Fatal(err)
		}
		for _, line := range strings.Split(string(b), "\n") {
			if strings.HasPrefix(line, "#line") || strings.HasPrefix(line, "//line") {
				fmt.Println(line)
			}
		}
	}
}