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

View as plain text