Skip to content

Commit b8bbc57

Browse files
authored
[libc] Use -nostdlibinc in the full build mode (#97461)
This avoids accidentally including system headers.
1 parent 139508e commit b8bbc57

File tree

2 files changed

+11
-5
lines changed

2 files changed

+11
-5
lines changed

libc/cmake/modules/CheckCompilerFeatures.cmake

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,3 +73,6 @@ check_cxx_compiler_flag("-ftrivial-auto-var-init=pattern" LIBC_CC_SUPPORTS_PATTE
7373

7474
# clang-6+, gcc-13+
7575
check_cxx_compiler_flag("-nostdlib++" LIBC_CC_SUPPORTS_NOSTDLIBPP)
76+
77+
# clang-3.0+
78+
check_cxx_compiler_flag("-nostdlibinc" LIBC_CC_SUPPORTS_NOSTDLIBINC)

libc/cmake/modules/LLVMLibCCompileOptionRules.cmake

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,14 @@ function(_get_common_compile_options output_var flags)
4646
list(APPEND compile_options "-DLIBC_FULL_BUILD")
4747
# Only add -ffreestanding flag in full build mode.
4848
list(APPEND compile_options "-ffreestanding")
49+
# Manually disable standard include paths to prevent system headers from
50+
# being included.
51+
if(LIBC_CC_SUPPORTS_NOSTDLIBINC)
52+
list(APPEND compile_options "-nostdlibinc")
53+
else()
54+
list(APPEND compile_options "-isystem${COMPILER_RESOURCE_DIR}/include")
55+
list(APPEND compile_options "-nostdinc")
56+
endif()
4957
endif()
5058

5159
if(LIBC_COMPILER_HAS_FIXED_POINT)
@@ -108,11 +116,6 @@ function(_get_common_compile_options output_var flags)
108116
elseif(LIBC_TARGET_ARCHITECTURE_IS_AMDGPU)
109117
list(APPEND compile_options "SHELL:-Xclang -mcode-object-version=none")
110118
endif()
111-
112-
# Manually disable all standard include paths and include the resource
113-
# directory to prevent system headers from being included.
114-
list(APPEND compile_options "-isystem${COMPILER_RESOURCE_DIR}/include")
115-
list(APPEND compile_options "-nostdinc")
116119
endif()
117120
set(${output_var} ${compile_options} PARENT_SCOPE)
118121
endfunction()

0 commit comments

Comments
 (0)