github.com/0xKiwi/rules_go@v0.24.3/tests/core/cgo/cc_libs_linux_test.go (about)

     1  // Copyright 2018 The Bazel Authors. All rights reserved.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //    http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package cc_libs_test
    16  
    17  import (
    18  	"debug/elf"
    19  	"path"
    20  	"strings"
    21  	"testing"
    22  
    23  	"github.com/bazelbuild/rules_go/go/tools/bazel"
    24  )
    25  
    26  func TestBinaries(t *testing.T) {
    27  	for _, test := range []struct {
    28  		shortPath string
    29  		wantLibs  map[string]bool
    30  	}{
    31  		{
    32  			shortPath: "tests/core/cgo/pure_bin",
    33  			wantLibs:  map[string]bool{"libc": false, "libstdc++": false},
    34  		}, {
    35  			shortPath: "tests/core/cgo/c_srcs_bin",
    36  			wantLibs:  map[string]bool{"libc": true, "libstdc++": false},
    37  		}, {
    38  			shortPath: "tests/core/cgo/cc_srcs_bin",
    39  			wantLibs:  map[string]bool{"libc": true, "libstdc++": true},
    40  		}, {
    41  			shortPath: "tests/core/cgo/cc_deps_bin",
    42  			wantLibs:  map[string]bool{"libc": true, "libstdc++": true},
    43  		},
    44  	} {
    45  		t.Run(path.Base(test.shortPath), func(t *testing.T) {
    46  			libs, err := listLibs(test.shortPath)
    47  			if err != nil {
    48  				t.Fatal(err)
    49  			}
    50  			haveLibs := make(map[string]bool)
    51  			for _, lib := range libs {
    52  				haveLibs[lib] = true
    53  			}
    54  			for haveLib := range haveLibs {
    55  				if wantLib, ok := test.wantLibs[haveLib]; ok && !wantLib {
    56  					t.Errorf("unexpected dependency on library %q", haveLib)
    57  				}
    58  			}
    59  			for wantLib, want := range test.wantLibs {
    60  				if want && !haveLibs[wantLib] {
    61  					t.Errorf("wanted dependency on library %q", wantLib)
    62  				}
    63  			}
    64  		})
    65  	}
    66  }
    67  
    68  func listLibs(shortPath string) ([]string, error) {
    69  	binPath, err := bazel.Runfile(shortPath)
    70  	if err != nil {
    71  		return nil, err
    72  	}
    73  	f, err := elf.Open(binPath)
    74  	if err != nil {
    75  		return nil, err
    76  	}
    77  	defer f.Close()
    78  	libs, err := f.ImportedLibraries()
    79  	if err != nil {
    80  		return nil, err
    81  	}
    82  	for i := range libs {
    83  		if pos := strings.LastIndexByte(libs[i], '/'); pos >= 0 {
    84  			libs[i] = libs[i][pos+1:]
    85  		}
    86  		if pos := strings.IndexByte(libs[i], '.'); pos >= 0 {
    87  			libs[i] = libs[i][:pos]
    88  		}
    89  	}
    90  	return libs, nil
    91  }