1cmake_minimum_required(VERSION 3.18 FATAL_ERROR) 2project(c10 CXX) 3 4set(CMAKE_CXX_STANDARD 17 CACHE STRING "The C++ standard whose features are requested to build this target.") 5set(CMAKE_EXPORT_COMPILE_COMMANDS ON) 6 7# Main build file for the C10 library. 8# 9# Note that the C10 library should maintain minimal dependencies - especially, 10# it should not depend on any library that is implementation specific or 11# backend specific. It should in particular NOT be dependent on any generated 12# protobuf header files, because protobuf header files will transitively force 13# one to link against a specific protobuf version. 14 15if(BUILD_LIBTORCHLESS) 16 find_library(C10_LIB c10 PATHS $ENV{LIBTORCH_LIB_PATH} NO_DEFAULT_PATH) 17else() 18 set(C10_LIB c10) 19endif() 20 21 # ---[ Configure macro file. 22 set(C10_USE_GFLAGS ${USE_GFLAGS}) # used in cmake_macros.h.in 23 set(C10_USE_GLOG ${USE_GLOG}) # used in cmake_macros.h.in 24 set(C10_BUILD_SHARED_LIBS ${BUILD_SHARED_LIBS}) # used in cmake_macros.h.in 25 set(C10_USE_NUMA ${USE_NUMA}) 26 set(C10_USE_MSVC_STATIC_RUNTIME ${CAFFE2_USE_MSVC_STATIC_RUNTIME}) 27 set(C10_USE_ROCM_KERNEL_ASSERT ${USE_ROCM_KERNEL_ASSERT}) 28 configure_file( 29 ${CMAKE_CURRENT_LIST_DIR}/macros/cmake_macros.h.in 30 ${CMAKE_BINARY_DIR}/c10/macros/cmake_macros.h) 31 32 # Note: if you want to add ANY dependency to the c10 library, make sure you 33 # check with the core PyTorch developers as the dependency will be 34 # transitively passed on to all libraries dependent on PyTorch. 35 file(GLOB C10_SRCS 36 *.cpp 37 core/*.cpp 38 core/impl/*.cpp 39 mobile/*.cpp 40 macros/*.cpp 41 util/*.cpp 42 ) 43 file(GLOB C10_HEADERS 44 *.h 45 core/*.h 46 core/impl/*.h 47 mobile/*.h 48 macros/*.h 49 util/*.h 50 ) 51if(NOT BUILD_LIBTORCHLESS) 52 add_library(c10 ${C10_SRCS} ${C10_HEADERS}) 53 target_compile_options_if_supported(c10 "-Wdeprecated") 54 if(HAVE_SOVERSION) 55 set_target_properties(c10 PROPERTIES 56 VERSION ${TORCH_VERSION} SOVERSION ${TORCH_SOVERSION}) 57 endif() 58 # If building shared library, set dllimport/dllexport proper. 59 target_compile_options(c10 PRIVATE "-DC10_BUILD_MAIN_LIB") 60 # Enable hidden visibility if compiler supports it. 61 if(${COMPILER_SUPPORTS_HIDDEN_VISIBILITY}) 62 target_compile_options(c10 PRIVATE "-fvisibility=hidden") 63 endif() 64 65 option(C10_USE_IWYU "Use include-what-you-use to clean up header inclusion" OFF) 66 if(C10_USE_IWYU) 67 find_program(iwyu NAMES include-what-you-use) 68 if(iwyu) 69 set(iwyu_cmd 70 "include-what-you-use" 71 "-Xiwyu" 72 "--transitive_includes_only" 73 "-Xiwyu" 74 "--no_fwd_decls" 75 "-Xiwyu" 76 "--prefix_header_includes=keep" 77 "-Xiwyu" 78 "--mapping_file=${CMAKE_CURRENT_LIST_DIR}/../tools/iwyu/all.imp" 79 ) 80 set_property(TARGET c10 PROPERTY CXX_INCLUDE_WHAT_YOU_USE ${iwyu_cmd}) 81 endif() 82 endif() 83 84 if(WERROR) 85 target_compile_options_if_supported(c10 PRIVATE "-Werror=sign-compare") 86 target_compile_options_if_supported(c10 PRIVATE "-Werror=shadow") 87 endif() 88 89 # ---[ Dependency of c10 90 if(C10_USE_GFLAGS) 91 target_link_libraries(c10 PUBLIC gflags) 92 endif() 93 94 if(C10_USE_GLOG) 95 target_link_libraries(c10 PUBLIC glog::glog) 96 endif() 97 target_link_libraries(c10 PRIVATE fmt::fmt-header-only) 98 target_link_libraries(c10 PRIVATE nlohmann) 99 100 if(C10_USE_NUMA) 101 message(STATUS "NUMA paths:") 102 message(STATUS ${Numa_INCLUDE_DIR}) 103 message(STATUS ${Numa_LIBRARIES}) 104 target_include_directories(c10 PRIVATE ${Numa_INCLUDE_DIR}) 105 target_link_libraries(c10 PRIVATE ${Numa_LIBRARIES}) 106 else() 107 message(STATUS "don't use NUMA") 108 endif() 109 110 if(NOT CMAKE_SYSTEM_PROCESSOR MATCHES "s390x" AND NOT CMAKE_SYSTEM_PROCESSOR MATCHES "ppc64le") 111 target_link_libraries(c10 PRIVATE cpuinfo) 112 endif() 113 114 find_package(Backtrace) 115 if(Backtrace_FOUND) 116 target_include_directories(c10 PRIVATE ${Backtrace_INCLUDE_DIRS}) 117 target_link_libraries(c10 PRIVATE ${Backtrace_LIBRARIES}) 118 target_compile_definitions(c10 PRIVATE SUPPORTS_BACKTRACE=1) 119 else() 120 target_compile_definitions(c10 PRIVATE SUPPORTS_BACKTRACE=0) 121 endif() 122 123 if(USE_MIMALLOC) 124 target_link_libraries(c10 PRIVATE "mimalloc-static") 125 add_dependencies(c10 mimalloc-static) 126 endif() 127 128 if(LINUX) 129 target_link_libraries(c10 PRIVATE Threads::Threads) 130 endif() 131 132 if(ANDROID) 133 target_link_libraries(c10 PRIVATE log) 134 endif() 135 136 target_include_directories( 137 c10 PUBLIC 138 $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/../> 139 $<BUILD_INTERFACE:${CMAKE_BINARY_DIR}> 140 $<INSTALL_INTERFACE:include>) 141endif() 142 143add_subdirectory(test) 144add_subdirectory(benchmark) 145 146if(USE_CUDA) 147 add_subdirectory(cuda) 148endif() 149 150if(USE_ROCM) 151 # NB: This directory is generated by the HIPIFY script; it's 152 # not checked in 153 add_subdirectory(hip) 154endif() 155 156if(USE_XPU) 157 add_subdirectory(xpu) 158endif() 159 160if(NOT BUILD_LIBTORCHLESS) 161 # ---[ Installation 162 # Note: for now, we will put all export path into one single Caffe2Targets group 163 # to deal with the cmake deployment need. Inside the Caffe2Targets set, the 164 # individual libraries like libc10.so and libcaffe2.so are still self-contained. 165 install(TARGETS c10 EXPORT Caffe2Targets DESTINATION lib) 166endif() 167 168install(DIRECTORY ${CMAKE_CURRENT_LIST_DIR} 169 DESTINATION include 170 FILES_MATCHING PATTERN "*.h") 171install(FILES ${CMAKE_BINARY_DIR}/c10/macros/cmake_macros.h 172 DESTINATION include/c10/macros) 173 174if(MSVC AND C10_BUILD_SHARED_LIBS) 175 install(FILES $<TARGET_PDB_FILE:c10> DESTINATION lib OPTIONAL) 176endif() 177