github.com/go-asm/go@v1.21.1-0.20240213172139-40c5ead50c48/cmd/compile/test/global_test.go (about)

     1  // Copyright 2015 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package test
     6  
     7  import (
     8  	"bytes"
     9  	"os"
    10  	"path/filepath"
    11  	"strings"
    12  	"testing"
    13  
    14  	"github.com/go-asm/go/testenv"
    15  )
    16  
    17  // Make sure "hello world" does not link in all the
    18  // fmt.scanf routines. See issue 6853.
    19  func TestScanfRemoval(t *testing.T) {
    20  	testenv.MustHaveGoBuild(t)
    21  	t.Parallel()
    22  
    23  	// Make a directory to work in.
    24  	dir := t.TempDir()
    25  
    26  	// Create source.
    27  	src := filepath.Join(dir, "test.go")
    28  	f, err := os.Create(src)
    29  	if err != nil {
    30  		t.Fatalf("could not create source file: %v", err)
    31  	}
    32  	f.Write([]byte(`
    33  package main
    34  import "fmt"
    35  func main() {
    36  	fmt.Println("hello world")
    37  }
    38  `))
    39  	f.Close()
    40  
    41  	// Name of destination.
    42  	dst := filepath.Join(dir, "test")
    43  
    44  	// Compile source.
    45  	cmd := testenv.Command(t, testenv.GoToolPath(t), "build", "-o", dst, src)
    46  	out, err := cmd.CombinedOutput()
    47  	if err != nil {
    48  		t.Fatalf("could not build target: %v\n%s", err, out)
    49  	}
    50  
    51  	// Check destination to see if scanf code was included.
    52  	cmd = testenv.Command(t, testenv.GoToolPath(t), "tool", "nm", dst)
    53  	out, err = cmd.CombinedOutput()
    54  	if err != nil {
    55  		t.Fatalf("could not read target: %v", err)
    56  	}
    57  	if bytes.Contains(out, []byte("scanInt")) {
    58  		t.Fatalf("scanf code not removed from helloworld")
    59  	}
    60  }
    61  
    62  // Make sure -S prints assembly code. See issue 14515.
    63  func TestDashS(t *testing.T) {
    64  	testenv.MustHaveGoBuild(t)
    65  	t.Parallel()
    66  
    67  	// Make a directory to work in.
    68  	dir := t.TempDir()
    69  
    70  	// Create source.
    71  	src := filepath.Join(dir, "test.go")
    72  	f, err := os.Create(src)
    73  	if err != nil {
    74  		t.Fatalf("could not create source file: %v", err)
    75  	}
    76  	f.Write([]byte(`
    77  package main
    78  import "fmt"
    79  func main() {
    80  	fmt.Println("hello world")
    81  }
    82  `))
    83  	f.Close()
    84  
    85  	// Compile source.
    86  	cmd := testenv.Command(t, testenv.GoToolPath(t), "build", "-gcflags", "-S", "-o", filepath.Join(dir, "test"), src)
    87  	out, err := cmd.CombinedOutput()
    88  	if err != nil {
    89  		t.Fatalf("could not build target: %v\n%s", err, out)
    90  	}
    91  
    92  	patterns := []string{
    93  		// It is hard to look for actual instructions in an
    94  		// arch-independent way. So we'll just look for
    95  		// pseudo-ops that are arch-independent.
    96  		"\tTEXT\t",
    97  		"\tFUNCDATA\t",
    98  		"\tPCDATA\t",
    99  	}
   100  	outstr := string(out)
   101  	for _, p := range patterns {
   102  		if !strings.Contains(outstr, p) {
   103  			println(outstr)
   104  			panic("can't find pattern " + p)
   105  		}
   106  	}
   107  }