[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:14:40 PDT 2024


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

>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 1/2] [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 <phosek at google.com>
Date: Tue, 2 Jul 2024 12:14:14 -0700
Subject: [PATCH 2/2] 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()



More information about the libc-commits mailing list