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