Adds example usage of cc_shared_library

This is only tested with Bazel at head. We modify the presubmits file so that we don't use release. The reason we don't use release is because we need very recent changes to Bazel to test cc_shared_library. In the future we will also test with release.

Also in following CLs, we will inspect in integration test the *.so output from these example builds to make sure that the correct symbols are linked statically and are visible.

After that we will add Starlark unit test to check error conditions.

RELNOTES:none
PiperOrigin-RevId: 280381939
Change-Id: I5de9b364a2a3db28d99558fbfca7bea8052e5114
diff --git a/.bazelci/presubmit.yml b/.bazelci/presubmit.yml
index bf47e2b..138231a 100644
--- a/.bazelci/presubmit.yml
+++ b/.bazelci/presubmit.yml
@@ -1,48 +1,55 @@
 ---
+x_defaults:
+  # YAML has a feature for "repeated nodes", BazelCI is fine with extra nodes
+  # it doesn't know about; so that is used to avoid repeating common subparts.
+  common: &common
+    # We have to list every package because even with exclusion notation -//foo
+    # Bazel will load the excluded package and it will be an error because at
+    # release Bazel the cc_libraries do not have all the attributes.
+    build_targets:
+    - "//:all"
+    - "//cc:all"
+    - "//cc/private/rules_impl:all"
+    - "//cc/private/toolchain:all"
+    - "//distro:all"
+    - "//examples:all"
+    - "//examples/my_c_archive:all"
+    - "//examples/my_c_compile:all"
+    - "//examples/write_cc_toolchain_cpu:all"
+    - "//tools/migration:all"
+    - "//tools/runfiles:all"
+    test_flags:
+    - "--test_timeout=300"
+    test_targets:
+    - "//:all"
+    - "//cc:all"
+    - "//cc/private/rules_impl:all"
+    - "//cc/private/toolchain:all"
+    - "//distro:all"
+    - "//examples:all"
+    - "//examples/my_c_archive:all"
+    - "//examples/my_c_compile:all"
+    - "//examples/write_cc_toolchain_cpu:all"
+    - "//tools/migration:all"
+    - "//tools/runfiles:all"
+
 buildifier:
   version: latest
   warnings: "all"
-platforms:
+
+tasks:
   ubuntu1604:
-    run_targets:
-    build_targets:
-    - "..."
-    test_flags:
-    - "--test_timeout=300"
-    test_targets:
-    - "..."
+    <<: *common
   ubuntu1804:
-    run_targets:
-    build_targets:
-    - "..."
-    test_flags:
-    - "--test_timeout=300"
-    test_targets:
-    - "..."
-  ubuntu1804_nojava:
-    run_targets:
-    build_flags:
-    - "--javabase=@openjdk11_linux_archive//:runtime"
-    build_targets:
-    - "..."
-    test_flags:
-    - "--test_timeout=300"
-    - "--javabase=@openjdk11_linux_archive//:runtime"
-    test_targets:
-    - "..."
+    <<: *common
   macos:
-    run_targets:
-    build_targets:
-    - "..."
-    test_flags:
-    - "--test_timeout=300"
-    test_targets:
-    - "..."
+    <<: *common
   windows:
-    run_targets:
+    <<: *common
+  examples:
+    platform: ubuntu1804
+    bazel: last_green
     build_targets:
