Skip to content

Conversation

@mikhailramalho
Copy link
Member

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.

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.
@llvmbot
Copy link
Member

llvmbot commented Jul 19, 2024

@llvm/pr-subscribers-libc

Author: Mikhail R. Gadelha (mikhailramalho)

Changes

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.


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

2 Files Affected:

  • (modified) libc/cmake/modules/LLVMLibCCompileOptionRules.cmake (+5-1)
  • (modified) libc/src/time/linux/nanosleep.cpp (+1-1)
diff --git a/libc/cmake/modules/LLVMLibCCompileOptionRules.cmake b/libc/cmake/modules/LLVMLibCCompileOptionRules.cmake
index 62e16d52cb3ea..a722fe709af5f 100644
--- a/libc/cmake/modules/LLVMLibCCompileOptionRules.cmake
+++ b/libc/cmake/modules/LLVMLibCCompileOptionRules.cmake
@@ -88,7 +88,11 @@ function(_get_common_compile_options output_var flags)
          (LIBC_CC_SUPPORTS_NOSTDLIBINC OR COMPILER_RESOURCE_DIR))
         # We use -idirafter to avoid preempting libc's own headers in case the
         # directory (e.g. /usr/include) contains other headers.
-        list(APPEND compile_options "-idirafter${LIBC_KERNEL_HEADERS}")
+        if(CMAKE_CROSSCOMPILING)
+          list(APPEND compile_options "-idirafter=${LIBC_KERNEL_HEADERS}")
+        else()
+          list(APPEND compile_options "-idirafter${LIBC_KERNEL_HEADERS}")
+        endif()
       endif()
     endif()
 
diff --git a/libc/src/time/linux/nanosleep.cpp b/libc/src/time/linux/nanosleep.cpp
index b267c3238b01f..7a856376ffb20 100644
--- a/libc/src/time/linux/nanosleep.cpp
+++ b/libc/src/time/linux/nanosleep.cpp
@@ -7,7 +7,7 @@
 //===----------------------------------------------------------------------===//
 
 #include "src/time/nanosleep.h"
-
+#include "hdr/time_macros.h"
 #include "src/__support/OSUtil/syscall.h" // For syscall functions.
 #include "src/__support/common.h"
 #include "src/__support/macros/config.h"


#include "src/time/nanosleep.h"

#include "hdr/time_macros.h"
Copy link
Member

Choose a reason for hiding this comment

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

This change seems unrelated, can you move it to a separate PR?

Copy link
Member Author

Choose a reason for hiding this comment

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

I forgot to mention it in the commit message, but once we fix the sysroot path, the compilation of the file fails in rv32 due to the missing definition of CLOCK_REALTIME.

I'll update the commit message when squashing and merging

@mikhailramalho mikhailramalho merged commit ac11430 into llvm:main Jul 19, 2024
@mikhailramalho mikhailramalho deleted the fix-rv32-syscalls branch July 19, 2024 15:20
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 when cross compiling, we should only look at the sysroot, and not /usr/include, right?

@mikhailramalho
Copy link
Member Author

So when cross compiling, we should only look at the sysroot, and not /usr/include, right?

IIUC, we need to add the $sysroot/usr/include explicitly because we add -nostdlibinc by default.

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