Skip to content

Commit ead92ae

Browse files
committed
[libc] Prevent system headers from being included for the GPU build
It's very important that the GPU build does not include any system directories. We currently use `-ffreestanding` to disable a lot of these features, but we can still accidentally include them if they are not provided by `libc` yet. This patch changes this to use `-nostdinc` to disable all standard search paths. Then we use the compiler's resource directory to pick up the provided headers like `stdint.h`. Differential Revision: https://reviews.llvm.org/D159445
1 parent 67fc0d3 commit ead92ae

File tree

2 files changed

+12
-3
lines changed

2 files changed

+12
-3
lines changed

libc/CMakeLists.txt

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,13 @@ if(COMMAND_RETURN_CODE EQUAL 0)
7171
message(STATUS "Set COMPILER_RESOURCE_DIR to "
7272
"${COMPILER_RESOURCE_DIR} using --print-resource-dir")
7373
else()
74-
set(COMPILER_RESOURCE_DIR OFF)
75-
message(STATUS "COMPILER_RESOURCE_DIR not set
76-
--print-resource-dir not supported by host compiler")
74+
if (LIBC_TARGET_ARCHITECTURE_IS_GPU)
75+
message(FATAL_ERROR "COMPILER_RESOURCE_DIR must be set for GPU builds")
76+
else()
77+
set(COMPILER_RESOURCE_DIR OFF)
78+
message(STATUS "COMPILER_RESOURCE_DIR not set
79+
--print-resource-dir not supported by host compiler")
80+
endif()
7781
endif()
7882

7983
option(LLVM_LIBC_FULL_BUILD "Build and test LLVM libc as if it is the full libc" OFF)

libc/cmake/modules/LLVMLibCObjectRules.cmake

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,11 @@ function(_get_common_compile_options output_var flags)
6868
if (LIBC_TARGET_ARCHITECTURE_IS_GPU)
6969
list(APPEND compile_options "-nogpulib")
7070
list(APPEND compile_options "-fvisibility=hidden")
71+
72+
# Manually disable all standard include paths and include the resource
73+
# directory to prevent system headers from being included.
74+
list(APPEND compile_options "-isystem${COMPILER_RESOURCE_DIR}/include")
75+
list(APPEND compile_options "-nostdinc")
7176
endif()
7277
set(${output_var} ${compile_options} PARENT_SCOPE)
7378
endfunction()

0 commit comments

Comments
 (0)