C++: Tests for cc_binary linking shared libraries
This is guarded behind the --experimental_cc_shared_library flag
PiperOrigin-RevId: 283982821
Change-Id: Ifec330c01d7b480b641f8432ce94175291e79238
diff --git a/examples/experimental_cc_shared_library.bzl b/examples/experimental_cc_shared_library.bzl
index 27c7856..aff1f78 100644
--- a/examples/experimental_cc_shared_library.bzl
+++ b/examples/experimental_cc_shared_library.bzl
@@ -227,7 +227,6 @@
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]
diff --git a/examples/test_cc_shared_library/BUILD b/examples/test_cc_shared_library/BUILD
index a1c4a5a..137a9ca 100644
--- a/examples/test_cc_shared_library/BUILD
+++ b/examples/test_cc_shared_library/BUILD
@@ -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",
@@ -26,10 +34,18 @@
)
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.
@@ -40,12 +56,14 @@
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"],
)
@@ -70,6 +88,7 @@
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",
@@ -79,6 +98,7 @@
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",
@@ -88,6 +108,7 @@
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",
@@ -99,6 +120,7 @@
srcs = ["cc_shared_library_integration_test.sh"],
data = [
":bar_so",
+ ":binary",
":foo_so",
],
)
diff --git a/examples/test_cc_shared_library/bar2.cc b/examples/test_cc_shared_library/bar2.cc
index e88a8d1..21f8957 100644
--- a/examples/test_cc_shared_library/bar2.cc
+++ b/examples/test_cc_shared_library/bar2.cc
@@ -1 +1,3 @@
+#include "examples/test_cc_shared_library/bar2.h"
+
int bar2() { return 42; }
diff --git a/examples/test_cc_shared_library/bar2.h b/examples/test_cc_shared_library/bar2.h
new file mode 100644
index 0000000..06f30f9
--- /dev/null
+++ b/examples/test_cc_shared_library/bar2.h
@@ -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_
diff --git a/examples/test_cc_shared_library/bar3.cc b/examples/test_cc_shared_library/bar3.cc
index 431789e..e2c2f01 100644
--- a/examples/test_cc_shared_library/bar3.cc
+++ b/examples/test_cc_shared_library/bar3.cc
@@ -1 +1,3 @@
+#include "examples/test_cc_shared_library/bar3.h"
+
int bar3() { return 42; }
diff --git a/examples/test_cc_shared_library/bar3.h b/examples/test_cc_shared_library/bar3.h
new file mode 100644
index 0000000..a526b69
--- /dev/null
+++ b/examples/test_cc_shared_library/bar3.h
@@ -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_
diff --git a/examples/test_cc_shared_library/bar4.cc b/examples/test_cc_shared_library/bar4.cc
index 479cf92..ad2a110 100644
--- a/examples/test_cc_shared_library/bar4.cc
+++ b/examples/test_cc_shared_library/bar4.cc
@@ -1 +1,3 @@
+#include "examples/test_cc_shared_library/bar4.h"
+
int bar4() { return 42; }
diff --git a/examples/test_cc_shared_library/bar4.h b/examples/test_cc_shared_library/bar4.h
new file mode 100644
index 0000000..e60ffff
--- /dev/null
+++ b/examples/test_cc_shared_library/bar4.h
@@ -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_
diff --git a/examples/test_cc_shared_library/baz.cc b/examples/test_cc_shared_library/baz.cc
index d9071f8..6e686e0 100644
--- a/examples/test_cc_shared_library/baz.cc
+++ b/examples/test_cc_shared_library/baz.cc
@@ -1 +1,3 @@
+#include "examples/test_cc_shared_library/baz.h"
+
int baz() { return 42; }
diff --git a/examples/test_cc_shared_library/baz.h b/examples/test_cc_shared_library/baz.h
new file mode 100644
index 0000000..0d368fa
--- /dev/null
+++ b/examples/test_cc_shared_library/baz.h
@@ -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_
diff --git a/examples/test_cc_shared_library/cc_shared_library_integration_test.sh b/examples/test_cc_shared_library/cc_shared_library_integration_test.sh
index 4acee3f..f999b03 100755
--- a/examples/test_cc_shared_library/cc_shared_library_integration_test.sh
+++ b/examples/test_cc_shared_library/cc_shared_library_integration_test.sh
@@ -29,18 +29,27 @@
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
}
-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
diff --git a/examples/test_cc_shared_library/foo.cc b/examples/test_cc_shared_library/foo.cc
index adc1d7b..ccbaef2 100644
--- a/examples/test_cc_shared_library/foo.cc
+++ b/examples/test_cc_shared_library/foo.cc
@@ -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;
}
diff --git a/examples/test_cc_shared_library/foo.h b/examples/test_cc_shared_library/foo.h
new file mode 100644
index 0000000..167d992
--- /dev/null
+++ b/examples/test_cc_shared_library/foo.h
@@ -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_
diff --git a/examples/test_cc_shared_library/main.cc b/examples/test_cc_shared_library/main.cc
new file mode 100644
index 0000000..283057f
--- /dev/null
+++ b/examples/test_cc_shared_library/main.cc
@@ -0,0 +1,8 @@
+#include <iostream>
+
+#include "examples/test_cc_shared_library/foo.h"
+
+int main() {
+ std::cout << "hello " << foo() << std::endl;
+ return 0;
+}
diff --git a/examples/test_cc_shared_library/preloaded_dep.cc b/examples/test_cc_shared_library/preloaded_dep.cc
new file mode 100644
index 0000000..bbd825c
--- /dev/null
+++ b/examples/test_cc_shared_library/preloaded_dep.cc
@@ -0,0 +1,3 @@
+#include "examples/test_cc_shared_library/preloaded_dep.h"
+
+int preloaded_dep() { return 42; }
diff --git a/examples/test_cc_shared_library/preloaded_dep.h b/examples/test_cc_shared_library/preloaded_dep.h
new file mode 100644
index 0000000..7a356ac
--- /dev/null
+++ b/examples/test_cc_shared_library/preloaded_dep.h
@@ -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_
diff --git a/examples/test_cc_shared_library/qux.cc b/examples/test_cc_shared_library/qux.cc
index ab0e2b6..23d0312 100644
--- a/examples/test_cc_shared_library/qux.cc
+++ b/examples/test_cc_shared_library/qux.cc
@@ -1 +1,3 @@
+#include "examples/test_cc_shared_library/qux.h"
+
int qux() { return 42; }
diff --git a/examples/test_cc_shared_library/qux.h b/examples/test_cc_shared_library/qux.h
new file mode 100644
index 0000000..f38f5a8
--- /dev/null
+++ b/examples/test_cc_shared_library/qux.h
@@ -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_