Skip to content

Instantly share code, notes, and snippets.

@keith
Created April 8, 2026 19:47
Show Gist options
  • Select an option

  • Save keith/d01644540fbe4d43d1eef907d28eb1df to your computer and use it in GitHub Desktop.

Select an option

Save keith/d01644540fbe4d43d1eef907d28eb1df to your computer and use it in GitHub Desktop.
An example macro for producing static and dynamic libraries for a cc_library
load("@rules_cc//cc:cc_library.bzl", "cc_library")
load("@rules_cc//cc:cc_shared_library.bzl", "cc_shared_library")
load("@rules_cc//cc/common:cc_info.bzl", "CcInfo")
load("@rules_cc//cc/common:cc_shared_library_info.bzl", "CcSharedLibraryInfo")
def _cc_library_reexporter_impl(ctx):
return [
ctx.attr.static_lib[CcInfo],
ctx.attr.dynamic_lib[CcSharedLibraryInfo],
]
_cc_library_reexporter = rule(
implementation = _cc_library_reexporter_impl,
attrs = {
"static_lib": attr.label(providers = [CcInfo]),
"dynamic_lib": attr.label(providers = [CcSharedLibraryInfo]),
},
)
def foo(
name,
additional_linker_inputs = [],
local_defines = [],
defines = [],
static_defines = [],
dynamic_defines = [],
user_link_flags = [],
shared_lib_name = None,
**kwargs):
cc_library(
name = name + ".underlying.static",
linkstatic = True,
defines = defines + static_defines,
**kwargs
)
cc_library(
name = name + ".underlying.shared",
linkstatic = True,
defines = defines,
local_defines = local_defines + dynamic_defines,
**kwargs
)
cc_shared_library(
name = name + ".underlying.dynamic_deps",
deps = [":" + name + ".underlying.shared"],
user_link_flags = user_link_flags,
shared_lib_name = shared_lib_name,
additional_linker_inputs = additional_linker_inputs,
exports_filter = [
":" + name + ".underlying.static",
":" + name + ".underlying.shared",
],
)
_cc_library_reexporter(
name = name,
static_lib = ":" + name + ".underlying.static",
dynamic_lib = ":" + name + ".underlying.dynamic_deps",
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment