Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions clang/cmake/caches/Fuchsia-stage2.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,7 @@ foreach(target armv6m-none-eabi;armv7m-none-eabi;armv7em-none-eabi;armv8m.main-n
foreach(lang C;CXX;ASM)
# TODO: The preprocessor defines workaround various issues in libc and libc++ integration.
# These should be addressed and removed over time.
set(RUNTIMES_${target}_CMAKE_${lang}_local_flags "--target=${target} -Wno-atomic-alignment \"-Dvfprintf(stream, format, vlist)=vprintf(format, vlist)\" \"-Dfprintf(stream, format, ...)=printf(format)\" \"-Dfputs(string, stream)=puts(string)\" -D_LIBCPP_PRINT=1")
set(RUNTIMES_${target}_CMAKE_${lang}_local_flags "--target=${target} -Wno-atomic-alignment")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You may also want to update libc/cmake/cache files introduced recently. e.g. https://github.com/llvm/llvm-project/blob/main/libc/cmake/caches/armv6m-none-eabi.cmake

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Those flags are only needed for building for libc++ which we don't do in those cache files, so we can remove those flags separately from this PR (I forgot to catch that in the review).

if(NOT ${target} STREQUAL "aarch64-none-elf")
set(RUNTIMES_${target}_CMAKE_${lang}_local_flags "${RUNTIMES_${target}_CMAKE_${lang}_local_flags} -mthumb")
endif()
Expand Down Expand Up @@ -372,6 +372,7 @@ foreach(target armv6m-none-eabi;armv7m-none-eabi;armv7em-none-eabi;armv8m.main-n
set(RUNTIMES_${target}_LIBCXX_ENABLE_RTTI OFF CACHE BOOL "")
set(RUNTIMES_${target}_LIBCXX_ENABLE_THREADS OFF CACHE BOOL "")
set(RUNTIMES_${target}_LIBCXX_ENABLE_MONOTONIC_CLOCK OFF CACHE BOOL "")
set(RUNTIMES_${target}_LIBCXX_HAS_TERMINAL_AVAILABLE OFF CACHE BOOL "")
set(RUNTIMES_${target}_LIBCXX_USE_COMPILER_RT ON CACHE BOOL "")
set(RUNTIMES_${target}_LLVM_INCLUDE_TESTS OFF CACHE BOOL "")
set(RUNTIMES_${target}_LLVM_ENABLE_ASSERTIONS OFF CACHE BOOL "")
Expand Down Expand Up @@ -406,7 +407,7 @@ foreach(target riscv32-unknown-elf)
foreach(lang C;CXX;ASM)
# TODO: The preprocessor defines workaround various issues in libc and libc++ integration.
# These should be addressed and removed over time.
set(RUNTIMES_${target}_CMAKE_${lang}_FLAGS "--target=${target} -march=rv32imafc -mabi=ilp32f -Wno-atomic-alignment \"-Dvfprintf(stream, format, vlist)=vprintf(format, vlist)\" \"-Dfprintf(stream, format, ...)=printf(format)\" \"-Dfputs(string, stream)=puts(string)\" -D_LIBCPP_PRINT=1" CACHE STRING "")
set(RUNTIMES_${target}_CMAKE_${lang}_FLAGS "--target=${target} -march=rv32imafc -mabi=ilp32f -Wno-atomic-alignment" CACHE STRING "")
endforeach()
foreach(type SHARED;MODULE;EXE)
set(RUNTIMES_${target}_CMAKE_${type}_LINKER_FLAGS "-fuse-ld=lld" CACHE STRING "")
Expand All @@ -427,6 +428,7 @@ foreach(target riscv32-unknown-elf)
set(RUNTIMES_${target}_LIBCXX_ENABLE_RTTI OFF CACHE BOOL "")
set(RUNTIMES_${target}_LIBCXX_ENABLE_THREADS OFF CACHE BOOL "")
set(RUNTIMES_${target}_LIBCXX_ENABLE_MONOTONIC_CLOCK OFF CACHE BOOL "")
set(RUNTIMES_${target}_LIBCXX_HAS_TERMINAL_AVAILABLE OFF CACHE BOOL "")
set(RUNTIMES_${target}_LIBCXX_USE_COMPILER_RT ON CACHE BOOL "")
set(RUNTIMES_${target}_LLVM_INCLUDE_TESTS OFF CACHE BOOL "")
set(RUNTIMES_${target}_LLVM_ENABLE_ASSERTIONS OFF CACHE BOOL "")
Expand Down
8 changes: 8 additions & 0 deletions libc/config/baremetal/aarch64/entrypoints.txt
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,15 @@ set(TARGET_LIBC_ENTRYPOINTS

# stdio.h entrypoints
libc.src.stdio.asprintf
libc.src.stdio.feof
libc.src.stdio.ferror
libc.src.stdio.fprintf
libc.src.stdio.fputc
libc.src.stdio.fputs
libc.src.stdio.fwrite
libc.src.stdio.getchar
libc.src.stdio.printf
libc.src.stdio.putc
libc.src.stdio.putchar
libc.src.stdio.puts
libc.src.stdio.remove
Expand All @@ -134,6 +141,7 @@ set(TARGET_LIBC_ENTRYPOINTS
libc.src.stdio.sprintf
libc.src.stdio.sscanf
libc.src.stdio.vasprintf
libc.src.stdio.vfprintf
libc.src.stdio.vprintf
libc.src.stdio.vscanf
libc.src.stdio.vsnprintf
Expand Down
8 changes: 8 additions & 0 deletions libc/config/baremetal/arm/entrypoints.txt
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,15 @@ set(TARGET_LIBC_ENTRYPOINTS

# stdio.h entrypoints
libc.src.stdio.asprintf
libc.src.stdio.feof
libc.src.stdio.ferror
libc.src.stdio.fprintf
libc.src.stdio.fputc
libc.src.stdio.fputs
libc.src.stdio.fwrite
libc.src.stdio.getchar
libc.src.stdio.printf
libc.src.stdio.putc
libc.src.stdio.putchar
libc.src.stdio.puts
libc.src.stdio.remove
Expand All @@ -134,6 +141,7 @@ set(TARGET_LIBC_ENTRYPOINTS
libc.src.stdio.sprintf
libc.src.stdio.sscanf
libc.src.stdio.vasprintf
libc.src.stdio.vfprintf
libc.src.stdio.vprintf
libc.src.stdio.vscanf
libc.src.stdio.vsnprintf
Expand Down
8 changes: 8 additions & 0 deletions libc/config/baremetal/riscv/entrypoints.txt
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,15 @@ set(TARGET_LIBC_ENTRYPOINTS

# stdio.h entrypoints
libc.src.stdio.asprintf
libc.src.stdio.feof
libc.src.stdio.ferror
libc.src.stdio.fprintf
libc.src.stdio.fputc
libc.src.stdio.fputs
libc.src.stdio.fwrite
libc.src.stdio.getchar
libc.src.stdio.printf
libc.src.stdio.putc
libc.src.stdio.putchar
libc.src.stdio.puts
libc.src.stdio.remove
Expand All @@ -134,6 +141,7 @@ set(TARGET_LIBC_ENTRYPOINTS
libc.src.stdio.sprintf
libc.src.stdio.sscanf
libc.src.stdio.vasprintf
libc.src.stdio.vfprintf
libc.src.stdio.vprintf
libc.src.stdio.vscanf
libc.src.stdio.vsnprintf
Expand Down
42 changes: 4 additions & 38 deletions libc/src/__support/OSUtil/baremetal/io.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,49 +8,15 @@

#include "io.h"

#include "hdr/types/FILE.h"
#include "src/__support/CPP/string_view.h"
#include "src/__support/macros/config.h"

namespace LIBC_NAMESPACE_DECL {

// These are intended to be provided by the vendor.
//
// The signature of these types and functions intentionally match `fopencookie`
// which allows the following:
//
// ```
// struct __llvm_libc_stdio_cookie { ... };
// ...
// struct __llvm_libc_stdio_cookie __llvm_libc_stdin_cookie;
// cookie_io_functions_t stdin_func = { .read = __llvm_libc_stdio_read };
// FILE *stdin = fopencookie(&__llvm_libc_stdin_cookie, "r", stdin_func);
// ...
// struct __llvm_libc_stdio_cookie __llvm_libc_stdout_cookie;
// cookie_io_functions_t stdout_func = { .write = __llvm_libc_stdio_write };
// FILE *stdout = fopencookie(&__llvm_libc_stdout_cookie, "w", stdout_func);
// ...
// struct __llvm_libc_stdio_cookie __llvm_libc_stderr_cookie;
// cookie_io_functions_t stderr_func = { .write = __llvm_libc_stdio_write };
// FILE *stderr = fopencookie(&__llvm_libc_stderr_cookie, "w", stderr_func);
// ```
//
// At the same time, implementation of functions like `printf` and `scanf` can
// use `__llvm_libc_stdio_read` and `__llvm_libc_stdio_write` directly to avoid
// the extra indirection.
//
// All three symbols `__llvm_libc_stdin_cookie`, `__llvm_libc_stdout_cookie`,
// and `__llvm_libc_stderr_cookie` must be provided, even if they don't point
// at anything.

struct __llvm_libc_stdio_cookie;

extern "C" struct __llvm_libc_stdio_cookie __llvm_libc_stdin_cookie;
extern "C" struct __llvm_libc_stdio_cookie __llvm_libc_stdout_cookie;
extern "C" struct __llvm_libc_stdio_cookie __llvm_libc_stderr_cookie;

extern "C" ssize_t __llvm_libc_stdio_read(void *cookie, char *buf, size_t size);
extern "C" ssize_t __llvm_libc_stdio_write(void *cookie, const char *buf,
size_t size);
extern "C" FILE *stdin = reinterpret_cast<FILE *>(&__llvm_libc_stdin_cookie);
extern "C" FILE *stdout = reinterpret_cast<FILE *>(&__llvm_libc_stdout_cookie);
extern "C" FILE *stderr = reinterpret_cast<FILE *>(&__llvm_libc_stderr_cookie);

ssize_t read_from_stdin(char *buf, size_t size) {
return __llvm_libc_stdio_read(static_cast<void *>(&__llvm_libc_stdin_cookie),
Expand Down
39 changes: 39 additions & 0 deletions libc/src/__support/OSUtil/baremetal/io.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,45 @@

namespace LIBC_NAMESPACE_DECL {

// These are intended to be provided by the vendor.
//
// The signature of these types and functions intentionally match `fopencookie`
// which allows the following:
//
// ```
// struct __llvm_libc_stdio_cookie { ... };
// ...
// struct __llvm_libc_stdio_cookie __llvm_libc_stdin_cookie;
// cookie_io_functions_t stdin_func = { .read = __llvm_libc_stdio_read };
// FILE *stdin = fopencookie(&__llvm_libc_stdin_cookie, "r", stdin_func);
// ...
// struct __llvm_libc_stdio_cookie __llvm_libc_stdout_cookie;
// cookie_io_functions_t stdout_func = { .write = __llvm_libc_stdio_write };
// FILE *stdout = fopencookie(&__llvm_libc_stdout_cookie, "w", stdout_func);
// ...
// struct __llvm_libc_stdio_cookie __llvm_libc_stderr_cookie;
// cookie_io_functions_t stderr_func = { .write = __llvm_libc_stdio_write };
// FILE *stderr = fopencookie(&__llvm_libc_stderr_cookie, "w", stderr_func);
// ```
//
// At the same time, implementation of functions like `printf` and `scanf` can
// use `__llvm_libc_stdio_read` and `__llvm_libc_stdio_write` directly to avoid
// the extra indirection.
//
// All three symbols `__llvm_libc_stdin_cookie`, `__llvm_libc_stdout_cookie`,
// and `__llvm_libc_stderr_cookie` must be provided, even if they don't point
// at anything.

struct __llvm_libc_stdio_cookie;

extern "C" struct __llvm_libc_stdio_cookie __llvm_libc_stdin_cookie;
extern "C" struct __llvm_libc_stdio_cookie __llvm_libc_stdout_cookie;
extern "C" struct __llvm_libc_stdio_cookie __llvm_libc_stderr_cookie;

extern "C" ssize_t __llvm_libc_stdio_read(void *cookie, char *buf, size_t size);
extern "C" ssize_t __llvm_libc_stdio_write(void *cookie, const char *buf,
size_t size);

ssize_t read_from_stdin(char *buf, size_t size);
void write_to_stderr(cpp::string_view msg);
void write_to_stdout(cpp::string_view msg);
Expand Down
151 changes: 133 additions & 18 deletions libc/src/stdio/baremetal/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,111 @@
add_header_library(
fputc_internal
HDRS
fputc_internal.h
DEPENDS
libc.hdr.types.FILE
libc.src.__support.CPP.string_view
libc.src.__support.OSUtil.osutil
)

add_header_library(
fputs_internal
HDRS
fputs_internal.h
DEPENDS
libc.hdr.types.FILE
libc.src.__support.CPP.string_view
libc.src.__support.OSUtil.osutil
)

add_header_library(
fwrite_internal
HDRS
fwrite_internal.h
DEPENDS
libc.hdr.types.FILE
libc.src.__support.OSUtil.osutil
)

add_header_library(
vfprintf_internal
HDRS
vfprintf_internal.h
DEPENDS
libc.hdr.types.FILE
libc.hdr.stdio_macros
libc.src.__support.arg_list
libc.src.__support.CPP.limits
libc.src.__support.CPP.string_view
libc.src.__support.libc_errno
libc.src.__support.OSUtil.osutil
libc.src.stdio.printf_core.printf_main
libc.src.stdio.printf_core.writer
libc.src.stdio.printf_core.error_mapper
libc.src.stdio.printf_core.core_structs
)

add_entrypoint_object(
feof
SRCS
feof.cpp
HDRS
../feof.h
DEPENDS
libc.hdr.types.FILE
)

add_entrypoint_object(
ferror
SRCS
ferror.cpp
HDRS
../ferror.h
DEPENDS
libc.hdr.types.FILE
)

add_entrypoint_object(
fprintf
SRCS
fprintf.cpp
HDRS
../fprintf.h
DEPENDS
.vfprintf_internal
libc.src.__support.arg_list
)

add_entrypoint_object(
fputc
SRCS
fputc.cpp
HDRS
../fputc.h
DEPENDS
.fputc_internal
)

add_entrypoint_object(
fputs
SRCS
fputs.cpp
HDRS
../fputc.h
DEPENDS
.fputs_internal
)

add_entrypoint_object(
fwrite
SRCS
fwrite.cpp
HDRS
../fwrite.h
DEPENDS
.fwrite_internal
)

add_entrypoint_object(
getchar
SRCS
Expand Down Expand Up @@ -27,14 +135,8 @@ add_entrypoint_object(
HDRS
../printf.h
DEPENDS
libc.src.stdio.printf_core.printf_main
libc.src.stdio.printf_core.writer
libc.src.stdio.printf_core.error_mapper
libc.src.stdio.printf_core.core_structs
.vfprintf_internal
libc.src.__support.arg_list
libc.src.__support.OSUtil.osutil
libc.src.__support.libc_errno
libc.src.__support.CPP.limits
)

add_entrypoint_object(
Expand All @@ -44,8 +146,17 @@ add_entrypoint_object(
HDRS
../putchar.h
DEPENDS
libc.src.__support.OSUtil.osutil
libc.src.__support.CPP.string_view
.fputc_internal
)

add_entrypoint_object(
putc
SRCS
putc.cpp
HDRS
../putc.h
DEPENDS
.fputc_internal
)

add_entrypoint_object(
Expand All @@ -55,8 +166,7 @@ add_entrypoint_object(
HDRS
../puts.h
DEPENDS
libc.src.__support.OSUtil.osutil
libc.src.__support.CPP.string_view
.fputs_internal
)

add_header_library(
Expand All @@ -82,21 +192,26 @@ add_entrypoint_object(
libc.src.__support.OSUtil.osutil
)

add_entrypoint_object(
vfprintf
SRCS
vfprintf.cpp
HDRS
../vfprintf.h
DEPENDS
.vfprintf_internal
libc.src.__support.arg_list
)

add_entrypoint_object(
vprintf
SRCS
vprintf.cpp
HDRS
../vprintf.h
DEPENDS
libc.src.stdio.printf_core.printf_main
libc.src.stdio.printf_core.writer
libc.src.stdio.printf_core.error_mapper
libc.src.stdio.printf_core.core_structs
.vfprintf_internal
libc.src.__support.arg_list
libc.src.__support.OSUtil.osutil
libc.src.__support.libc_errno
libc.src.__support.CPP.limits
)

add_entrypoint_object(
Expand Down
Loading
Loading