Skip to content

[libc] Add the <endian.h> header. #125168

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 4 commits into from
Feb 7, 2025
Merged

[libc] Add the <endian.h> header. #125168

merged 4 commits into from
Feb 7, 2025

Conversation

c8ef
Copy link
Contributor

@c8ef c8ef commented Jan 31, 2025

Closes #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.

@c8ef c8ef changed the title Draft [libc] Add the <endian.h> header. Jan 31, 2025
@c8ef c8ef marked this pull request as ready for review January 31, 2025 05:35
@llvmbot llvmbot added the libc label Jan 31, 2025
@llvmbot
Copy link
Member

llvmbot commented Jan 31, 2025

@llvm/pr-subscribers-libc

Author: None (c8ef)

Changes

Closes #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.


Full diff: https://github.com/llvm/llvm-project/pull/125168.diff

7 Files Affected:

  • (modified) libc/config/linux/aarch64/headers.txt (+1)
  • (modified) libc/config/linux/x86_64/headers.txt (+1)
  • (modified) libc/include/CMakeLists.txt (+9)
  • (added) libc/include/endian.h.def (+17)
  • (added) libc/include/endian.yaml (+9)
  • (modified) libc/include/llvm-libc-macros/CMakeLists.txt (+6)
  • (added) libc/include/llvm-libc-macros/endian-macros.h (+16)
diff --git a/libc/config/linux/aarch64/headers.txt b/libc/config/linux/aarch64/headers.txt
index 05f15a0e4e5cbb..7ff55899805508 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 8750100302ea7e..fe1fd596960dd3 100644
--- a/libc/config/linux/x86_64/headers.txt
+++ b/libc/config/linux/x86_64/headers.txt
@@ -5,6 +5,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/include/CMakeLists.txt b/libc/include/CMakeLists.txt
index e5ceea360d3965..96d715cde9de9d 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 00000000000000..8448d93a86acc3
--- /dev/null
+++ b/libc/include/endian.h.def
@@ -0,0 +1,17 @@
+//===-- System V 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 00000000000000..6497f897b63933
--- /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 9d5d9f65442889..d165ada6612c84 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 00000000000000..94e1d60f8ff404
--- /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

@c8ef
Copy link
Contributor Author

c8ef commented Feb 4, 2025

Friendly ping!

Copy link
Member

@nickdesaulniers nickdesaulniers left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While you're here, do you mind wiring up docgen so we generate documentation for this newly added header? d398c0c is a good example.

@c8ef
Copy link
Contributor Author

c8ef commented Feb 5, 2025

While you're here, do you mind wiring up docgen so we generate documentation for this newly added header? d398c0c is a good example.

Thank you for your guidance! Done.

@c8ef c8ef requested a review from nickdesaulniers February 5, 2025 14:59
in-latest-posix: ''
BYTE_ORDER:
in-latest-posix: ''

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you mind stubbing out the rest of the functions listed at https://pubs.opengroup.org/onlinepubs/9799919799/basedefs/endian.h.html? Otherwise, we will render a doc where it looks like our implementation of endian.h is complete; it's not, since we don't have the functions defined. That way, folks know what to expect, and can find more places to contribute.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

I added these as macros because glibc, musl, and FreeBSD all implement them as macros. According to the POSIX standard, they can be either functions or macros.

I would like to implement these in following patches : )

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

SGTM; thanks! I wonder what happens if someone needs to link against them as strong symbols...

@c8ef c8ef requested a review from nickdesaulniers February 6, 2025 14:57
@c8ef c8ef merged commit 6807164 into llvm:main Feb 7, 2025
15 checks passed
@c8ef c8ef deleted the endian branch February 7, 2025 01:20
Icohedron pushed a commit to Icohedron/llvm-project that referenced this pull request Feb 11, 2025
Closes [llvm#124631](llvm#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.
c8ef added a commit that referenced this pull request Feb 12, 2025
Follow up of #125168.

This patch adds endian-related macros to `endian.h`. We utilize compiler
built-ins for byte swap functions, which are already included in our
minimal supported compiler version.
flovent pushed a commit to flovent/llvm-project that referenced this pull request Feb 13, 2025
Follow up of llvm#125168.

This patch adds endian-related macros to `endian.h`. We utilize compiler
built-ins for byte swap functions, which are already included in our
minimal supported compiler version.
joaosaffran pushed a commit to joaosaffran/llvm-project that referenced this pull request Feb 14, 2025
Follow up of llvm#125168.

This patch adds endian-related macros to `endian.h`. We utilize compiler
built-ins for byte swap functions, which are already included in our
minimal supported compiler version.
sivan-shani pushed a commit to sivan-shani/llvm-project that referenced this pull request Feb 24, 2025
Follow up of llvm#125168.

This patch adds endian-related macros to `endian.h`. We utilize compiler
built-ins for byte swap functions, which are already included in our
minimal supported compiler version.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[libc] implement endian.h
4 participants