From 99047c0501e0fe5c60bb583185f3b45bdc112199 Mon Sep 17 00:00:00 2001 From: Michael Jones Date: Tue, 14 Mar 2023 15:08:56 -0700 Subject: [PATCH] [libc][bazel] add targets for sprintf The bazel build is currently overlay mode only, so the FILE functions are still out of reach for it, but sprintf only uses strings. This adds targets for sprintf, snprintf, and all the interal printf pieces, as well as tests. Reviewed By: sivachandra, lntue Differential Revision: https://reviews.llvm.org/D146100 --- libc/src/stdio/printf_core/CMakeLists.txt | 4 + utils/bazel/llvm-project-overlay/libc/BUILD.bazel | 275 +++++++++++++++++++-- .../libc/test/UnitTest/BUILD.bazel | 16 ++ .../libc/test/src/stdio/BUILD.bazel | 77 ++++++ 4 files changed, 353 insertions(+), 19 deletions(-) create mode 100644 utils/bazel/llvm-project-overlay/libc/test/src/stdio/BUILD.bazel diff --git a/libc/src/stdio/printf_core/CMakeLists.txt b/libc/src/stdio/printf_core/CMakeLists.txt index 54a5cba..31db8ad 100644 --- a/libc/src/stdio/printf_core/CMakeLists.txt +++ b/libc/src/stdio/printf_core/CMakeLists.txt @@ -20,6 +20,7 @@ add_object_library( libc.src.__support.ctype_utils libc.src.__support.str_to_integer libc.src.__support.CPP.bit + libc.src.__support.CPP.optional libc.src.__support.CPP.string_view libc.src.__support.CPP.type_traits libc.src.__support.common @@ -37,6 +38,7 @@ add_object_library( libc.src.__support.ctype_utils libc.src.__support.str_to_integer libc.src.__support.CPP.bit + libc.src.__support.CPP.optional libc.src.__support.CPP.string_view libc.src.__support.CPP.type_traits libc.src.__support.common @@ -63,6 +65,8 @@ add_object_library( writer.cpp HDRS writer.h + DEPENDS + libc.src.__support.CPP.string_view ) add_object_library( diff --git a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel index 23c288d..5e563fe 100644 --- a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel +++ b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel @@ -215,6 +215,30 @@ libc_support_library( ) libc_support_library( + name = "__support_arg_list", + hdrs = ["src/__support/arg_list.h"], + deps = [ + ":libc_root", + ], +) + +libc_support_library( + name = "__support_float_to_string", + hdrs = [ + "src/__support/float_to_string.h", + "src/__support/ryu_constants.h", + ], + deps = [ + ":__support_cpp_type_traits", + ":__support_fputil_fp_bits", + ":__support_uint", + ":__support_libc_assert", + ":__support_common", + ":libc_root", + ], +) + +libc_support_library( name = "__support_number_pair", hdrs = ["src/__support/number_pair.h"], deps = [ @@ -277,6 +301,31 @@ libc_support_library( ) libc_support_library( + name = "__support_integer_to_string", + hdrs = ["src/__support/integer_to_string.h"], + deps = [ + ":__support_cpp_type_traits", + ":__support_cpp_optional", + ":__support_cpp_span", + ":__support_cpp_string_view", + ":__support_common", + ":libc_root", + ], +) + +libc_support_library( + name = "__support_libc_assert", + hdrs = ["src/__support/libc_assert.h"], + deps = [ + ":__support_osutil_io", + ":__support_osutil_quick_exit", + ":__support_integer_to_string", + ":__support_macros_attributes", + ":libc_root", + ], +) + +libc_support_library( name = "__support_ctype_utils", hdrs = ["src/__support/ctype_utils.h"], ) @@ -588,7 +637,7 @@ libc_support_library( ) libc_support_library( - name = "__support_osutil", + name = "__support_osutil_syscall", hdrs = ["src/__support/OSUtil/syscall.h"], textual_hdrs = [ "src/__support/OSUtil/linux/syscall.h", @@ -600,6 +649,35 @@ libc_support_library( ":libc_root", ], ) + +libc_support_library( + name = "__support_osutil_io", + hdrs = ["src/__support/OSUtil/io.h"], + textual_hdrs = [ + "src/__support/OSUtil/linux/io.h", + ], + deps = [ + ":__support_common", + ":__support_osutil_syscall", + ":string_utils", + ":libc_root", + ], +) + +libc_support_library( + name = "__support_osutil_quick_exit", + hdrs = ["src/__support/OSUtil/quick_exit.h"], + textual_hdrs = [ + "src/__support/OSUtil/linux/quick_exit.h", + #TODO: add support for GPU quick_exit (isn't just in a header.) + ], + deps = [ + ":__support_osutil_syscall", + ":libc_root", + ], +) + + ############################### errno targets ################################ libc_function( @@ -1780,7 +1858,7 @@ libc_function( hdrs = ["src/unistd/chdir.h"], deps = [ ":__support_common", - ":__support_osutil", + ":__support_osutil_syscall", ":errno", ], ) @@ -1791,7 +1869,7 @@ libc_function( hdrs = ["src/unistd/close.h"], deps = [ ":__support_common", - ":__support_osutil", + ":__support_osutil_syscall", ":errno", ], ) @@ -1802,7 +1880,7 @@ libc_function( hdrs = ["src/unistd/fchdir.h"], deps = [ ":__support_common", - ":__support_osutil", + ":__support_osutil_syscall", ":errno", ], ) @@ -1813,7 +1891,7 @@ libc_function( hdrs = ["src/unistd/fsync.h"], deps = [ ":__support_common", - ":__support_osutil", + ":__support_osutil_syscall", ":errno", ], ) @@ -1824,7 +1902,7 @@ libc_function( hdrs = ["src/unistd/ftruncate.h"], deps = [ ":__support_common", - ":__support_osutil", + ":__support_osutil_syscall", ":errno", ], ) @@ -1835,7 +1913,7 @@ libc_function( hdrs = ["src/unistd/link.h"], deps = [ ":__support_common", - ":__support_osutil", + ":__support_osutil_syscall", ":errno", ], ) @@ -1846,7 +1924,7 @@ libc_function( hdrs = ["src/unistd/linkat.h"], deps = [ ":__support_common", - ":__support_osutil", + ":__support_osutil_syscall", ":errno", ], ) @@ -1857,7 +1935,7 @@ libc_function( hdrs = ["src/unistd/lseek.h"], deps = [ ":__support_common", - ":__support_osutil", + ":__support_osutil_syscall", ":errno", ], ) @@ -1868,7 +1946,7 @@ libc_function( hdrs = ["src/unistd/read.h"], deps = [ ":__support_common", - ":__support_osutil", + ":__support_osutil_syscall", ":errno", ], ) @@ -1879,7 +1957,7 @@ libc_function( hdrs = ["src/unistd/readlink.h"], deps = [ ":__support_common", - ":__support_osutil", + ":__support_osutil_syscall", ":errno", ], ) @@ -1890,7 +1968,7 @@ libc_function( hdrs = ["src/unistd/readlinkat.h"], deps = [ ":__support_common", - ":__support_osutil", + ":__support_osutil_syscall", ":errno", ], ) @@ -1901,7 +1979,7 @@ libc_function( hdrs = ["src/unistd/rmdir.h"], deps = [ ":__support_common", - ":__support_osutil", + ":__support_osutil_syscall", ":errno", ], ) @@ -1912,7 +1990,7 @@ libc_function( hdrs = ["src/unistd/symlink.h"], deps = [ ":__support_common", - ":__support_osutil", + ":__support_osutil_syscall", ":errno", ], ) @@ -1923,7 +2001,7 @@ libc_function( hdrs = ["src/unistd/symlinkat.h"], deps = [ ":__support_common", - ":__support_osutil", + ":__support_osutil_syscall", ":errno", ], ) @@ -1934,7 +2012,7 @@ libc_function( hdrs = ["src/unistd/truncate.h"], deps = [ ":__support_common", - ":__support_osutil", + ":__support_osutil_syscall", ":errno", ], ) @@ -1945,7 +2023,7 @@ libc_function( hdrs = ["src/unistd/unlink.h"], deps = [ ":__support_common", - ":__support_osutil", + ":__support_osutil_syscall", ":errno", ], ) @@ -1956,7 +2034,7 @@ libc_function( hdrs = ["src/unistd/unlinkat.h"], deps = [ ":__support_common", - ":__support_osutil", + ":__support_osutil_syscall", ":errno", ], ) @@ -1967,7 +2045,166 @@ libc_function( hdrs = ["src/unistd/write.h"], deps = [ ":__support_common", - ":__support_osutil", + ":__support_osutil_syscall", + ":errno", + ], +) + +################################ stdio targets ################################# + +libc_support_library( + name = "printf_core_structs", + hdrs = ["src/stdio/printf_core/core_structs.h"], + deps = [ + ":__support_cpp_string_view", + ":__support_fputil_fp_bits", + ":libc_root", + ], +) + +libc_support_library( + name = "printf_config", + hdrs = ["src/stdio/printf_core/printf_config.h"], + deps = [ + ":libc_root", + ], +) + + +libc_support_library( + name = "printf_parser", + hdrs = ["src/stdio/printf_core/parser.h"], + srcs = ["src/stdio/printf_core/parser.cpp"], + deps = [ + ":printf_core_structs", + ":printf_config", + ":__support_cpp_string_view", + ":__support_cpp_type_traits", + ":__support_cpp_optional", + ":__support_cpp_bit", + ":__support_fputil_fp_bits", + ":__support_arg_list", + ":__support_ctype_utils", + ":__support_str_to_integer", + ":__support_common", + ":libc_root", + ], +) + +# Only used for testing. +libc_support_library( + name = "printf_mock_parser", + hdrs = ["src/stdio/printf_core/parser.h"], + srcs = ["src/stdio/printf_core/parser.cpp"], + deps = [ + ":printf_core_structs", + ":printf_config", + ":__support_cpp_string_view", + ":__support_cpp_type_traits", + ":__support_cpp_optional", + ":__support_cpp_bit", + ":__support_fputil_fp_bits", + ":__support_arg_list", + ":__support_ctype_utils", + ":__support_str_to_integer", + ":__support_common", + ":libc_root", + ], + copts = ["-DLIBC_COPT_MOCK_ARG_LIST"], +) + +libc_support_library( + name = "printf_string_writer", + hdrs = ["src/stdio/printf_core/string_writer.h"], + srcs = ["src/stdio/printf_core/string_writer.cpp"], + deps = [ + ":__support_cpp_string_view", + ":string_memory_utils", + ":printf_core_structs", + ":libc_root", + ], +) + +libc_support_library( + name = "printf_writer", + hdrs = ["src/stdio/printf_core/writer.h"], + srcs = ["src/stdio/printf_core/writer.cpp"], + deps = [ + ":__support_cpp_string_view", + ":libc_root", + ], +) + +libc_support_library( + name = "printf_converter", + hdrs = [ + "src/stdio/printf_core/converter.h", + "src/stdio/printf_core/converter_utils.h", + "src/stdio/printf_core/converter_atlas.h", + "src/stdio/printf_core/string_converter.h", + "src/stdio/printf_core/char_converter.h", + "src/stdio/printf_core/int_converter.h", + "src/stdio/printf_core/ptr_converter.h", + "src/stdio/printf_core/write_int_converter.h", + "src/stdio/printf_core/float_inf_nan_converter.h", + "src/stdio/printf_core/float_hex_converter.h", + "src/stdio/printf_core/float_dec_converter.h", + ], + srcs = ["src/stdio/printf_core/converter.cpp"], + deps = [ + ":printf_writer", + ":printf_core_structs", + ":__support_cpp_string_view", + ":__support_cpp_limits", + ":__support_cpp_span", + ":__support_fputil_fp_bits", + ":__support_fputil_fenv_impl", + ":__support_libc_assert", + ":__support_uint", + ":__support_uint128", + ":__support_integer_to_string", + ":__support_float_to_string", + ":__support_common", + ":libc_root", + ], +) + +libc_support_library( + name = "printf_main", + hdrs = ["src/stdio/printf_core/printf_main.h"], + srcs = ["src/stdio/printf_core/printf_main.cpp"], + deps = [ + ":printf_parser", + ":printf_converter", + ":printf_writer", + ":printf_core_structs", + ":__support_arg_list", + ":libc_root", + ], +) + +libc_function( + name = "sprintf", + srcs = ["src/stdio/sprintf.cpp"], + hdrs = ["src/stdio/sprintf.h"], + deps = [ + ":__support_arg_list", + ":printf_main", + ":printf_string_writer", + ":printf_writer", + ":errno", + ], +) + +libc_function( + name = "snprintf", + srcs = ["src/stdio/snprintf.cpp"], + hdrs = ["src/stdio/snprintf.h"], + deps = [ + ":__support_arg_list", + ":printf_main", + ":printf_string_writer", + ":printf_writer", ":errno", ], ) diff --git a/utils/bazel/llvm-project-overlay/libc/test/UnitTest/BUILD.bazel b/utils/bazel/llvm-project-overlay/libc/test/UnitTest/BUILD.bazel index bf01bd4..297b98c 100644 --- a/utils/bazel/llvm-project-overlay/libc/test/UnitTest/BUILD.bazel +++ b/utils/bazel/llvm-project-overlay/libc/test/UnitTest/BUILD.bazel @@ -74,6 +74,22 @@ cc_library( ) cc_library( + name = "printf_matcher", + srcs = [ + "PrintfMatcher.cpp", + ], + hdrs = [ + "PrintfMatcher.h", + ], + deps = [ + ":LibcUnitTest", + ":string_utils", + "//libc:__support_fputil_fp_bits", + "//libc:printf_core_structs", + ], +) + +cc_library( name = "string_utils", hdrs = [ "StringUtils.h", diff --git a/utils/bazel/llvm-project-overlay/libc/test/src/stdio/BUILD.bazel b/utils/bazel/llvm-project-overlay/libc/test/src/stdio/BUILD.bazel new file mode 100644 index 0000000..4f49ec7 --- /dev/null +++ b/utils/bazel/llvm-project-overlay/libc/test/src/stdio/BUILD.bazel @@ -0,0 +1,77 @@ +# 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 stdio.h functions. + +load("//libc/test:libc_test_rules.bzl", "libc_test") + +package(default_visibility = ["//visibility:public"]) + +licenses(["notice"]) + +libc_test( + name = "printf_parser_test", + srcs = ["printf_core/parser_test.cpp"], + libc_function_deps = [ + ], + deps = [ + "//libc:printf_parser", + "//libc:printf_core_structs", + "//libc:__support_cpp_string_view", + "//libc:__support_cpp_bit", + "//libc:__support_arg_list", + "//libc/utils/testutils:libc_test_utils", + "//libc/test/UnitTest:printf_matcher", + ], +) + +libc_test( + name = "printf_string_writer_test", + srcs = ["printf_core/string_writer_test.cpp"], + libc_function_deps = [ + ], + deps = [ + "//libc:printf_string_writer", + "//libc:printf_writer", + "//libc:printf_core_structs", + "//libc:__support_cpp_string_view", + "//libc:__support_arg_list", + ], +) + +libc_test( + name = "printf_converter_test", + srcs = ["printf_core/converter_test.cpp"], + libc_function_deps = [ + ], + deps = [ + "//libc:printf_converter", + "//libc:printf_string_writer", + "//libc:printf_writer", + "//libc:printf_core_structs", + "//libc:__support_cpp_string_view", + "//libc:__support_arg_list", + ], +) + +libc_test( + name = "sprintf_test", + srcs = ["sprintf_test.cpp"], + libc_function_deps = [ + "//libc:sprintf", + ], + deps = [ + "//libc:__support_fputil_fp_bits", + "//libc:__support_fputil_platform_defs", + "//libc/utils/testutils:libc_test_utils", + ], +) + +libc_test( + name = "snprintf_test", + srcs = ["snprintf_test.cpp"], + libc_function_deps = [ + "//libc:snprintf", + ], +) -- 2.7.4