...
Run Format

Source file src/cmd/go/note_test.go

Documentation: cmd/go

     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 main_test
     6  
     7  import (
     8  	"go/build"
     9  	"runtime"
    10  	"testing"
    11  
    12  	"cmd/internal/buildid"
    13  )
    14  
    15  func TestNoteReading(t *testing.T) {
    16  	// cmd/internal/buildid already has tests that the basic reading works.
    17  	// This test is essentially checking that -ldflags=-buildid=XXX works,
    18  	// both in internal and external linking mode.
    19  	tg := testgo(t)
    20  	defer tg.cleanup()
    21  	tg.tempFile("hello.go", `package main; func main() { print("hello, world\n") }`)
    22  	const buildID = "TestNoteReading-Build-ID"
    23  	tg.run("build", "-ldflags", "-buildid="+buildID, "-o", tg.path("hello.exe"), tg.path("hello.go"))
    24  	id, err := buildid.ReadFile(tg.path("hello.exe"))
    25  	if err != nil {
    26  		t.Fatalf("reading build ID from hello binary: %v", err)
    27  	}
    28  	if id != buildID {
    29  		t.Fatalf("buildID in hello binary = %q, want %q", id, buildID)
    30  	}
    31  
    32  	switch {
    33  	case !build.Default.CgoEnabled:
    34  		t.Skipf("skipping - no cgo, so assuming external linking not available")
    35  	case runtime.GOOS == "openbsd" && runtime.GOARCH == "arm":
    36  		t.Skipf("skipping - external linking not supported, golang.org/issue/10619")
    37  	case runtime.GOOS == "plan9":
    38  		t.Skipf("skipping - external linking not supported")
    39  	}
    40  
    41  	tg.run("build", "-ldflags", "-buildid="+buildID+" -linkmode=external", "-o", tg.path("hello2.exe"), tg.path("hello.go"))
    42  	id, err = buildid.ReadFile(tg.path("hello2.exe"))
    43  	if err != nil {
    44  		t.Fatalf("reading build ID from hello binary (linkmode=external): %v", err)
    45  	}
    46  	if id != buildID {
    47  		t.Fatalf("buildID in hello binary = %q, want %q (linkmode=external)", id, buildID)
    48  	}
    49  
    50  	switch runtime.GOOS {
    51  	case "dragonfly", "freebsd", "linux", "netbsd", "openbsd":
    52  		// Test while forcing use of the gold linker, since in the past
    53  		// we've had trouble reading the notes generated by gold.
    54  		err := tg.doRun([]string{"build", "-ldflags", "-buildid=" + buildID + " -linkmode=external -extldflags=-fuse-ld=gold", "-o", tg.path("hello3.exe"), tg.path("hello.go")})
    55  		if err != nil {
    56  			if tg.grepCountBoth("(invalid linker|gold|cannot find 'ld')") > 0 {
    57  				// It's not an error if gold isn't there. gcc claims it "cannot find 'ld'" if
    58  				// ld.gold is missing, see issue #22340.
    59  				t.Log("skipping gold test")
    60  				break
    61  			}
    62  			t.Fatalf("building hello binary: %v", err)
    63  		}
    64  		id, err = buildid.ReadFile(tg.path("hello3.exe"))
    65  		if err != nil {
    66  			t.Fatalf("reading build ID from hello binary (linkmode=external -extldflags=-fuse-ld=gold): %v", err)
    67  		}
    68  		if id != buildID {
    69  			t.Fatalf("buildID in hello binary = %q, want %q (linkmode=external -extldflags=-fuse-ld=gold)", id, buildID)
    70  		}
    71  	}
    72  }
    73  

View as plain text