| 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_X86_64") |
| 12 |
|
| 13 |
def libc_common_copts(): |
| 14 |
root_label = Label(":libc") |
| 15 |
libc_include_path = paths.join(root_label.workspace_root, root_label.package) |
| 16 |
return [ |
| 17 |
"-I" + libc_include_path, |
| 18 |
"-I" + paths.join(libc_include_path, "include"), |
| 19 |
"-DLIBC_NAMESPACE=" + LIBC_NAMESPACE, |
| 20 |
] |
| 21 |
|
| 22 |
def libc_release_copts(): |
| 23 |
copts = [ |
| 24 |
"-DLIBC_COPT_PUBLIC_PACKAGING", |
| 25 |
# This is used to explicitly give public symbols "default" visibility. |
| 26 |
# See src/__support/common.h for more information. |
| 27 |
"-DLLVM_LIBC_FUNCTION_ATTR='[[gnu::visibility(\"default\")]]'", |
| 28 |
# All other libc sources need to be compiled with "hidden" visibility. |
| 29 |
"-fvisibility=hidden", |
| 30 |
"-O3", |
| 31 |
"-fno-builtin", |
| 32 |
"-fno-lax-vector-conversions", |
| 33 |
"-ftrivial-auto-var-init=pattern", |
| 34 |
"-fno-omit-frame-pointer", |
| 35 |
"-fstack-protector-strong", |
| 36 |
] |
| 37 |
|
| 38 |
platform_copts = selects.with_or({ |
| 39 |
PLATFORM_CPU_X86_64: ["-mno-omit-leaf-frame-pointer"], |
| 40 |
"//conditions:default": [], |
| 41 |
}) |
| 42 |
return copts + platform_copts |
| 43 |
|
| 44 |
def _libc_library(name, **kwargs): |
| 45 |
"""Internal macro to serve as a base for all other libc library rules. |
| 46 |
|
| 47 |
Args: |
| 48 |
name: Target name. |
| 49 |
**kwargs: All other attributes relevant for the cc_library rule. |
| 50 |
""" |
| 51 |
|
| 52 |
for attr in ["copts", "local_defines"]: |
| 53 |
if attr in kwargs: |
| 54 |
fail("disallowed attribute: '{}' in rule: '{}'".format(attr, name)) |
| 55 |
native.cc_library( |
| 56 |
name = name, |
| 57 |
copts = libc_common_copts(), |
| 58 |
local_defines = LIBC_CONFIGURE_OPTIONS, |
| 59 |
linkstatic = 1, |
| 60 |
**kwargs |
| 61 |
) |
| 62 |
|
| 63 |
# A convenience function which should be used to list all libc support libraries. |
| 64 |
# Any library which does not define a public function should be listed with |
| 65 |
# libc_support_library. |
| 66 |
def libc_support_library(name, **kwargs): |
| 67 |
_libc_library(name = name, **kwargs) |
| 68 |
|
| 69 |
def libc_function(name, **kwargs): |
| 70 |
"""Add target for a libc function. |
| 71 |
|
| 72 |
This macro creates an internal cc_library that can be used to test this |
| 73 |
function. |
| 74 |
|
| 75 |
Args: |
| 76 |
name: Target name. Typically the name of the function this target is for. |
| 77 |
**kwargs: Other attributes relevant for a cc_library. For example, deps. |
| 78 |
""" |
| 79 |
|
| 80 |
# Builds "internal" library with a function, exposed as a C++ function in |
| 81 |
# the "LIBC_NAMESPACE" namespace. This allows us to test the function in the |
| 82 |
# presence of another libc. |
| 83 |
_libc_library(name = name, **kwargs) |
| 84 |
|
| 85 |
LibcLibraryInfo = provider( |
| 86 |
"All source files and textual headers for building a particular library.", |
| 87 |
fields = ["srcs", "textual_hdrs"], |
| 88 |
) |
| 89 |
|
| 90 |
def _get_libc_info_aspect_impl( |
| 91 |
target, # @unused |
| 92 |
ctx): |
| 93 |
maybe_srcs = getattr(ctx.rule.attr, "srcs", []) |
| 94 |
maybe_hdrs = getattr(ctx.rule.attr, "hdrs", []) |
| 95 |
maybe_textual_hdrs = getattr(ctx.rule.attr, "textual_hdrs", []) |
| 96 |
maybe_deps = getattr(ctx.rule.attr, "deps", []) |
| 97 |
return LibcLibraryInfo( |
| 98 |
srcs = depset( |
| 99 |
transitive = [ |
| 100 |
dep[LibcLibraryInfo].srcs |
| 101 |
for dep in maybe_deps |
| 102 |
if LibcLibraryInfo in dep |
| 103 |
] + [ |
| 104 |
src.files |
| 105 |
for src in maybe_srcs + maybe_hdrs |
| 106 |
], |
| 107 |
), |
| 108 |
textual_hdrs = depset( |
| 109 |
transitive = [ |
| 110 |
dep[LibcLibraryInfo].textual_hdrs |
| 111 |
for dep in maybe_deps |
| 112 |
if LibcLibraryInfo in dep |
| 113 |
] + [ |
| 114 |
hdr.files |
| 115 |
for hdr in maybe_textual_hdrs |
| 116 |
], |
| 117 |
), |
| 118 |
) |
| 119 |
|
| 120 |
_get_libc_info_aspect = aspect( |
| 121 |
implementation = _get_libc_info_aspect_impl, |
| 122 |
attr_aspects = ["deps"], |
| 123 |
) |
| 124 |
|
| 125 |
def _libc_srcs_filegroup_impl(ctx): |
| 126 |
srcs = depset(transitive = [ |
| 127 |
fn[LibcLibraryInfo].srcs |
| 128 |
for fn in ctx.attr.libs |
| 129 |
]) |
| 130 |
if ctx.attr.enforce_headers_only: |
| 131 |
paths = [f.short_path for f in srcs.to_list() if f.extension != "h"] |
| 132 |
if paths: |
| 133 |
fail("Unexpected non-header files: {}".format(paths)) |
| 134 |
return DefaultInfo(files = srcs) |
| 135 |
|
| 136 |
_libc_srcs_filegroup = rule( |
| 137 |
doc = "Returns all sources for building the specified libraries.", |
| 138 |
implementation = _libc_srcs_filegroup_impl, |
| 139 |
attrs = { |
| 140 |
"libs": attr.label_list( |
| 141 |
mandatory = True, |
| 142 |
aspects = [_get_libc_info_aspect], |
| 143 |
), |
| 144 |
"enforce_headers_only": attr.bool(default = False), |
| 145 |
}, |
| 146 |
) |
| 147 |
|
| 148 |
def _libc_textual_hdrs_filegroup_impl(ctx): |
| 149 |
return DefaultInfo( |
| 150 |
files = depset(transitive = [ |
| 151 |
fn[LibcLibraryInfo].textual_hdrs |
| 152 |
for fn in ctx.attr.libs |
| 153 |
]), |
| 154 |
) |
| 155 |
|
| 156 |
_libc_textual_hdrs_filegroup = rule( |
| 157 |
doc = "Returns all textual headers for compiling the specified libraries.", |
| 158 |
implementation = _libc_textual_hdrs_filegroup_impl, |
| 159 |
attrs = { |
| 160 |
"libs": attr.label_list( |
| 161 |
mandatory = True, |
| 162 |
aspects = [_get_libc_info_aspect], |
| 163 |
), |
| 164 |
}, |
| 165 |
) |
| 166 |
|
| 167 |
def libc_release_library( |
| 168 |
name, |
| 169 |
libc_functions, |
| 170 |
weak_symbols = [], |
| 171 |
**kwargs): |
| 172 |
"""Create the release version of a libc library. |
| 173 |
|
| 174 |
Args: |
| 175 |
name: Name of the cc_library target. |
| 176 |
libc_functions: List of functions to include in the library. They should be |
| 177 |
created by libc_function macro. |
| 178 |
weak_symbols: List of function names that should be marked as weak symbols. |
| 179 |
**kwargs: Other arguments relevant to cc_library. |
| 180 |
""" |
| 181 |
|
| 182 |
_libc_srcs_filegroup( |
| 183 |
name = name + "_srcs", |
| 184 |
libs = libc_functions, |
| 185 |
) |
| 186 |
|
| 187 |
_libc_textual_hdrs_filegroup( |
| 188 |
name = name + "_textual_hdrs", |
| 189 |
libs = libc_functions, |
| 190 |
) |
| 191 |
native.cc_library( |
| 192 |
name = name + "_textual_hdr_library", |
| 193 |
textual_hdrs = [":" + name + "_textual_hdrs"], |
| 194 |
) |
| 195 |
|
| 196 |
weak_attributes = [ |
| 197 |
"LLVM_LIBC_FUNCTION_ATTR_" + name + "='LLVM_LIBC_EMPTY, [[gnu::weak]]'" |
| 198 |
for name in weak_symbols |
| 199 |
] |
| 200 |
|
| 201 |
native.cc_library( |
| 202 |
name = name, |
| 203 |
srcs = [":" + name + "_srcs"], |
| 204 |
copts = libc_common_copts() + libc_release_copts(), |
| 205 |
local_defines = weak_attributes + LIBC_CONFIGURE_OPTIONS, |
| 206 |
deps = [ |
| 207 |
":" + name + "_textual_hdr_library", |
| 208 |
], |
| 209 |
**kwargs |
| 210 |
) |
| 211 |
|
| 212 |
def libc_header_library(name, hdrs, deps = [], **kwargs): |
| 213 |
"""Creates a header-only library to share libc functionality. |
| 214 |
|
| 215 |
Args: |
| 216 |
name: Name of the cc_library target. |
| 217 |
hdrs: List of headers to be shared. |
| 218 |
deps: The list of libc_support_library dependencies if any. |
| 219 |
**kwargs: All other attributes relevant for the cc_library rule. |
| 220 |
""" |
| 221 |
|
| 222 |
_libc_srcs_filegroup( |
| 223 |
name = name + "_hdr_deps", |
| 224 |
libs = deps, |
| 225 |
enforce_headers_only = True, |
| 226 |
) |
| 227 |
|
| 228 |
_libc_textual_hdrs_filegroup( |
| 229 |
name = name + "_textual_hdrs", |
| 230 |
libs = deps, |
| 231 |
) |
| 232 |
native.cc_library( |
| 233 |
name = name + "_textual_hdr_library", |
| 234 |
textual_hdrs = [":" + name + "_textual_hdrs"], |
| 235 |
) |
| 236 |
|
| 237 |
native.cc_library( |
| 238 |
name = name, |
| 239 |
hdrs = hdrs, |
| 240 |
# We put _hdr_deps in srcs, as they are not a part of this cc_library |
| 241 |
# interface, but instead are used to implement shared headers. |
| 242 |
srcs = [":" + name + "_hdr_deps"], |
| 243 |
deps = [":" + name + "_textual_hdr_library"], |
| 244 |
# copts don't really matter, since it's a header-only library, but we |
| 245 |
# need proper -I flags for header validation, which are specified in |
| 246 |
# libc_common_copts(). |
| 247 |
copts = libc_common_copts(), |
| 248 |
**kwargs |
| 249 |
) |
| 250 |
|
| 251 |
def libc_generated_header(name, hdr, yaml_template, other_srcs = []): |
| 252 |
"""Generates a libc header file from YAML template. |
| 253 |
|
| 254 |
Args: |
| 255 |
name: Name of the genrule target. |
| 256 |
hdr: Path of the header file to generate. |
| 257 |
yaml_template: Path of the YAML template file. |
| 258 |
other_srcs: Other files required to generate the header, if any. |
| 259 |
""" |
| 260 |
hdrgen = "//libc:hdrgen" |
| 261 |
cmd = "$(location {hdrgen}) $(location {yaml}) -o $@".format( |
| 262 |
hdrgen = hdrgen, |
| 263 |
yaml = yaml_template, |
| 264 |
) |
| 265 |
|
| 266 |
if not hdr.startswith("staging/"): |
| 267 |
fail( |
| 268 |
"Generated headers should be placed in a 'staging/' directory " + |
| 269 |
"so that they can be treated differently from constant source files " + |
| 270 |
"when bootstrapping builds.", |
| 271 |
) |
| 272 |
|
| 273 |
native.genrule( |
| 274 |
name = name, |
| 275 |
outs = [hdr], |
| 276 |
srcs = [yaml_template] + other_srcs, |
| 277 |
cmd = cmd, |
| 278 |
tools = [hdrgen], |
| 279 |
) |
| 280 |
|
| 281 |
def libc_math_function( |
| 282 |
name, |
| 283 |
additional_deps = None): |
| 284 |
"""Add a target for a math function. |
| 285 |
|
| 286 |
Args: |
| 287 |
name: The name of the function. |
| 288 |
additional_deps: Other deps like helper cc_library targets used by the |
| 289 |
math function. |
| 290 |
""" |
| 291 |
additional_deps = additional_deps or [] |
| 292 |
|
| 293 |
#TODO(michaelrj): Fix the floating point dependencies |
| 294 |
OLD_FPUTIL_DEPS = [ |
| 295 |
":__support_fputil_basic_operations", |
| 296 |
":__support_fputil_division_and_remainder_operations", |
| 297 |
":__support_fputil_fenv_impl", |
| 298 |
":__support_fputil_fp_bits", |
| 299 |
":__support_fputil_hypot", |
| 300 |
":__support_fputil_manipulation_functions", |
| 301 |
":__support_fputil_nearest_integer_operations", |
| 302 |
":__support_fputil_normal_float", |
| 303 |
":__support_math_extras", |
| 304 |
":__support_fputil_except_value_utils", |
| 305 |
] |
| 306 |
libc_function( |
| 307 |
name = name, |
| 308 |
srcs = ["src/math/generic/" + name + ".cpp"], |
| 309 |
hdrs = ["src/math/" + name + ".h"], |
| 310 |
deps = [":__support_common"] + OLD_FPUTIL_DEPS + additional_deps, |
| 311 |
) |