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-07-08 12:20:29 +00:00
committed by PyTorch MergeBot
parent ab655816b8
commit 7381c77724
2 changed files with 2 additions and 24 deletions

View File

@ -1185,20 +1185,11 @@ if(USE_XPU)
if(NOT TARGET torch_xpu_ops)
message(WARNING "Failed to include ATen XPU implementation target")
else()
target_link_libraries(torch_xpu PRIVATE torch_xpu_ops)
# USE_C10D_XCCL to decide if XCCL backend is enabled in torch-xpu-ops build.
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