Skip to content

Commit a30233f

Browse files
authored
[libc] Fix standalone cross compiling build for the GPU (#84042)
Summary: This patch fixes some issues with building the GPU target manually without the runtimes bootstrapping build. This fixes the install directory and sets the default namespace to something more sensible if not set. Also I got rid of the check on `-mcpu=native`. it was a neat trick, but CMake in its INFINITE wisdom does not allow you to set link flags on the compiler flag check. So I just went with the old tool usage.
1 parent 433b711 commit a30233f

File tree

2 files changed

+24
-4
lines changed

2 files changed

+24
-4
lines changed

libc/CMakeLists.txt

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,11 @@ set(LIBC_BUILD_DIR ${CMAKE_CURRENT_BINARY_DIR})
3939
set(LIBC_ENABLE_USE_BY_CLANG OFF CACHE BOOL "Whether or not to place libc in a build directory findable by a just built clang")
4040

4141
# Defining a global namespace to enclose all libc functions.
42-
set(LIBC_NAMESPACE "__llvm_libc_${LLVM_VERSION_MAJOR}_${LLVM_VERSION_MINOR}_${LLVM_VERSION_PATCH}_${LLVM_VERSION_SUFFIX}"
42+
set(default_namespace "__llvm_libc")
43+
if(LLVM_VERSION_MAJOR)
44+
set(default_namespace "__llvm_libc_${LLVM_VERSION_MAJOR}_${LLVM_VERSION_MINOR}_${LLVM_VERSION_PATCH}_${LLVM_VERSION_SUFFIX}")
45+
endif()
46+
set(LIBC_NAMESPACE ${default_namespace}
4347
CACHE STRING "The namespace to use to enclose internal implementations. Must start with '__llvm_libc'."
4448
)
4549

@@ -232,7 +236,13 @@ else()
232236
set(LIBC_LIBRARY_DIR ${CMAKE_BINARY_DIR}/lib${LLVM_LIBDIR_SUFFIX})
233237
endif()
234238
if(LIBC_TARGET_OS_IS_GPU)
235-
set(LIBC_INSTALL_INCLUDE_DIR ${CMAKE_INSTALL_INCLUDEDIR}/${LLVM_DEFAULT_TARGET_TRIPLE})
239+
if(LLVM_RUNTIMES_TARGET)
240+
set(LIBC_INSTALL_INCLUDE_DIR ${CMAKE_INSTALL_INCLUDEDIR}/${LLVM_RUNTIMES_TARGET})
241+
elseif(LIBC_TARGET_TRIPLE)
242+
set(LIBC_INSTALL_INCLUDE_DIR ${CMAKE_INSTALL_INCLUDEDIR}/${LIBC_TARGET_TRIPLE})
243+
else()
244+
set(LIBC_INSTALL_INCLUDE_DIR ${CMAKE_INSTALL_INCLUDEDIR}/${LLVM_DEFAULT_TARGET_TRIPLE})
245+
endif()
236246
else()
237247
set(LIBC_INSTALL_INCLUDE_DIR ${CMAKE_INSTALL_INCLUDEDIR})
238248
endif()

libc/cmake/modules/prepare_libc_gpu_build.cmake

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,20 @@ endif()
4848

4949
set(LIBC_GPU_TEST_ARCHITECTURE "" CACHE STRING "Architecture for the GPU tests")
5050
if(LIBC_TARGET_ARCHITECTURE_IS_AMDGPU)
51-
check_cxx_compiler_flag("-nogpulib -mcpu=native" PLATFORM_HAS_GPU)
51+
# Identify any locally installed NVIDIA GPUs on the system using 'nvptx-arch'.
52+
find_program(LIBC_AMDGPU_ARCH
53+
NAMES amdgpu-arch NO_DEFAULT_PATH
54+
PATHS ${LLVM_BINARY_DIR}/bin ${compiler_path})
55+
if(LIBC_AMDGPU_ARCH)
56+
execute_process(COMMAND ${LIBC_AMDGPU_ARCH}
57+
OUTPUT_VARIABLE arch_tool_output
58+
ERROR_QUIET OUTPUT_STRIP_TRAILING_WHITESPACE)
59+
if(arch_tool_output MATCHES "^gfx[0-9]+")
60+
set(PLATFORM_HAS_GPU TRUE)
61+
endif()
62+
endif()
5263
elseif(LIBC_TARGET_ARCHITECTURE_IS_NVPTX)
5364
# Identify any locally installed NVIDIA GPUs on the system using 'nvptx-arch'.
54-
# Using 'check_cxx_compiler_flag' does not work currently due to the link job.
5565
find_program(LIBC_NVPTX_ARCH
5666
NAMES nvptx-arch NO_DEFAULT_PATH
5767
PATHS ${LLVM_BINARY_DIR}/bin ${compiler_path})

0 commit comments

Comments
 (0)