Fix building with system GLOO (#140275)

Leverage existing FindGloo CMake module to locate system's library and headers. Add system's gloo headers to include path rather than the gloo from third party when USE_SYSTEM_GLOO is specified.

Fixes #140274

Pull Request resolved: https://github.com/pytorch/pytorch/pull/140275
Approved by: https://github.com/malfet
This commit is contained in:
Nathan Brown 2024-11-11 22:58:39 +00:00 committed by PyTorch MergeBot
parent b742d11b1c
commit a290c1d748

View File

@ -1181,20 +1181,23 @@ if(USE_GLOO)
endif()
set(GLOO_USE_CUDA_TOOLKIT ON CACHE BOOL "" FORCE)
add_subdirectory(${CMAKE_CURRENT_LIST_DIR}/../third_party/gloo)
# Here is a little bit hacky. We have to put PROJECT_BINARY_DIR in front
# of PROJECT_SOURCE_DIR with/without conda system. The reason is that
# gloo generates a new config.h in the binary diretory.
include_directories(BEFORE SYSTEM ${CMAKE_CURRENT_LIST_DIR}/../third_party/gloo)
include_directories(BEFORE SYSTEM ${PROJECT_BINARY_DIR}/third_party/gloo)
else()
add_library(gloo SHARED IMPORTED)
find_library(GLOO_LIBRARY gloo)
if(NOT GLOO_LIBRARY)
find_package(Gloo)
if(NOT Gloo_FOUND)
message(FATAL_ERROR "Cannot find gloo")
endif()
message("Found gloo: ${GLOO_LIBRARY}")
set_target_properties(gloo PROPERTIES IMPORTED_LOCATION ${GLOO_LIBRARY})
message("Found gloo: ${Gloo_LIBRARY}")
message("Found gloo include directories: ${Gloo_INCLUDE_DIRS}")
add_library(gloo SHARED IMPORTED)
set_target_properties(gloo PROPERTIES IMPORTED_LOCATION ${Gloo_LIBRARY})
# need to use Gloo_INCLUDE_DIRS over third_party/gloo to find Gloo's auto-generated config.h
include_directories(BEFORE SYSTEM ${Gloo_INCLUDE_DIRS})
endif()
# Here is a little bit hacky. We have to put PROJECT_BINARY_DIR in front
# of PROJECT_SOURCE_DIR with/without conda system. The reason is that
# gloo generates a new config.h in the binary diretory.
include_directories(BEFORE SYSTEM ${CMAKE_CURRENT_LIST_DIR}/../third_party/gloo)
include_directories(BEFORE SYSTEM ${PROJECT_BINARY_DIR}/third_party/gloo)
set(BUILD_TEST ${__BUILD_TEST})
set(BUILD_BENCHMARK ${__BUILD_BENCHMARK})