-    - "..."
-    test_flags:
-    - "--test_timeout=300"
-    test_targets:
-    - "..."
+    - "//examples/test_cc_shared_library/..."
+    build_flags:
+    - "--experimental_cc_shared_library"
diff --git a/examples/experimental_cc_shared_library.bzl b/examples/experimental_cc_shared_library.bzl
index 716e02c..5d13a31 100644
--- a/examples/experimental_cc_shared_library.bzl
+++ b/examples/experimental_cc_shared_library.bzl
@@ -1,6 +1,7 @@
-""" This is an experimental implementation of cc_shared_library. We may change
-the implementation at any moment or even delete this file. Do not rely on this.
-It requires bazel >1.2  and passing the flag
+"""This is an experimental implementation of cc_shared_library.
+
+We may change the implementation at any moment or even delete this file. Do not
+rely on this. It requires bazel >1.2  and passing the flag
 --experimental_cc_shared_library
 """
 
@@ -179,7 +180,10 @@
         CcSharedLibraryInfo(
             dynamic_deps = merged_cc_shared_library_info,
             exports = ctx.attr.exports,
-            linker_input = cc_common.create_linker_input(owner = ctx.label, libraries = depset([linking_outputs.library_to_link])),
+            linker_input = cc_common.create_linker_input(
+                owner = ctx.label,
+                libraries = depset([linking_outputs.library_to_link]),
+            ),
         ),
     ]
 
diff --git a/examples/test_cc_shared_library/BUILD b/examples/test_cc_shared_library/BUILD
new file mode 100644
index 0000000..8034dc2
--- /dev/null
+++ b/examples/test_cc_shared_library/BUILD
@@ -0,0 +1,95 @@
+load("//cc:defs.bzl", "cc_library")
+load("//examples:experimental_cc_shared_library.bzl", "cc_shared_library")
+
+cc_shared_library(
+    name = "foo_so",
+    dynamic_deps = ["bar_so"],
+    visibility_file = "foo.lds",
+    exports = [
+        "foo",
+        # Not a problem to export "baz" which is depended on by foo
+        "baz",
+        # Case1
+        # This is fine. bar3 can be exported by "foo_so"
+        # even though bar3 is linked statically by bar_so, since bar_so
+        # doesn't export bar3 this is fine.
+        "bar3",
+        # Case2
+        # This is fine. foo depends on bar4. Even though bar is
+        # linked dynamically, when bar is pruned, we still have a dependency
+        # edge to bar4 from foo. Also bar_so doesn't export bar4.
+        #
+        # Note that even though the target claims to export bar4. Unless the version script is
+        # changed, the symbols from bar4 won't be exported.
+        "bar4",
+    ],
+)
+
+cc_library(
+    name = "foo",
+    srcs = ["foo.cc"],
+    linked_statically_by = ["//examples/test_cc_shared_library:foo_so"],
+    deps = [
+        "bar",
+        "baz",
+        # Not exported.
+        "qux",
+    ],
+)
+
+cc_library(
+    name = "baz",
+    srcs = ["baz.cc"],
+    linked_statically_by = ["//examples/test_cc_shared_library:foo_so"],
+)
+
+cc_library(
+    name = "qux",
+    srcs = ["qux.cc"],
+    linked_statically_by = ["//examples/test_cc_shared_library:foo_so"],
+)
+
+cc_shared_library(
+    name = "bar_so",
+    visibility_file = "bar.lds",
+    exports = [
+        "bar",
+        "bar2",
+    ],
+)
+
+cc_library(
+    name = "bar",
+    srcs = ["bar.cc"],
+    hdrs = ["bar.h"],
+    linked_statically_by = [
+        "//examples/test_cc_shared_library:bar_so",
+    ],
+)
+
+cc_library(
+    name = "bar2",
+    srcs = ["bar2.cc"],
+    linked_statically_by = [
+        "//examples/test_cc_shared_library:bar_so",
+        "//examples/test_cc_shared_library:foo_so",
+    ],
+)
+
+cc_library(
+    name = "bar3",
+    srcs = ["bar3.cc"],
+    linked_statically_by = [
+        "//examples/test_cc_shared_library:bar_so",
+        "//examples/test_cc_shared_library:foo_so",
+    ],
+)
+
+cc_library(
+    name = "bar4",
+    srcs = ["bar4.cc"],
+    linked_statically_by = [
+        "//examples/test_cc_shared_library:bar_so",
+        "//examples/test_cc_shared_library:foo_so",
+    ],
+)
diff --git a/examples/test_cc_shared_library/bar.cc b/examples/test_cc_shared_library/bar.cc
new file mode 100644
index 0000000..dd2da99
--- /dev/null
+++ b/examples/test_cc_shared_library/bar.cc
@@ -0,0 +1,3 @@
+#include "examples/test_cc_shared_library/bar.h"
+
+int bar() { return 42; }
diff --git a/examples/test_cc_shared_library/bar.h b/examples/test_cc_shared_library/bar.h
new file mode 100644
index 0000000..4a8fefe
--- /dev/null
+++ b/examples/test_cc_shared_library/bar.h
@@ -0,0 +1,6 @@
+#ifndef EXAMPLES_TEST_CC_SHARED_LIBRARY_BAR_H_
+#define EXAMPLES_TEST_CC_SHARED_LIBRARY_BAR_H_
+
+int bar();
+
+#endif  // EXAMPLES_TEST_CC_SHARED_LIBRARY_BAR_H_
diff --git a/examples/test_cc_shared_library/bar.lds b/examples/test_cc_shared_library/bar.lds
new file mode 100644
index 0000000..cfe541c
--- /dev/null
+++ b/examples/test_cc_shared_library/bar.lds
@@ -0,0 +1,5 @@
+VERS_1.1 {
+  global:
+    _Z3barv;
+  local: *;
+};
diff --git a/examples/test_cc_shared_library/bar2.cc b/examples/test_cc_shared_library/bar2.cc
new file mode 100644
index 0000000..e88a8d1
--- /dev/null
+++ b/examples/test_cc_shared_library/bar2.cc
@@ -0,0 +1 @@
+int bar2() { return 42; }
diff --git a/examples/test_cc_shared_library/bar3.cc b/examples/test_cc_shared_library/bar3.cc
new file mode 100644
index 0000000..431789e
--- /dev/null
+++ b/examples/test_cc_shared_library/bar3.cc
@@ -0,0 +1 @@
+int bar3() { return 42; }
diff --git a/examples/test_cc_shared_library/bar4.cc b/examples/test_cc_shared_library/bar4.cc
new file mode 100644
index 0000000..479cf92
--- /dev/null
+++ b/examples/test_cc_shared_library/bar4.cc
@@ -0,0 +1 @@
+int bar4() { return 42; }
diff --git a/examples/test_cc_shared_library/baz.cc b/examples/test_cc_shared_library/baz.cc
new file mode 100644
index 0000000..d9071f8
--- /dev/null
+++ b/examples/test_cc_shared_library/baz.cc
@@ -0,0 +1 @@
+int baz() { return 42; }
diff --git a/examples/test_cc_shared_library/foo.cc b/examples/test_cc_shared_library/foo.cc
new file mode 100644
index 0000000..adc1d7b
--- /dev/null
+++ b/examples/test_cc_shared_library/foo.cc
@@ -0,0 +1,6 @@
+#include "examples/test_cc_shared_library/bar.h"
+
+int foo() {
+  bar();
+  return 42;
+}
diff --git a/examples/test_cc_shared_library/foo.lds b/examples/test_cc_shared_library/foo.lds
new file mode 100644
index 0000000..54dbe17
--- /dev/null
+++ b/examples/test_cc_shared_library/foo.lds
@@ -0,0 +1,6 @@
+VERS_1.1 {
+  global:
+    _Z3foov;
+    _Z3bazv;
+  local: *;
+};
diff --git a/examples/test_cc_shared_library/not_depended_on.cc b/examples/test_cc_shared_library/not_depended_on.cc
new file mode 100644
index 0000000..adc1d7b
--- /dev/null
+++ b/examples/test_cc_shared_library/not_depended_on.cc
@@ -0,0 +1,6 @@
+#include "examples/test_cc_shared_library/bar.h"
+
+int foo() {
+  bar();
+  return 42;
+}
diff --git a/examples/test_cc_shared_library/quux.cc b/examples/test_cc_shared_library/quux.cc
new file mode 100644
index 0000000..b09c46e
--- /dev/null
+++ b/examples/test_cc_shared_library/quux.cc
@@ -0,0 +1 @@
+int quux() { return 42; }
diff --git a/examples/test_cc_shared_library/qux.cc b/examples/test_cc_shared_library/qux.cc
new file mode 100644
index 0000000..ab0e2b6
--- /dev/null
+++ b/examples/test_cc_shared_library/qux.cc
@@ -0,0 +1 @@
+int qux() { return 42; }