[libc-commits] [libc] 6807164 - [libc] Add the `<endian.h>` header. (#125168)

via libc-commits libc-commits at lists.llvm.org
Thu Feb 6 17:20:22 PST 2025


Author: c8ef
Date: 2025-02-07T09:20:18+08:00
New Revision: 6807164500e9920638e2ab0cdb4bf8321d24f8eb

URL: https://github.com/llvm/llvm-project/commit/6807164500e9920638e2ab0cdb4bf8321d24f8eb
DIFF: https://github.com/llvm/llvm-project/commit/6807164500e9920638e2ab0cdb4bf8321d24f8eb.diff

LOG: [libc] Add the `<endian.h>` header. (#125168)

Closes [#124631](https://github.com/llvm/llvm-project/issues/124631).
ref:
https://pubs.opengroup.org/onlinepubs/9799919799/basedefs/endian.h.html

This patch adds the implementation of `endian.h`, which includes the
header itself and three related macros. These macros in the header rely
on the compiler preprocessor, similar to how
https://github.com/llvm/llvm-project/blob/main/libc/src/__support/endian_internal.h
does. Hopefully this will meet the requirements for compiling llvm with
llvm-libc.

Added: 
    libc/include/endian.h.def
    libc/include/endian.yaml
    libc/include/llvm-libc-macros/endian-macros.h
    libc/utils/docgen/endian.yaml

Modified: 
    libc/config/linux/aarch64/headers.txt
    libc/config/linux/x86_64/headers.txt
    libc/docs/CMakeLists.txt
    libc/docs/headers/index.rst
    libc/include/CMakeLists.txt
    libc/include/llvm-libc-macros/CMakeLists.txt

Removed: 
    


################################################################################
diff  --git a/libc/config/linux/aarch64/headers.txt b/libc/config/linux/aarch64/headers.txt
index 32edfb2cd9413e0..54c65765abe4ab2 100644
--- a/libc/config/linux/aarch64/headers.txt
+++ b/libc/config/linux/aarch64/headers.txt
@@ -4,6 +4,7 @@ set(TARGET_PUBLIC_HEADERS
     libc.include.ctype
     libc.include.dlfcn
     libc.include.elf
+    libc.include.endian
     libc.include.errno
     libc.include.features
     libc.include.fenv

diff  --git a/libc/config/linux/x86_64/headers.txt b/libc/config/linux/x86_64/headers.txt
index 8d422698821bc1b..bd2fc3df75964b5 100644
--- a/libc/config/linux/x86_64/headers.txt
+++ b/libc/config/linux/x86_64/headers.txt
@@ -6,6 +6,7 @@ set(TARGET_PUBLIC_HEADERS
     libc.include.dirent
     libc.include.dlfcn
     libc.include.elf
+    libc.include.endian
     libc.include.errno
     libc.include.fcntl
     libc.include.features

diff  --git a/libc/docs/CMakeLists.txt b/libc/docs/CMakeLists.txt
index fc5e505c3be6915..97f27fe6a6e0c6b 100644
--- a/libc/docs/CMakeLists.txt
+++ b/libc/docs/CMakeLists.txt
@@ -39,6 +39,7 @@ if (SPHINX_FOUND)
       assert
       cpio
       ctype
+      endian
       errno
       fenv
       float

diff  --git a/libc/docs/headers/index.rst b/libc/docs/headers/index.rst
index bd48dd5989bcd0d..745b6f44750dbcf 100644
--- a/libc/docs/headers/index.rst
+++ b/libc/docs/headers/index.rst
@@ -10,6 +10,7 @@ Implementation Status
    complex
    cpio
    ctype
+   endian
    errno
    fenv
    float

diff  --git a/libc/include/CMakeLists.txt b/libc/include/CMakeLists.txt
index 624f83a4ec28340..63745542662d593 100644
--- a/libc/include/CMakeLists.txt
+++ b/libc/include/CMakeLists.txt
@@ -73,6 +73,15 @@ add_header_macro(
     .llvm_libc_common_h
 )
 
+add_header_macro(
+  endian
+  ../libc/include/endian.yaml
+  endian.h
+  DEPENDS
+    .llvm-libc-macros.endian_macros
+    .llvm_libc_common_h
+)
+
 add_header_macro(
   features
   ../libc/include/features.yaml

diff  --git a/libc/include/endian.h.def b/libc/include/endian.h.def
new file mode 100644
index 000000000000000..f331f08edc68964
--- /dev/null
+++ b/libc/include/endian.h.def
@@ -0,0 +1,17 @@
+//===-- POSIX header endian.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_ENDIAN_H
+#define LLVM_LIBC_ENDIAN_H
+
+#include "__llvm-libc-common.h"
+#include "llvm-libc-macros/endian-macros.h"
+
+%%public_api()
+
+#endif // LLVM_LIBC_ENDIAN_H

diff  --git a/libc/include/endian.yaml b/libc/include/endian.yaml
new file mode 100644
index 000000000000000..6497f897b63933a
--- /dev/null
+++ b/libc/include/endian.yaml
@@ -0,0 +1,9 @@
+header: endian.h
+header_template: endian.h.def
+standards:
+  - POSIX
+macros: []
+types: []
+enums: []
+objects: []
+functions: []

diff  --git a/libc/include/llvm-libc-macros/CMakeLists.txt b/libc/include/llvm-libc-macros/CMakeLists.txt
index 441b550543f67d8..ea892a87dbe7ab9 100644
--- a/libc/include/llvm-libc-macros/CMakeLists.txt
+++ b/libc/include/llvm-libc-macros/CMakeLists.txt
@@ -310,6 +310,12 @@ add_macro_header(
     elf-macros.h
 )
 
+add_macro_header(
+  endian_macros
+  HDR
+    endian-macros.h
+)
+
 add_macro_header(
   locale_macros
   HDR

diff  --git a/libc/include/llvm-libc-macros/endian-macros.h b/libc/include/llvm-libc-macros/endian-macros.h
new file mode 100644
index 000000000000000..94e1d60f8ff404f
--- /dev/null
+++ b/libc/include/llvm-libc-macros/endian-macros.h
@@ -0,0 +1,16 @@
+//===-- Definition of macros from endian.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_MACROS_ENDIAN_MACROS_H
+#define LLVM_LIBC_MACROS_ENDIAN_MACROS_H
+
+#define LITTLE_ENDIAN __ORDER_LITTLE_ENDIAN__
+#define BIG_ENDIAN __ORDER_BIG_ENDIAN__
+#define BYTE_ORDER __BYTE_ORDER__
+
+#endif // LLVM_LIBC_MACROS_ENDIAN_MACROS_H

diff  --git a/libc/utils/docgen/endian.yaml b/libc/utils/docgen/endian.yaml
new file mode 100644
index 000000000000000..f100a11260667be
--- /dev/null
+++ b/libc/utils/docgen/endian.yaml
@@ -0,0 +1,32 @@
+macros:
+  LITTLE_ENDIAN:
+    in-latest-posix: ''
+  BIG_ENDIAN:
+    in-latest-posix: ''
+  BYTE_ORDER:
+    in-latest-posix: ''
+  be16toh:
+    in-latest-posix: ''
+  be32toh:
+    in-latest-posix: ''
+  be64toh:
+    in-latest-posix: ''
+  htobe16:
+    in-latest-posix: ''
+  htobe32:
+    in-latest-posix: ''
+  htobe64:
+    in-latest-posix: ''
+  htole16:
+    in-latest-posix: ''
+  htole32:
+    in-latest-posix: ''
+  htole64:
+    in-latest-posix: ''
+  le16toh:
+    in-latest-posix: ''
+  le32toh:
+    in-latest-posix: ''
+  le64toh:
+    in-latest-posix: ''
+


        


More information about the libc-commits mailing list