1 # This file is licensed under the Apache License v2.0 with LLVM Exceptions.
2 # See https://llvm.org/LICENSE.txt for license information.
3 # SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
5 """A macro to produce a loadable plugin library for the target OS.
7 This macro produces a `cc_binary` rule with the name `name + "_impl"`. It
8 forces the rule to statically link in its dependencies but to be linked as a
9 shared "plugin" library. It then creates binary aliases to `.so`, `.dylib`
10 ,and `.dll` suffixed names for use on various platforms and selects between
11 these into a filegroup with the exact name passed to the macro.
14 load("@rules_cc//cc:defs.bzl", "cc_binary")
15 load(":binary_alias.bzl", "binary_alias")
17 def cc_plugin_library(name, **kwargs):
18 # Neither the name of the plugin binary nor tags on whether it is built are
19 # configurable. Instead, we build a `cc_binary` that implements the plugin
20 # library using a `_impl` suffix. Bazel will use appropriate flags to cause
21 # this file to be a plugin library regardless of its name. We then create
22 # binary aliases in the different possible platform names, and select
23 # between these different names into a filegroup. The macro's name becomes
24 # the filegroup name and it contains exactly one target that is the target
25 # platform suffixed plugin library.
27 # All-in-all, this is a pretty poor workaround. I think this is part of the
28 # Bazel issue: https://github.com/bazelbuild/bazel/issues/7538
30 name = name + "_impl",
37 binary = ":" + name + "_impl",
41 binary = ":" + name + "_impl",
44 name = name + ".dylib",
45 binary = ":" + name + "_impl",
50 "@bazel_tools//src/conditions:windows": [":" + name + ".dll"],
51 "@bazel_tools//src/conditions:darwin": [":" + name + ".dylib"],
52 "//conditions:default": [":" + name + ".so"],