Skip to content
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

[libc] Include Linux kernel headers in the full build #97486

Merged
merged 3 commits into from
Jul 2, 2024

Conversation

petrhosek
Copy link
Member

When doing a full build for Linux, as of #97461 we no longer include system headers, but we need to include Linux kernel headers.

When doing a full build for Linux, as of llvm#97461 we no longer include
system headers, but we need to include Linux kernel headers.
@llvmbot
Copy link
Member

llvmbot commented Jul 2, 2024

@llvm/pr-subscribers-libc

Author: Petr Hosek (petrhosek)

Changes

When doing a full build for Linux, as of #97461 we no longer include system headers, but we need to include Linux kernel headers.


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

2 Files Affected:

  • (modified) libc/CMakeLists.txt (+2)
  • (modified) libc/cmake/modules/LLVMLibCCompileOptionRules.cmake (+3)
diff --git a/libc/CMakeLists.txt b/libc/CMakeLists.txt
index 4ffcd55ba9500..013b17b03f570 100644
--- a/libc/CMakeLists.txt
+++ b/libc/CMakeLists.txt
@@ -39,6 +39,8 @@ set(LIBC_BUILD_DIR ${CMAKE_CURRENT_BINARY_DIR})
 
 set(LIBC_ENABLE_USE_BY_CLANG OFF CACHE BOOL "Whether or not to place libc in a build directory findable by a just built clang")
 
+set(LIBC_KERNEL_HEADERS "/usr/include" CACHE STRING "Path to Linux kernel headers")
+
 # Defining a global namespace to enclose all libc functions.
 set(default_namespace "__llvm_libc")
 if(LLVM_VERSION_MAJOR)
diff --git a/libc/cmake/modules/LLVMLibCCompileOptionRules.cmake b/libc/cmake/modules/LLVMLibCCompileOptionRules.cmake
index 28379213029a3..d6a8764c3de16 100644
--- a/libc/cmake/modules/LLVMLibCCompileOptionRules.cmake
+++ b/libc/cmake/modules/LLVMLibCCompileOptionRules.cmake
@@ -54,6 +54,9 @@ function(_get_common_compile_options output_var flags)
         list(APPEND compile_options "-isystem${COMPILER_RESOURCE_DIR}/include")
         list(APPEND compile_options "-nostdinc")
       endif()
+      if(LIBC_TARGET_OS_IS_LINUX)
+        list(APPEND compile_options "-idirafter${LIBC_KERNEL_HEADERS}")
+      endif()
     endif()
 
     if(LIBC_COMPILER_HAS_FIXED_POINT)

@@ -54,6 +54,9 @@ function(_get_common_compile_options output_var flags)
list(APPEND compile_options "-isystem${COMPILER_RESOURCE_DIR}/include")
list(APPEND compile_options "-nostdinc")
endif()
if(LIBC_TARGET_OS_IS_LINUX)
list(APPEND compile_options "-idirafter${LIBC_KERNEL_HEADERS}")
Copy link
Contributor

Choose a reason for hiding this comment

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

It merits a comment about why we want to use -idirafter, which is in case it's a directory like /usr/include that might contain other headers that we don't want to risk preempting libc's own headers.

Copy link
Member Author

Choose a reason for hiding this comment

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

Done.

Copy link
Contributor

@michaelrj-google michaelrj-google left a comment

Choose a reason for hiding this comment

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

tested and works on my machine, LGTM

@petrhosek
Copy link
Member Author

@frobtech Should we add this flag this unconditionally or only when -nostdinc or -nostdlibcinc is set?

@michaelrj-google
Copy link
Contributor

I'm going to merge this to fix the buildbots for now. Feel free to create followup patches.

@michaelrj-google michaelrj-google merged commit a1c4926 into llvm:main Jul 2, 2024
4 of 5 checks passed
lravenclaw pushed a commit to lravenclaw/llvm-project that referenced this pull request Jul 3, 2024
When doing a full build for Linux, as of llvm#97461 we no longer include
system headers, but we need to include Linux kernel headers.
kbluck pushed a commit to kbluck/llvm-project that referenced this pull request Jul 6, 2024
When doing a full build for Linux, as of llvm#97461 we no longer include
system headers, but we need to include Linux kernel headers.
mikhailramalho added a commit to mikhailramalho/llvm-project that referenced this pull request Jul 19, 2024
When crosscompiling, we need to search for the linux kernel headers in
the sysroot but since llvm#97486 the linux kernel headers were always
searched in /usr/include.

This patch fixes this behaviour by prepending a '=' to where we search
for the kernel headers. As per the gcc/clang's documentation a '='
before the path is replaced by the sysroot.
mikhailramalho added a commit that referenced this pull request Jul 19, 2024
#99588)

When crosscompiling, we need to search for the linux kernel headers in the sysroot but since #97486 the linux kernel headers were always searched in /usr/include.

This patch fixes this behaviour by prepending a '=' to where we search for the kernel headers. As per the gcc/clang's documentation a '=' before the path is replaced by the sysroot.

This patch also includes a fix for rv32, that fails to compile due to a missing definition of CLOCK_REALTIME after this change.
sgundapa pushed a commit to sgundapa/upstream_effort that referenced this pull request Jul 23, 2024
llvm#99588)

When crosscompiling, we need to search for the linux kernel headers in the sysroot but since llvm#97486 the linux kernel headers were always searched in /usr/include.

This patch fixes this behaviour by prepending a '=' to where we search for the kernel headers. As per the gcc/clang's documentation a '=' before the path is replaced by the sysroot.

This patch also includes a fix for rv32, that fails to compile due to a missing definition of CLOCK_REALTIME after this change.
yuxuanchen1997 pushed a commit that referenced this pull request Jul 25, 2024
#99588)

Summary:
When crosscompiling, we need to search for the linux kernel headers in the sysroot but since #97486 the linux kernel headers were always searched in /usr/include.

This patch fixes this behaviour by prepending a '=' to where we search for the kernel headers. As per the gcc/clang's documentation a '=' before the path is replaced by the sysroot.

This patch also includes a fix for rv32, that fails to compile due to a missing definition of CLOCK_REALTIME after this change.

Test Plan: 

Reviewers: 

Subscribers: 

Tasks: 

Tags: 


Differential Revision: https://phabricator.intern.facebook.com/D60251638
@nickdesaulniers
Copy link
Member

So to be more hermetic, I'd like to get to the point where we encourage the user to build the kernel headers from source BEFORE building llvm-libc, then provide those headers.

That way we don't look in /usr/include AT ALL.

For example, I'm not sure that the kernel headers on the host are ok to use when cross compiling.

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.

5 participants