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

View as plain text