pytorch/cmake/Caffe2Config.cmake.in
Edward Yang 38986e1dea Split libtorch.so back into libtorch_{cpu,cuda,hip} (#30315)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/30315

The new structure is that libtorch_cpu contains the bulk of our
code, and libtorch depends on libtorch_cpu and libtorch_cuda.
This is a reland of https://github.com/pytorch/pytorch/pull/29731 but
I've extracted all of the prep work into separate PRs which can be
landed before this one.

Some things of note:

* torch/csrc/cuda/nccl.cpp was added to the wrong list of SRCS, now fixed (this didn't matter before because previously they were all in the same library)
* The dummy file for libtorch was brought back from the dead; it was previously deleted in #20774
In an initial version of the patch, I forgot to make torch_cuda explicitly depend on torch_cpu. This lead to some very odd errors, most notably "bin/blob_test: hidden symbol `_ZNK6google8protobuf5Arena17OnArenaAllocationEPKSt9type_infom' in lib/libprotobuf.a(arena.cc.o) is referenced by DSO"
* A number of places in Android/iOS builds have to add torch_cuda explicitly as a library, as they do not have transitive dependency calculation working correctly
* I had to torch_cpu/torch_cuda caffe2_interface_library so that they get whole-archived linked into torch when you statically link. And I had to do this in an *exported* fashion because torch needs to depend on torch_cpu_library. In the end I exported everything and removed the redefinition in the Caffe2Config.cmake. However, I am not too sure why the old code did it in this way in the first place; however, it doesn't seem to have broken anything to switch it this way.
* There's some uses of `__HIP_PLATFORM_HCC__` still in `torch_cpu` code, so I had to apply it to that library too (UGH). This manifests as a failer when trying to run the CUDA fuser. This doesn't really matter substantively right now because we still in-place HIPify, but it would be good to fix eventually. This was a bit difficult to debug because of an unrelated HIP bug, see https://github.com/ROCm-Developer-Tools/HIP/issues/1706

Fixes #27215 (as our libraries are smaller), and executes on
part of the plan in #29235.

Signed-off-by: Edward Z. Yang <ezyang@fb.com>

Test Plan: Imported from OSS

Differential Revision: D18790941

Pulled By: ezyang

fbshipit-source-id: 01296f6089d3de5e8365251b490c51e694f2d6c7
2019-12-04 08:04:57 -08:00

136 lines
5.3 KiB
CMake

# - Config file for the Caffe2 package
# It defines the following variable(s)
# CAFFE2_INCLUDE_DIRS - include directories for FooBar
# as well as Caffe2 targets for other cmake libraries to use.
# library version information
set(CAFFE2_VERSION_MAJOR @CAFFE2_VERSION_MAJOR@)
set(CAFFE2_VERSION_MINOR @CAFFE2_VERSION_MINOR@)
set(CAFFE2_VERSION_PATCH @CAFFE2_VERSION_PATCH@)
set(CAFFE2_VERSION "@CAFFE2_VERSION@")
# Utils functions.
include("${CMAKE_CURRENT_LIST_DIR}/public/utils.cmake")
# Include threads lib.
include("${CMAKE_CURRENT_LIST_DIR}/public/threads.cmake")
# Depending on whether Caffe2 uses gflags during compile time or
# not, invoke gflags.
if (@USE_GFLAGS@)
include("${CMAKE_CURRENT_LIST_DIR}/public/gflags.cmake")
if (NOT TARGET gflags)
message(FATAL_ERROR
"Your installed Caffe2 version uses gflags but the gflags library "
"cannot be found. Did you accidentally remove it, or have you set "
"the right CMAKE_PREFIX_PATH and/or GFLAGS_ROOT_DIR? If you do not "
"have gflags, you will need to install gflags and set the library "
"path accordingly.")
endif()
endif()
# Depending on whether Caffe2 uses glog during compile time or
# not, invoke glog.
if (@USE_GLOG@)
include("${CMAKE_CURRENT_LIST_DIR}/public/glog.cmake")
if (NOT TARGET glog::glog)
message(FATAL_ERROR
"Your installed Caffe2 version uses glog but the glog library "
"cannot be found. Did you accidentally remove it, or have you set "
"the right CMAKE_PREFIX_PATH and/or GFLAGS_ROOT_DIR? If you do not "
"have glog, you will need to install glog and set the library "
"path accordingly.")
endif()
endif()
# Protobuf
if (@CAFFE2_LINK_LOCAL_PROTOBUF@)
if (NOT TARGET protobuf::libprotobuf)
# Define protobuf::libprotobuf as a dummy target to resolve references to
# protobuf::libprotobuf in Caffe2Targets.cmake.
add_library(dummy INTERFACE)
add_library(protobuf::libprotobuf ALIAS dummy)
endif()
else()
include("${CMAKE_CURRENT_LIST_DIR}/public/protobuf.cmake")
if (NOT TARGET protobuf::libprotobuf)
message(FATAL_ERROR
"Your installed Caffe2 version uses protobuf but the protobuf library "
"cannot be found. Did you accidentally remove it, or have you set "
"the right CMAKE_PREFIX_PATH? If you do not have protobuf, you will "
"need to install protobuf and set the library path accordingly.")
endif()
message(STATUS "Caffe2: Protobuf version " ${Protobuf_VERSION})
# If during build time we know the protobuf version, we will also do a sanity
# check to ensure that the protobuf library that Caffe2 found is consistent
# with the compiled version.
if (@CAFFE2_KNOWN_PROTOBUF_VERSION@)
if (NOT (${Protobuf_VERSION} VERSION_EQUAL @Protobuf_VERSION@))
message(FATAL_ERROR
"Your installed Caffe2 is built with protobuf "
"@Protobuf_VERSION@"
", while your current cmake setting discovers protobuf version "
${Protobuf_VERSION}
". Please specify a protobuf version that is the same as the built "
"version.")
endif()
endif()
endif()
if (@USE_CUDA@)
# The file public/cuda.cmake exclusively uses CAFFE2_USE_*.
# If Caffe2 was compiled with the libraries below, they must
# be found again when including the Caffe2 target.
set(CAFFE2_USE_CUDA @USE_CUDA@)
set(CAFFE2_USE_CUDNN @USE_CUDNN@)
set(CAFFE2_USE_TENSORRT @USE_TENSORRT@)
include("${CMAKE_CURRENT_LIST_DIR}/public/cuda.cmake")
if (@CAFFE2_USE_CUDA@ AND NOT CAFFE2_USE_CUDA)
message(FATAL_ERROR
"Your installed Caffe2 version uses CUDA but I cannot find the CUDA "
"libraries. Please set the proper CUDA prefixes and / or install "
"CUDA.")
endif()
if (@CAFFE2_USE_CUDNN@ AND NOT CAFFE2_USE_CUDNN)
message(FATAL_ERROR
"Your installed Caffe2 version uses cuDNN but I cannot find the cuDNN "
"libraries. Please set the proper cuDNN prefixes and / or install "
"cuDNN.")
endif()
if (@CAFFE2_USE_TENSORRT@ AND NOT CAFFE2_USE_TENSORRT)
message(FATAL_ERROR
"Your installed Caffe2 version uses TensorRT but I cannot find the TensorRT "
"libraries. Please set the proper TensorRT prefixes and / or install "
"TensorRT.")
endif()
endif()
include("${CMAKE_CURRENT_LIST_DIR}/public/mkl.cmake")
if (@USE_MKLDNN@)
include("${CMAKE_CURRENT_LIST_DIR}/public/mkldnn.cmake")
endif()
# import targets
include ("${CMAKE_CURRENT_LIST_DIR}/Caffe2Targets.cmake")
# Interface libraries, that allows one to build proper link flags.
# We will also define a helper variable, Caffe2_MAIN_LIBS, that resolves to
# the main caffe2 libraries in cases of cuda presence / absence.
set(Caffe2_MAIN_LIBS torch_library)
# include directory.
#
# Newer versions of CMake set the INTERFACE_INCLUDE_DIRECTORIES property
# of the imported targets. It is hence not necessary to add this path
# manually to the include search path for targets which link to gflags.
# The following lines are here for backward compatibility, in case one
# would like to use the old-style include path.
get_filename_component(
CMAKE_CURRENT_LIST_DIR "${CMAKE_CURRENT_LIST_FILE}" PATH)
# Note: the current list dir is _INSTALL_PREFIX/share/cmake/Gloo.
get_filename_component(
_INSTALL_PREFIX "${CMAKE_CURRENT_LIST_DIR}/../../../" ABSOLUTE)
set(CAFFE2_INCLUDE_DIRS "${_INSTALL_PREFIX}/include")