diff --git a/libc/src/stdio/fprintf.cpp b/libc/src/stdio/fprintf.cpp --- a/libc/src/stdio/fprintf.cpp +++ b/libc/src/stdio/fprintf.cpp @@ -8,7 +8,6 @@ #include "src/stdio/fprintf.h" -#include "src/__support/File/file.h" #include "src/__support/arg_list.h" #include "src/stdio/printf_core/vfprintf_internal.h" @@ -18,6 +17,7 @@ namespace __llvm_libc { #ifndef LIBC_COPT_PRINTF_USE_SYSTEM_FILE +#include "src/__support/File/file.h" using FileT = __llvm_libc::File; #else // defined(LIBC_COPT_PRINTF_USE_SYSTEM_FILE) using FileT = ::FILE; diff --git a/libc/test/src/stdio/CMakeLists.txt b/libc/test/src/stdio/CMakeLists.txt --- a/libc/test/src/stdio/CMakeLists.txt +++ b/libc/test/src/stdio/CMakeLists.txt @@ -1,5 +1,13 @@ add_libc_testsuite(libc_stdio_unittests) +add_header_library( + testdata_path_util + HDRS + testdata_path_util.h + DEPENDS + libc.include.stdlib +) + add_libc_unittest( fileop_test SUITE diff --git a/libc/test/src/stdio/fprintf_test.cpp b/libc/test/src/stdio/fprintf_test.cpp --- a/libc/test/src/stdio/fprintf_test.cpp +++ b/libc/test/src/stdio/fprintf_test.cpp @@ -17,6 +17,8 @@ #include "test/UnitTest/Test.h" +#include "test/src/stdio/testdata_path_util.h" + #include namespace printf_test { @@ -34,8 +36,12 @@ } // namespace printf_test TEST(LlvmLibcFPrintfTest, WriteToFile) { - constexpr char FILENAME[] = "testdata/fprintf_output.test"; - ::FILE *file = printf_test::fopen(FILENAME, "w"); + constexpr char FILENAME[] = "fprintf_output.test"; + char FILE_PATH[1024] = {'\0'}; + + write_file_path(FILE_PATH, FILENAME); + + ::FILE *file = printf_test::fopen(FILE_PATH, "w"); ASSERT_FALSE(file == nullptr); int written; @@ -55,7 +61,7 @@ ASSERT_EQ(0, printf_test::fclose(file)); - file = printf_test::fopen(FILENAME, "r"); + file = printf_test::fopen(FILE_PATH, "r"); ASSERT_FALSE(file == nullptr); char data[50]; diff --git a/libc/test/src/stdio/testdata_path_util.h b/libc/test/src/stdio/testdata_path_util.h new file mode 100644 --- /dev/null +++ b/libc/test/src/stdio/testdata_path_util.h @@ -0,0 +1,35 @@ +//===-- Utilities for finding the testdata path ---------------------------===// +// +// Part of the LLVM Project, 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 +// +//===----------------------------------------------------------------------===// + +#include +#include + +// This is to support building with Bazel. +static const char *const UNDECLARED_OUTPUTS_PATH = + getenv("TEST_UNDECLARED_OUTPUTS_DIR"); + +constexpr char DEFAULT_DIR[] = "testdata/"; + +inline void concat_path(char *output, const char *path, const char *filename) { + size_t i = 0; + for (; path[i] != '\0'; ++i) { + output[i] = path[i]; + } + for (size_t j = 0; filename[j] != '\0'; ++i, ++j) { + output[i] = filename[j]; + } +} + +inline void write_file_path(char *output, const char *filename) { + if (UNDECLARED_OUTPUTS_PATH == nullptr || + UNDECLARED_OUTPUTS_PATH[0] == '\0') { + concat_path(output, DEFAULT_DIR, filename); + } else { + concat_path(output, UNDECLARED_OUTPUTS_PATH, filename); + } +} diff --git a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel --- a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel +++ b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel @@ -152,6 +152,14 @@ ], ) +libc_support_library( + name = "__support_cpp_expected", + hdrs = ["src/__support/CPP/expected.h"], + deps = [ + ":libc_root", + ], +) + libc_support_library( name = "__support_cpp_functional", hdrs = ["src/__support/CPP/functional.h"], @@ -164,6 +172,17 @@ deps = [":libc_root"], ) + +libc_support_library( + name = "__support_cpp_new", + hdrs = ["src/__support/CPP/new.h"], + srcs = ["src/__support/CPP/new.cpp"], + deps = [ + ":libc_root", + ":__support_common", + ], +) + libc_support_library( name = "__support_cpp_optional", hdrs = ["src/__support/CPP/optional.h"], @@ -223,6 +242,16 @@ ], ) +libc_support_library( + name = "__support_error_or", + hdrs = ["src/__support/error_or.h"], + deps = [ + ":libc_root", + ":__support_cpp_expected", + ":__support_common", + ], +) + libc_support_library( name = "__support_float_to_string", hdrs = [ @@ -376,6 +405,20 @@ ], ) +libc_support_library( + name = "__support_file_file", + hdrs = ["src/__support/File/file.h"], + srcs = ["src/__support/File/file.cpp"], + deps = [ + ":libc_root", + ":__support_cpp_new", + ":__support_error_or", + ":__support_cpp_span", + ":__support_threads_mutex", + ":errno", + ], +) + libc_support_library( name = "__support_named_pair", hdrs = ["src/__support/named_pair.h"], @@ -678,6 +721,22 @@ ], ) +libc_support_library( + name = "__support_threads_mutex", + hdrs = [ + "src/__support/threads/mutex.h", + "src/__support/threads/mutex_common.h", + ], + textual_hdrs = [ + "src/__support/threads/linux/mutex.h", + "src/__support/threads/linux/futex_word.h", + ], + deps = [ + ":libc_root", + ":__support_cpp_atomic", + ":__support_osutil_syscall", + ], +) ############################### errno targets ################################ @@ -2126,6 +2185,18 @@ ], ) +libc_support_library( + name = "printf_file_writer", + hdrs = ["src/stdio/printf_core/file_writer.h"], + deps = [ + ":__support_cpp_string_view", + ":__support_file_file", + ":printf_core_structs", + ":libc_root", + ], + copts = ["-DLIBC_COPT_PRINTF_USE_SYSTEM_FILE"], +) + libc_support_library( name = "printf_writer", hdrs = ["src/stdio/printf_core/writer.h"], @@ -2209,3 +2280,41 @@ ":errno", ], ) + +libc_support_library( + name = "vfprintf_internal", + hdrs = ["src/stdio/printf_core/vfprintf_internal.h"], + deps = [ + ":__support_arg_list", + ":__support_file_file", + ":__support_macros_attributes", + ":printf_main", + ":printf_file_writer", + ":printf_writer", + ], +) + +libc_function( + name = "printf", + srcs = ["src/stdio/printf.cpp"], + hdrs = ["src/stdio/printf.h"], + deps = [ + ":__support_arg_list", + ":vfprintf_internal", + ":errno", + ], + copts = ["-DLIBC_COPT_PRINTF_USE_SYSTEM_FILE"], +) + + +libc_function( + name = "fprintf", + srcs = ["src/stdio/fprintf.cpp"], + hdrs = ["src/stdio/fprintf.h"], + deps = [ + ":__support_arg_list", + ":vfprintf_internal", + ":errno", + ], + copts = ["-DLIBC_COPT_PRINTF_USE_SYSTEM_FILE"], +) 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 --- a/utils/bazel/llvm-project-overlay/libc/test/libc_test_rules.bzl +++ b/utils/bazel/llvm-project-overlay/libc/test/libc_test_rules.bzl @@ -14,7 +14,7 @@ load("//libc:libc_build_rules.bzl", "INTERNAL_SUFFIX") -def libc_test(name, srcs, libc_function_deps, deps = [], **kwargs): +def libc_test(name, srcs, libc_function_deps, deps = [], output_file = "", **kwargs): """Add target for a libc test. Args: 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 --- a/utils/bazel/llvm-project-overlay/libc/test/src/stdio/BUILD.bazel +++ b/utils/bazel/llvm-project-overlay/libc/test/src/stdio/BUILD.bazel @@ -10,6 +10,12 @@ licenses(["notice"]) + +cc_library( + name = "testdata_path_util", + hdrs = ["testdata_path_util.h"], +) + libc_test( name = "printf_parser_test", srcs = ["printf_core/parser_test.cpp"], @@ -75,3 +81,23 @@ "//libc:snprintf", ], ) + +libc_test( + name = "printf_test", + srcs = ["printf_test.cpp"], + libc_function_deps = [ + "//libc:printf", + ], +) + +libc_test( + name = "fprintf_test", + srcs = ["fprintf_test.cpp"], + libc_function_deps = [ + "//libc:fprintf", + ], + deps = [ + ":testdata_path_util", + ], + copts = ["-DLIBC_COPT_PRINTF_USE_SYSTEM_FILE"], +)