[libc] Simplify generation of errno.h.
authorSiva Chandra Reddy <sivachandra@google.com>
Tue, 20 Dec 2022 08:45:50 +0000 (08:45 +0000)
committerSiva Chandra Reddy <sivachandra@google.com>
Tue, 20 Dec 2022 19:04:13 +0000 (19:04 +0000)
Reviewed By: lntue

Differential Revision: https://reviews.llvm.org/D140375

libc/config/linux/api.td
libc/config/linux/errno.h.in [deleted file]
libc/include/CMakeLists.txt
libc/include/errno.h.def
libc/include/llvm-libc-macros/CMakeLists.txt
libc/include/llvm-libc-macros/generic-error-number-macros.h [new file with mode: 0644]

index 76cc716..3afb925 100644 (file)
@@ -43,13 +43,6 @@ def NullMacro : MacroDef<"NULL"> {
   }];
 }
 
-def ErrnoMacro : MacroDef<"errno"> {
-  let Defn = [{
-    extern _Thread_local int __llvmlibc_errno;
-    #define errno __llvmlibc_errno
-  }];
-}
-
 def AssertAPI : PublicAPI<"assert.h"> {
   let Macros = [
     AssertMacro,
@@ -183,25 +176,6 @@ def TimeAPI : PublicAPI<"time.h"> {
   ];
 }
 
-def ErrnoAPI : PublicAPI<"errno.h"> {
-  let Macros = [
-    ErrnoMacro,
-    // We largely depend on linux/errno.h to give us the
-    // various error macro definitions. However, some libc
-    // implementations have chosen to provide definitions
-    // for some of the error macros to account for the ones
-    // missing in linux/errno.h. There is no harm in doing
-    // the same here if we define the macros only when they
-    // are not already defined.
-    MacroDefineIfNot<"ENOTSUP", "EOPNOTSUPP">,
-    MacroDefineIfNot<"ECANCELED", "125">,
-    MacroDefineIfNot<"EOWNERDEAD", "130">,
-    MacroDefineIfNot<"ENOTRECOVERABLE", "131">,
-    MacroDefineIfNot<"ERFKILL", "132">,
-    MacroDefineIfNot<"EHWPOISON", "133">,
-  ];
-}
-
 def SchedAPI : PublicAPI<"sched.h"> {
   let Types = ["pid_t", "size_t", "cpu_set_t"];
 }
diff --git a/libc/config/linux/errno.h.in b/libc/config/linux/errno.h.in
deleted file mode 100644 (file)
index 50bbefb..0000000
+++ /dev/null
@@ -1,11 +0,0 @@
-//===-- Linux specific errno.h definitions --------------------------------===//
-//
-// 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
-//
-//===----------------------------------------------------------------------===//
-
-%%begin()
-
-#include <linux/errno.h>
index 40adef0..91ef757 100644 (file)
@@ -128,11 +128,9 @@ add_gen_header(
 add_gen_header(
   errno
   DEF_FILE errno.h.def
-  PARAMS
-    platform_errno=../config/${LIBC_TARGET_OS}/errno.h.in
   GEN_HDR errno.h
-  DATA_FILES
-    ../config/${LIBC_TARGET_OS}/errno.h.in
+  DEPENDS
+    .llvm-libc-macros.generic_error_number_macros
 )
 
 add_gen_header(
index da60aed..580a53f 100644 (file)
 
 #include <__llvm-libc-common.h>
 
-%%include_file(${platform_errno})
+#ifdef __linux__
 
-%%public_api()
+#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
+
+#else // __linux__
+#include <llvm-libc-macros/generic-error-number-macros.h>
+#endif
+
+extern _Thread_local int __llvmlibc_errno;
+#define errno __llvmlibc_errno
 
 #endif // LLVM_LIBC_ERRNO_H
index fe2652a..1cab22a 100644 (file)
@@ -1,6 +1,12 @@
 add_subdirectory(linux)
 
 add_header(
+  generic_error_number_macros
+  HDR
+    generic-error-number-macros.h
+)
+
+add_header(
   fcntl_macros
   HDR
     fcntl-macros.h
diff --git a/libc/include/llvm-libc-macros/generic-error-number-macros.h b/libc/include/llvm-libc-macros/generic-error-number-macros.h
new file mode 100644 (file)
index 0000000..e652c60
--- /dev/null
@@ -0,0 +1,47 @@
+//===-- Definition of generic error number macros -------------------------===//
+//
+// 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_MACROS_GENERIC_ERROR_NUMBER_MACROS_H
+#define __LLVM_LIBC_MACROS_GENERIC_ERROR_NUMBER_MACROS_H
+
+#define EPERM 1
+#define ENOENT 2
+#define ESRCH 3
+#define EINTR 4
+#define EIO 5
+#define ENXIO 6
+#define E2BIG 7
+#define ENOEXEC 8
+#define EBADF 9
+#define ECHILD 10
+#define EAGAIN 11
+#define ENOMEM 12
+#define EACCES 13
+#define EFAULT 14
+#define ENOTBLK 15
+#define EBUSY 16
+#define EEXIST 17
+#define EXDEV 18
+#define ENODEV 19
+#define ENOTDIR 20
+#define EISDIR 21
+#define EINVAL 22
+#define ENFILE 23
+#define EMFILE 24
+#define ENOTTY 25
+#define ETXTBSY 26
+#define EFBIG 27
+#define ENOSPC 28
+#define ESPIPE 29
+#define EROFS 30
+#define EMLINK 31
+#define EPIPE 32
+#define EDOM 33
+#define ERANGE 34
+
+#endif // __LLVM_LIBC_MACROS_GENERIC_ERROR_NUMBER_MACROS_H