Skip to content

[libc][errno] Use macro instead of system header #91150

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 13, 2024
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
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions libc/hdr/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,16 @@ add_proxy_header_library(
libc.include.math
)

add_proxy_header_library(
errno_macros
HDRS
errno_macros.h
FULL_BUILD_DEPENDS
libc.include.errno
libc.include.llvm-libc-macros.error_number_macros
libc.include.llvm-libc-macros.generic_error_number_macros
)

add_proxy_header_library(
fcntl_macros
HDRS
Expand Down
26 changes: 26 additions & 0 deletions libc/hdr/errno_macros.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
//===-- Definition of macros from errno.h ---------------------------------===//
//
// 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_HDR_ERRNO_MACROS_H
#define LLVM_LIBC_HDR_ERRNO_MACROS_H

#ifdef LIBC_FULL_BUILD

#ifdef __linux__
#include "llvm-libc-macros/error-number-macros.h"
#else // __linux__
#include "llvm-libc-macros/generic-error-number-macros.h"
#endif

#else // Overlay mode

#include <errno.h>

#endif // LLVM_LIBC_FULL_BUILD

#endif // LLVM_LIBC_HDR_ERRNO_MACROS_H
20 changes: 1 addition & 19 deletions libc/include/errno.h.def
Original file line number Diff line number Diff line change
Expand Up @@ -15,29 +15,11 @@

#include <linux/errno.h>

#ifndef ERFKILL
#define ERFKILL 132
#endif // ERFKILL

#ifndef EOWNERDEAD
#define EOWNERDEAD 130
#endif // EOWNERDEAD

#ifndef EHWPOISON
#define EHWPOISON 133
#endif // EHWPOISON

#ifndef ECANCELED
#define ECANCELED 125
#endif // ECANCELED

#ifndef ENOTSUP
#define ENOTSUP EOPNOTSUPP
#endif // ENOTSUP

#ifndef ENOTRECOVERABLE
#define ENOTRECOVERABLE 131
#endif // ENOTRECOVERABLE
#include "llvm-libc-macros/linux/error-number-macros.h"

#else // __linux__
#include "llvm-libc-macros/generic-error-number-macros.h"
Expand Down
6 changes: 6 additions & 0 deletions libc/include/llvm-libc-macros/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,12 @@ add_macro_header(
assert-macros.h
)

add_macro_header(
error_number_macros
HDR
error-number-macros.h
)

add_macro_header(
generic_error_number_macros
HDR
Expand Down
8 changes: 8 additions & 0 deletions libc/include/llvm-libc-macros/error-number-macros.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#ifndef LLVM_LIBC_MACROS_ERROR_NUMBER_MACROS_H
#define LLVM_LIBC_MACROS_ERROR_NUMBER_MACROS_H

#ifdef __linux__
#include "linux/error-number-macros.h"
#endif

#endif // LLVM_LIBC_MACROS_ERROR_NUMBER_MACROS_H
2 changes: 2 additions & 0 deletions libc/include/llvm-libc-macros/generic-error-number-macros.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,5 +44,7 @@
#define EDOM 33
#define ERANGE 34
#define EILSEQ 35
#define ENAMETOOLONG 36
#define EOVERFLOW 75

#endif // LLVM_LIBC_MACROS_GENERIC_ERROR_NUMBER_MACROS_H
12 changes: 12 additions & 0 deletions libc/include/llvm-libc-macros/linux/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,15 @@
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/mips)
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/sparc)

add_header(
error_number_macros
HDR
error-number-macros.h
DEPENDS
.mips.error_number_macros
.sparc.error_number_macros
)

