[libc-commits] [libc] [libc] Use -nostdlibinc in the full build mode (PR #97461)

Petr Hosek via libc-commits libc-commits at lists.llvm.org
Tue Jul 2 12:05:33 PDT 2024


https://github.com/petrhosek created https://github.com/llvm/llvm-project/pull/97461

This avoids accidentally including system headers.

>From 37e910cf3138d370da905cb79857dcd2cf77ca0b Mon Sep 17 00:00:00 2001
From: Petr Hosek <phosek at google.com>
Date: Tue, 2 Jul 2024 12:04:31 -0700
Subject: [PATCH] [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)



More information about the libc-commits mailing list