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