[reland][libc][bazel] Add tests to the bazel build
authorGuillaume Chatelet <gchatelet@google.com>
Fri, 18 Nov 2022 10:14:37 +0000 (10:14 +0000)
committerGuillaume Chatelet <gchatelet@google.com>
Fri, 18 Nov 2022 13:20:52 +0000 (13:20 +0000)
This patch adds bazel tests for llvm-libc.

Some math tests rely on the `mpfr` library. This is controlled via the `--@llvm-project//libc:libc_math_mpfr` flag. It can take three values:
 - `external` (default) will build `mpfr` and `gmp` from source.
 - `system` will use the system installed `mpfr` library.
 - `disable` will skip tests relying on `mpfr`.

Reviewed By: sivachandra, GMNGeoffrey

Differential Revision: https://reviews.llvm.org/D119547

15 files changed:
utils/bazel/.bazelrc
utils/bazel/WORKSPACE
utils/bazel/llvm-project-overlay/libc/BUILD.bazel
utils/bazel/llvm-project-overlay/libc/test/BUILD.bazel [new file with mode: 0644]
utils/bazel/llvm-project-overlay/libc/test/libc_test_rules.bzl [new file with mode: 0644]
utils/bazel/llvm-project-overlay/libc/test/src/fenv/BUILD.bazel [new file with mode: 0644]
utils/bazel/llvm-project-overlay/libc/test/src/math/BUILD.bazel [new file with mode: 0644]
utils/bazel/llvm-project-overlay/libc/test/src/math/libc_math_test_rules.bzl [new file with mode: 0644]
utils/bazel/llvm-project-overlay/libc/test/src/stdlib/BUILD.bazel [new file with mode: 0644]
utils/bazel/llvm-project-overlay/libc/test/src/string/BUILD.bazel [new file with mode: 0644]
utils/bazel/llvm-project-overlay/libc/utils/MPFRWrapper/BUILD.bazel [new file with mode: 0644]
utils/bazel/llvm-project-overlay/libc/utils/UnitTest/BUILD.bazel [new file with mode: 0644]
utils/bazel/llvm-project-overlay/libc/utils/testutils/BUILD.bazel [new file with mode: 0644]
utils/bazel/third_party_build/gmp.BUILD [new file with mode: 0644]
utils/bazel/third_party_build/mpfr.BUILD [new file with mode: 0644]

index 71007f2..f15da71 100644 (file)
@@ -168,6 +168,10 @@ build:ci --config=generic_clang
 # Speedup bazel using a ramdisk.
 build:ci --sandbox_base=/dev/shm
 
+# Use system's mpfr instead of building it from source.
+# This is non hermetic but helps with compile time.
+build:ci --@llvm-project//libc:mpfr=system
+
 # Don't build/test targets tagged with "nobuildkite".
 build:ci --build_tag_filters=-nobuildkite
 build:ci --test_tag_filters=-nobuildkite
index 43171d5..c2f6f78 100644 (file)
@@ -4,6 +4,7 @@
 
 load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
 load("@bazel_tools//tools/build_defs/repo:utils.bzl", "maybe")
+load("@bazel_tools//tools/build_defs/repo:git.bzl", "git_repository")
 
 SKYLIB_VERSION = "1.3.0"
 
@@ -71,3 +72,40 @@ maybe(
     name = "vulkan_sdk",
 )
 
+# llvm libc math tests reply on `mpfr`.
+# The availability of `mpfr` is controlled by a flag and can be either `disable`, `system` or `external`.
+# Continuous integration uses `system` to speed up the build process (see .bazelrc).
+# Otherwise by default it is set to `external`: `mpfr` and `gmp` are built from source by using `rules_foreign_cc`.
+# Note: that building from source requires `m4` to be installed on the host machine.
+# This is a known issue: https://github.com/bazelbuild/rules_foreign_cc/issues/755.
+
+git_repository(
+    name = "rules_foreign_cc",
+    tag = "0.9.0",
+    remote = "https://github.com/bazelbuild/rules_foreign_cc.git"
+)
+
+load("@rules_foreign_cc//foreign_cc:repositories.bzl", "rules_foreign_cc_dependencies")
+rules_foreign_cc_dependencies()
+
+maybe(
+    http_archive,
+    name = "gmp",
+    build_file = "@llvm-raw//utils/bazel/third_party_build:gmp.BUILD",
+    sha256 = "fd4829912cddd12f84181c3451cc752be224643e87fac497b69edddadc49b4f2",
+    strip_prefix = "gmp-6.2.1",
+    urls = [
+        "https://gmplib.org/download/gmp/gmp-6.2.1.tar.xz",
+        "https://ftp.gnu.org/gnu/gmp/gmp-6.2.1.tar.xz",
+    ],
+)
+
+maybe(
+    http_archive,
+    name = "mpfr",
+    build_file = "@llvm-raw//utils/bazel/third_party_build:mpfr.BUILD",
+    sha256 = "3127fe813218f3a1f0adf4e8899de23df33b4cf4b4b3831a5314f78e65ffa2d6",
+    strip_prefix = "mpfr-4.1.0",
+    urls = ["https://www.mpfr.org/mpfr-current/mpfr-4.1.0.tar.gz"],
+)
+
index 0d1aef2..fbc4a85 100644 (file)
@@ -6,6 +6,7 @@
 load(":libc_build_rules.bzl", "libc_function", "libc_math_function")
 load(":platforms.bzl", "PLATFORM_CPU_ARM64", "PLATFORM_CPU_X86_64")
 load("@bazel_skylib//lib:selects.bzl", "selects")
