Skip to content

Commit 456a4f5

Browse files
[libc][cmake][linux] require new LLVM_LIBC_USE_HOST_KERNEL_HEADERS or LIBC_KERNEL_HEADERS
When cross compiling, we DO NOT want to use the host's kernel headers. Adding all of /usr/include via `-dirafter` also isn't very hermetic since it can pull in more than just kernel headers. But peeking at /usr/include simplifies setting up the libc, since then the kernel headers don't have to be built from source. (Building kernel headers from source is quite trivial though). We already support setting -DLIBC_KERNEL_HEADERS=/path/to/kernel/headers. Add a new boolean cmake flag, LLVM_LIBC_USE_HOST_KERNEL_HEADERS, which indicates that the user is opting into just using the hosts kernel headers. Existing host builds may break and need to set this. Setting LIBC_KERNEL_HEADERS currently produces a few unit test failures that I still need to debug, but ideally users targeting linux will always pass LIBC_KERNEL_HEADERS in the future. Add some checks to ensure that at least one of these flags are set, that they're mutually exclusive, that the headers are required when cross compiling, and that the headers path is a directory.
1 parent 953354c commit 456a4f5

File tree

3 files changed

+27
-1
lines changed

3 files changed

+27
-1
lines changed

libc/CMakeLists.txt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,11 @@ set(LIBC_BUILD_DIR ${CMAKE_CURRENT_BINARY_DIR})
4646

4747
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")
4848

49-
set(LIBC_KERNEL_HEADERS "/usr/include" CACHE STRING "Path to Linux kernel headers")
49+
# LLVM_LIBC_USE_HOST_KERNEL_HEADERS is brittle, and frowned upon (prefer
50+
# setting LIBC_KERNEL_HEADERS), but is simpler from a hello world linux host
51+
# build.
52+
set(LLVM_LIBC_USE_HOST_KERNEL_HEADERS OFF CACHE BOOL "Add /usr/include to the header seach path")
53+
set(LIBC_KERNEL_HEADERS "" CACHE STRING "Path to Linux kernel headers")
5054

5155
# Defining a global namespace to enclose all libc functions.
5256
set(default_namespace "__llvm_libc")

libc/cmake/modules/LLVMLibCArchitectures.cmake

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,27 @@ if (LIBC_TARGET_OS_IS_WINDOWS AND LLVM_LIBC_FULL_BUILD)
216216
message(FATAL_ERROR "Windows does not support full mode build.")
217217
endif ()
218218

219+
if (LIBC_TARGET_OS_IS_LINUX)
220+
if (NOT LLVM_LIBC_USE_HOST_KERNEL_HEADERS AND NOT LIBC_KERNEL_HEADERS STREQUAL "")
221+
message(FATAL_ERROR "MUST specify either LIBC_KERNEL_HEADERS or LLVM_LIBC_USE_HOST_KERNEL_HEADERS")
222+
endif()
223+
if (LLVM_LIBC_USE_HOST_KERNEL_HEADERS AND NOT LIBC_KERNEL_HEADERS STREQUAL "")
224+
message(FATAL_ERROR "LLVM_LIBC_USE_HOST_KERNEL_HEADERS and LIBC_USE_HOST_KERNEL_HEADERS are mutually exclusive")
225+
endif()
226+
if(LIBC_TARGET_TRIPLE AND LLVM_LIBC_USE_HOST_KERNEL_HEADERS)
227+
# This is because the syscall numbers are frequently different between
228+
# different target architectures. Code may compile, but you'll get spooky
229+
# runtime failures.
230+
message(FATAL_ERROR "LLVM_LIBC_USE_HOST_KERNEL_HEADERS should not be set when using LIBC_TARGET_TRIPLE, set LIBC_KERNEL_HEADERS instead")
231+
endif()
232+
if (NOT LIBC_KERNEL_HEADERS STREQUAL "" AND NOT IS_DIRECTORY LIBC_KERNEL_HEADERS)
233+
message(FATAL_ERROR "LIBC_KERNEL_HEADERS should be a path to an existing directory")
234+
endif()
235+
if (LLVM_LIBC_USE_HOST_KERNEL_HEADERS)
236+
set(LIBC_KERNEL_HEADERS "/usr/include" CACHE STRING "Path to Linux kernel headers" FORCE)
237+
endif()
238+
endif()
239+
219240

220241
message(STATUS
221242
"Building libc for ${LIBC_TARGET_ARCHITECTURE} on ${LIBC_TARGET_OS} with

libc/docs/getting_started.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ Install dependencies first:
2121
-DCMAKE_CXX_COMPILER=clang++ \
2222
-DCMAKE_C_COMPILER=clang \
2323
-DLLVM_LIBC_FULL_BUILD=ON \
24+
-DLLVM_LIBC_USE_HOST_KERNEL_HEADERS=ON \
2425
-DLLVM_LIBC_INCLUDE_SCUDO=ON \
2526
-DCOMPILER_RT_BUILD_SCUDO_STANDALONE_WITH_LLVM_LIBC=ON \
2627
-DCOMPILER_RT_BUILD_GWP_ASAN=OFF \

0 commit comments

Comments
 (0)