Debian Codegraph @

llvm-project/utils/bazel/llvm-project-overlay/libc/libc_build_rules.bzl

raw
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
4
5 """LLVM libc starlark rules for building individual functions."""
6
7 load(":platforms.bzl", "PLATFORM_CPU_ARM64", "PLATFORM_CPU_X86_64")
8 load("@bazel_skylib//lib:selects.bzl", "selects")
9
10 LIBC_ROOT_TARGET = ":libc_root"
11 INTERNAL_SUFFIX = ".__internal__"
12
13 def _libc_library(name, copts = None, **kwargs):
14 """Internal macro to serve as a base for all other libc library rules.
15
16 Args:
17 name: Target name.
18 copts: The special compiler options for the target.
19 **kwargs: All other attributes relevant for the cc_library rule.
20 """
21 copts = copts or []
22
23 # We want all libc sources to be compiled with "hidden" visibility.
24 # The public symbols will be given "default" visibility explicitly.
25 # See src/__support/common.h for more information.
26 copts.append("-fvisibility=hidden")
27 native.cc_library(
28 name = name,
29 copts = copts,
30 linkstatic = 1,
31 **kwargs
32 )
33
34 # A convenience var which should be used to list all libc support libraries.
35 # Any library which does not define a public function should be listed with
36 # libc_support_library.
37 libc_support_library = _libc_library
38
39 def libc_function(
40 name,
41 srcs,
42 weak = False,
43 deps = None,
44 copts = None,
45 local_defines = None,
46 **kwargs):
47 """Add target for a libc function.
48
49 The libc function is eventually available as a cc_library target by name
50 "name". LLVM libc implementations of libc functions are in C++. So, this
51 rule internally generates a C wrapper for the C++ implementation and adds
52 it to the source list of the cc_library. This way, the C++ implementation
53 and the C wrapper are both available in the cc_library.
54
55 Args:
56 name: Target name. It is normally the name of the function this target is
57 for.
58 srcs: The .cpp files which contain the function implementation.
59 weak: Make the symbol corresponding to the libc function "weak".
60 deps: The list of target dependencies if any.
61 copts: The list of options to add to the C++ compilation command.
62 local_defines: The preprocessor defines which will be prepended with -D
63 and passed to the compile command of this target but not
64 its deps.
65 **kwargs: Other attributes relevant for a cc_library. For example, deps.
66 """
67 deps = deps or []
68 deps.append(LIBC_ROOT_TARGET)
69 copts = copts or []
70 copts.append("-O3")
71 copts.append("-fno-builtin")
72 copts.append("-fno-lax-vector-conversions")
73
74 # We compile the code twice, the first target is suffixed with ".__internal__" and contains the
75 # C++ functions in the "__llvm_libc" namespace. This allows us to test the function in the
76 # presence of another libc.
77 native.cc_library(
78 name = name + INTERNAL_SUFFIX,
79 srcs = srcs,
80 deps = deps,
81 copts = copts,
82 linkstatic = 1,
83 **kwargs
84 )
85
86 # This second target is the llvm libc C function.
87
88 func_attrs = ["__attribute__((visibility(\"default\")))"]
89 if weak:
90 func_attrs.append("__attribute__((weak))")
91 local_defines = local_defines or ["LLVM_LIBC_PUBLIC_PACKAGING"]
92 local_defines.append("LLVM_LIBC_FUNCTION_ATTR='%s'" % " ".join(func_attrs))
93 _libc_library(
94 name = name,
95 srcs = srcs,
96 deps = deps,
97 copts = copts,
98 local_defines = local_defines,
99 **kwargs
100 )
101
102 def libc_math_function(
103 name,
104 specializations = None,
105 additional_deps = None):
106 """Add a target for a math function.
107
108 Args:
109 name: The name of the function.
110 specializations: List of machine specializations available for this
111 function. Possible specializations are "generic",
112 "aarch64" and "x86_64".
113 additional_deps: Other deps like helper cc_library targes used by the
114 math function.
115 """
116 additional_deps = additional_deps or []
117 specializations = specializations or ["generic"]
118 select_map = {}
119 if "generic" in specializations:
120 select_map["//conditions:default"] = ["src/math/generic/" + name + ".cpp"]
121 if "aarch64" in specializations:
122 select_map[PLATFORM_CPU_ARM64] = ["src/math/aarch64/" + name + ".cpp"]
123 if "x86_64" in specializations:
124 select_map[PLATFORM_CPU_X86_64] = ["src/math/x86_64/" + name + ".cpp"]
125
126 #TODO(michaelrj): Fix the floating point dependencies
127 OLD_FPUTIL_DEPS = [
128 ":__support_fputil_basic_operations",
129 ":__support_fputil_division_and_remainder_operations",
130 ":__support_fputil_fenv_impl",
131 ":__support_fputil_fp_bits",
132 ":__support_fputil_float_properties",
133 ":__support_fputil_hypot",
134 ":__support_fputil_manipulation_functions",
135 ":__support_fputil_nearest_integer_operations",
136 ":__support_fputil_normal_float",
137 ":__support_fputil_platform_defs",
138 ":__support_builtin_wrappers",
139 ":__support_fputil_except_value_utils",
140 ]
141 libc_function(
142 name = name,
143 srcs = selects.with_or(select_map),
144 hdrs = ["src/math/" + name + ".h"],
145 deps = [":__support_common"] + OLD_FPUTIL_DEPS + additional_deps,
146 )