Skip to content

Commit

Permalink
C++: Tests for cc_binary linking shared libraries
Browse files Browse the repository at this point in the history
This is guarded behind the --experimental_cc_shared_library flag

PiperOrigin-RevId: 283982821
Change-Id: Ifec330c01d7b480b641f8432ce94175291e79238
  • Loading branch information
oquenchil authored and copybara-github committed Dec 5, 2019
1 parent 01d4a48 commit cd7e8a6
Show file tree
Hide file tree
Showing 18 changed files with 104 additions and 5 deletions.
1 change: 0 additions & 1 deletion examples/experimental_cc_shared_library.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,6 @@ def _cc_shared_library_impl(ctx):
additional_inputs = []
if ctx.file.visibility_file != None:
user_link_flags = [
"-Wl,--no-undefined", # Just here for testing.
"-Wl,--version-script=" + ctx.file.visibility_file.path,
]
additional_inputs = [ctx.file.visibility_file]
Expand Down
24 changes: 23 additions & 1 deletion examples/test_cc_shared_library/BUILD
Original file line number Diff line number Diff line change
@@ -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")

cc_binary(
name = "binary",
srcs = ["main.cc"],
dynamic_deps = ["foo_so"],
deps = ["foo"],
)

cc_shared_library(
name = "foo_so",
dynamic_deps = ["bar_so"],
preloaded_deps = ["preloaded_dep"],
visibility_file = "foo.lds",
exports = [
"foo",
Expand All @@ -25,11 +33,19 @@ cc_shared_library(
],
)

cc_library(
name = "preloaded_dep",
srcs = ["preloaded_dep.cc"],
hdrs = ["preloaded_dep.h"],
)

cc_library(
name = "foo",
srcs = ["foo.cc"],
hdrs = ["foo.h"],
linked_statically_by = ["//examples/test_cc_shared_library:foo_so"],
deps = [
"preloaded_dep",
"bar",
"baz",
# Not exported.
Expand All @@ -40,12 +56,14 @@ cc_library(
cc_library(
name = "baz",
srcs = ["baz.cc"],
hdrs = ["baz.h"],
linked_statically_by = ["//examples/test_cc_shared_library:foo_so"],
)

cc_library(
name = "qux",
srcs = ["qux.cc"],
hdrs = ["qux.h"],
linked_statically_by = ["//examples/test_cc_shared_library:foo_so"],
)

Expand All @@ -70,6 +88,7 @@ cc_library(
cc_library(
name = "bar2",
srcs = ["bar2.cc"],
hdrs = ["bar2.h"],
linked_statically_by = [
"//examples/test_cc_shared_library:bar_so",
"//examples/test_cc_shared_library:foo_so",
Expand All @@ -79,6 +98,7 @@ cc_library(
cc_library(
name = "bar3",
srcs = ["bar3.cc"],
hdrs = ["bar3.h"],
linked_statically_by = [
"//examples/test_cc_shared_library:bar_so",
"//examples/test_cc_shared_library:foo_so",
Expand All @@ -88,6 +108,7 @@ cc_library(
cc_library(
name = "bar4",
srcs = ["bar4.cc"],
hdrs = ["bar4.h"],
linked_statically_by = [
"//examples/test_cc_shared_library:bar_so",
"//examples/test_cc_shared_library:foo_so",
Expand All @@ -99,6 +120,7 @@ sh_test(
srcs = ["cc_shared_library_integration_test.sh"],
data = [
":bar_so",
":binary",
":foo_so",
],
)
2 changes: 2 additions & 0 deletions examples/test_cc_shared_library/bar2.cc
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
#include "examples/test_cc_shared_library/bar2.h"

int bar2() { return 42; }
6 changes: 6 additions & 0 deletions examples/test_cc_shared_library/bar2.h
Original file line number Diff line number Diff line change
@@ -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_
2 changes: 2 additions & 0 deletions examples/test_cc_shared_library/bar3.cc
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
#include "examples/test_cc_shared_library/bar3.h"

int bar3() { return 42; }
6 changes: 6 additions & 0 deletions examples/test_cc_shared_library/bar3.h
Original file line number Diff line number Diff line change
@@ -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_
2 changes: 2 additions & 0 deletions examples/test_cc_shared_library/bar4.cc
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
#include "examples/test_cc_shared_library/bar4.h"

int bar4() { return 42; }
6 changes: 6 additions & 0 deletions examples/test_cc_shared_library/bar4.h
Original file line number Diff line number Diff line change
@@ -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_
2 changes: 2 additions & 0 deletions examples/test_cc_shared_library/baz.cc
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
#include "examples/test_cc_shared_library/baz.h"

int baz() { return 42; }
6 changes: 6 additions & 0 deletions examples/test_cc_shared_library/baz.h
Original file line number Diff line number Diff line change
@@ -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_
Original file line number Diff line number Diff line change
Expand Up @@ -29,18 +29,27 @@ function check_symbol_absent() {
fi
}

function test_output {
function test_shared_library_symbols() {
foo_so=$(find . -name libfoo_so.so)
symbols=$(nm -D $foo_so)
check_symbol_present "$symbols" "U _Z3barv"
check_symbol_present "$symbols" "T _Z3bazv"
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" "_Z4bar3v"
check_symbol_absent "$symbols" "_Z4bar4v"
}

exit 0
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_output
test_shared_library_symbols
test_binary
6 changes: 6 additions & 0 deletions examples/test_cc_shared_library/foo.cc
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
#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() {
bar();
baz();
qux();
preloaded_dep();
return 42;
}
6 changes: 6 additions & 0 deletions examples/test_cc_shared_library/foo.h
Original file line number Diff line number Diff line change
@@ -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_
8 changes: 8 additions & 0 deletions examples/test_cc_shared_library/main.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#include <iostream>

#include "examples/test_cc_shared_library/foo.h"

int main() {
std::cout << "hello " << foo() << std::endl;
return 0;
}
3 changes: 3 additions & 0 deletions examples/test_cc_shared_library/preloaded_dep.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#include "examples/test_cc_shared_library/preloaded_dep.h"

int preloaded_dep() { return 42; }
6 changes: 6 additions & 0 deletions examples/test_cc_shared_library/preloaded_dep.h
Original file line number Diff line number Diff line change
@@ -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_
2 changes: 2 additions & 0 deletions examples/test_cc_shared_library/qux.cc
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
#include "examples/test_cc_shared_library/qux.h"

int qux() { return 42; }
6 changes: 6 additions & 0 deletions examples/test_cc_shared_library/qux.h
Original file line number Diff line number Diff line change
@@ -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_

0 comments on commit cd7e8a6

Please sign in to comment.