...

Text file src/cmd/go/testdata/script/build_overlay.txt

Documentation: cmd/go/testdata/script

     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 vet -overlay overlay.json ./printpath
    26
    27go build -overlay overlay.json -o print_trimpath$GOEXE -trimpath ./printpath
    28exec ./print_trimpath$GOEXE
    29stdout ^m[/\\]printpath[/\\]main.go
    30
    31go build -overlay overlay.json -o print_trimpath_two_files$GOEXE printpath/main.go printpath/other.go
    32exec ./print_trimpath_two_files$GOEXE
    33stdout $WORK[/\\]gopath[/\\]src[/\\]m[/\\]printpath[/\\]main.go
    34stdout $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
    48go build -overlay overlay.json -o main_call_asm$GOEXE ./call_asm
    49exec ./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
    59env OLD_GOCACHE=$GOCACHE
    60env GOCACHE=$WORK/cache  # use a fresh cache so that multiple runs of the test don't interfere
    61go build -x -overlay overlay.json ./test_cache
    62stderr '(compile|gccgo)( |\.exe).*test_cache.go'
    63go build -x -overlay overlay.json ./test_cache
    64! stderr '(compile|gccgo)( |\.exe).*test_cache.go'  # cached
    65cp overlay/test_cache_different.go overlay/test_cache.go
    66go build -x -overlay overlay.json ./test_cache
    67stderr '(compile|gccgo)( |\.exe).*test_cache.go'  # not cached
    68env CACHE=$OLD_GOCACHE
    69
    70# Run same tests but with gccgo.
    71env GO111MODULE=off
    72[!exec:gccgo] stop
    73[cross] stop  # gccgo can't necessarily cross-compile
    74
    75! go build -compiler=gccgo .
    76go build -compiler=gccgo -overlay overlay.json -o main_gccgo$GOEXE .
    77exec ./main_gccgo$goexe
    78stdout '^hello$'
    79
    80go build -compiler=gccgo -overlay overlay.json -o print_abspath_gccgo$GOEXE ./printpath
    81exec ./print_abspath_gccgo$GOEXE
    82stdout $WORK[/\\]gopath[/\\]src[/\\]m[/\\]printpath[/\\]main.go
    83
    84go build -compiler=gccgo -overlay overlay.json -o print_trimpath_gccgo$GOEXE -trimpath ./printpath
    85exec ./print_trimpath_gccgo$GOEXE
    86stdout ^\.[/\\]printpath[/\\]main.go
    87
    88
    89go build -compiler=gccgo  -overlay overlay.json -o main_cgo_replace_gccgo$GOEXE ./cgo_hello_replace
    90exec ./main_cgo_replace_gccgo$GOEXE
    91stdout '^hello cgo\r?\n'
    92
    93go build -compiler=gccgo  -overlay overlay.json -o main_cgo_quote_gccgo$GOEXE ./cgo_hello_quote
    94exec ./main_cgo_quote_gccgo$GOEXE
    95stdout '^hello cgo\r?\n'
    96
    97go build -compiler=gccgo  -overlay overlay.json -o main_cgo_angle_gccgo$GOEXE ./cgo_hello_angle
    98exec ./main_cgo_angle_gccgo$GOEXE
    99stdout '^hello cgo\r?\n'
   100
   101go build -compiler=gccgo -overlay overlay.json -o main_call_asm_gccgo$GOEXE ./call_asm
   102exec ./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)
   108module m
   109
   110go 1.16
   111
   112-- m/dir2/h.go --
   113package dir2
   114
   115func PrintMessage() {
   116	printMessage()
   117}
   118-- m/dir/foo.txt --
   119The build action code currently expects the package directory
   120to exist, so it can run the compiler in that directory.
   121TODO(matloob): Remove this requirement.
   122-- m/printpath/about.txt --
   123the 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 --
   144package main
   145
   146// #include "cgo_header.h"
   147import "C"
   148
   149func 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.
   154void say_goodbye();
   155
   156-- m/cgo_hello_replace/hello.c --
   157#include <stdio.h>
   158
   159void say_goodbye() { puts("goodbye cgo\n"); fflush(stdout); }
   160
   161-- m/overlay/f.go --
   162package main
   163
   164import "m/dir2"
   165
   166func main() {
   167	dir2.PrintMessage()
   168}
   169-- m/call_asm/main.go --
   170package main
   171
   172func foo() // There will be a "missing function body" error if the assembly file isn't found.
   173
   174func main() {
   175	foo()
   176}
   177-- m/overlay/dir_g.go --
   178package dir
   179
   180import "fmt"
   181
   182func PrintMessage() {
   183	fmt.Println("hello")
   184}
   185-- m/overlay/printpath.go --
   186package main
   187
   188import (
   189	"fmt"
   190	"path/filepath"
   191	"runtime"
   192)
   193
   194func 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 --
   203package main
   204
   205import (
   206	"fmt"
   207	"path/filepath"
   208	"runtime"
   209)
   210
   211func init() {
   212	_, file, _, _ := runtime.Caller(0)
   213	fmt.Println(filepath.FromSlash(file))
   214}
   215-- m/overlay/dir2_i.go --
   216package dir2
   217
   218import "m/dir"
   219
   220func printMessage() {
   221	dir.PrintMessage()
   222}
   223-- m/overlay/cgo_hello_quote.go --
   224package main
   225
   226// #include "cgo_header.h"
   227import "C"
   228
   229func main() {
   230	C.say_hello()
   231}
   232-- m/overlay/cgo_hello_angle.go --
   233package main
   234
   235// #include <cgo_header.h>
   236import "C"
   237
   238func main() {
   239	C.say_hello()
   240}
   241-- m/overlay/cgo_head.h --
   242void say_hello();
   243-- m/overlay/hello.c --
   244#include <stdio.h>
   245
   246void say_hello() { puts("hello cgo\n"); fflush(stdout); }
   247-- m/overlay/asm_gc.s --
   248// +build gc
   249
   250TEXT ·foo(SB),0,$0
   251	RET
   252
   253-- m/overlay/asm_gccgo.s --
   254// +build gccgo
   255
   256.globl main.foo
   257.text
   258main.foo:
   259	ret
   260
   261-- m/overlay/test_cache.go --
   262package foo
   263
   264import "fmt"
   265
   266func bar() {
   267	fmt.Println("something")
   268}
   269-- m/overlay/test_cache_different.go --
   270package foo
   271
   272import "fmt"
   273
   274func bar() {
   275	fmt.Println("different")
   276}
   277-- m/cgo_hello_quote/hello.c --
   278#include <stdio.h>
   279
   280void say_hello() { puts("hello cgo\n"); fflush(stdout); }
   281-- m/cgo_hello_angle/hello.c --
   282#include <stdio.h>
   283
   284void say_hello() { puts("hello cgo\n"); fflush(stdout); }
   285
   286-- print_line_comments.go --
   287package main
   288
   289import (
   290	"fmt"
   291	"io/ioutil"
   292	"log"
   293	"os"
   294	"strings"
   295)
   296
   297func 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}

View as plain text