C++: Tests for cc_binary linking shared libraries

This is guarded behind the --experimental_cc_shared_library flag

PiperOrigin-RevId: 283982821
Change-Id: Ifec330c01d7b480b641f8432ce94175291e79238
This commit is contained in:
Googler 2019-12-05 08:49:37 -08:00 committed by Copybara-Service
parent 01d4a48911
commit cd7e8a690c
18 changed files with 105 additions and 6 deletions

View File

@ -227,7 +227,6 @@ def _cc_shared_library_impl(ctx):
additional_inputs = [] additional_inputs = []
if ctx.file.visibility_file != None: if ctx.file.visibility_file != None:
user_link_flags = [ user_link_flags = [
"-Wl,--no-undefined", # Just here for testing.
"-Wl,--version-script=" + ctx.file.visibility_file.path, "-Wl,--version-script=" + ctx.file.visibility_file.path,
] ]
additional_inputs = [ctx.file.visibility_file] additional_inputs = [ctx.file.visibility_file]

View File

@ -1,9 +1,17 @@
load("//cc:defs.bzl", "cc_library") load("//cc:defs.bzl", "cc_binary", "cc_library")
load("//examples:experimental_cc_shared_library.bzl", "cc_shared_library") load("//examples:experimental_cc_shared_library.bzl", "cc_shared_library")
cc_binary(
name = "binary",
srcs = ["main.cc"],
dynamic_deps = ["foo_so"],
deps = ["foo"],
)
cc_shared_library( cc_shared_library(
name = "foo_so", name = "foo_so",
dynamic_deps = ["bar_so"], dynamic_deps = ["bar_so"],
preloaded_deps = ["preloaded_dep"],
visibility_file = "foo.lds", visibility_file = "foo.lds",
exports = [ exports = [
"foo", "foo",
@ -25,11 +33,19 @@ cc_shared_library(
], ],
) )
cc_library(
name = "preloaded_dep",
srcs = ["preloaded_dep.cc"],
hdrs = ["preloaded_dep.h"],
)
cc_library( cc_library(
name = "foo", name = "foo",
srcs = ["foo.cc"], srcs = ["foo.cc"],
hdrs = ["foo.h"],
linked_statically_by = ["//examples/test_cc_shared_library:foo_so"], linked_statically_by = ["//examples/test_cc_shared_library:foo_so"],
deps = [ deps = [
"preloaded_dep",
"bar", "bar",
"baz", "baz",
# Not exported. # Not exported.
@ -40,12 +56,14 @@ cc_library(
cc_library( cc_library(
name = "baz", name = "baz",
srcs = ["baz.cc"], srcs = ["baz.cc"],
hdrs = ["baz.h"],
linked_statically_by = ["//examples/test_cc_shared_library:foo_so"], linked_statically_by = ["//examples/test_cc_shared_library:foo_so"],
) )
cc_library( cc_library(
name = "qux", name = "qux",
srcs = ["qux.cc"], srcs = ["qux.cc"],
hdrs = ["qux.h"],
linked_statically_by = ["//examples/test_cc_shared_library:foo_so"], linked_statically_by = ["//examples/test_cc_shared_library:foo_so"],
) )
@ -70,6 +88,7 @@ cc_library(
cc_library( cc_library(
name = "bar2", name = "bar2",
srcs = ["bar2.cc"], srcs = ["bar2.cc"],
hdrs = ["bar2.h"],
linked_statically_by = [ linked_statically_by = [
"//examples/test_cc_shared_library:bar_so", "//examples/test_cc_shared_library:bar_so",
"//examples/test_cc_shared_library:foo_so", "//examples/test_cc_shared_library:foo_so",
@ -79,6 +98,7 @@ cc_library(
cc_library( cc_library(
name = "bar3", name = "bar3",
srcs = ["bar3.cc"], srcs = ["bar3.cc"],
hdrs = ["bar3.h"],
linked_statically_by = [ linked_statically_by = [
"//examples/test_cc_shared_library:bar_so", "//examples/test_cc_shared_library:bar_so",
"//examples/test_cc_shared_library:foo_so", "//examples/test_cc_shared_library:foo_so",
@ -88,6 +108,7 @@ cc_library(
cc_library( cc_library(
name = "bar4", name = "bar4",
srcs = ["bar4.cc"], srcs = ["bar4.cc"],
hdrs = ["bar4.h"],
linked_statically_by = [ linked_statically_by = [
"//examples/test_cc_shared_library:bar_so", "//examples/test_cc_shared_library:bar_so",
"//examples/test_cc_shared_library:foo_so", "//examples/test_cc_shared_library:foo_so",
@ -99,6 +120,7 @@ sh_test(
srcs = ["cc_shared_library_integration_test.sh"], srcs = ["cc_shared_library_integration_test.sh"],
data = [ data = [
":bar_so", ":bar_so",
":binary",
":foo_so", ":foo_so",
], ],
) )

View File

@ -1 +1,3 @@
#include "examples/test_cc_shared_library/bar2.h"
int bar2() { return 42; } int bar2() { return 42; }

View File

@ -0,0 +1,6 @@
#ifndef EXAMPLES_TEST_CC_SHARED_LIBRARY_BAR_2_H_
#define EXAMPLES_TEST_CC_SHARED_LIBRARY_BAR_2_H_
int bar2();
#endif // EXAMPLES_TEST_CC_SHARED_LIBRARY_BAR_2_H_

View File

@ -1 +1,3 @@
#include "examples/test_cc_shared_library/bar3.h"
int bar3() { return 42; } int bar3() { return 42; }

View File

@ -0,0 +1,6 @@
#ifndef EXAMPLES_TEST_CC_SHARED_LIBRARY_BAR_3_H_
#define EXAMPLES_TEST_CC_SHARED_LIBRARY_BAR_3_H_
int bar3();
#endif // EXAMPLES_TEST_CC_SHARED_LIBRARY_BAR_3_H_

View File

@ -1 +1,3 @@
#include "examples/test_cc_shared_library/bar4.h"
int bar4() { return 42; } int bar4() { return 42; }

View File

@ -0,0 +1,6 @@
#ifndef EXAMPLES_TEST_CC_SHARED_LIBRARY_BAR_4_H_
#define EXAMPLES_TEST_CC_SHARED_LIBRARY_BAR_4_H_
int bar4();
#endif // EXAMPLES_TEST_CC_SHARED_LIBRARY_BAR_4_H_

View File

@ -1 +1,3 @@
#include "examples/test_cc_shared_library/baz.h"
int baz() { return 42; } int baz() { return 42; }

View File

@ -0,0 +1,6 @@
#ifndef EXAMPLES_TEST_CC_SHARED_LIBRARY_BAZ_H_
#define EXAMPLES_TEST_CC_SHARED_LIBRARY_BAZ_H_
int baz();
#endif // EXAMPLES_TEST_CC_SHARED_LIBRARY_BAZ_H_

View File

@ -29,18 +29,27 @@ function check_symbol_absent() {
fi fi
} }
function test_output { function test_shared_library_symbols() {
foo_so=$(find . -name libfoo_so.so) foo_so=$(find . -name libfoo_so.so)
symbols=$(nm -D $foo_so) symbols=$(nm -D $foo_so)
check_symbol_present "$symbols" "U _Z3barv" check_symbol_present "$symbols" "U _Z3barv"
check_symbol_present "$symbols" "T _Z3bazv" check_symbol_present "$symbols" "T _Z3bazv"
check_symbol_present "$symbols" "T _Z3foov" check_symbol_present "$symbols" "T _Z3foov"
# Check that the preloaded dep symbol is not present
check_symbol_present "$symbols" "U _Z13preloaded_depv"
check_symbol_absent "$symbols" "_Z3quxv" check_symbol_absent "$symbols" "_Z3quxv"
check_symbol_absent "$symbols" "_Z4bar3v" check_symbol_absent "$symbols" "_Z4bar3v"
check_symbol_absent "$symbols" "_Z4bar4v" check_symbol_absent "$symbols" "_Z4bar4v"
exit 0
} }
test_output function test_binary() {
binary=$(find . -name binary)
symbols=$(nm -D $binary)
check_symbol_present "$symbols" "T _Z13preloaded_depv"
check_symbol_present "$symbols" "U _Z3foov"
$binary | (grep -q "hello 42" || (echo "Expected 'hello 42'" && exit 1))
}
test_shared_library_symbols
test_binary

View File

@ -1,6 +1,12 @@
#include "examples/test_cc_shared_library/bar.h" #include "examples/test_cc_shared_library/bar.h"
#include "examples/test_cc_shared_library/baz.h"
#include "examples/test_cc_shared_library/preloaded_dep.h"
#include "examples/test_cc_shared_library/qux.h"
int foo() { int foo() {
bar(); bar();
baz();
qux();
preloaded_dep();
return 42; return 42;
} }

View File

@ -0,0 +1,6 @@
#ifndef EXAMPLES_TEST_CC_SHARED_LIBRARY_FOO_H_
#define EXAMPLES_TEST_CC_SHARED_LIBRARY_FOO_H_
int foo();
#endif // EXAMPLES_TEST_CC_SHARED_LIBRARY_FOO_H_

View File

@ -0,0 +1,8 @@
#include <iostream>
#include "examples/test_cc_shared_library/foo.h"
int main() {
std::cout << "hello " << foo() << std::endl;
return 0;
}

View File

@ -0,0 +1,3 @@
#include "examples/test_cc_shared_library/preloaded_dep.h"
int preloaded_dep() { return 42; }

View File

@ -0,0 +1,6 @@
#ifndef EXAMPLES_TEST_CC_SHARED_LIBRARY_PRELOADED_DEP_H_
#define EXAMPLES_TEST_CC_SHARED_LIBRARY_PRELOADED_DEP_H_
int preloaded_dep();
#endif // EXAMPLES_TEST_CC_SHARED_LIBRARY_PRELOADED_DEP_H_

View File

@ -1 +1,3 @@
#include "examples/test_cc_shared_library/qux.h"
int qux() { return 42; } int qux() { return 42; }

View File

@ -0,0 +1,6 @@
#ifndef EXAMPLES_TEST_CC_SHARED_LIBRARY_QUX_H_
#define EXAMPLES_TEST_CC_SHARED_LIBRARY_QUX_H_
int qux();
#endif // EXAMPLES_TEST_CC_SHARED_LIBRARY_QUX_H_