From b8bbc57b68454fda9811fd956a1d2caa61d4d323 Mon Sep 17 00:00:00 2001 From: Petr Hosek Date: Tue, 2 Jul 2024 12:34:06 -0700 Subject: [libc] Use -nostdlibinc in the full build mode (#97461) This avoids accidentally including system headers. --- libc/cmake/modules/CheckCompilerFeatures.cmake | 3 +++ libc/cmake/modules/LLVMLibCCompileOptionRules.cmake | 13 ++++++++----- 2 files changed, 11 insertions(+), 5 deletions(-) (limited to 'libc') diff --git a/libc/cmake/modules/CheckCompilerFeatures.cmake b/libc/cmake/modules/CheckCompilerFeatures.cmake index 17806588..d84c07b 100644 --- a/libc/cmake/modules/CheckCompilerFeatures.cmake +++ b/libc/cmake/modules/CheckCompilerFeatures.cmake @@ -73,3 +73,6 @@ check_cxx_compiler_flag("-ftrivial-auto-var-init=pattern" LIBC_CC_SUPPORTS_PATTE # clang-6+, gcc-13+ check_cxx_compiler_flag("-nostdlib++" LIBC_CC_SUPPORTS_NOSTDLIBPP) + +# clang-3.0+ +check_cxx_compiler_flag("-nostdlibinc" LIBC_CC_SUPPORTS_NOSTDLIBINC) diff --git a/libc/cmake/modules/LLVMLibCCompileOptionRules.cmake b/libc/cmake/modules/LLVMLibCCompileOptionRules.cmake index 3bf4293..d283e96 100644 --- a/libc/cmake/modules/LLVMLibCCompileOptionRules.cmake +++ b/libc/cmake/modules/LLVMLibCCompileOptionRules.cmake @@ -46,6 +46,14 @@ function(_get_common_compile_options output_var flags) list(APPEND compile_options "-DLIBC_FULL_BUILD") # Only add -ffreestanding flag in full build mode. list(APPEND compile_options "-ffreestanding") + # Manually disable standard include paths to prevent system headers from + # being included. + if(LIBC_CC_SUPPORTS_NOSTDLIBINC) + list(APPEND compile_options "-nostdlibinc") + else() + list(APPEND compile_options "-isystem${COMPILER_RESOURCE_DIR}/include") + list(APPEND compile_options "-nostdinc") + endif() endif() if(LIBC_COMPILER_HAS_FIXED_POINT) @@ -108,11 +116,6 @@ function(_get_common_compile_options output_var flags) elseif(LIBC_TARGET_ARCHITECTURE_IS_AMDGPU) list(APPEND compile_options "SHELL:-Xclang -mcode-object-version=none") endif() - - # Manually disable all standard include paths and include the resource - # directory to prevent system headers from being included. - list(APPEND compile_options "-isystem${COMPILER_RESOURCE_DIR}/include") - list(APPEND compile_options "-nostdinc") endif() set(${output_var} ${compile_options} PARENT_SCOPE) endfunction() -- cgit v1.1