treewide: introduce cc_static_library_with_headers

To properly wire up cc_libraries we need to build them static and
keep the headers. Thats not possible with vanilla cc_static_library.

Change-Id: Ia72660636086132eec6722f19b4233d45ce8f837
Reviewed-on: https://review.monogon.dev/c/monogon/+/4098
Tested-by: Jenkins CI
Reviewed-by: Jan Schär <jan@monogon.tech>
diff --git a/.bazelrc b/.bazelrc
index 0615477..9ecfa43 100644
--- a/.bazelrc
+++ b/.bazelrc
@@ -99,6 +99,7 @@
 
 # Use new-style C++ toolchain resolution.
 build --incompatible_enable_cc_toolchain_resolution
+build --experimental_cc_static_library
 
 # Use new-style proto toolchain resolution.
 build --incompatible_enable_proto_toolchain_resolution
diff --git a/build/cc_static_library_with_headers/BUILD.bazel b/build/cc_static_library_with_headers/BUILD.bazel
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/build/cc_static_library_with_headers/BUILD.bazel
diff --git a/build/cc_static_library_with_headers/def.bzl b/build/cc_static_library_with_headers/def.bzl
new file mode 100644
index 0000000..606180f
--- /dev/null
+++ b/build/cc_static_library_with_headers/def.bzl
@@ -0,0 +1,34 @@
+load("@rules_cc//cc/common:cc_info.bzl", "CcInfo")
+
+# This is a workaround for the linux kernel build, as it requires static
+# linked libraries but also their headers. This is fairly cursed and should
+# be removed as fast as possible.
+def cc_static_library_with_headers(name, dep):
+    # The artifact name has to be the same as the name of the
+    # static library, so that the linker can find it.
+    artifact_name = Label(dep).name
+    native.cc_static_library(
+        name = artifact_name,
+        deps = [dep],
+    )
+
+    _cc_static_library_wrapper(
+        name = name,
+        static_library = artifact_name,
+        library = dep,
+    )
+
+def _cc_static_library_wrapper_impl(ctx):
+    return [
+        ctx.attr.static_library[DefaultInfo],
+        ctx.attr.static_library[OutputGroupInfo],
+        ctx.attr.library[CcInfo],
+    ]
+
+_cc_static_library_wrapper = rule(
+    implementation = _cc_static_library_wrapper_impl,
+    attrs = {
+        "static_library": attr.label(),
+        "library": attr.label(),
+    },
+)