Use CMake wholearchive group (#156393)

Use CMake wholearchive group to simplify code. It may also support more OSes.

Pull Request resolved: https://github.com/pytorch/pytorch/pull/156393
Approved by: https://github.com/ezyang
This commit is contained in:
cyy
2025-06-23 06:22:31 +00:00
committed by PyTorch MergeBot
parent 099d0d6121
commit d1b4e0fa9a
2 changed files with 2 additions and 23 deletions

View File

@ -1157,15 +1157,7 @@ if(USE_XPU)
if(USE_C10D_XCCL)
target_compile_definitions(torch_xpu PUBLIC USE_C10D_XCCL)
endif()
if(MSVC)
# Windows
target_link_options(torch_xpu PRIVATE
"-WHOLEARCHIVE:$<TARGET_FILE:torch_xpu_ops>")
else()
# Linux
target_link_options(torch_xpu PRIVATE
"-Wl,--whole-archive,$<TARGET_FILE:torch_xpu_ops>,--no-whole-archive")
endif()
target_link_libraries(torch_xpu PRIVATE $<LINK_LIBRARY:WHOLE_ARCHIVE,torch_xpu_ops>)
# Set cached ${ATen_XPU_INCLUDE_DIRS} to torch
include_directories(SYSTEM ${ATen_XPU_INCLUDE_DIRS})

View File

@ -163,20 +163,7 @@ macro(caffe2_interface_library SRC DST)
# link command for the specific SRC library.
if(${__src_target_type} STREQUAL "STATIC_LIBRARY")
# In the case of static library, we will need to add whole-static flags.
if(APPLE)
target_link_libraries(
${DST} INTERFACE -Wl,-force_load,\"$<TARGET_FILE:${SRC}>\")
elseif(MSVC)
# In MSVC, we will add whole archive in default.
target_link_libraries(
${DST} INTERFACE "$<TARGET_FILE:${SRC}>")
target_link_options(
${DST} INTERFACE "-WHOLEARCHIVE:$<TARGET_FILE:${SRC}>")
else()
# Assume everything else is like gcc
target_link_libraries(${DST} INTERFACE
"-Wl,--whole-archive,\"$<TARGET_FILE:${SRC}>\" -Wl,--no-whole-archive")
endif()
target_link_libraries(${DST} INTERFACE $<LINK_LIBRARY:WHOLE_ARCHIVE,${SRC}>)
# Link all interface link libraries of the src target as well.
# For static library, we need to explicitly depend on all the libraries
# that are the dependent library of the source library. Note that we cannot