[libc-commits] [PATCH] D159445: [libc] Prevent system headers from being included for the GPU build

Joseph Huber via Phabricator via libc-commits libc-commits at lists.llvm.org
Tue Sep 5 06:17:23 PDT 2023


jhuber6 created this revision.
jhuber6 added reviewers: JonChesterfield, sivachandra, lntue, michaelrj.
Herald added projects: libc-project, All.
Herald added a subscriber: libc-commits.
jhuber6 requested review of this revision.

It's very important that the GPU build does not include any system
directories. We currently use `-ffreestanding` to disable a lot of these
features, but we can still accidentally include them if they are not
provided by `libc` yet. This patch changes this to use `-nostdinc` to
disable all standard search paths. Then we use the compiler's resource
directory to pick up the provided headers like `stdint.h`.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D159445

Files:
  libc/CMakeLists.txt
  libc/cmake/modules/LLVMLibCObjectRules.cmake


Index: libc/cmake/modules/LLVMLibCObjectRules.cmake
===================================================================
--- libc/cmake/modules/LLVMLibCObjectRules.cmake
+++ libc/cmake/modules/LLVMLibCObjectRules.cmake
@@ -68,6 +68,11 @@
   if (LIBC_TARGET_ARCHITECTURE_IS_GPU)
     list(APPEND compile_options "-nogpulib")
     list(APPEND compile_options "-fvisibility=hidden")
+
+    # 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()
Index: libc/CMakeLists.txt
===================================================================
--- libc/CMakeLists.txt
+++ libc/CMakeLists.txt
@@ -71,9 +71,13 @@
   message(STATUS "Set COMPILER_RESOURCE_DIR to "
                  "${COMPILER_RESOURCE_DIR} using --print-resource-dir")
 else()
-  set(COMPILER_RESOURCE_DIR OFF)
-  message(STATUS "COMPILER_RESOURCE_DIR not set
-                  --print-resource-dir not supported by host compiler")
+  if (LIBC_TARGET_ARCHITECTURE_IS_GPU)
+    message(FATAL_ERROR "COMPILER_RESOURCE_DIR must be set for GPU builds")
+  else()
+    set(COMPILER_RESOURCE_DIR OFF)
+    message(STATUS "COMPILER_RESOURCE_DIR not set
+                    --print-resource-dir not supported by host compiler")
+  endif()
 endif()
 
 option(LLVM_LIBC_FULL_BUILD "Build and test LLVM libc as if it is the full libc" OFF)


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D159445.555854.patch
Type: text/x-patch
Size: 1571 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/libc-commits/attachments/20230905/54bd2676/attachment.bin>


More information about the libc-commits mailing list