[libc-commits] [libc] ead92ae - [libc] Prevent system headers from being included for the GPU build

Joseph Huber via libc-commits libc-commits at lists.llvm.org
Tue Sep 5 06:43:31 PDT 2023


Author: Joseph Huber
Date: 2023-09-05T08:43:07-05:00
New Revision: ead92ae5fe8b6c281506d83acdbde550eeeaeb62

URL: https://github.com/llvm/llvm-project/commit/ead92ae5fe8b6c281506d83acdbde550eeeaeb62
DIFF: https://github.com/llvm/llvm-project/commit/ead92ae5fe8b6c281506d83acdbde550eeeaeb62.diff

LOG: [libc] Prevent system headers from being included for the GPU build

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`.

Differential Revision: https://reviews.llvm.org/D159445

Added: 
    

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

Removed: 
    


################################################################################
diff  --git a/libc/CMakeLists.txt b/libc/CMakeLists.txt
index 0a0e0060f2a9e0..c885080cc33e9a 100644
--- a/libc/CMakeLists.txt
+++ b/libc/CMakeLists.txt
@@ -71,9 +71,13 @@ if(COMMAND_RETURN_CODE EQUAL 0)
   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)

diff  --git a/libc/cmake/modules/LLVMLibCObjectRules.cmake b/libc/cmake/modules/LLVMLibCObjectRules.cmake
index fa450dade92ffe..c5862a829be5f9 100644
--- a/libc/cmake/modules/LLVMLibCObjectRules.cmake
+++ b/libc/cmake/modules/LLVMLibCObjectRules.cmake
@@ -68,6 +68,11 @@ function(_get_common_compile_options output_var flags)
   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()


        


More information about the libc-commits mailing list