-
-
Notifications
You must be signed in to change notification settings - Fork 670
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Filter out -lstdc++ for targets with no C++ code (#1686)
This avoids an unnecessary run-time dependency on libstdc++.so, which may not be present in container environments. go build only adds this dependency if C++ code is included in the build. We now filter out this dependency from the cc toolchain flags unless we have C++/ObjC source or we have a dependency on a cc_library or objc_library. Fixes #1681 Related bazelbuild/bazel#2954
- Loading branch information
Showing
9 changed files
with
277 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
#include <iostream> | ||
|
||
void bar() { | ||
std::cout << "bar" << std::endl; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
// Copyright 2018 The Bazel Authors. All rights reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package cc_libs_test | ||
|
||
import ( | ||
"debug/macho" | ||
"path" | ||
"strings" | ||
"testing" | ||
|
||
"github.com/bazelbuild/rules_go/go/tools/bazel" | ||
) | ||
|
||
func TestBinaries(t *testing.T) { | ||
for _, test := range []struct { | ||
shortPath string | ||
wantLibs map[string]bool | ||
}{ | ||
{ | ||
shortPath: "tests/core/cgo/pure_bin", | ||
wantLibs: map[string]bool{"libSystem": false, "libc++": false}, | ||
}, { | ||
shortPath: "tests/core/cgo/c_srcs_bin", | ||
wantLibs: map[string]bool{"libSystem": true, "libc++": false}, | ||
}, { | ||
shortPath: "tests/core/cgo/cc_srcs_bin", | ||
wantLibs: map[string]bool{"libSystem": true, "libc++": true}, | ||
}, { | ||
shortPath: "tests/core/cgo/cc_deps_bin", | ||
wantLibs: map[string]bool{"libSystem": true, "libc++": true}, | ||
}, | ||
} { | ||
t.Run(path.Base(test.shortPath), func(t *testing.T) { | ||
libs, err := listLibs(test.shortPath) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
haveLibs := make(map[string]bool) | ||
for _, lib := range libs { | ||
haveLibs[lib] = true | ||
} | ||
for haveLib := range haveLibs { | ||
if wantLib, ok := test.wantLibs[haveLib]; ok && !wantLib { | ||
t.Errorf("unexpected dependency on library %q", haveLib) | ||
} | ||
} | ||
for wantLib, want := range test.wantLibs { | ||
if want && !haveLibs[wantLib] { | ||
t.Errorf("wanted dependency on library %q", wantLib) | ||
} | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func listLibs(shortPath string) ([]string, error) { | ||
binPath, err := bazel.Runfile(shortPath) | ||
if err != nil { | ||
return nil, err | ||
} | ||
f, err := macho.Open(binPath) | ||
if err != nil { | ||
return nil, err | ||
} | ||
defer f.Close() | ||
libs, err := f.ImportedLibraries() | ||
if err != nil { | ||
return nil, err | ||
} | ||
for i := range libs { | ||
if pos := strings.LastIndexByte(libs[i], '/'); pos >= 0 { | ||
libs[i] = libs[i][pos+1:] | ||
} | ||
if pos := strings.IndexByte(libs[i], '.'); pos >= 0 { | ||
libs[i] = libs[i][:pos] | ||
} | ||
} | ||
return libs, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
// Copyright 2018 The Bazel Authors. All rights reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package cc_libs_test | ||
|
||
import ( | ||
"debug/elf" | ||
"path" | ||
"strings" | ||
"testing" | ||
|
||
"github.com/bazelbuild/rules_go/go/tools/bazel" | ||
) | ||
|
||
func TestBinaries(t *testing.T) { | ||
for _, test := range []struct { | ||
shortPath string | ||
wantLibs map[string]bool | ||
}{ | ||
{ | ||
shortPath: "tests/core/cgo/pure_bin", | ||
wantLibs: map[string]bool{"libc": false, "libstdc++": false}, | ||
}, { | ||
shortPath: "tests/core/cgo/c_srcs_bin", | ||
wantLibs: map[string]bool{"libc": true, "libstdc++": false}, | ||
}, { | ||
shortPath: "tests/core/cgo/cc_srcs_bin", | ||
wantLibs: map[string]bool{"libc": true, "libstdc++": true}, | ||
}, { | ||
shortPath: "tests/core/cgo/cc_deps_bin", | ||
wantLibs: map[string]bool{"libc": true, "libstdc++": true}, | ||
}, | ||
} { | ||
t.Run(path.Base(test.shortPath), func(t *testing.T) { | ||
libs, err := listLibs(test.shortPath) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
haveLibs := make(map[string]bool) | ||
for _, lib := range libs { | ||
haveLibs[lib] = true | ||
} | ||
for haveLib := range haveLibs { | ||
if wantLib, ok := test.wantLibs[haveLib]; ok && !wantLib { | ||
t.Errorf("unexpected dependency on library %q", haveLib) | ||
} | ||
} | ||
for wantLib, want := range test.wantLibs { | ||
if want && !haveLibs[wantLib] { | ||
t.Errorf("wanted dependency on library %q", wantLib) | ||
} | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func listLibs(shortPath string) ([]string, error) { | ||
binPath, err := bazel.Runfile(shortPath) | ||
if err != nil { | ||
return nil, err | ||
} | ||
f, err := elf.Open(binPath) | ||
if err != nil { | ||
return nil, err | ||
} | ||
defer f.Close() | ||
libs, err := f.ImportedLibraries() | ||
if err != nil { | ||
return nil, err | ||
} | ||
for i := range libs { | ||
if pos := strings.LastIndexByte(libs[i], '/'); pos >= 0 { | ||
libs[i] = libs[i][pos+1:] | ||
} | ||
if pos := strings.IndexByte(libs[i], '.'); pos >= 0 { | ||
libs[i] = libs[i][:pos] | ||
} | ||
} | ||
return libs, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
#include <stdio.h> | ||
|
||
void foo() { | ||
printf("foo\n"); | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
package main | ||
|
||
func main() {} |