add_header(
fcntl_macros
HDR
Expand Down
32 changes: 32 additions & 0 deletions libc/include/llvm-libc-macros/linux/error-number-macros.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#ifndef LLVM_LIBC_MACROS_LINUX_ERROR_NUMBER_MACROS_H
#define LLVM_LIBC_MACROS_LINUX_ERROR_NUMBER_MACROS_H

#if defined(__mips__)
#include "mips/error-number-macros.h"

#elif defined(__sparc__)
#include "sparc/error-number-macros.h"

#else
#ifndef ECANCELED
#define ECANCELED 125
#endif // ECANCELED

#ifndef EOWNERDEAD
#define EOWNERDEAD 130
#endif // EOWNERDEAD

#ifndef ENOTRECOVERABLE
#define ENOTRECOVERABLE 131
#endif // ENOTRECOVERABLE

#ifndef ERFKILL
#define ERFKILL 132
#endif // ERFKILL

#ifndef EHWPOISON
#define EHWPOISON 133
#endif // EHWPOISON
#endif

#endif // LLVM_LIBC_MACROS_LINUX_ERROR_NUMBER_MACROS_H
5 changes: 5 additions & 0 deletions libc/include/llvm-libc-macros/linux/mips/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
add_header(
error_number_macros
HDR
error-number-macros.h
)
24 changes: 24 additions & 0 deletions libc/include/llvm-libc-macros/linux/mips/error-number-macros.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#ifndef LLVM_LIBC_MACROS_LINUX_MIPS_ERROR_NUMBER_MACROS_H
#define LLVM_LIBC_MACROS_LINUX_MIPS_ERROR_NUMBER_MACROS_H

#ifndef ECANCELED
#define ECANCELED 158
#endif // ECANCELED

#ifndef EOWNERDEAD
#define EOWNERDEAD 165
#endif // EOWNERDEAD

#ifndef ENOTRECOVERABLE
#define ENOTRECOVERABLE 166
#endif // ENOTRECOVERABLE

#ifndef ERFKILL
#define ERFKILL 167
#endif // ERFKILL

#ifndef EHWPOISON
#define EHWPOISON 168
#endif // EHWPOISON

#endif // LLVM_LIBC_MACROS_LINUX_MIPS_ERROR_NUMBER_MACROS_H
5 changes: 5 additions & 0 deletions libc/include/llvm-libc-macros/linux/sparc/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
add_header(
error_number_macros
HDR
error-number-macros.h
)
24 changes: 24 additions & 0 deletions libc/include/llvm-libc-macros/linux/sparc/error-number-macros.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#ifndef LLVM_LIBC_MACROS_LINUX_SPARC_ERROR_NUMBER_MACROS_H
#define LLVM_LIBC_MACROS_LINUX_SPARC_ERROR_NUMBER_MACROS_H

#ifndef ECANCELED
#define ECANCELED 127
#endif // ECANCELED

#ifndef EOWNERDEAD
#define EOWNERDEAD 132
#endif // EOWNERDEAD

#ifndef ENOTRECOVERABLE
#define ENOTRECOVERABLE 133
#endif // ENOTRECOVERABLE

#ifndef ERFKILL
#define ERFKILL 134
#endif // ERFKILL

#ifndef EHWPOISON
#define EHWPOISON 135
#endif // EHWPOISON

#endif // LLVM_LIBC_MACROS_LINUX_SPARC_ERROR_NUMBER_MACROS_H
2 changes: 1 addition & 1 deletion libc/src/errno/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,6 @@ add_entrypoint_object(
COMPILE_OPTIONS
${full_build_flag}
DEPENDS
libc.include.errno
libc.hdr.errno_macros
libc.src.__support.common
)
2 changes: 1 addition & 1 deletion libc/src/errno/libc_errno.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ LIBC_NAMESPACE::Errno::operator int() { return __llvmlibc_errno; }

#else
// In overlay mode, we simply use the system errno.
#include <errno.h>
#include "hdr/errno_macros.h"

void LIBC_NAMESPACE::Errno::operator=(int a) { errno = a; }
LIBC_NAMESPACE::Errno::operator int() { return errno; }
Expand Down
6 changes: 1 addition & 5 deletions libc/src/errno/libc_errno.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,7 @@
#include "src/__support/macros/attributes.h"
#include "src/__support/macros/properties/architectures.h"

// TODO: https://github.com/llvm/llvm-project/issues/80172
// Separate just the definition of errno numbers in
// include/llvm-libc-macros/* and only include that instead of the system
// <errno.h>.
#include <errno.h>
#include "hdr/errno_macros.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
Expand Down
Loading