...
1[short] skip
2
3# Test building in overlays.
4# TODO(#39958): add a test case where the destination file in the replace map
5# isn't a go file. Either completely exclude that case in fs.IsDirWithGoFiles
6# if the compiler doesn't allow it, or test that it works all the way.
7# TODO(#39958): add a test that both gc and gccgo assembly files can include .h
8# files.
9
10# The main package (m) is contained in an overlay. It imports m/dir2 which has one
11# file in an overlay and one file outside the overlay, which in turn imports m/dir,
12# which only has source files in the overlay.
13
14cd m
15
16! go build .
17go build -overlay overlay.json -o main$GOEXE .
18exec ./main$goexe
19stdout '^hello$'
20
21go build -overlay overlay.json -o print_abspath$GOEXE ./printpath
22exec ./print_abspath$GOEXE
23stdout $WORK[/\\]gopath[/\\]src[/\\]m[/\\]printpath[/\\]main.go
24
25go build -overlay overlay.json -o print_trimpath$GOEXE -trimpath ./printpath
26exec ./print_trimpath$GOEXE
27stdout ^m[/\\]printpath[/\\]main.go
28
29go build -overlay overlay.json -o print_trimpath_two_files$GOEXE printpath/main.go printpath/other.go
30exec ./print_trimpath_two_files$GOEXE
31stdout $WORK[/\\]gopath[/\\]src[/\\]m[/\\]printpath[/\\]main.go
32stdout $WORK[/\\]gopath[/\\]src[/\\]m[/\\]printpath[/\\]other.go
33
34[cgo] go build -overlay overlay.json -o main_cgo_replace$GOEXE ./cgo_hello_replace
35[cgo] exec ./main_cgo_replace$GOEXE
36[cgo] stdout '^hello cgo\r?\n'
37
38[cgo] go build -overlay overlay.json -o main_cgo_quote$GOEXE ./cgo_hello_quote
39[cgo] exec ./main_cgo_quote$GOEXE
40[cgo] stdout '^hello cgo\r?\n'
41
42[cgo] go build -overlay overlay.json -o main_cgo_angle$GOEXE ./cgo_hello_angle
43[cgo] exec ./main_cgo_angle$GOEXE
44[cgo] stdout '^hello cgo\r?\n'
45
46go build -overlay overlay.json -o main_call_asm$GOEXE ./call_asm
47exec ./main_call_asm$GOEXE
48! stdout .
49
50[cgo] go list -compiled -overlay overlay.json -f '{{range .CompiledGoFiles}}{{. | printf "%s\n"}}{{end}}' ./cgo_hello_replace
51[cgo] cp stdout compiled_cgo_sources.txt
52[cgo] go run ../print_line_comments.go compiled_cgo_sources.txt
53[cgo] stdout $GOPATH[/\\]src[/\\]m[/\\]cgo_hello_replace[/\\]cgo_hello_replace.go
54[cgo] ! stdout $GOPATH[/\\]src[/\\]m[/\\]overlay[/\\]hello.c
55
56# Change the contents of a file in the overlay and ensure that makes the target stale
57env OLD_GOCACHE=$GOCACHE
58env GOCACHE=$WORK/cache # use a fresh cache so that multiple runs of the test don't interfere
59go build -x -overlay overlay.json ./test_cache
60stderr '(compile|gccgo)( |\.exe).*test_cache.go'
61go build -x -overlay overlay.json ./test_cache
62! stderr '(compile|gccgo)( |\.exe).*test_cache.go' # cached
63cp overlay/test_cache_different.go overlay/test_cache.go
64go build -x -overlay overlay.json ./test_cache
65stderr '(compile|gccgo)( |\.exe).*test_cache.go' # not cached
66env CACHE=$OLD_GOCACHE
67
68# Run same tests but with gccgo.
69env GO111MODULE=off
70[!exec:gccgo] stop
71[cross] stop # gccgo can't necessarily cross-compile
72
73! go build -compiler=gccgo .
74go build -compiler=gccgo -overlay overlay.json -o main_gccgo$GOEXE .
75exec ./main_gccgo$goexe
76stdout '^hello$'
77
78go build -compiler=gccgo -overlay overlay.json -o print_abspath_gccgo$GOEXE ./printpath
79exec ./print_abspath_gccgo$GOEXE
80stdout $WORK[/\\]gopath[/\\]src[/\\]m[/\\]printpath[/\\]main.go
81
82go build -compiler=gccgo -overlay overlay.json -o print_trimpath_gccgo$GOEXE -trimpath ./printpath
83exec ./print_trimpath_gccgo$GOEXE
84stdout ^\.[/\\]printpath[/\\]main.go
85
86
87go build -compiler=gccgo -overlay overlay.json -o main_cgo_replace_gccgo$GOEXE ./cgo_hello_replace
88exec ./main_cgo_replace_gccgo$GOEXE
89stdout '^hello cgo\r?\n'
90
91go build -compiler=gccgo -overlay overlay.json -o main_cgo_quote_gccgo$GOEXE ./cgo_hello_quote
92exec ./main_cgo_quote_gccgo$GOEXE
93stdout '^hello cgo\r?\n'
94
95go build -compiler=gccgo -overlay overlay.json -o main_cgo_angle_gccgo$GOEXE ./cgo_hello_angle
96exec ./main_cgo_angle_gccgo$GOEXE
97stdout '^hello cgo\r?\n'
98
99go build -compiler=gccgo -overlay overlay.json -o main_call_asm_gccgo$GOEXE ./call_asm
100exec ./main_call_asm_gccgo$GOEXE
101! stdout .
102
103
104-- m/go.mod --
105// TODO(matloob): how do overlays work with go.mod (especially if mod=readonly)
106module m
107
108go 1.16
109
110-- m/dir2/h.go --
111package dir2
112
113func PrintMessage() {
114 printMessage()
115}
116-- m/dir/foo.txt --
117The build action code currently expects the package directory
118to exist, so it can run the compiler in that directory.
119TODO(matloob): Remove this requirement.
120-- m/printpath/about.txt --
121the actual code is in the overlay
122-- m/overlay.json --
123{
124 "Replace": {
125 "f.go": "overlay/f.go",
126 "dir/g.go": "overlay/dir_g.go",
127 "dir2/i.go": "overlay/dir2_i.go",
128 "printpath/main.go": "overlay/printpath.go",
129 "printpath/other.go": "overlay2/printpath2.go",
130 "call_asm/asm_gc.s": "overlay/asm_gc.s",
131 "call_asm/asm_gccgo.s": "overlay/asm_gccgo.s",
132 "test_cache/main.go": "overlay/test_cache.go",
133 "cgo_hello_replace/cgo_header.h": "overlay/cgo_head.h",
134 "cgo_hello_replace/hello.c": "overlay/hello.c",
135 "cgo_hello_quote/cgo_hello.go": "overlay/cgo_hello_quote.go",
136 "cgo_hello_quote/cgo_header.h": "overlay/cgo_head.h",
137 "cgo_hello_angle/cgo_hello.go": "overlay/cgo_hello_angle.go",
138 "cgo_hello_angle/cgo_header.h": "overlay/cgo_head.h"
139 }
140}
141-- m/cgo_hello_replace/cgo_hello_replace.go --
142package main
143
144// #include "cgo_header.h"
145import "C"
146
147func main() {
148 C.say_hello()
149}
150-- m/cgo_hello_replace/cgo_header.h --
151 // Test that this header is replaced with one that has the proper declaration.
152void say_goodbye();
153
154-- m/cgo_hello_replace/hello.c --
155#include <stdio.h>
156
157void say_goodbye() { puts("goodbye cgo\n"); fflush(stdout); }
158
159-- m/overlay/f.go --
160package main
161
162import "m/dir2"
163
164func main() {
165 dir2.PrintMessage()
166}
167-- m/call_asm/main.go --
168package main
169
170func foo() // There will be a "missing function body" error if the assembly file isn't found.
171
172func main() {
173 foo()
174}
175-- m/overlay/dir_g.go --
176package dir
177
178import "fmt"
179
180func PrintMessage() {
181 fmt.Println("hello")
182}
183-- m/overlay/printpath.go --
184package main
185
186import (
187 "fmt"
188 "path/filepath"
189 "runtime"
190)
191
192func main() {
193 _, file, _, _ := runtime.Caller(0)
194
195 // Since https://golang.org/cl/214286, the runtime's debug paths are
196 // slash-separated regardless of platform, so normalize them to system file
197 // paths.
198 fmt.Println(filepath.FromSlash(file))
199}
200-- m/overlay2/printpath2.go --
201package main
202
203import (
204 "fmt"
205 "path/filepath"
206 "runtime"
207)
208
209func init() {
210 _, file, _, _ := runtime.Caller(0)
211 fmt.Println(filepath.FromSlash(file))
212}
213-- m/overlay/dir2_i.go --
214package dir2
215
216import "m/dir"
217
218func printMessage() {
219 dir.PrintMessage()
220}
221-- m/overlay/cgo_hello_quote.go --
222package main
223
224// #include "cgo_header.h"
225import "C"
226
227func main() {
228 C.say_hello()
229}
230-- m/overlay/cgo_hello_angle.go --
231package main
232
233// #include <cgo_header.h>
234import "C"
235
236func main() {
237 C.say_hello()
238}
239-- m/overlay/cgo_head.h --
240void say_hello();
241-- m/overlay/hello.c --
242#include <stdio.h>
243
244void say_hello() { puts("hello cgo\n"); fflush(stdout); }
245-- m/overlay/asm_gc.s --
246// +build gc
247
248TEXT ·foo(SB),0,$0
249 RET
250
251-- m/overlay/asm_gccgo.s --
252// +build gccgo
253
254.globl main.foo
255.text
256main.foo:
257 ret
258
259-- m/overlay/test_cache.go --
260package foo
261
262import "fmt"
263
264func bar() {
265 fmt.Println("something")
266}
267-- m/overlay/test_cache_different.go --
268package foo
269
270import "fmt"
271
272func bar() {
273 fmt.Println("different")
274}
275-- m/cgo_hello_quote/hello.c --
276#include <stdio.h>
277
278void say_hello() { puts("hello cgo\n"); fflush(stdout); }
279-- m/cgo_hello_angle/hello.c --
280#include <stdio.h>
281
282void say_hello() { puts("hello cgo\n"); fflush(stdout); }
283
284-- print_line_comments.go --
285package main
286
287import (
288 "fmt"
289 "io/ioutil"
290 "log"
291 "os"
292 "strings"
293)
294
295func main() {
296 compiledGoFilesArg := os.Args[1]
297 b, err := ioutil.ReadFile(compiledGoFilesArg)
298 if err != nil {
299 log.Fatal(err)
300 }
301 compiledGoFiles := strings.Split(strings.TrimSpace(string(b)), "\n")
302 for _, f := range compiledGoFiles {
303 b, err := ioutil.ReadFile(f)
304 if err != nil {
305 log.Fatal(err)
306 }
307 for _, line := range strings.Split(string(b), "\n") {
308 if strings.HasPrefix(line, "#line") || strings.HasPrefix(line, "//line") {
309 fmt.Println(line)
310 }
311 }
312 }
313}
View as plain text