diff --git a/libc/src/errno/CMakeLists.txt b/libc/src/errno/CMakeLists.txt --- a/libc/src/errno/CMakeLists.txt +++ b/libc/src/errno/CMakeLists.txt @@ -1,17 +1,10 @@ -if (LLVM_LIBC_FULL_BUILD) add_object_library( errno SRCS - errno.cpp + libc_errno.cpp HDRS - llvmlibc_errno.h + libc_errno.h # Include this + llvmlibc_errno.h # DEPRECATED: Will be removed soon + DEPENDS + libc.include.errno ) -else() -add_object_library( - errno - SRCS - dummy_errno.cpp - HDRS - dummy_errno.h -) -endif() diff --git a/libc/src/errno/dummy_errno.h b/libc/src/errno/dummy_errno.h deleted file mode 100644 --- a/libc/src/errno/dummy_errno.h +++ /dev/null @@ -1,7 +0,0 @@ -//===-- An empty file to be used for mixed mode builds ----------*- 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 -// -//===----------------------------------------------------------------------===// diff --git a/libc/src/errno/dummy_errno.cpp b/libc/src/errno/dummy_errno.cpp deleted file mode 100644 --- a/libc/src/errno/dummy_errno.cpp +++ /dev/null @@ -1,7 +0,0 @@ -//===-- An empty file to be used for mixed mode builds --------------------===// -// -// 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 -// -//===----------------------------------------------------------------------===// diff --git a/libc/src/errno/errno.cpp b/libc/src/errno/errno.cpp deleted file mode 100644 --- a/libc/src/errno/errno.cpp +++ /dev/null @@ -1,9 +0,0 @@ -//===-- Implementation of __errno_location --------------------------------===// -// -// 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 -// -//===----------------------------------------------------------------------===// - -thread_local int __llvmlibc_errno = 0; diff --git a/libc/src/errno/libc_errno.h b/libc/src/errno/libc_errno.h new file mode 100644 --- /dev/null +++ b/libc/src/errno/libc_errno.h @@ -0,0 +1,37 @@ +//===-- Implementation header for 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_ERRNO_LLVMLIBC_ERRNO_H +#define LLVM_LIBC_SRC_ERRNO_LLVMLIBC_ERRNO_H + +#include + +// All of the libc runtime and test code should use the "libc_errno" macro. They +// should not refer to the "errno" macro directly. +#ifdef LIBC_COPT_PUBLIC_PACKAGING +// This macro will resolve to errno from the errno.h file included above. Under +// full build, this will be LLVM libc's errno. In overlay build, it will be +// system libc's errno. +#define libc_errno errno +#else +namespace __llvm_libc { + +extern "C" { +extern thread_local int __llvmlibc_internal_errno; +} // extern "C" + +// TODO: After all of libc/src and libc/test are switched over to use +// libc_errno, this header file will be "shipped" via an add_entrypoint_object +// target. At which point libc_errno, should point to __llvmlibc_internal_errno +// if LIBC_COPT_PUBLIC_PACKAGING is not defined. +#define libc_errno errno + +} // namespace __llvm_libc +#endif + +#endif // LLVM_LIBC_SRC_ERRNO_LLVMLIBC_ERRNO_H diff --git a/libc/src/errno/libc_errno.cpp b/libc/src/errno/libc_errno.cpp new file mode 100644 --- /dev/null +++ b/libc/src/errno/libc_errno.cpp @@ -0,0 +1,25 @@ +//===-- Implementation of errno -------------------------------------------===// +// +// 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 +// +//===----------------------------------------------------------------------===// + +namespace __llvm_libc { + +extern "C" { +// TODO: Declare __llvmlibc_errno only under LIBC_COPT_PUBLIC_PACKAGING and +// __llvmlibc_internal_errno otherwise. +// +// In overlay mode, this will be an unused thread local variable as libc_errno +// will resolve to errno from the system libc's errno.h. In full build mode +// however, libc_errno will resolve to this thread local variable via the errno +// macro defined in LLVM libc's public errno.h header file. +// TODO: Use a macro to distinguish full build and overlay build which can be +// used to exclude __llvmlibc_errno under overlay build. +thread_local int __llvmlibc_errno; +thread_local int __llvmlibc_internal_errno; +} // extern "C" + +} // namespace __llvm_libc diff --git a/libc/src/errno/llvmlibc_errno.h b/libc/src/errno/llvmlibc_errno.h --- a/libc/src/errno/llvmlibc_errno.h +++ b/libc/src/errno/llvmlibc_errno.h @@ -9,9 +9,13 @@ #ifndef LLVM_LIBC_SRC_ERRNO_LLVMLIBC_ERRNO_H #define LLVM_LIBC_SRC_ERRNO_LLVMLIBC_ERRNO_H -// Internal code should use this and not use the errno macro from the -// public header. -extern thread_local int __llvmlibc_errno; -#define llvmlibc_errno __llvmlibc_errno +#include + +// DEPRECATED: Use libc_errno from libc_errno.h instead. This macro is only +// present to facilitate gradual transition (as in, in multiple simple patches) +// to libc_errno. +// TODO: After all of libc/src and libc/test is switched over to use libc_errno, +// remove this macro and header file. +#define llvmlibc_errno errno #endif // LLVM_LIBC_SRC_ERRNO_LLVMLIBC_ERRNO_H diff --git a/libc/src/string/CMakeLists.txt b/libc/src/string/CMakeLists.txt --- a/libc/src/string/CMakeLists.txt +++ b/libc/src/string/CMakeLists.txt @@ -176,7 +176,6 @@ DEPENDS .memory_utils.memcpy_implementation .string_utils - libc.include.errno libc.include.stdlib libc.src.errno.errno ) diff --git a/libc/src/string/strdup.cpp b/libc/src/string/strdup.cpp --- a/libc/src/string/strdup.cpp +++ b/libc/src/string/strdup.cpp @@ -7,12 +7,12 @@ //===----------------------------------------------------------------------===// #include "src/string/strdup.h" +#include "src/errno/libc_errno.h" #include "src/string/allocating_string_utils.h" #include "src/string/memory_utils/memcpy_implementations.h" #include "src/__support/common.h" -#include #include namespace __llvm_libc { @@ -22,7 +22,7 @@ if (dup) return *dup; if (src != nullptr) - errno = ENOMEM; + libc_errno = ENOMEM; return nullptr; }