xref: /aosp_15_r20/external/pytorch/cmake/TorchConfig.cmake.in (revision da0073e96a02ea20f0ac840b70461e3646d07c45)
1*da0073e9SAndroid Build Coastguard Worker# FindTorch
2*da0073e9SAndroid Build Coastguard Worker# -------
3*da0073e9SAndroid Build Coastguard Worker#
4*da0073e9SAndroid Build Coastguard Worker# Finds the Torch library
5*da0073e9SAndroid Build Coastguard Worker#
6*da0073e9SAndroid Build Coastguard Worker# This will define the following variables:
7*da0073e9SAndroid Build Coastguard Worker#
8*da0073e9SAndroid Build Coastguard Worker#   TORCH_FOUND        -- True if the system has the Torch library
9*da0073e9SAndroid Build Coastguard Worker#   TORCH_INCLUDE_DIRS -- The include directories for torch
10*da0073e9SAndroid Build Coastguard Worker#   TORCH_LIBRARIES    -- Libraries to link against
11*da0073e9SAndroid Build Coastguard Worker#   TORCH_CXX_FLAGS    -- Additional (required) compiler flags
12*da0073e9SAndroid Build Coastguard Worker#
13*da0073e9SAndroid Build Coastguard Worker# and the following imported targets:
14*da0073e9SAndroid Build Coastguard Worker#
15*da0073e9SAndroid Build Coastguard Worker#   torch
16*da0073e9SAndroid Build Coastguard Workermacro(append_torchlib_if_found)
17*da0073e9SAndroid Build Coastguard Worker  foreach (_arg ${ARGN})
18*da0073e9SAndroid Build Coastguard Worker    find_library(${_arg}_LIBRARY ${_arg} PATHS "${TORCH_INSTALL_PREFIX}/lib")
19*da0073e9SAndroid Build Coastguard Worker    if(${_arg}_LIBRARY)
20*da0073e9SAndroid Build Coastguard Worker      list(APPEND TORCH_LIBRARIES ${${_arg}_LIBRARY})
21*da0073e9SAndroid Build Coastguard Worker    else()
22*da0073e9SAndroid Build Coastguard Worker      message(WARNING "static library ${${_arg}_LIBRARY} not found.")
23*da0073e9SAndroid Build Coastguard Worker    endif()
24*da0073e9SAndroid Build Coastguard Worker  endforeach()
25*da0073e9SAndroid Build Coastguard Workerendmacro()
26*da0073e9SAndroid Build Coastguard Worker
27*da0073e9SAndroid Build Coastguard Workermacro(append_wholearchive_lib_if_found)
28*da0073e9SAndroid Build Coastguard Worker  foreach (_arg ${ARGN})
29*da0073e9SAndroid Build Coastguard Worker    find_library(${_arg}_LIBRARY ${_arg} PATHS "${TORCH_INSTALL_PREFIX}/lib")
30*da0073e9SAndroid Build Coastguard Worker    if(${_arg}_LIBRARY)
31*da0073e9SAndroid Build Coastguard Worker      if(APPLE)
32*da0073e9SAndroid Build Coastguard Worker        list(APPEND TORCH_LIBRARIES "-Wl,-force_load,${${_arg}_LIBRARY}")
33*da0073e9SAndroid Build Coastguard Worker      elseif(MSVC)
34*da0073e9SAndroid Build Coastguard Worker        list(APPEND TORCH_LIBRARIES "-WHOLEARCHIVE:${${_arg}_LIBRARY}")
35*da0073e9SAndroid Build Coastguard Worker      else()
36*da0073e9SAndroid Build Coastguard Worker        # Linux
37*da0073e9SAndroid Build Coastguard Worker        list(APPEND TORCH_LIBRARIES "-Wl,--whole-archive ${${_arg}_LIBRARY} -Wl,--no-whole-archive")
38*da0073e9SAndroid Build Coastguard Worker      endif()
39*da0073e9SAndroid Build Coastguard Worker    else()
40*da0073e9SAndroid Build Coastguard Worker      message(WARNING "static library ${${_arg}_LIBRARY} not found.")
41*da0073e9SAndroid Build Coastguard Worker    endif()
42*da0073e9SAndroid Build Coastguard Worker  endforeach()
43*da0073e9SAndroid Build Coastguard Workerendmacro()
44*da0073e9SAndroid Build Coastguard Worker
45*da0073e9SAndroid Build Coastguard Workerinclude(FindPackageHandleStandardArgs)
46*da0073e9SAndroid Build Coastguard Worker
47*da0073e9SAndroid Build Coastguard Workerif(DEFINED ENV{TORCH_INSTALL_PREFIX})
48*da0073e9SAndroid Build Coastguard Worker  set(TORCH_INSTALL_PREFIX $ENV{TORCH_INSTALL_PREFIX})
49*da0073e9SAndroid Build Coastguard Workerelse()
50*da0073e9SAndroid Build Coastguard Worker  # Assume we are in <install-prefix>/share/cmake/Torch/TorchConfig.cmake
51*da0073e9SAndroid Build Coastguard Worker  get_filename_component(CMAKE_CURRENT_LIST_DIR "${CMAKE_CURRENT_LIST_FILE}" PATH)
52*da0073e9SAndroid Build Coastguard Worker  get_filename_component(TORCH_INSTALL_PREFIX "${CMAKE_CURRENT_LIST_DIR}/../../../" ABSOLUTE)
53*da0073e9SAndroid Build Coastguard Workerendif()
54*da0073e9SAndroid Build Coastguard Worker
55*da0073e9SAndroid Build Coastguard Worker# Include directories.
56*da0073e9SAndroid Build Coastguard Workerif(EXISTS "${TORCH_INSTALL_PREFIX}/include")
57*da0073e9SAndroid Build Coastguard Worker  set(TORCH_INCLUDE_DIRS
58*da0073e9SAndroid Build Coastguard Worker    ${TORCH_INSTALL_PREFIX}/include
59*da0073e9SAndroid Build Coastguard Worker    ${TORCH_INSTALL_PREFIX}/include/torch/csrc/api/include)
60*da0073e9SAndroid Build Coastguard Workerelse()
61*da0073e9SAndroid Build Coastguard Worker  set(TORCH_INCLUDE_DIRS
62*da0073e9SAndroid Build Coastguard Worker    ${TORCH_INSTALL_PREFIX}/include
63*da0073e9SAndroid Build Coastguard Worker    ${TORCH_INSTALL_PREFIX}/include/torch/csrc/api/include)
64*da0073e9SAndroid Build Coastguard Workerendif()
65*da0073e9SAndroid Build Coastguard Worker
66*da0073e9SAndroid Build Coastguard Worker# Library dependencies.
67*da0073e9SAndroid Build Coastguard Workerif(@BUILD_SHARED_LIBS@)
68*da0073e9SAndroid Build Coastguard Worker  find_package(Caffe2 REQUIRED PATHS ${CMAKE_CURRENT_LIST_DIR}/../Caffe2)
69*da0073e9SAndroid Build Coastguard Worker  set(TORCH_LIBRARIES torch ${Caffe2_MAIN_LIBS})
70*da0073e9SAndroid Build Coastguard Worker  append_torchlib_if_found(c10)
71*da0073e9SAndroid Build Coastguard Workerelse()
72*da0073e9SAndroid Build Coastguard Worker  add_library(torch STATIC IMPORTED) # set imported_location at the bottom
73*da0073e9SAndroid Build Coastguard Worker  #library need whole archive
74*da0073e9SAndroid Build Coastguard Worker  append_wholearchive_lib_if_found(torch torch_cpu)
75*da0073e9SAndroid Build Coastguard Worker  if(@USE_CUDA@)
76*da0073e9SAndroid Build Coastguard Worker    append_wholearchive_lib_if_found(torch_cuda c10_cuda)
77*da0073e9SAndroid Build Coastguard Worker  endif()
78*da0073e9SAndroid Build Coastguard Worker
79*da0073e9SAndroid Build Coastguard Worker  # We need manually add dependent libraries when they are not linked into the
80*da0073e9SAndroid Build Coastguard Worker  # shared library.
81*da0073e9SAndroid Build Coastguard Worker  # TODO: this list might be incomplete.
82*da0073e9SAndroid Build Coastguard Worker  append_torchlib_if_found(c10)
83*da0073e9SAndroid Build Coastguard Worker
84*da0073e9SAndroid Build Coastguard Worker  if(@USE_NNPACK@)
85*da0073e9SAndroid Build Coastguard Worker    append_torchlib_if_found(nnpack)
86*da0073e9SAndroid Build Coastguard Worker  endif()
87*da0073e9SAndroid Build Coastguard Worker
88*da0073e9SAndroid Build Coastguard Worker  if(@USE_PYTORCH_QNNPACK@)
89*da0073e9SAndroid Build Coastguard Worker    append_torchlib_if_found(pytorch_qnnpack)
90*da0073e9SAndroid Build Coastguard Worker  endif()
91*da0073e9SAndroid Build Coastguard Worker
92*da0073e9SAndroid Build Coastguard Worker  if(@USE_XNNPACK@)
93*da0073e9SAndroid Build Coastguard Worker    append_torchlib_if_found(XNNPACK)
94*da0073e9SAndroid Build Coastguard Worker  endif()
95*da0073e9SAndroid Build Coastguard Worker
96*da0073e9SAndroid Build Coastguard Worker  append_torchlib_if_found(caffe2_protos protobuf-lite protobuf protoc)
97*da0073e9SAndroid Build Coastguard Worker  append_torchlib_if_found(onnx onnx_proto)
98*da0073e9SAndroid Build Coastguard Worker
99*da0073e9SAndroid Build Coastguard Worker  append_torchlib_if_found(fmt)
100*da0073e9SAndroid Build Coastguard Worker  append_torchlib_if_found(cpuinfo clog)
101*da0073e9SAndroid Build Coastguard Worker
102*da0073e9SAndroid Build Coastguard Worker  if(NOT @USE_INTERNAL_PTHREADPOOL_IMPL@)
103*da0073e9SAndroid Build Coastguard Worker    append_torchlib_if_found(pthreadpool)
104*da0073e9SAndroid Build Coastguard Worker  endif()
105*da0073e9SAndroid Build Coastguard Worker
106*da0073e9SAndroid Build Coastguard Worker  append_torchlib_if_found(eigen_blas)
107*da0073e9SAndroid Build Coastguard Worker
108*da0073e9SAndroid Build Coastguard Worker  if(@USE_FBGEMM@)
109*da0073e9SAndroid Build Coastguard Worker    append_torchlib_if_found(fbgemm)
110*da0073e9SAndroid Build Coastguard Worker  endif()
111*da0073e9SAndroid Build Coastguard Worker
112*da0073e9SAndroid Build Coastguard Worker  if(@USE_MKLDNN@)
113*da0073e9SAndroid Build Coastguard Worker    append_torchlib_if_found(dnnl mkldnn)
114*da0073e9SAndroid Build Coastguard Worker  endif()
115*da0073e9SAndroid Build Coastguard Worker
116*da0073e9SAndroid Build Coastguard Worker  append_torchlib_if_found(sleef asmjit)
117*da0073e9SAndroid Build Coastguard Workerendif()
118*da0073e9SAndroid Build Coastguard Worker
119*da0073e9SAndroid Build Coastguard Workerif(@USE_KINETO@)
120*da0073e9SAndroid Build Coastguard Worker  append_torchlib_if_found(kineto)
121*da0073e9SAndroid Build Coastguard Workerendif()
122*da0073e9SAndroid Build Coastguard Worker
123*da0073e9SAndroid Build Coastguard Workerif(@USE_CUDA@)
124*da0073e9SAndroid Build Coastguard Worker  if(MSVC)
125*da0073e9SAndroid Build Coastguard Worker    find_library(CAFFE2_NVRTC_LIBRARY caffe2_nvrtc PATHS "${TORCH_INSTALL_PREFIX}/lib")
126*da0073e9SAndroid Build Coastguard Worker    list(APPEND TORCH_CUDA_LIBRARIES ${CAFFE2_NVRTC_LIBRARY})
127*da0073e9SAndroid Build Coastguard Worker  else()
128*da0073e9SAndroid Build Coastguard Worker    set(TORCH_CUDA_LIBRARIES ${CUDA_NVRTC_LIB})
129*da0073e9SAndroid Build Coastguard Worker  endif()
130*da0073e9SAndroid Build Coastguard Worker  if(TARGET torch::nvtoolsext)
131*da0073e9SAndroid Build Coastguard Worker    list(APPEND TORCH_CUDA_LIBRARIES torch::nvtoolsext)
132*da0073e9SAndroid Build Coastguard Worker  endif()
133*da0073e9SAndroid Build Coastguard Worker
134*da0073e9SAndroid Build Coastguard Worker  if(@BUILD_SHARED_LIBS@)
135*da0073e9SAndroid Build Coastguard Worker    find_library(C10_CUDA_LIBRARY c10_cuda PATHS "${TORCH_INSTALL_PREFIX}/lib")
136*da0073e9SAndroid Build Coastguard Worker    list(APPEND TORCH_CUDA_LIBRARIES ${C10_CUDA_LIBRARY} ${Caffe2_PUBLIC_CUDA_DEPENDENCY_LIBS})
137*da0073e9SAndroid Build Coastguard Worker  endif()
138*da0073e9SAndroid Build Coastguard Worker  list(APPEND TORCH_LIBRARIES ${TORCH_CUDA_LIBRARIES})
139*da0073e9SAndroid Build Coastguard Workerendif()
140*da0073e9SAndroid Build Coastguard Worker
141*da0073e9SAndroid Build Coastguard Worker# When we build libtorch with the old libstdc++ ABI, dependent libraries must too.
142*da0073e9SAndroid Build Coastguard Workerif(CMAKE_SYSTEM_NAME STREQUAL "Linux")
143*da0073e9SAndroid Build Coastguard Worker  set(TORCH_CXX_FLAGS "-D_GLIBCXX_USE_CXX11_ABI=@GLIBCXX_USE_CXX11_ABI@")
144*da0073e9SAndroid Build Coastguard Workerendif()
145*da0073e9SAndroid Build Coastguard Worker
146*da0073e9SAndroid Build Coastguard Workerfind_library(TORCH_LIBRARY torch PATHS "${TORCH_INSTALL_PREFIX}/lib")
147*da0073e9SAndroid Build Coastguard Worker# the statements below changes target properties on
148*da0073e9SAndroid Build Coastguard Worker# - the imported target from Caffe2Targets.cmake in shared library mode (see the find_package above)
149*da0073e9SAndroid Build Coastguard Worker#    - this is untested whether it is the correct (or desired) methodology in CMake
150*da0073e9SAndroid Build Coastguard Worker# - the imported target created in this file in static library mode
151*da0073e9SAndroid Build Coastguard Workerif(NOT @BUILD_SHARED_LIBS@)
152*da0073e9SAndroid Build Coastguard Worker  # do not set this property on the shared library target, as it will cause confusion in some builds
153*da0073e9SAndroid Build Coastguard Worker  # as the configuration specific property is set in the Caffe2Targets.cmake file
154*da0073e9SAndroid Build Coastguard Worker  set_target_properties(torch PROPERTIES
155*da0073e9SAndroid Build Coastguard Worker      IMPORTED_LOCATION "${TORCH_LIBRARY}"
156*da0073e9SAndroid Build Coastguard Worker  )
157*da0073e9SAndroid Build Coastguard Workerendif()
158*da0073e9SAndroid Build Coastguard Workerset_target_properties(torch PROPERTIES
159*da0073e9SAndroid Build Coastguard Worker    INTERFACE_INCLUDE_DIRECTORIES "${TORCH_INCLUDE_DIRS}"
160*da0073e9SAndroid Build Coastguard Worker    CXX_STANDARD 17
161*da0073e9SAndroid Build Coastguard Worker)
162*da0073e9SAndroid Build Coastguard Workerif(TORCH_CXX_FLAGS)
163*da0073e9SAndroid Build Coastguard Worker  set_property(TARGET torch PROPERTY INTERFACE_COMPILE_OPTIONS "${TORCH_CXX_FLAGS}")
164*da0073e9SAndroid Build Coastguard Workerendif()
165*da0073e9SAndroid Build Coastguard Worker
166*da0073e9SAndroid Build Coastguard Workerfind_package_handle_standard_args(Torch DEFAULT_MSG TORCH_LIBRARY TORCH_INCLUDE_DIRS)
167