diff --git a/libc/CMakeLists.txt b/libc/CMakeLists.txt index 4ffcd55ba9500f2..013b17b03f57014 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 28379213029a3b6..6d38bb491044e57 100644 --- a/libc/cmake/modules/LLVMLibCCompileOptionRules.cmake +++ b/libc/cmake/modules/LLVMLibCCompileOptionRules.cmake @@ -51,9 +51,17 @@ 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() + # 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}") + endif() endif() if(LIBC_COMPILER_HAS_FIXED_POINT)