TorchConfig.cmake 5.8 KB

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