Skip to content

Commit

Permalink
[libc][bazel] Allow configure options to alter all targets (#89251)
Browse files Browse the repository at this point in the history
The previous state was leading to inconsistencies. Some targets would
get the options and some wouldn't. As an example, the `MEMORY_COPTS`
definitions would only apply to the `:string_memory_utils` target but
not to the `:memcpy` target. This patch makes sure definitions are
applied throughout the LLVM libc targets as `local_defines`. This
ensures that the preprocessor definitions don't propagate to depending
targets outside of LLVM libc, and that all libc targets have consistent
preprocessor definitions.
  • Loading branch information
gchatelet authored Apr 24, 2024
1 parent 603ba4c commit 788d159
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 30 deletions.
29 changes: 1 addition & 28 deletions utils/bazel/llvm-project-overlay/libc/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,6 @@ package(

licenses(["notice"])

PRINTF_COPTS = [
"LIBC_COPT_STDIO_USE_SYSTEM_FILE",
"LIBC_COPT_PRINTF_DISABLE_WRITE_INT",
]

MEMORY_COPTS = [
# "LIBC_COPT_MEMCPY_X86_USE_REPMOVSB_FROM_SIZE=0",
# "LIBC_COPT_MEMCPY_X86_USE_SOFTWARE_PREFETCHING",
# "LIBC_COPT_MEMSET_X86_USE_SOFTWARE_PREFETCHING",
]

# A flag to pick which `mpfr` to use for math tests.
# Usage: `--@llvm-project//libc:mpfr=<disable|external|system>`.
# Flag documentation: https://bazel.build/extending/config
Expand Down Expand Up @@ -2421,7 +2410,6 @@ libc_support_library(
"src/string/memory_utils/op_x86.h",
"src/string/memory_utils/utils.h",
],
defines = MEMORY_COPTS,
textual_hdrs = [
"src/string/memory_utils/aarch64/inline_bcmp.h",
"src/string/memory_utils/aarch64/inline_memcmp.h",
Expand Down Expand Up @@ -3191,15 +3179,13 @@ libc_function(
libc_support_library(
name = "printf_config",
hdrs = ["src/stdio/printf_core/printf_config.h"],
defines = PRINTF_COPTS,
deps = [
],
)

libc_support_library(
name = "printf_core_structs",
hdrs = ["src/stdio/printf_core/core_structs.h"],
defines = PRINTF_COPTS,
deps = [
":__support_cpp_string_view",
":__support_fputil_fp_bits",
Expand All @@ -3210,7 +3196,6 @@ libc_support_library(
libc_support_library(
name = "printf_parser",
hdrs = ["src/stdio/printf_core/parser.h"],
defines = PRINTF_COPTS,
deps = [
":__support_arg_list",
":__support_common",
Expand All @@ -3231,7 +3216,7 @@ libc_support_library(
libc_support_library(
name = "printf_mock_parser",
hdrs = ["src/stdio/printf_core/parser.h"],
defines = PRINTF_COPTS + ["LIBC_COPT_MOCK_ARG_LIST"],
local_defines = ["LIBC_COPT_MOCK_ARG_LIST"],
deps = [
":__support_arg_list",
":__support_common",
Expand All @@ -3251,7 +3236,6 @@ libc_support_library(
name = "printf_writer",
srcs = ["src/stdio/printf_core/writer.cpp"],
hdrs = ["src/stdio/printf_core/writer.h"],
defines = PRINTF_COPTS,
deps = [
":__support_cpp_string_view",
":__support_macros_optimization",
Expand All @@ -3276,7 +3260,6 @@ libc_support_library(
"src/stdio/printf_core/string_converter.h",
"src/stdio/printf_core/write_int_converter.h",
],
defines = PRINTF_COPTS,
deps = [
":__support_big_int",
":__support_common",
Expand All @@ -3300,7 +3283,6 @@ libc_support_library(
name = "printf_main",
srcs = ["src/stdio/printf_core/printf_main.cpp"],
hdrs = ["src/stdio/printf_core/printf_main.h"],
defines = PRINTF_COPTS,
deps = [
":__support_arg_list",
":printf_converter",
Expand All @@ -3313,7 +3295,6 @@ libc_support_library(
libc_support_library(
name = "vfprintf_internal",
hdrs = ["src/stdio/printf_core/vfprintf_internal.h"],
defines = PRINTF_COPTS,
deps = [
":__support_arg_list",
":__support_file_file",
Expand All @@ -3327,7 +3308,6 @@ libc_function(
name = "sprintf",
srcs = ["src/stdio/sprintf.cpp"],
hdrs = ["src/stdio/sprintf.h"],
defines = PRINTF_COPTS,
deps = [
":__support_arg_list",
":__support_cpp_limits",
Expand All @@ -3341,7 +3321,6 @@ libc_function(
name = "snprintf",
srcs = ["src/stdio/snprintf.cpp"],
hdrs = ["src/stdio/snprintf.h"],
defines = PRINTF_COPTS,
deps = [
":__support_arg_list",
":errno",
Expand All @@ -3354,7 +3333,6 @@ libc_function(
name = "printf",
srcs = ["src/stdio/printf.cpp"],
hdrs = ["src/stdio/printf.h"],
defines = PRINTF_COPTS,
deps = [
":__support_arg_list",
":__support_file_file",
Expand All @@ -3367,7 +3345,6 @@ libc_function(
name = "fprintf",
srcs = ["src/stdio/fprintf.cpp"],
hdrs = ["src/stdio/fprintf.h"],
defines = PRINTF_COPTS,
deps = [
":__support_arg_list",
":__support_file_file",
Expand All @@ -3380,7 +3357,6 @@ libc_function(
name = "vsprintf",
srcs = ["src/stdio/vsprintf.cpp"],
hdrs = ["src/stdio/vsprintf.h"],
defines = PRINTF_COPTS,
deps = [
":__support_arg_list",
":__support_cpp_limits",
Expand All @@ -3394,7 +3370,6 @@ libc_function(
name = "vsnprintf",
srcs = ["src/stdio/vsnprintf.cpp"],
hdrs = ["src/stdio/vsnprintf.h"],
defines = PRINTF_COPTS,
deps = [
":__support_arg_list",
":errno",
Expand All @@ -3407,7 +3382,6 @@ libc_function(
name = "vprintf",
srcs = ["src/stdio/vprintf.cpp"],
hdrs = ["src/stdio/vprintf.h"],
defines = PRINTF_COPTS,
deps = [
":__support_arg_list",
":__support_file_file",
Expand All @@ -3420,7 +3394,6 @@ libc_function(
name = "vfprintf",
srcs = ["src/stdio/vfprintf.cpp"],
hdrs = ["src/stdio/vfprintf.h"],
defines = PRINTF_COPTS,
deps = [
":__support_arg_list",
":__support_file_file",
Expand Down
5 changes: 4 additions & 1 deletion utils/bazel/llvm-project-overlay/libc/libc_build_rules.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

load("@bazel_skylib//lib:paths.bzl", "paths")
load("@bazel_skylib//lib:selects.bzl", "selects")
load(":libc_configure_options.bzl", "LIBC_CONFIGURE_OPTIONS")
load(":libc_namespace.bzl", "LIBC_NAMESPACE")
load(":platforms.bzl", "PLATFORM_CPU_ARM64", "PLATFORM_CPU_X86_64")

Expand All @@ -21,13 +22,14 @@ def libc_common_copts():
"-DLIBC_NAMESPACE=" + LIBC_NAMESPACE,
]

def _libc_library(name, hidden, copts = [], deps = [], **kwargs):
def _libc_library(name, hidden, copts = [], deps = [], local_defines = [], **kwargs):
"""Internal macro to serve as a base for all other libc library rules.
Args:
name: Target name.
copts: The special compiler options for the target.
deps: The list of target dependencies if any.
local_defines: The list of target local_defines if any.
hidden: Whether the symbols should be explicitly hidden or not.
**kwargs: All other attributes relevant for the cc_library rule.
"""
Expand All @@ -40,6 +42,7 @@ def _libc_library(name, hidden, copts = [], deps = [], **kwargs):
native.cc_library(
name = name,
copts = copts + libc_common_copts(),
local_defines = local_defines + LIBC_CONFIGURE_OPTIONS,
deps = deps,
linkstatic = 1,
**kwargs
Expand Down
49 changes: 49 additions & 0 deletions utils/bazel/llvm-project-overlay/libc/libc_configure_options.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# This file is licensed under the Apache License v2.0 with LLVM Exceptions.
# See https://llvm.org/LICENSE.txt for license information.
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception

"""LLVM libc configuration options.
The canonical list of user options is in 'libc/config/config.json'.
These options are then processed by CMake and turned into preprocessor
definitions. We don't have this logic in Bazel yet but the list of definitions
is discoverable with the following command:
> git grep -hoE '\bLIBC_COPT_\\w*' -- '*.h' '*.cpp' | sort -u
"""

# This list of definitions is used to customize LLVM libc.
LIBC_CONFIGURE_OPTIONS = [
# Documentation in libc/docs/dev/printf_behavior.rst
# "LIBC_COPT_FLOAT_TO_STR_NO_SPECIALIZE_LD",
# "LIBC_COPT_FLOAT_TO_STR_NO_TABLE",
# "LIBC_COPT_FLOAT_TO_STR_USE_DYADIC_FLOAT",
# "LIBC_COPT_FLOAT_TO_STR_USE_DYADIC_FLOAT_LD",
# "LIBC_COPT_FLOAT_TO_STR_USE_INT_CALC",
# "LIBC_COPT_FLOAT_TO_STR_USE_MEGA_LONG_DOUBLE_TABLE",

# Documentation in libc/src/string/memory_utils/...
# "LIBC_COPT_MEMCPY_USE_EMBEDDED_TINY",
# "LIBC_COPT_MEMCPY_X86_USE_REPMOVSB_FROM_SIZE",
# "LIBC_COPT_MEMCPY_X86_USE_SOFTWARE_PREFETCHING",
# "LIBC_COPT_MEMSET_X86_USE_SOFTWARE_PREFETCHING",

# Documentation in libc/docs/dev/printf_behavior.rst
# "LIBC_COPT_PRINTF_CONV_ATLAS",
# "LIBC_COPT_PRINTF_DISABLE_FIXED_POINT",
# "LIBC_COPT_PRINTF_DISABLE_FLOAT",
# "LIBC_COPT_PRINTF_DISABLE_INDEX_MODE",
"LIBC_COPT_PRINTF_DISABLE_WRITE_INT",
# "LIBC_COPT_PRINTF_HEX_LONG_DOUBLE",
# "LIBC_COPT_PRINTF_INDEX_ARR_LEN",
# "LIBC_COPT_PRINTF_NO_NULLPTR_CHECKS",
# "LIBC_COPT_SCANF_DISABLE_FLOAT",
# "LIBC_COPT_SCANF_DISABLE_INDEX_MODE",
"LIBC_COPT_STDIO_USE_SYSTEM_FILE",
# "LIBC_COPT_STRING_UNSAFE_WIDE_READ",
# "LIBC_COPT_STRTOFLOAT_DISABLE_CLINGER_FAST_PATH",
# "LIBC_COPT_STRTOFLOAT_DISABLE_EISEL_LEMIRE",
# "LIBC_COPT_STRTOFLOAT_DISABLE_SIMPLE_DECIMAL_CONVERSION",

# Documentation in libc/src/__support/libc_assert.h
# "LIBC_COPT_USE_C_ASSERT",
]
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@ When performing tests we make sure to always use the internal version.
"""

load("//libc:libc_build_rules.bzl", "libc_common_copts", "libc_internal_target")
load("//libc:libc_configure_options.bzl", "LIBC_CONFIGURE_OPTIONS")

def libc_test(name, srcs, libc_function_deps = [], copts = [], deps = [], **kwargs):
def libc_test(name, srcs, libc_function_deps = [], copts = [], deps = [], local_defines = [], **kwargs):
"""Add target for a libc test.
Args:
Expand All @@ -23,12 +24,14 @@ def libc_test(name, srcs, libc_function_deps = [], copts = [], deps = [], **kwar
libc_function_deps: List of libc_function targets used by this test.
copts: The list of options to add to the C++ compilation command.
deps: The list of other libraries to be linked in to the test target.
local_defines: The list of target local_defines if any.
**kwargs: Attributes relevant for a libc_test. For example, name, srcs.
"""
all_function_deps = libc_function_deps + ["//libc:errno"]
native.cc_test(
name = name,
srcs = srcs,
local_defines = local_defines + LIBC_CONFIGURE_OPTIONS,
deps = [libc_internal_target(d) for d in all_function_deps] + [
"//libc/test/UnitTest:LibcUnitTest",
] + deps,
Expand Down

0 comments on commit 788d159

Please sign in to comment.