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 """LLVM libc startlark rules for building individual functions."""
7 load(":platforms.bzl", "PLATFORM_CPU_ARM64", "PLATFORM_CPU_X86_64")
8 load("@bazel_skylib//lib:selects.bzl", "selects")
10 LIBC_ROOT_TARGET = ":libc_root"
11 INTERNAL_SUFFIX = ".__internal__"
13 def libc_function(name, srcs, deps = None, copts = None, **kwargs):
14 """Add target for a libc function.
16 The libc function is eventually available as a cc_library target by name
17 "name". LLVM libc implementations of libc functions are in C++. So, this
18 rule internally generates a C wrapper for the C++ implementation and adds
19 it to the source list of the cc_library. This way, the C++ implementation
20 and the C wrapper are both available in the cc_library.
23 name: Target name. It is normally the name of the function this target is
25 srcs: The .cpp files which contain the function implementation.
26 deps: The list of target dependencies if any.
27 copts: The list of options to add to the C++ compilation command.
28 **kwargs: Other attributes relevant for a cc_library. For example, deps.
31 deps.append(LIBC_ROOT_TARGET)
35 # We compile the code twice, the first target is suffixed with ".__internal__" and contains the
36 # C++ functions in the "__llvm_libc" namespace. This allows us to test the function in the
37 # presence of another libc.
39 name = name + INTERNAL_SUFFIX,
47 # This second target is the llvm libc C function.
48 copts.append("-DLLVM_LIBC_PUBLIC_PACKAGING")
58 def libc_math_function(
60 specializations = None,
61 additional_deps = None):
62 """Add a target for a math function.
65 name: The name of the function.
66 specializations: List of machine specializations available for this
67 function. Possible specializations are "generic",
68 "aarch64" and "x86_64".
69 additional_deps: Other deps like helper cc_library targes used by the
72 additional_deps = additional_deps or []
73 specializations = specializations or ["generic"]
75 if "generic" in specializations:
76 select_map["//conditions:default"] = ["src/math/generic/" + name + ".cpp"]
77 if "aarch64" in specializations:
78 select_map[PLATFORM_CPU_ARM64] = ["src/math/aarch64/" + name + ".cpp"]
79 if "x86_64" in specializations:
80 select_map[PLATFORM_CPU_X86_64] = ["src/math/x86_64/" + name + ".cpp"]
83 srcs = selects.with_or(select_map),
84 hdrs = ["src/math/" + name + ".h"],
85 deps = [":__support_common", ":__support_fputil"] + additional_deps,