From 6edadbc47ad6307ec05e7159ad33b2a3a93b2d42 Mon Sep 17 00:00:00 2001 From: Petr Hosek Date: Tue, 2 Jul 2024 14:50:55 -0700 Subject: [PATCH 1/3] [libc] Include Linux kernel headers in the full build When doing a full build for Linux, as of #97461 we no longer include system headers, but we need to include Linux kernel headers. --- libc/CMakeLists.txt | 2 ++ libc/cmake/modules/LLVMLibCCompileOptionRules.cmake | 3 +++ 2 files changed, 5 insertions(+) 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) From db7380ae5aa5cf0373d55af1d53aae8e20071ad5 Mon Sep 17 00:00:00 2001 From: Petr Hosek Date: Tue, 2 Jul 2024 14:59:12 -0700 Subject: [PATCH 2/3] Add a comment explaining the use of -idirafter --- libc/cmake/modules/LLVMLibCCompileOptionRules.cmake | 2 ++ 1 file changed, 2 insertions(+) diff --git a/libc/cmake/modules/LLVMLibCCompileOptionRules.cmake b/libc/cmake/modules/LLVMLibCCompileOptionRules.cmake index d6a8764c3de16..dd5319be0f9d9 100644 --- a/libc/cmake/modules/LLVMLibCCompileOptionRules.cmake +++ b/libc/cmake/modules/LLVMLibCCompileOptionRules.cmake @@ -55,6 +55,8 @@ function(_get_common_compile_options output_var flags) list(APPEND compile_options "-nostdinc") endif() if(LIBC_TARGET_OS_IS_LINUX) + # 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}") endif() endif() From e78388ff0078439564d7dc4da88bab3f4e8d2e20 Mon Sep 17 00:00:00 2001 From: Petr Hosek Date: Tue, 2 Jul 2024 15:34:21 -0700 Subject: [PATCH 3/3] Add TODO for COMPILER_RESOURCE_DIR --- libc/cmake/modules/LLVMLibCCompileOptionRules.cmake | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/libc/cmake/modules/LLVMLibCCompileOptionRules.cmake b/libc/cmake/modules/LLVMLibCCompileOptionRules.cmake index dd5319be0f9d9..6d38bb491044e 100644 --- a/libc/cmake/modules/LLVMLibCCompileOptionRules.cmake +++ b/libc/cmake/modules/LLVMLibCCompileOptionRules.cmake @@ -51,10 +51,13 @@ function(_get_common_compile_options output_var flags) if(LIBC_CC_SUPPORTS_NOSTDLIBINC) list(APPEND compile_options "-nostdlibinc") elseif(COMPILER_RESOURCE_DIR) + # TODO: We should require COMPILER_RESOURCE_DIR to be set. list(APPEND compile_options "-isystem${COMPILER_RESOURCE_DIR}/include") list(APPEND compile_options "-nostdinc") endif() - if(LIBC_TARGET_OS_IS_LINUX) + # TODO: We should set this unconditionally on Linux. + if(LIBC_TARGET_OS_IS_LINUX AND + (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}")