[DAGCombiner] Add target hook function to decide folding (mul (add x, c1), c2)
[llvm-project.git] / utils / bazel / llvm-project-overlay / llvm / cc_plugin_library.bzl
blobb3206652d6585cd49d3900c44e13c724aeac6080
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.
12 """
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.
26     #
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
29     cc_binary(
30         name = name + "_impl",
31         linkshared = True,
32         linkstatic = True,
33         **kwargs
34     )
35     binary_alias(
36         name = name + ".so",
37         binary = ":" + name + "_impl",
38     )
39     binary_alias(
40         name = name + ".dll",
41         binary = ":" + name + "_impl",
42     )
43     binary_alias(
44         name = name + ".dylib",
45         binary = ":" + name + "_impl",
46     )
47     native.filegroup(
48         name = name,
49         srcs = select({
50             "@bazel_tools//src/conditions:windows": [":" + name + ".dll"],
51             "@bazel_tools//src/conditions:darwin": [":" + name + ".dylib"],
52             "//conditions:default": [":" + name + ".so"],
53         }),
54     )