+load("@bazel_skylib//rules:common_settings.bzl", "string_flag")
 
 package(
     default_visibility = ["//visibility:public"],
@@ -14,6 +15,34 @@ package(
 
 licenses(["notice"])
 
+# 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
+string_flag(
+    name = "mpfr",
+    build_setting_default = "external",
+    values = [
+        "disable",  # Skip tests that need mpfr
+        "external",  # Build mpfr from source
+        "system",  # Use system mpfr (non hermetic)
+    ],
+)
+
+config_setting(
+    name = "mpfr_disable",
+    flag_values = {":mpfr": "disable"},
+)
+
+config_setting(
+    name = "mpfr_external",
+    flag_values = {":mpfr": "external"},
+)
+
+config_setting(
+    name = "mpfr_system",
+    flag_values = {":mpfr": "system"},
+)
+
 # This empty root library helps us add an include path to this directory
 # using the 'includes' attribute. The strings listed in the includes attribute
 # are relative paths wrt this library but are inherited by the dependents
@@ -151,13 +180,13 @@ cc_library(
     name = "__support_uint",
     hdrs = ["src/__support/UInt.h"],
     deps = [
+        "__support_builtin_wrappers",
         "__support_cpp_array",
         "__support_cpp_limits",
-        "__support_cpp_type_traits",
         "__support_cpp_optional",
-        "__support_builtin_wrappers",
-        "__support_number_pair",
+        "__support_cpp_type_traits",
         "__support_integer_utils",
+        "__support_number_pair",
         ":libc_root",
     ],
 )
diff --git a/utils/bazel/llvm-project-overlay/libc/test/BUILD.bazel b/utils/bazel/llvm-project-overlay/libc/test/BUILD.bazel
new file mode 100644 (file)
index 0000000..3b65f08
--- /dev/null
@@ -0,0 +1,3 @@
+# 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
diff --git a/utils/bazel/llvm-project-overlay/libc/test/libc_test_rules.bzl b/utils/bazel/llvm-project-overlay/libc/test/libc_test_rules.bzl
new file mode 100644 (file)
index 0000000..a4fb267
--- /dev/null
@@ -0,0 +1,36 @@
+# 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 starlark rules for tests.
+
+libc functions are created though the libc_build_rules.bzl:libc_function.
+They come in two flavors:
+ - the internal one that is scoped into the `__llvm_libc` namespace.
+ - the libc one that is the regular C function.
+
+When performing tests we make sure to always use the internal version.
+"""
+
+load("//libc:libc_build_rules.bzl", "INTERNAL_SUFFIX")
+
+def libc_test(name, srcs, libc_function_deps, deps = [], **kwargs):
+    """Add target for a libc test.
+
+    Args:
+      name: Test target name
+      srcs: List of sources for the test.
+      libc_function_deps: List of libc_function targets used by this test.
+      deps: The list of other libraries to be linked in to the test target.
+      **kwargs: Attributes relevant for a cc_test. For example, name, srcs.
+    """
+    native.cc_test(
+        name = name,
+        srcs = srcs,
+        deps = [d + INTERNAL_SUFFIX for d in libc_function_deps] + [
+            "//libc:libc_root",
+            "//libc/utils/UnitTest:LibcUnitTest",
+        ] + deps,
+        features = ["-link_llvmlibc"],  # Do not link libllvmlibc.a
+        **kwargs
+    )
diff --git a/utils/bazel/llvm-project-overlay/libc/test/src/fenv/BUILD.bazel b/utils/bazel/llvm-project-overlay/libc/test/src/fenv/BUILD.bazel
new file mode 100644 (file)
index 0000000..a21477c
--- /dev/null
@@ -0,0 +1,124 @@
+# 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
+
+# Tests for LLVM libc math.h functions.
+
+load("//libc/test:libc_test_rules.bzl", "libc_test")
+
+package(default_visibility = ["//visibility:public"])
+
+licenses(["notice"])
+
+libc_test(
+    name = "exception_status_test",
+    srcs = ["exception_status_test.cpp"],
+    libc_function_deps = [
+        "//libc:feclearexcept",
+        "//libc:feraiseexcept",
+        "//libc:fetestexcept",
+    ],
+    deps = [
+        "//libc:__support_fputil_fenv_impl",
+    ],
+)
+
+libc_test(
+    name = "rounding_mode_test",
+    srcs = ["rounding_mode_test.cpp"],
+    libc_function_deps = [
+        "//libc:fegetround",
+        "//libc:fesetround",
+    ],
+)
+
+libc_test(
+    name = "enabled_exceptions_test",
+    srcs = ["enabled_exceptions_test.cpp"],
+    libc_function_deps = [
+        "//libc:feclearexcept",
+        "//libc:feraiseexcept",
+        "//libc:fetestexcept",
+    ],
+    tags = ["nosan"],
+    deps = [
+        "//libc:__support_common",
+        "//libc:__support_fputil_fenv_impl",
+        "//libc/utils/UnitTest:fp_test_helpers",
+    ],
+)
+
+libc_test(
+    name = "feholdexcept_test",
+    srcs = ["feholdexcept_test.cpp"],
+    libc_function_deps = [
+        "//libc:feholdexcept",
+    ],
+    tags = ["nosan"],
+    deps = [
+        "//libc:__support_common",
+        "//libc:__support_fputil_fenv_impl",
+        "//libc/utils/UnitTest:fp_test_helpers",
+    ],
+)
+
+libc_test(
+    name = "exception_flags_test",
+    srcs = ["exception_flags_test.cpp"],
+    libc_function_deps = [
+        "//libc:fegetexceptflag",
+        "//libc:fesetexceptflag",
+    ],
+    deps = [
+        "//libc:__support_fputil_fenv_impl",
+    ],
+)
+
+libc_test(
+    name = "feclearexcept_test",
+    srcs = ["feclearexcept_test.cpp"],
+    libc_function_deps = [
+        "//libc:feclearexcept",
+    ],
+    deps = [
+        "//libc:__support_fputil_fenv_impl",
+    ],
+)
+
+libc_test(
+    name = "feenableexcept_test",
+    srcs = ["feenableexcept_test.cpp"],
+    libc_function_deps = [
+        "//libc:fedisableexcept",
+        "//libc:feenableexcept",
+        "//libc:fegetexcept",
+    ],
+    deps = [
+        "//libc:__support_common",
+    ],
+)
+
+libc_test(
+    name = "feupdateenv_test",
+    srcs = ["feupdateenv_test.cpp"],
+    libc_function_deps = [
+        "//libc:feupdateenv",
+    ],
+    deps = [
+        "//libc:__support_fputil_fenv_impl",
+    ],
+)
+
+libc_test(
+    name = "getenv_and_setenv_test",
+    srcs = ["getenv_and_setenv_test.cpp"],
+    libc_function_deps = [
+        "//libc:fegetenv",
+        "//libc:fegetround",
+        "//libc:fesetenv",
+        "//libc:fesetround",
+    ],
+    deps = [
+        "//libc:__support_fputil_fenv_impl",
+    ],
+)
diff --git a/utils/bazel/llvm-project-overlay/libc/test/src/math/BUILD.bazel b/utils/bazel/llvm-project-overlay/libc/test/src/math/BUILD.bazel
new file mode 100644 (file)
index 0000000..25687c9
--- /dev/null
@@ -0,0 +1,574 @@
+# 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
+
+# Tests for LLVM libc math.h functions.
+
+load("//libc/test/src/math:libc_math_test_rules.bzl", "math_test")
+
+package(default_visibility = ["//visibility:public"])
+
+licenses(["notice"])
+
+math_test(
+    name = "fabs",
+    hdrs = ["FAbsTest.h"],
+    deps = ["//libc/utils/MPFRWrapper:mpfr_wrapper"],
+)
+
+math_test(
+    name = "fabsf",
+    hdrs = ["FAbsTest.h"],
+    deps = ["//libc/utils/MPFRWrapper:mpfr_wrapper"],
+)
+
+math_test(
+    name = "fabsl",
+    hdrs = ["FAbsTest.h"],
+    deps = ["//libc/utils/MPFRWrapper:mpfr_wrapper"],
+)
+
+math_test(
+    name = "ceil",
+    hdrs = ["CeilTest.h"],
+    deps = ["//libc/utils/MPFRWrapper:mpfr_wrapper"],
+)
+
+math_test(
+    name = "ceilf",
+    hdrs = ["CeilTest.h"],
+    deps = ["//libc/utils/MPFRWrapper:mpfr_wrapper"],
+)
+
+math_test(
+    name = "ceill",
+    hdrs = ["CeilTest.h"],
+    deps = ["//libc/utils/MPFRWrapper:mpfr_wrapper"],
+)
+
+math_test(
+    name = "floor",
+    hdrs = ["FloorTest.h"],
+    deps = ["//libc/utils/MPFRWrapper:mpfr_wrapper"],
+)
+
+math_test(
+    name = "floorf",
+    hdrs = ["FloorTest.h"],
+    deps = ["//libc/utils/MPFRWrapper:mpfr_wrapper"],
+)
+
+math_test(
+    name = "floorl",
+    hdrs = ["FloorTest.h"],
+    deps = ["//libc/utils/MPFRWrapper:mpfr_wrapper"],
+)
+
+math_test(
+    name = "trunc",
+    hdrs = ["TruncTest.h"],
+    deps = ["//libc/utils/MPFRWrapper:mpfr_wrapper"],
+)
+
+math_test(
+    name = "truncf",
+    hdrs = ["TruncTest.h"],
+    deps = ["//libc/utils/MPFRWrapper:mpfr_wrapper"],
+)
+
+math_test(
+    name = "truncl",
+    hdrs = ["TruncTest.h"],
+    deps = ["//libc/utils/MPFRWrapper:mpfr_wrapper"],
+)
+
+math_test(
+    name = "round",
+    hdrs = ["RoundTest.h"],
+    deps = ["//libc/utils/MPFRWrapper:mpfr_wrapper"],
+)
+
+math_test(
+    name = "roundf",
+    hdrs = ["RoundTest.h"],
+    deps = ["//libc/utils/MPFRWrapper:mpfr_wrapper"],
+)
+
+math_test(
+    name = "roundl",
+    hdrs = ["RoundTest.h"],
+    deps = ["//libc/utils/MPFRWrapper:mpfr_wrapper"],
+)
+
+math_test(
+    name = "frexp",
+    hdrs = ["FrexpTest.h"],
+    deps = ["//libc/utils/MPFRWrapper:mpfr_wrapper"],
+)
+
+math_test(
+    name = "frexpf",
+    hdrs = ["FrexpTest.h"],
+    deps = ["//libc/utils/MPFRWrapper:mpfr_wrapper"],
+)
+
+math_test(
+    name = "frexpl",
+    hdrs = ["FrexpTest.h"],
+    deps = ["//libc/utils/MPFRWrapper:mpfr_wrapper"],
+)
+
+math_test(
+    name = "hypot",
+    hdrs = ["HypotTest.h"],
+    deps = ["//libc/utils/MPFRWrapper:mpfr_wrapper"],
+)
+
+math_test(
+    name = "hypotf",
+    hdrs = [
+        "HypotTest.h",
+        "hypotf_hard_to_round.h",
+    ],
+    deps = ["//libc/utils/MPFRWrapper:mpfr_wrapper"],
+)
+
+math_test(
+    name = "logb",
+    hdrs = ["LogbTest.h"],
+    deps = ["//libc/utils/MPFRWrapper:mpfr_wrapper"],
+)
+
+math_test(
+    name = "logbf",
+    hdrs = ["LogbTest.h"],
+    deps = ["//libc/utils/MPFRWrapper:mpfr_wrapper"],
+)
+
+math_test(
+    name = "logbl",
+    hdrs = ["LogbTest.h"],
+    deps = ["//libc/utils/MPFRWrapper:mpfr_wrapper"],
+)
+
+math_test(
+    name = "modf",
+    hdrs = ["ModfTest.h"],
+    deps = ["//libc/utils/MPFRWrapper:mpfr_wrapper"],
+)
+
+math_test(
+    name = "modff",
+    hdrs = ["ModfTest.h"],
+    deps = ["//libc/utils/MPFRWrapper:mpfr_wrapper"],
+)
+
+math_test(
+    name = "modfl",
+    hdrs = ["ModfTest.h"],
+    deps = ["//libc/utils/MPFRWrapper:mpfr_wrapper"],
+)
+
+cc_library(
+    name = "remquo_test_template",
+    hdrs = ["RemQuoTest.h"],
+    deps = [
+        "//libc:__support_fputil_basic_operations",
+        "//libc:__support_fputil_fp_bits",
+        "//libc/utils/MPFRWrapper:mpfr_wrapper",
+        "//libc/utils/UnitTest:LibcUnitTest",
+        "//libc/utils/UnitTest:fp_test_helpers",
+    ],
+)
+
+math_test(
+    name = "remquo",
+    deps = [
+        ":remquo_test_template",
+        "//libc/utils/MPFRWrapper:mpfr_wrapper",
+    ],
+)
+
+math_test(
+    name = "remquof",
+    deps = [
+        ":remquo_test_template",
+        "//libc/utils/MPFRWrapper:mpfr_wrapper",
+    ],
+)
+
+math_test(
+    name = "remquol",
+    deps = [
+        ":remquo_test_template",
+        "//libc/utils/MPFRWrapper:mpfr_wrapper",
+    ],
+)
+
+math_test(
+    name = "fmin",
+    hdrs = ["FMinTest.h"],
+    deps = ["//libc/utils/MPFRWrapper:mpfr_wrapper"],
+)
+
+math_test(
+    name = "fminf",
+    hdrs = ["FMinTest.h"],
+    deps = ["//libc/utils/MPFRWrapper:mpfr_wrapper"],
+)
+
+math_test(
+    name = "fminl",
+    hdrs = ["FMinTest.h"],
+    deps = ["//libc/utils/MPFRWrapper:mpfr_wrapper"],
+)
+
+math_test(
+    name = "fmax",
+    hdrs = ["FMaxTest.h"],
+    deps = ["//libc/utils/MPFRWrapper:mpfr_wrapper"],
+)
+
+math_test(
+    name = "fmaxf",
+    hdrs = ["FMaxTest.h"],
+    deps = ["//libc/utils/MPFRWrapper:mpfr_wrapper"],
+)
+
+math_test(
+    name = "fmaxl",
+    hdrs = ["FMaxTest.h"],
+    deps = ["//libc/utils/MPFRWrapper:mpfr_wrapper"],
+)
+
+math_test(
+    name = "sqrt",
+    hdrs = ["SqrtTest.h"],
+    deps = [
+        "//libc:__support_cpp_bit",
+        "//libc/utils/MPFRWrapper:mpfr_wrapper",
+    ],
+)
+
+math_test(
+    name = "sqrtf",
+    hdrs = ["SqrtTest.h"],
+    deps = [
+        "//libc:__support_cpp_bit",
+        "//libc/utils/MPFRWrapper:mpfr_wrapper",
+    ],
+)
+
+math_test(
+    name = "sqrtl",
+    hdrs = ["SqrtTest.h"],
+    deps = [
+        "//libc:__support_cpp_bit",
+        "//libc/utils/MPFRWrapper:mpfr_wrapper",
+    ],
+)
+
+math_test(
+    name = "copysign",
+    hdrs = ["CopySignTest.h"],
+    deps = ["//libc/utils/MPFRWrapper:mpfr_wrapper"],
+)
+
+math_test(
+    name = "copysignf",
+    hdrs = ["CopySignTest.h"],
+    deps = ["//libc/utils/MPFRWrapper:mpfr_wrapper"],
+)
+
+math_test(
+    name = "copysignl",
+    hdrs = ["CopySignTest.h"],
+    deps = ["//libc/utils/MPFRWrapper:mpfr_wrapper"],
+)
+
+cc_library(
+    name = "ilogb_test_template",
+    hdrs = ["ILogbTest.h"],
+    deps = [
+        "//libc:__support_fputil_fp_bits",
+        "//libc:__support_fputil_manipulation_functions",
+        "//libc/utils/UnitTest:LibcUnitTest",
+    ],
+)
+
+math_test(
+    name = "ilogb",
+    deps = [":ilogb_test_template"],
+)
+
+math_test(
+    name = "ilogbf",
+    deps = [":ilogb_test_template"],
+)
+
+math_test(
+    name = "ilogbl",
+    deps = [":ilogb_test_template"],
+)
+
+cc_library(
+    name = "fdim_test_template",
+    hdrs = ["FDimTest.h"],
+    deps = [
+        "//libc:__support_fputil_basic_operations",
+        "//libc:__support_fputil_fenv_impl",
+        "//libc:__support_fputil_fp_bits",
+        "//libc/utils/UnitTest:LibcUnitTest",
+        "//libc/utils/UnitTest:fp_test_helpers",
+    ],
+)
+
+math_test(
+    name = "fdim",
+    deps = [":fdim_test_template"],
+)
+
+math_test(
+    name = "fdimf",
+    deps = [":fdim_test_template"],
+)
+
+math_test(
+    name = "fdiml",
+    deps = [":fdim_test_template"],
+)
+
+cc_library(
+    name = "ldexp_test_template",
+    hdrs = ["LdExpTest.h"],
+    deps = [
+        "//libc:__support_fputil_fp_bits",
+        "//libc:__support_fputil_normal_float",
+        "//libc/utils/UnitTest:LibcUnitTest",
+        "//libc/utils/UnitTest:fp_test_helpers",
+    ],
+)
+
+math_test(
+    name = "ldexp",
+    deps = [":ldexp_test_template"],
+)
+
+math_test(
+    name = "ldexpf",
+    deps = [":ldexp_test_template"],
+)
+
+math_test(
+    name = "ldexpl",
+    deps = [":ldexp_test_template"],
+)
+
+cc_library(
+    name = "rint_test_template",
+    hdrs = ["RIntTest.h"],
+    deps = [
+        "//libc:__support_fputil_fenv_impl",
+        "//libc:__support_fputil_fp_bits",
+        "//libc/utils/MPFRWrapper:mpfr_wrapper",
+        "//libc/utils/UnitTest:LibcUnitTest",
+        "//libc/utils/UnitTest:fp_test_helpers",
+    ],
+)
+
+math_test(
+    name = "rint",
+    deps = [
+        ":rint_test_template",
+        "//libc/utils/MPFRWrapper:mpfr_wrapper",
+    ],
+)
+
+math_test(
+    name = "rintf",
+    deps = [
+        ":rint_test_template",
+        "//libc/utils/MPFRWrapper:mpfr_wrapper",
+    ],
+)
+
+math_test(
+    name = "rintl",
+    deps = [
+        ":rint_test_template",
+        "//libc/utils/MPFRWrapper:mpfr_wrapper",
+    ],
+)
+
+cc_library(
+    name = "round_to_integer_test_template",
+    hdrs = ["RoundToIntegerTest.h"],
+    deps = [
+        "//libc:__support_fputil_fenv_impl",
+        "//libc:__support_fputil_fp_bits",
+        "//libc/utils/MPFRWrapper:mpfr_wrapper",
+        "//libc/utils/UnitTest:LibcUnitTest",
+        "//libc/utils/UnitTest:fp_test_helpers",
+    ],
+)
+
+math_test(
+    name = "lrint",
+    deps = [
+        ":round_to_integer_test_template",
+        "//libc/utils/MPFRWrapper:mpfr_wrapper",
+    ],
+)
+
+math_test(
+    name = "lrintf",
+    deps = [
+        ":round_to_integer_test_template",
+        "//libc/utils/MPFRWrapper:mpfr_wrapper",
+    ],
+)
+
+math_test(
+    name = "lrintl",
+    deps = [
+        ":round_to_integer_test_template",
+        "//libc/utils/MPFRWrapper:mpfr_wrapper",
+    ],
+)
+
+math_test(
+    name = "llrint",
+    deps = [
+        ":round_to_integer_test_template",
+        "//libc/utils/MPFRWrapper:mpfr_wrapper",
+    ],
+)
+
+math_test(
+    name = "llrintf",
+    deps = [
+        ":round_to_integer_test_template",
+        "//libc/utils/MPFRWrapper:mpfr_wrapper",
+    ],
+)
+
+math_test(
+    name = "llrintl",
+    deps = [
+        ":round_to_integer_test_template",
+        "//libc/utils/MPFRWrapper:mpfr_wrapper",
+    ],
+)
+
+math_test(
+    name = "lround",
+    deps = [
+        ":round_to_integer_test_template",
+        "//libc/utils/MPFRWrapper:mpfr_wrapper",
+    ],
+)
+
+math_test(
+    name = "lroundf",
+    deps = [
+        ":round_to_integer_test_template",
+        "//libc/utils/MPFRWrapper:mpfr_wrapper",
+    ],
+)
+
+math_test(
+    name = "lroundl",
+    deps = [
+        ":round_to_integer_test_template",
+        "//libc/utils/MPFRWrapper:mpfr_wrapper",
+    ],
+)
+
+math_test(
+    name = "llround",
+    deps = [
+        ":round_to_integer_test_template",
+        "//libc/utils/MPFRWrapper:mpfr_wrapper",
+    ],
+)
+
+math_test(
+    name = "llroundf",
+    deps = [
+        ":round_to_integer_test_template",
+        "//libc/utils/MPFRWrapper:mpfr_wrapper",
+    ],
+)
+
+math_test(
+    name = "llroundl",
+    deps = [
+        ":round_to_integer_test_template",
+        "//libc/utils/MPFRWrapper:mpfr_wrapper",
+    ],
+)
+
+cc_library(
+    name = "nextafter_test_template",
+    hdrs = ["NextAfterTest.h"],
+    deps = [
+        "//libc:__support_cpp_array",
+        "//libc:__support_cpp_bit",
+        "//libc:__support_cpp_type_traits",
+        "//libc:__support_fputil_basic_operations",
+        "//libc:__support_fputil_fp_bits",
+        "//libc/utils/UnitTest:LibcUnitTest",
+        "//libc/utils/UnitTest:fp_test_helpers",
+    ],
+)
+
+math_test(
+    name = "nextafter",
+    deps = [":nextafter_test_template"],
+)
+
+math_test(
+    name = "nextafterf",
+    deps = [":nextafter_test_template"],
+)
+
+math_test(
+    name = "nextafterl",
+    deps = [":nextafter_test_template"],
+)
+
+cc_library(
+    name = "sdcomp26094",
+    hdrs = ["sdcomp26094.h"],
+    deps = [
+        "//libc:__support_cpp_array",
+        "//libc:libc_root",
+    ],
+)
+
+math_test(
+    name = "cosf",
+    deps = [
+        ":sdcomp26094",
+        "//libc:__support_cpp_array",
+        "//libc/utils/MPFRWrapper:mpfr_wrapper",
+    ],
+)
+
+math_test(
+    name = "sincosf",
+    deps = [
+        ":sdcomp26094",
+        "//libc:__support_cpp_array",
+        "//libc/utils/MPFRWrapper:mpfr_wrapper",
+    ],
+)
+
+math_test(
+    name = "sinf",
+    deps = [
+        ":sdcomp26094",
+        "//libc:__support_cpp_array",
+        "//libc/utils/MPFRWrapper:mpfr_wrapper",
+    ],
+)
diff --git a/utils/bazel/llvm-project-overlay/libc/test/src/math/libc_math_test_rules.bzl b/utils/bazel/llvm-project-overlay/libc/test/src/math/libc_math_test_rules.bzl
new file mode 100644 (file)
index 0000000..b8d72ff
--- /dev/null
@@ -0,0 +1,39 @@
+# 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 starlark rules for math tests.
+
+This rule delegates testing to libc_test_rules.bzl:libc_test.
+It adds common math dependencies.
+"""
+
+load("//libc/test:libc_test_rules.bzl", "libc_test")
+
+def math_test(name, hdrs = [], deps = [], **kwargs):
+    """Add a target for the unittest of a math function.
+
+    Args:
+      name: The name of the function being tested.
+      hdrs: List of headers to add.
+      deps: The list of other libraries to be linked in to the test target.
+      **kwargs: Attributes relevant for a cc_test. For example, name, srcs.
+    """
+    test_name = name + "_test"
+    libc_test(
+        name = test_name,
+        srcs = [test_name + ".cpp"] + hdrs,
+        libc_function_deps = ["//libc:" + name],
+        deps = [
+            "//libc:__support_fputil_basic_operations",
+            "//libc:__support_builtin_wrappers",
+            "//libc:__support_fputil_fenv_impl",
+            "//libc:__support_fputil_float_properties",
+            "//libc:__support_fputil_fp_bits",
+            "//libc:__support_uint128",
+            "//libc:__support_fputil_manipulation_functions",
+            "//libc:__support_fputil_nearest_integer_operations",
+            "//libc/utils/UnitTest:fp_test_helpers",
+        ] + deps,
+        **kwargs
+    )
diff --git a/utils/bazel/llvm-project-overlay/libc/test/src/stdlib/BUILD.bazel b/utils/bazel/llvm-project-overlay/libc/test/src/stdlib/BUILD.bazel
new file mode 100644 (file)
index 0000000..bc353e9
--- /dev/null
@@ -0,0 +1,83 @@
+# 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
+
+# Tests for LLVM libc stdlib.h functions.
+
+load("//libc/test:libc_test_rules.bzl", "libc_test")
+
+package(default_visibility = ["//visibility:public"])
+
+licenses(["notice"])
+
+libc_test(
+    name = "atoi_test",
+    srcs = ["atoi_test.cpp"],
+    libc_function_deps = [
+        "//libc:atoi",
+    ],
+)
+
+libc_test(
+    name = "atol_test",
+    srcs = ["atol_test.cpp"],
+    libc_function_deps = [
+        "//libc:atol",
+    ],
+)
+
+libc_test(
+    name = "atoll_test",
+    srcs = ["atoll_test.cpp"],
+    libc_function_deps = [
+        "//libc:atoll",
+    ],
+)
+
+libc_test(
+    name = "bsearch_test",
+    srcs = ["bsearch_test.cpp"],
+    libc_function_deps = [
+        "//libc:bsearch",
+    ],
+)
+
+libc_test(
+    name = "qsort_test",
+    srcs = ["qsort_test.cpp"],
+    libc_function_deps = [
+        "//libc:qsort",
+    ],
+)
+
+libc_test(
+    name = "strtol_test",
+    srcs = ["strtol_test.cpp"],
+    libc_function_deps = [
+        "//libc:strtol",
+    ],
+)
+
+libc_test(
+    name = "strtoll_test",
+    srcs = ["strtoll_test.cpp"],
+    libc_function_deps = [
+        "//libc:strtoll",
+    ],
+)
+
+libc_test(
+    name = "strtoul_test",
+    srcs = ["strtoul_test.cpp"],
+    libc_function_deps = [
+        "//libc:strtoul",
+    ],
+)
+
+libc_test(
+    name = "strtoull_test",
+    srcs = ["strtoull_test.cpp"],
+    libc_function_deps = [
+        "//libc:strtoull",
+    ],
+)
diff --git a/utils/bazel/llvm-project-overlay/libc/test/src/string/BUILD.bazel b/utils/bazel/llvm-project-overlay/libc/test/src/string/BUILD.bazel
new file mode 100644 (file)
index 0000000..8ccc627
--- /dev/null
@@ -0,0 +1,174 @@
+# 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
+
+# Tests for LLVM libc string.h functions.
+
+load("//libc/test:libc_test_rules.bzl", "libc_test")
+
+package(default_visibility = ["//visibility:public"])
+
+licenses(["notice"])
+
+libc_test(
+    name = "strlen_test",
+    srcs = ["strlen_test.cpp"],
+    libc_function_deps = [
+        "//libc:strlen",
+    ],
+)
+
+libc_test(
+    name = "strcpy_test",
+    srcs = ["strcpy_test.cpp"],
+    libc_function_deps = [
+        "//libc:strcpy_sanitized",
+    ],
+)
+
+libc_test(
+    name = "strcmp_test",
+    srcs = ["strcmp_test.cpp"],
+    libc_function_deps = [
+        "//libc:strcmp",
+    ],
+)
+
+libc_test(
+    name = "memchr_test",
+    srcs = ["memchr_test.cpp"],
+    libc_function_deps = [
+        "//libc:memchr",
+    ],
+)
+
+libc_test(
+    name = "strchr_test",
+    srcs = ["strchr_test.cpp"],
+    libc_function_deps = [
+        "//libc:strchr",
+    ],
+)
+
+libc_test(
+    name = "strstr_test",
+    srcs = ["strstr_test.cpp"],
+    libc_function_deps = [
+        "//libc:strstr",
+    ],
+)
+
+libc_test(
+    name = "strnlen_test",
+    srcs = ["strnlen_test.cpp"],
+    libc_function_deps = [
+        "//libc:strnlen",
+    ],
+)
+
+libc_test(
+    name = "memrchr_test",
+    srcs = ["memrchr_test.cpp"],
+    libc_function_deps = [
+        "//libc:memrchr",
+    ],
+)
+
+libc_test(
+    name = "strrchr_test",
+    srcs = ["strrchr_test.cpp"],
+    libc_function_deps = [
+        "//libc:strrchr",
+    ],
+)
+
+libc_test(
+    name = "strcspn_test",
+    srcs = ["strcspn_test.cpp"],
+    libc_function_deps = [
+        "//libc:strcspn",
+    ],
+)
+
+libc_test(
+    name = "strspn_test",
+    srcs = ["strspn_test.cpp"],
+    libc_function_deps = [
+        "//libc:strspn",
+    ],
+)
+
+libc_test(
+    name = "strtok_test",
+    srcs = ["strtok_test.cpp"],
+    libc_function_deps = [
+        "//libc:strtok",
+    ],
+)
+
+cc_library(
+    name = "memory_check_utils",
+    hdrs = ["memory_utils/memory_check_utils.h"],
+    deps = [
+        "//libc:__support_cpp_span",
+        "//libc:string_memory_utils",
+    ],
+)
+
+libc_test(
+    name = "memcpy_test",
+    srcs = ["memcpy_test.cpp"],
+    libc_function_deps = [
+        "//libc:memcpy",
+    ],
+    deps = [":memory_check_utils"],
+)
+
+libc_test(
+    name = "memset_test",
+    srcs = ["memset_test.cpp"],
+    libc_function_deps = [
+        "//libc:memset",
+    ],
+    deps = [":memory_check_utils"],
+)
+
+libc_test(
+    name = "memmove_test",
+    srcs = ["memmove_test.cpp"],
+    libc_function_deps = [
+        "//libc:memcmp",
+        "//libc:memmove",
+    ],
+    deps = [
+        "//libc:__support_cpp_span",
+        "//libc/utils/UnitTest:memory_matcher",
+    ],
+)
+
+libc_test(
+    name = "memcmp_test",
+    srcs = ["memcmp_test.cpp"],
+    libc_function_deps = [
+        "//libc:memcmp",
+    ],
+    deps = [":memory_check_utils"],
+)
+
+libc_test(
+    name = "bcmp_test",
+    srcs = ["bcmp_test.cpp"],
+    libc_function_deps = [
+        "//libc:bcmp",
+    ],
+    deps = [":memory_check_utils"],
+)
+
+libc_test(
+    name = "bzero_test",
+    srcs = ["bzero_test.cpp"],
+    libc_function_deps = [
+        "//libc:bzero",
+    ],
+    deps = [":memory_check_utils"],
+)
diff --git a/utils/bazel/llvm-project-overlay/libc/utils/MPFRWrapper/BUILD.bazel b/utils/bazel/llvm-project-overlay/libc/utils/MPFRWrapper/BUILD.bazel
new file mode 100644 (file)
index 0000000..a0791aa
--- /dev/null
@@ -0,0 +1,44 @@
+# 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
+
+# A wrapper library over MPFR for use with LLVM libc math unittests.
+
+package(default_visibility = ["//visibility:public"])
+
+licenses(["notice"])
+
+cc_library(
+    name = "mpfr_impl",
+    target_compatible_with = select({
+        "//conditions:default": [],
+        "//libc:mpfr_disable": ["@platforms//:incompatible"],
+    }),
+    deps = select(
+        {
+            "//conditions:default": [],
+            "//libc:mpfr_external": ["@mpfr//:mpfr_external"],
+            "//libc:mpfr_system": ["@mpfr//:mpfr_system"],
+        },
+    ),
+)
+
+cc_library(
+    name = "mpfr_wrapper",
+    srcs = ["MPFRUtils.cpp"],
+    hdrs = ["MPFRUtils.h"],
+    deps = [
+        "//libc:__support_common",
+        "//libc:__support_cpp_bit",
+        "//libc:__support_cpp_bitset",
+        "//libc:__support_cpp_string_view",
+        "//libc:__support_cpp_type_traits",
+        "//libc:__support_fputil_fp_bits",
+        "//libc:__support_fputil_platform_defs",
+        "//libc:libc_root",
+        "//libc/utils/MPFRWrapper:mpfr_impl",
+        "//libc/utils/UnitTest:LibcUnitTest",
+        "//libc/utils/UnitTest:fp_test_helpers",
+        "//libc/utils/testutils:libc_test_utils",
+    ],
+)
diff --git a/utils/bazel/llvm-project-overlay/libc/utils/UnitTest/BUILD.bazel b/utils/bazel/llvm-project-overlay/libc/utils/UnitTest/BUILD.bazel
new file mode 100644 (file)
index 0000000..34ae437
--- /dev/null
@@ -0,0 +1,83 @@
+# 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 unittest library.
+
+package(default_visibility = ["//visibility:public"])
+
+licenses(["notice"])
+
+cc_library(
+    name = "LibcUnitTest",
+    srcs = [
+        "LibcTest.cpp",
+        "LibcTestMain.cpp",
+    ],
+    hdrs = [
+        "LibcTest.h",
+        "PlatformDefs.h",
+        "Test.h",
+    ],
+    deps = [
+        "//libc:__support_cpp_bit",
+        "//libc:__support_cpp_bitset",
+        "//libc:__support_cpp_span",
+        "//libc:__support_cpp_string_view",
+        "//libc:__support_cpp_type_traits",
+        "//libc:__support_uint128",
+        "//libc:libc_root",
+        "//libc/utils/testutils:libc_test_utils",
+        "//llvm:Support",
+    ],
+)
+
+cc_library(
+    name = "fp_test_helpers",
+    srcs = [
+        "FPExceptMatcher.cpp",
+        "FPMatcher.cpp",
+    ],
+    hdrs = [
+        "FPExceptMatcher.h",
+        "FPMatcher.h",
+    ],
+    deps = [
+        ":LibcUnitTest",
+        ":string_utils",
+        "//libc:__support_cpp_bit",
+        "//libc:__support_cpp_bitset",
+        "//libc:__support_cpp_span",
+        "//libc:__support_cpp_type_traits",
+        "//libc:__support_fputil_fenv_impl",
+        "//libc:__support_fputil_fp_bits",
+        "//libc:libc_root",
+    ],
+)
+
+cc_library(
+    name = "memory_matcher",
+    srcs = [
+        "MemoryMatcher.cpp",
+    ],
+    hdrs = [
+        "MemoryMatcher.h",
+    ],
+    deps = [
+        ":LibcUnitTest",
+        "//libc:__support_cpp_bit",
+        "//libc:__support_cpp_bitset",
+        "//libc:__support_cpp_span",
+        "//libc:__support_cpp_type_traits",
+    ],
+)
+
+cc_library(
+    name = "string_utils",
+    hdrs = [
+        "StringUtils.h",
+    ],
+    deps = [
+        "//libc:__support_cpp_type_traits",
+    ],
+)
diff --git a/utils/bazel/llvm-project-overlay/libc/utils/testutils/BUILD.bazel b/utils/bazel/llvm-project-overlay/libc/utils/testutils/BUILD.bazel
new file mode 100644 (file)
index 0000000..1e07e4a
--- /dev/null
@@ -0,0 +1,27 @@
+# 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
+
+package(default_visibility = ["//visibility:public"])
+
+licenses(["notice"])
+
+cc_library(
+    name = "libc_test_utils",
+    srcs = [
+        "ExecuteFunctionUnix.cpp",
+        "FDReaderUnix.cpp",
+        "RoundingModeUtils.cpp",
+        "StreamWrapper.cpp",
+    ],
+    hdrs = [
+        "ExecuteFunction.h",
+        "FDReader.h",
+        "RoundingModeUtils.h",
+        "StreamWrapper.h",
+    ],
+    deps = [
+        "//libc:libc_root",
+        "//llvm:Support",
+    ],
+)
diff --git a/utils/bazel/third_party_build/gmp.BUILD b/utils/bazel/third_party_build/gmp.BUILD
new file mode 100644 (file)
index 0000000..fb0c3bf
--- /dev/null
@@ -0,0 +1,20 @@
+# 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
+
+load("@rules_foreign_cc//foreign_cc:defs.bzl", "configure_make_variant")
+
+filegroup(
+    name = "sources",
+    srcs = glob(["**"]),
+)
+
+configure_make_variant(
+    name = "gmp",
+    configure_options = ["--with-pic"],
+    copts = ["-w"],
+    lib_name = "libgmp",
+    lib_source = ":sources",
+    toolchain = "@rules_foreign_cc//toolchains:preinstalled_autoconf_toolchain",
+    visibility = ["//visibility:public"],
+)
diff --git a/utils/bazel/third_party_build/mpfr.BUILD b/utils/bazel/third_party_build/mpfr.BUILD
new file mode 100644 (file)
index 0000000..6b47874
--- /dev/null
@@ -0,0 +1,33 @@
+# 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
+
+load("@rules_foreign_cc//foreign_cc:defs.bzl", "configure_make_variant")
+
+filegroup(
+    name = "sources",
+    srcs = glob(["**"]),
+)
+
+configure_make_variant(
+    name = "mpfr",
+    configure_options = ["--with-pic"],
+    copts = ["-w"],
+    lib_name = "libmpfr",
+    lib_source = ":sources",
+    toolchain = "@rules_foreign_cc//toolchains:preinstalled_autoconf_toolchain",
+    visibility = ["//visibility:public"],
+    deps = ["@gmp//:gmp_"],
+)
+
+alias(
+    name = "mpfr_external",
+    actual = "@mpfr//:mpfr_",
+    visibility = ["//visibility:public"],
+)
+
+cc_library(
+    name = "mpfr_system",
+    linkopts = ["-lmpfr"],
+    visibility = ["//visibility:public"],
+)