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

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

View as plain text