diff --git a/libc/cmake/modules/prepare_libc_gpu_build.cmake b/libc/cmake/modules/prepare_libc_gpu_build.cmake index 88538caaa3bc5b..dc8beb14fd7f44 100644 --- a/libc/cmake/modules/prepare_libc_gpu_build.cmake +++ b/libc/cmake/modules/prepare_libc_gpu_build.cmake @@ -18,20 +18,28 @@ endif() # Identify the program used to package multiple images into a single binary. get_filename_component(compiler_path ${CMAKE_CXX_COMPILER} DIRECTORY) -find_program(LIBC_CLANG_OFFLOAD_PACKAGER - NAMES clang-offload-packager NO_DEFAULT_PATH - PATHS ${LLVM_BINARY_DIR}/bin ${compiler_path}) -if(NOT LIBC_CLANG_OFFLOAD_PACKAGER) - message(FATAL_ERROR "Cannot find the 'clang-offload-packager' for the GPU " - "build") +if(TARGET clang-offload-packager) + get_target_property(LIBC_CLANG_OFFLOAD_PACKAGER clang-offload-packager LOCATION) +else() + find_program(LIBC_CLANG_OFFLOAD_PACKAGER + NAMES clang-offload-packager NO_DEFAULT_PATH + PATHS ${LLVM_BINARY_DIR}/bin ${compiler_path}) + if(NOT LIBC_CLANG_OFFLOAD_PACKAGER) + message(FATAL_ERROR "Cannot find the 'clang-offload-packager' for the GPU " + "build") + endif() endif() # Identify llvm-link program so we can merge the output IR into a single blob. -find_program(LIBC_LLVM_LINK - NAMES llvm-link NO_DEFAULT_PATH - PATHS ${LLVM_BINARY_DIR}/bin ${compiler_path}) -if(NOT LIBC_LLVM_LINK) - message(FATAL_ERROR "Cannot find 'llvm-link' for the GPU build") +if(TARGET llvm-link) + get_target_property(LIBC_LLVM_LINK llvm-link LOCATION) +else() + find_program(LIBC_LLVM_LINK + NAMES llvm-link NO_DEFAULT_PATH + PATHS ${LLVM_BINARY_DIR}/bin ${compiler_path}) + if(NOT LIBC_LLVM_LINK) + message(FATAL_ERROR "Cannot find 'llvm-link' for the GPU build") + endif() endif() # Optionally set up a job pool to limit the number of GPU tests run in parallel. @@ -48,10 +56,14 @@ endif() set(LIBC_GPU_TEST_ARCHITECTURE "" CACHE STRING "Architecture for the GPU tests") if(LIBC_TARGET_ARCHITECTURE_IS_AMDGPU) - # Identify any locally installed NVIDIA GPUs on the system using 'nvptx-arch'. - find_program(LIBC_AMDGPU_ARCH - NAMES amdgpu-arch NO_DEFAULT_PATH - PATHS ${LLVM_BINARY_DIR}/bin ${compiler_path}) + # Identify any locally installed AMD GPUs on the system using 'amdgpu-arch'. + if(TARGET amdgpu-arch) + get_target_property(LIBC_AMDGPU_ARCH amdgpu-arch LOCATION) + else() + find_program(LIBC_AMDGPU_ARCH + NAMES amdgpu-arch NO_DEFAULT_PATH + PATHS ${LLVM_BINARY_DIR}/bin ${compiler_path}) + endif() if(LIBC_AMDGPU_ARCH) execute_process(COMMAND ${LIBC_AMDGPU_ARCH} OUTPUT_VARIABLE arch_tool_output @@ -62,9 +74,13 @@ if(LIBC_TARGET_ARCHITECTURE_IS_AMDGPU) endif() elseif(LIBC_TARGET_ARCHITECTURE_IS_NVPTX) # Identify any locally installed NVIDIA GPUs on the system using 'nvptx-arch'. - find_program(LIBC_NVPTX_ARCH - NAMES nvptx-arch NO_DEFAULT_PATH - PATHS ${LLVM_BINARY_DIR}/bin ${compiler_path}) + if(TARGET nvptx-arch) + get_target_property(LIBC_NVPTX_ARCH nvptx-arch LOCATION) + else() + find_program(LIBC_NVPTX_ARCH + NAMES nvptx-arch NO_DEFAULT_PATH + PATHS ${LLVM_BINARY_DIR}/bin ${compiler_path}) + endif() if(LIBC_NVPTX_ARCH) execute_process(COMMAND ${LIBC_NVPTX_ARCH} OUTPUT_VARIABLE arch_tool_output