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