Files
pytorch/c10/CMakeLists.txt
Nikita Shulga 36ac095ff8 Migrate PyTorch to C++17 (#85969)
With CUDA-10.2 gone we can finally do it!

This PR mostly contains build system related changes, invasive functional ones are to be followed.
Among many expected tweaks to the build system, here are few unexpected ones:
 - Force onnx_proto project to be updated to C++17 to avoid `duplicate symbols` error when compiled by gcc-7.5.0, as storage rule for `constexpr` changed in C++17, but gcc does not seem to follow it
 - Do not use `std::apply` on CUDA but rely on the built-in variant, as it results in test failures when CUDA runtime picks host rather than device function when `std::apply` is invoked from CUDA code.
 - `std::decay_t` -> `::std::decay_t` and `std::move`->`::std::move` as VC++ for some reason claims that `std` symbol is ambigious
 - Disable use of `std::aligned_alloc` on Android, as its `libc++` does not implement it.

Some prerequisites:
 - https://github.com/pytorch/pytorch/pull/89297
 - https://github.com/pytorch/pytorch/pull/89605
 - https://github.com/pytorch/pytorch/pull/90228
 - https://github.com/pytorch/pytorch/pull/90389
 - https://github.com/pytorch/pytorch/pull/90379
 - https://github.com/pytorch/pytorch/pull/89570
 - https://github.com/facebookincubator/gloo/pull/336
 - https://github.com/facebookincubator/gloo/pull/343
 - 919676fb32

Fixes https://github.com/pytorch/pytorch/issues/56055

Pull Request resolved: https://github.com/pytorch/pytorch/pull/85969
Approved by: https://github.com/ezyang, https://github.com/kulinseth
2022-12-08 02:27:48 +00:00

126 lines
4.0 KiB
CMake

cmake_minimum_required(VERSION 3.10 FATAL_ERROR)
project(c10 CXX)
set(CMAKE_CXX_STANDARD 17 CACHE STRING "The C++ standard whose features are requested to build this target.")
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
# Main build file for the C10 library.
#
# Note that the C10 library should maintain minimal dependencies - especially,
# it should not depend on any library that is implementation specific or
# backend specific. It should in particular NOT be dependent on any generated
# protobuf header files, because protobuf header files will transitively force
# one to link against a specific protobuf version.
# ---[ Configure macro file.
set(C10_USE_GFLAGS ${USE_GFLAGS}) # used in cmake_macros.h.in
set(C10_USE_GLOG ${USE_GLOG}) # used in cmake_macros.h.in
set(C10_BUILD_SHARED_LIBS ${BUILD_SHARED_LIBS}) # used in cmake_macros.h.in
set(C10_USE_NUMA ${USE_NUMA})
set(C10_USE_MSVC_STATIC_RUNTIME ${CAFFE2_USE_MSVC_STATIC_RUNTIME})
configure_file(
${CMAKE_CURRENT_LIST_DIR}/macros/cmake_macros.h.in
${CMAKE_BINARY_DIR}/c10/macros/cmake_macros.h)
# Note: if you want to add ANY dependency to the c10 library, make sure you
# check with the core PyTorch developers as the dependency will be
# transitively passed on to all libraries dependent on PyTorch.
file(GLOB C10_SRCS
*.cpp
core/*.cpp
core/impl/*.cpp
mobile/*.cpp
macros/*.cpp
util/*.cpp
)
file(GLOB C10_HEADERS
*.h
core/*.h
core/impl/*.h
mobile/*.h
macros/*.h
util/*.h
)
add_library(c10 ${C10_SRCS} ${C10_HEADERS})
if(HAVE_SOVERSION)
set_target_properties(c10 PROPERTIES
VERSION ${TORCH_VERSION} SOVERSION ${TORCH_SOVERSION})
endif()
# If building shared library, set dllimport/dllexport proper.
target_compile_options(c10 PRIVATE "-DC10_BUILD_MAIN_LIB")
# Enable hidden visibility if compiler supports it.
if(${COMPILER_SUPPORTS_HIDDEN_VISIBILITY})
target_compile_options(c10 PRIVATE "-fvisibility=hidden")
endif()
if(WERROR)
target_compile_options_if_supported(c10 PRIVATE "-Werror=sign-compare")
target_compile_options_if_supported(c10 PRIVATE "-Werror=shadow")
endif()
# ---[ Dependency of c10
if(${USE_GFLAGS})
target_link_libraries(c10 PUBLIC gflags)
endif()
if(${USE_GLOG})
target_link_libraries(c10 PUBLIC glog::glog)
endif()
target_link_libraries(c10 PRIVATE fmt::fmt-header-only)
find_package(Backtrace)
if(Backtrace_FOUND)
target_include_directories(c10 PRIVATE ${Backtrace_INCLUDE_DIRS})
target_link_libraries(c10 PRIVATE ${Backtrace_LIBRARIES})
target_compile_definitions(c10 PRIVATE SUPPORTS_BACKTRACE=1)
else()
target_compile_definitions(c10 PRIVATE SUPPORTS_BACKTRACE=0)
endif()
if(USE_NUMA)
message(STATUS "NUMA paths:")
message(STATUS ${Numa_INCLUDE_DIR})
message(STATUS ${Numa_LIBRARIES})
include_directories(SYSTEM ${Numa_INCLUDE_DIR})
target_link_libraries(c10 PRIVATE ${Numa_LIBRARIES})
else()
message(STATUS "don't use NUMA")
endif()
if(ANDROID)
target_link_libraries(c10 PRIVATE log)
endif()
target_include_directories(
c10 PUBLIC
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/../>
$<BUILD_INTERFACE:${CMAKE_BINARY_DIR}>
$<INSTALL_INTERFACE:include>)
add_subdirectory(test)
add_subdirectory(benchmark)
if(USE_CUDA)
add_subdirectory(cuda)
endif()
if(USE_ROCM)
# NB: This directory is generated by the HIPIFY script; it's
# not checked in
add_subdirectory(hip)
endif()
# ---[ Installation
# Note: for now, we will put all export path into one single Caffe2Targets group
# to deal with the cmake deployment need. Inside the Caffe2Targets set, the
# individual libraries like libc10.so and libcaffe2.so are still self-contained.
install(TARGETS c10 EXPORT Caffe2Targets DESTINATION lib)
install(DIRECTORY ${CMAKE_CURRENT_LIST_DIR}
DESTINATION include
FILES_MATCHING PATTERN "*.h")
install(FILES ${CMAKE_BINARY_DIR}/c10/macros/cmake_macros.h
DESTINATION include/c10/macros)
if(MSVC AND C10_BUILD_SHARED_LIBS)
install(FILES $<TARGET_PDB_FILE:c10> DESTINATION lib OPTIONAL)
endif()