From 37e910cf3138d370da905cb79857dcd2cf77ca0b Mon Sep 17 00:00:00 2001 From: Petr Hosek Date: Tue, 2 Jul 2024 12:04:31 -0700 Subject: [PATCH 1/3] [libc] Use -nostdlibinc in the full build mode This avoids accidentally including system headers. --- libc/cmake/modules/CheckCompilerFeatures.cmake | 3 +++ libc/cmake/modules/LLVMLibCCompileOptionRules.cmake | 4 ++++ 2 files changed, 7 insertions(+) diff --git a/libc/cmake/modules/CheckCompilerFeatures.cmake b/libc/cmake/modules/CheckCompilerFeatures.cmake index 17806588550eb..52090d2ab4097 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_c_compiler_flag("-nostdlibinc" LIBC_C_SUPPORTS_NOSTDLIBINC) diff --git a/libc/cmake/modules/LLVMLibCCompileOptionRules.cmake b/libc/cmake/modules/LLVMLibCCompileOptionRules.cmake index 3bf429381d4af..441a816176e68 100644 --- a/libc/cmake/modules/LLVMLibCCompileOptionRules.cmake +++ b/libc/cmake/modules/LLVMLibCCompileOptionRules.cmake @@ -46,6 +46,10 @@ 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 all standard include paths. + if(LIBC_C_SUPPORTS_NOSTDLIBINC) + list(APPEND compile_options "-nostdlibinc") + endif() endif() if(LIBC_COMPILER_HAS_FIXED_POINT) From 15e118874c6b386770a2a31cfd5d9e5c3ed8ecd3 Mon Sep 17 00:00:00 2001 From: Petr Hosek Date: Tue, 2 Jul 2024 12:14:14 -0700 Subject: [PATCH 2/3] Use -nostdlibinc for the GPU build as well --- .../modules/LLVMLibCCompileOptionRules.cmake | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/libc/cmake/modules/LLVMLibCCompileOptionRules.cmake b/libc/cmake/modules/LLVMLibCCompileOptionRules.cmake index 441a816176e68..4c1bf143a63eb 100644 --- a/libc/cmake/modules/LLVMLibCCompileOptionRules.cmake +++ b/libc/cmake/modules/LLVMLibCCompileOptionRules.cmake @@ -46,9 +46,13 @@ 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 all standard include paths. + # Manually disable standard include paths to prevent system headers from + # being included. if(LIBC_C_SUPPORTS_NOSTDLIBINC) list(APPEND compile_options "-nostdlibinc") + else() + list(APPEND compile_options "-isystem${COMPILER_RESOURCE_DIR}/include") + list(APPEND compile_options "-nostdinc") endif() endif() @@ -113,10 +117,14 @@ function(_get_common_compile_options output_var flags) 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") + # Manually disable standard include paths to prevent system headers from + # being included. + if(LIBC_C_SUPPORTS_NOSTDLIBINC) + list(APPEND compile_options "-nostdlibinc") + else() + list(APPEND compile_options "-isystem${COMPILER_RESOURCE_DIR}/include") + list(APPEND compile_options "-nostdinc") + endif() endif() set(${output_var} ${compile_options} PARENT_SCOPE) endfunction() From 7a4fa32d1ad88de42cc3606e59e3d923c3914780 Mon Sep 17 00:00:00 2001 From: Petr Hosek Date: Tue, 2 Jul 2024 12:22:59 -0700 Subject: [PATCH 3/3] Address review feedback --- libc/cmake/modules/CheckCompilerFeatures.cmake | 2 +- libc/cmake/modules/LLVMLibCCompileOptionRules.cmake | 11 +---------- 2 files changed, 2 insertions(+), 11 deletions(-) diff --git a/libc/cmake/modules/CheckCompilerFeatures.cmake b/libc/cmake/modules/CheckCompilerFeatures.cmake index 52090d2ab4097..d84c07b35d2d7 100644 --- a/libc/cmake/modules/CheckCompilerFeatures.cmake +++ b/libc/cmake/modules/CheckCompilerFeatures.cmake @@ -75,4 +75,4 @@ check_cxx_compiler_flag("-ftrivial-auto-var-init=pattern" LIBC_CC_SUPPORTS_PATTE check_cxx_compiler_flag("-nostdlib++" LIBC_CC_SUPPORTS_NOSTDLIBPP) # clang-3.0+ -check_c_compiler_flag("-nostdlibinc" LIBC_C_SUPPORTS_NOSTDLIBINC) +check_cxx_compiler_flag("-nostdlibinc" LIBC_CC_SUPPORTS_NOSTDLIBINC) diff --git a/libc/cmake/modules/LLVMLibCCompileOptionRules.cmake b/libc/cmake/modules/LLVMLibCCompileOptionRules.cmake index 4c1bf143a63eb..d283e966bffcf 100644 --- a/libc/cmake/modules/LLVMLibCCompileOptionRules.cmake +++ b/libc/cmake/modules/LLVMLibCCompileOptionRules.cmake @@ -48,7 +48,7 @@ function(_get_common_compile_options output_var flags) list(APPEND compile_options "-ffreestanding") # Manually disable standard include paths to prevent system headers from # being included. - if(LIBC_C_SUPPORTS_NOSTDLIBINC) + if(LIBC_CC_SUPPORTS_NOSTDLIBINC) list(APPEND compile_options "-nostdlibinc") else() list(APPEND compile_options "-isystem${COMPILER_RESOURCE_DIR}/include") @@ -116,15 +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 standard include paths to prevent system headers from - # being included. - if(LIBC_C_SUPPORTS_NOSTDLIBINC) - list(APPEND compile_options "-nostdlibinc") - else() - list(APPEND compile_options "-isystem${COMPILER_RESOURCE_DIR}/include") - list(APPEND compile_options "-nostdinc") - endif() endif() set(${output_var} ${compile_options} PARENT_SCOPE) endfunction()