Skip to content
Merged
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
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
4 changes: 4 additions & 0 deletions libc/cmake/modules/LLVMLibCCompileOptionRules.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,10 @@ function(_get_compile_options_from_config output_var)
list(APPEND config_options "-DLIBC_MATH=${LIBC_CONF_MATH_OPTIMIZATIONS}")
endif()

if(LIBC_CONF_ERRNO_MODE)
set(APPEND config_options "-DLIBC_ERRNO_MODE=${LIBC_CONF_ERRNO_MODE}")
endif()

set(${output_var} ${config_options} PARENT_SCOPE)
endfunction(_get_compile_options_from_config)

Expand Down
2 changes: 1 addition & 1 deletion libc/config/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"errno": {
"LIBC_CONF_ERRNO_MODE": {
"value": "LIBC_ERRNO_MODE_DEFAULT",
"doc": "The implementation used for errno, acceptable values are LIBC_ERRNO_MODE_DEFAULT, LIBC_ERRNO_MODE_UNDEFINED, LIBC_ERRNO_MODE_THREAD_LOCAL, LIBC_ERRNO_MODE_SHARED, LIBC_ERRNO_MODE_EXTERNAL, and LIBC_ERRNO_MODE_SYSTEM."
"doc": "The implementation used for errno, acceptable values are LIBC_ERRNO_MODE_DEFAULT, LIBC_ERRNO_MODE_UNDEFINED, LIBC_ERRNO_MODE_THREAD_LOCAL, LIBC_ERRNO_MODE_SHARED, LIBC_ERRNO_MODE_EXTERNAL, LIBC_ERRNO_MODE_SYSTEM, and LIBC_ERRNO_MODE_SYSTEM_INLINE."
}
},
"printf": {
Expand Down
4 changes: 2 additions & 2 deletions libc/docs/dev/code_style.rst
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ test infrastructure itself can be affected. To avoid perturbing the unit test
infrastructure around the setting of ``errno``, the following rules are to be
followed:

#. A special macro named ``libc_errno`` defined in ``src/errno/libc_errno.h``
#. A special macro named ``libc_errno`` defined in ``src/__support/libc_errno.h``
should be used when setting ``errno`` from libc runtime code. For example,
code to set ``errno`` to ``EINVAL`` should be:

Expand All @@ -117,7 +117,7 @@ followed:
`ErrorOr <https://github.com/llvm/llvm-project/blob/main/libc/src/__support/error_or.h>`_
to return error values.

#. The header file ``src/errno/libc_errno.h`` is shipped as part of the target
#. The header file ``src/__support/libc_errno.h`` is shipped as part of the target
corresponding to the ``errno`` entrypoint ``libc.src.errno.errno``. We do
not in general allow dependencies between entrypoints. However, the ``errno``
entrypoint is the only exceptional entrypoint on which other entrypoints
Expand Down
1 change: 1 addition & 0 deletions libc/shared/fp_bits.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#ifndef LLVM_LIBC_SHARED_FP_BITS_H
#define LLVM_LIBC_SHARED_FP_BITS_H

#include "libc_common.h"
#include "src/__support/FPUtil/FPBits.h"

namespace LIBC_NAMESPACE_DECL {
Expand Down
26 changes: 26 additions & 0 deletions libc/shared/libc_common.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
//===-- Common defines for sharing LLVM libc with LLVM projects -*- C++ -*-===//
//
// 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
//
//===----------------------------------------------------------------------===//

#ifndef LLVM_LIBC_SHARED_LIBC_COMMON_H
#define LLVM_LIBC_SHARED_LIBC_COMMON_H

// Use system errno.
#ifdef LIBC_ERRNO_MODE
#if LIBC_ERRNO_MODE != LIBC_ERRNO_MODE_SYSTEM_INLINE
#error \
"LIBC_ERRNO_MODE was set to something different from LIBC_ERRNO_MODE_SYSTEM_INLINE."
#endif // LIBC_ERRNO_MODE != LIBC_ERRNO_MODE_SYSTEM_INLINE
#else
#define LIBC_ERRNO_MODE LIBC_ERRNO_MODE_SYSTEM_INLINE
#endif // LIBC_ERRNO_MODE

#ifndef LIBC_NAMESPACE
#define LIBC_NAMESPACE __llvm_libc
#endif // LIBC_NAMESPACE

#endif // LLVM_LIBC_SHARED_LIBC_COMMON_H
1 change: 1 addition & 0 deletions libc/shared/rpc_server.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#ifndef LLVM_LIBC_SHARED_RPC_SERVER_H
#define LLVM_LIBC_SHARED_RPC_SERVER_H

#include "libc_common.h"
#include "src/__support/RPC/rpc_server.h"

namespace LIBC_NAMESPACE_DECL {
Expand Down
1 change: 1 addition & 0 deletions libc/shared/str_to_float.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#ifndef LLVM_LIBC_SHARED_STR_TO_FLOAT_H
#define LLVM_LIBC_SHARED_STR_TO_FLOAT_H

#include "libc_common.h"
#include "src/__support/str_to_float.h"

namespace LIBC_NAMESPACE_DECL {
Expand Down
1 change: 1 addition & 0 deletions libc/shared/str_to_integer.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#ifndef LLVM_LIBC_SHARED_STR_TO_INTEGER_H
#define LLVM_LIBC_SHARED_STR_TO_INTEGER_H

#include "libc_common.h"
#include "src/__support/str_to_integer.h"

namespace LIBC_NAMESPACE_DECL {
Expand Down
9 changes: 9 additions & 0 deletions libc/src/__support/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
add_subdirectory(CPP)
add_subdirectory(macros)

add_header_library(
libc_errno
HDRS
libc_errno.h
DEPENDS
libc.hdr.errno_macros
libc.src.__support.macros.config
)

add_header_library(
block
HDRS
Expand Down
2 changes: 1 addition & 1 deletion libc/src/__support/FPUtil/FEnvImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@
#include "hdr/fenv_macros.h"
#include "hdr/math_macros.h"
#include "hdr/types/fenv_t.h"
#include "src/__support/libc_errno.h"
#include "src/__support/macros/attributes.h" // LIBC_INLINE
#include "src/__support/macros/config.h"
#include "src/__support/macros/properties/architectures.h"
#include "src/errno/libc_errno.h"

#if defined(LIBC_TARGET_ARCH_IS_AARCH64) && defined(__ARM_FP)
#if defined(__APPLE__)
Expand Down
2 changes: 1 addition & 1 deletion libc/src/__support/File/dir.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
#include "src/__support/CPP/mutex.h" // lock_guard
#include "src/__support/CPP/new.h"
#include "src/__support/error_or.h"
#include "src/__support/libc_errno.h" // For error macros
#include "src/__support/macros/config.h"
#include "src/errno/libc_errno.h" // For error macros

namespace LIBC_NAMESPACE_DECL {

Expand Down
2 changes: 1 addition & 1 deletion libc/src/__support/File/file.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
#include "hdr/types/off_t.h"
#include "src/__support/CPP/new.h"
#include "src/__support/CPP/span.h"
#include "src/__support/libc_errno.h" // For error macros
#include "src/__support/macros/config.h"
#include "src/errno/libc_errno.h" // For error macros

namespace LIBC_NAMESPACE_DECL {

Expand Down
2 changes: 1 addition & 1 deletion libc/src/__support/File/linux/file.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
#include "src/__support/File/linux/lseekImpl.h"
#include "src/__support/OSUtil/fcntl.h"
#include "src/__support/OSUtil/syscall.h" // For internal syscall function.
#include "src/__support/libc_errno.h" // For error macros
#include "src/__support/macros/config.h"
#include "src/errno/libc_errno.h" // For error macros

#include "hdr/fcntl_macros.h" // For mode_t and other flags to the open syscall
#include <sys/stat.h> // For S_IS*, S_IF*, and S_IR* flags.
Expand Down
2 changes: 1 addition & 1 deletion libc/src/__support/File/linux/lseekImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
#include "src/__support/OSUtil/syscall.h" // For internal syscall function.
#include "src/__support/common.h"
#include "src/__support/error_or.h"
#include "src/__support/libc_errno.h"
#include "src/__support/macros/config.h"
#include "src/errno/libc_errno.h"

#include <stdint.h> // For uint64_t.
#include <sys/syscall.h> // For syscall numbers.
Expand Down
2 changes: 1 addition & 1 deletion libc/src/__support/HashTable/randomness.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
#include "src/__support/macros/attributes.h"
#include "src/__support/macros/config.h"
#if defined(LIBC_HASHTABLE_USE_GETRANDOM)
#include "src/errno/libc_errno.h"
#include "src/__support/libc_errno.h"
#include "src/sys/random/getrandom.h"
#endif

Expand Down
2 changes: 1 addition & 1 deletion libc/src/__support/OSUtil/linux/fcntl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
#include "hdr/types/struct_flock64.h"
#include "src/__support/OSUtil/syscall.h" // For internal syscall function.
#include "src/__support/common.h"
#include "src/__support/libc_errno.h"
#include "src/__support/macros/config.h"
#include "src/errno/libc_errno.h"

#include <stdarg.h>
#include <sys/syscall.h> // For syscall numbers.
Expand Down
2 changes: 1 addition & 1 deletion libc/src/__support/OSUtil/linux/vdso.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@
#include "src/__support/CPP/array.h"
#include "src/__support/CPP/optional.h"
#include "src/__support/CPP/string_view.h"
#include "src/__support/libc_errno.h"
#include "src/__support/threads/callonce.h"
#include "src/__support/threads/linux/futex_word.h"
#include "src/errno/libc_errno.h"
#include "src/sys/auxv/getauxval.h"
#include <linux/auxvec.h>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
#define LLVM_LIBC_SRC___SUPPORT_STRINGUTIL_TABLES_LINUX_EXTENSION_ERRORS_H

#include "src/__support/StringUtil/message_mapper.h"
#include "src/__support/libc_errno.h"
#include "src/__support/macros/config.h"
#include "src/errno/libc_errno.h"

namespace LIBC_NAMESPACE_DECL {

Expand Down
108 changes: 108 additions & 0 deletions libc/src/__support/libc_errno.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
//===-- Implementation header for libc_errno --------------------*- C++ -*-===//
//
// 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
//
//===----------------------------------------------------------------------===//

#ifndef LLVM_LIBC_SRC___SUPPORT_LIBC_ERRNO_H
#define LLVM_LIBC_SRC___SUPPORT_LIBC_ERRNO_H

// This header is to be consumed by internal implementations, in which all of
// them should refer to `libc_errno` instead of using `errno` directly from
// <errno.h> header.

// Unit and hermetic tests should:
// - #include "src/__support/libc_errno.h"
// - NOT #include <errno.h>
// - Only use `libc_errno` in the code
// - Depend on libc.src.errno.errno

// Integration tests should:
// - NOT #include "src/__support/libc_errno.h"
// - #include <errno.h>
// - Use regular `errno` in the code
// - Still depend on libc.src.errno.errno

// libc uses a fallback default value, either system or thread local.
#define LIBC_ERRNO_MODE_DEFAULT 0
// libc never stores a value; `errno` macro uses get link-time failure.
#define LIBC_ERRNO_MODE_UNDEFINED 1
// libc maintains per-thread state (requires C++ `thread_local` support).
#define LIBC_ERRNO_MODE_THREAD_LOCAL 2
// libc maintains shared state used by all threads, contrary to standard C
// semantics unless always single-threaded; nothing prevents data races.
#define LIBC_ERRNO_MODE_SHARED 3
// libc doesn't maintain any internal state, instead the embedder must define
// `int *__llvm_libc_errno(void);` C function.
#define LIBC_ERRNO_MODE_EXTERNAL 4
// libc uses system `<errno.h>` `errno` macro directly in the overlay mode; in
// fullbuild mode, effectively the same as `LIBC_ERRNO_MODE_EXTERNAL`.
// In this mode, the public C++ symbol `LIBC_NAMESPACE::libc_errno ` is still
// exported and get redirected to the system `errno` inside its implementation.

// TODO: Investigate deprecating LIBC_ERRNO_MODE_SYSTEM in favor of
// LIBC_ERRNO_MODE_SYSTEM_INLINE.
// https://github.com/llvm/llvm-project/issues/143454
#define LIBC_ERRNO_MODE_SYSTEM 5
// In this mode, the libc_errno is simply a macro resolved to `errno` from the
// system header <errno.h>. There is no need to link against the
// `libc.src.errno.errno` object.
#define LIBC_ERRNO_MODE_SYSTEM_INLINE 6

#if !defined(LIBC_ERRNO_MODE) || LIBC_ERRNO_MODE == LIBC_ERRNO_MODE_DEFAULT
#undef LIBC_ERRNO_MODE
#if defined(LIBC_FULL_BUILD) || !defined(LIBC_COPT_PUBLIC_PACKAGING)
#define LIBC_ERRNO_MODE LIBC_ERRNO_MODE_THREAD_LOCAL
#else
#define LIBC_ERRNO_MODE LIBC_ERRNO_MODE_SYSTEM
#endif
#endif // LIBC_ERRNO_MODE

#if LIBC_ERRNO_MODE != LIBC_ERRNO_MODE_DEFAULT && \
LIBC_ERRNO_MODE != LIBC_ERRNO_MODE_UNDEFINED && \
LIBC_ERRNO_MODE != LIBC_ERRNO_MODE_THREAD_LOCAL && \
LIBC_ERRNO_MODE != LIBC_ERRNO_MODE_SHARED && \
LIBC_ERRNO_MODE != LIBC_ERRNO_MODE_EXTERNAL && \
LIBC_ERRNO_MODE != LIBC_ERRNO_MODE_SYSTEM && \
LIBC_ERRNO_MODE != LIBC_ERRNO_MODE_SYSTEM_INLINE
#error LIBC_ERRNO_MODE must be one of the following values: \
LIBC_ERRNO_MODE_DEFAULT, \
LIBC_ERRNO_MODE_UNDEFINED, \
LIBC_ERRNO_MODE_THREAD_LOCAL, \
LIBC_ERRNO_MODE_SHARED, \
LIBC_ERRNO_MODE_EXTERNAL, \
LIBC_ERRNO_MODE_SYSTEM, \
LIBC_ERRNO_MODE_SYSTEM_INLINE.
#endif

#if LIBC_ERRNO_MODE == LIBC_ERRNO_MODE_SYSTEM_INLINE

#include <errno.h>

#define libc_errno errno

#else // !LIBC_ERRNO_MODE_SYSTEM_INLINE

#include "hdr/errno_macros.h"
#include "src/__support/macros/config.h"

namespace LIBC_NAMESPACE_DECL {

extern "C" int *__llvm_libc_errno() noexcept;

struct Errno {
void operator=(int);
operator int();
};

extern Errno libc_errno;

} // namespace LIBC_NAMESPACE_DECL

using LIBC_NAMESPACE::libc_errno;

#endif // LIBC_ERRNO_MODE_SYSTEM_INLINE

#endif // LLVM_LIBC_SRC___SUPPORT_LIBC_ERRNO_H
2 changes: 1 addition & 1 deletion libc/src/__support/threads/linux/thread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@
#include "src/__support/OSUtil/syscall.h" // For syscall functions.
#include "src/__support/common.h"
#include "src/__support/error_or.h"
#include "src/__support/libc_errno.h" // For error macros
#include "src/__support/macros/config.h"
#include "src/__support/threads/linux/futex_utils.h" // For FutexWordType
#include "src/errno/libc_errno.h" // For error macros

#ifdef LIBC_TARGET_ARCH_IS_AARCH64
#include <arm_acle.h>
Expand Down
2 changes: 1 addition & 1 deletion libc/src/dirent/closedir.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@

#include "src/__support/File/dir.h"
#include "src/__support/common.h"
#include "src/__support/libc_errno.h"
#include "src/__support/macros/config.h"
#include "src/errno/libc_errno.h"

#include <dirent.h>

Expand Down
2 changes: 1 addition & 1 deletion libc/src/dirent/opendir.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@

#include "src/__support/File/dir.h"
#include "src/__support/common.h"
#include "src/__support/libc_errno.h"
#include "src/__support/macros/config.h"
#include "src/errno/libc_errno.h"

#include <dirent.h>

Expand Down
2 changes: 1 addition & 1 deletion libc/src/dirent/readdir.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@

#include "src/__support/File/dir.h"
#include "src/__support/common.h"
#include "src/__support/libc_errno.h"
#include "src/__support/macros/config.h"
#include "src/errno/libc_errno.h"

#include <dirent.h>

Expand Down
20 changes: 4 additions & 16 deletions libc/src/errno/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,28 +1,16 @@
# If we are in full build mode, we will provide the errno definition ourselves,
# and if we are in overlay mode, we will just re-use the system's errno.
# We are passing LIBC_FULL_BUILD flag in full build mode so that the
# implementation of libc_errno will know if we are in full build mode or not.

# TODO: Move LIBC_FULL_BUILD flag to _get_common_compile_options.
set(full_build_flag "")
if(LLVM_LIBC_FULL_BUILD)
set(full_build_flag "-DLIBC_FULL_BUILD")
endif()

if(LIBC_CONF_ERRNO_MODE)
set(errno_config_copts "-DLIBC_ERRNO_MODE=${LIBC_CONF_ERRNO_MODE}")
endif()

add_entrypoint_object(
errno
SRCS
libc_errno.cpp
HDRS
libc_errno.h # Include this
COMPILE_OPTIONS
${full_build_flag}
${errno_config_copts}
../__support/libc_errno.h
DEPENDS
libc.hdr.errno_macros
libc.src.__support.common
libc.src.__support.libc_errno
libc.src.__support.macros.attributes
libc.src.__support.macros.config
)
Loading
Loading