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

     1  [short] skip
     2  
     3  # Build our simple toolexec program.
     4  go build ./cmd/mytool
     5  
     6  # Use an ephemeral build cache so that our toolexec output is not cached
     7  # for any stale standard-library dependencies.
     8  #
     9  # TODO(#27628): This should not be necessary.
    10  env GOCACHE=$WORK/gocache
    11  
    12  # Build the main package with our toolexec program. For each action, it will
    13  # print the tool's name and the TOOLEXEC_IMPORTPATH value. We expect to compile
    14  # each package once, and link the main package once.
    15  # Don't check the entire output at once, because the order in which the tools
    16  # are run is irrelevant here.
    17  # Finally, note that asm and cgo are run twice.
    18  
    19  go build -toolexec=$PWD/mytool
    20  [GOARCH:amd64] stderr -count=2 '^asm'${GOEXE}' TOOLEXEC_IMPORTPATH="test/main/withasm"$'
    21  stderr -count=1 '^compile'${GOEXE}' TOOLEXEC_IMPORTPATH="test/main/withasm"$'
    22  [cgo] stderr -count=2 '^cgo'${GOEXE}' TOOLEXEC_IMPORTPATH="test/main/withcgo"$'
    23  [cgo] stderr -count=1 '^compile'${GOEXE}' TOOLEXEC_IMPORTPATH="test/main/withcgo"$'
    24  stderr -count=1 '^compile'${GOEXE}' TOOLEXEC_IMPORTPATH="test/main"$'
    25  stderr -count=1 '^link'${GOEXE}' TOOLEXEC_IMPORTPATH="test/main"$'
    26  
    27  # Test packages are a little bit trickier.
    28  # We have four variants of test/main, as reported by 'go list -test':
    29  #
    30  #    test/main                        - the regular non-test package
    31  #    test/main.test                   - the generated test program
    32  #    test/main [test/main.test]       - the test package for foo_test.go
    33  #    test/main_test [test/main.test]  - the test package for foo_separate_test.go
    34  #
    35  # As such, TOOLEXEC_IMPORTPATH must see the same strings, to be able to uniquely
    36  # identify each package being built as reported by 'go list -f {{.ImportPath}}'.
    37  # Note that these are not really "import paths" anymore, but that naming is
    38  # consistent with 'go list -json' at least.
    39  
    40  go test -toolexec=$PWD/mytool
    41  
    42  stderr -count=2 '^# test/main\.test$'
    43  stderr -count=1 '^compile'${GOEXE}' TOOLEXEC_IMPORTPATH="test/main\.test"$'
    44  stderr -count=1 '^link'${GOEXE}' TOOLEXEC_IMPORTPATH="test/main\.test"$'
    45  
    46  stderr -count=1 '^# test/main \[test/main\.test\]$'
    47  stderr -count=1 '^compile'${GOEXE}' TOOLEXEC_IMPORTPATH="test/main \[test/main\.test\]"$'
    48  
    49  stderr -count=1 '^# test/main_test \[test/main\.test\]$'
    50  stderr -count=1 '^compile'${GOEXE}' TOOLEXEC_IMPORTPATH="test/main_test \[test/main\.test\]"$'
    51  
    52  -- go.mod --
    53  module test/main
    54  -- foo.go --
    55  // Simple package so we can test a program build with -toolexec.
    56  // With a dummy import, to test different TOOLEXEC_IMPORTPATH values.
    57  // Includes dummy uses of cgo and asm, to cover those tools as well.
    58  package main
    59  
    60  import (
    61  	_ "test/main/withasm"
    62  	_ "test/main/withcgo"
    63  )
    64  
    65  func main() {}
    66  -- foo_test.go --
    67  package main
    68  
    69  import "testing"
    70  
    71  func TestFoo(t *testing.T) {}
    72  -- foo_separate_test.go --
    73  package main_test
    74  
    75  import "testing"
    76  
    77  func TestSeparateFoo(t *testing.T) {}
    78  -- withcgo/withcgo.go --
    79  package withcgo
    80  
    81  // int fortytwo()
    82  // {
    83  //     return 42;
    84  // }
    85  import "C"
    86  -- withcgo/stub.go --
    87  package withcgo
    88  
    89  // Stub file to ensure we build without cgo too.
    90  -- withasm/withasm.go --
    91  package withasm
    92  
    93  // Note that we don't need to declare the Add func at all.
    94  -- withasm/withasm_amd64.s --
    95  TEXT ¬∑Add(SB),$0-24
    96  	MOVQ a+0(FP), AX
    97  	ADDQ b+8(FP), AX
    98  	MOVQ AX, ret+16(FP)
    99  	RET
   100  -- cmd/mytool/main.go --
   101  package main
   102  
   103  import (
   104  	"fmt"
   105  	"os"
   106  	"os/exec"
   107  	"path/filepath"
   108  )
   109  
   110  func main() {
   111  	tool, args := os.Args[1], os.Args[2:]
   112  	toolName := filepath.Base(tool)
   113  	if len(args) > 0 && args[0] == "-V=full" {
   114  		// We can't alter the version output.
   115  	} else {
   116  		// Print which tool we're running, and on what package.
   117  		fmt.Fprintf(os.Stdout, "%s TOOLEXEC_IMPORTPATH=%q\n", toolName, os.Getenv("TOOLEXEC_IMPORTPATH"))
   118  	}
   119  
   120  	// Simply run the tool.
   121  	cmd := exec.Command(tool, args...)
   122  	cmd.Stdout = os.Stdout
   123  	cmd.Stderr = os.Stderr
   124  	if err := cmd.Run(); err != nil {
   125  		fmt.Fprintln(os.Stderr, err)
   126  		os.Exit(1)
   127  	}
   128  }
   129  

View as plain text