"Fossies" - the Fresh Open Source Software Archive

Member "pytorch-1.8.2/CMakeLists.txt" (23 Jul 2021, 36846 Bytes) of package /linux/misc/pytorch-1.8.2.tar.gz:


As a special service "Fossies" has tried to format the requested text file into HTML format (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file. See also the last Fossies "Diffs" side-by-side code changes report for "CMakeLists.txt": 1.13.0-rc3_vs_1.13.0-rc4.

    1 cmake_minimum_required(VERSION 3.5 FATAL_ERROR)
    2 #cmake_policy(SET CMP0022 NEW)
    3 #cmake_policy(SET CMP0023 NEW)
    4 
    5 # Use compiler ID "AppleClang" instead of "Clang" for XCode.
    6 # Not setting this sometimes makes XCode C compiler gets detected as "Clang",
    7 # even when the C++ one is detected as "AppleClang".
    8 cmake_policy(SET CMP0010 NEW)
    9 cmake_policy(SET CMP0025 NEW)
   10 
   11 # Suppress warning flags in default MSVC configuration.  It's not
   12 # mandatory that we do this (and we don't if cmake is old), but it's
   13 # nice when it's possible, and it's possible on our Windows configs.
   14 if(NOT CMAKE_VERSION VERSION_LESS 3.15.0)
   15   cmake_policy(SET CMP0092 NEW)
   16 endif()
   17 
   18 if(NOT CMAKE_VERSION VERSION_LESS 3.10)
   19   set(FIND_CUDA_MODULE_DEPRECATED ON)
   20 endif()
   21 
   22 # ---[ Project and semantic versioning.
   23 project(Torch CXX C)
   24 
   25 if(${CMAKE_SYSTEM_NAME} STREQUAL "Linux")
   26   set(LINUX TRUE)
   27 else()
   28   set(LINUX FALSE)
   29 endif()
   30 
   31 set(CMAKE_INSTALL_MESSAGE NEVER)
   32 
   33 # check and set CMAKE_CXX_STANDARD
   34 string(FIND "${CMAKE_CXX_FLAGS}" "-std=c++" env_cxx_standard)
   35 if(env_cxx_standard GREATER -1)
   36   message(
   37       WARNING "C++ standard version definition detected in environment variable."
   38       "PyTorch requires -std=c++14. Please remove -std=c++ settings in your environment.")
   39 endif()
   40 set(CMAKE_CXX_STANDARD 14)
   41 set(CMAKE_C_STANDARD 11)
   42 
   43 if(DEFINED GLIBCXX_USE_CXX11_ABI)
   44   if(${GLIBCXX_USE_CXX11_ABI} EQUAL 1)
   45     set(CXX_STANDARD_REQUIRED ON)
   46     set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -D_GLIBCXX_USE_CXX11_ABI=1")
   47   endif()
   48 endif()
   49 
   50 set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
   51 
   52 # One variable that determines whether the current cmake process is being run
   53 # with the main Caffe2 library. This is useful for building modules - if
   54 # modules are built with the main Caffe2 library then one does not need to do
   55 # find caffe2 in the cmake script. One can usually guard it in some way like
   56 #    if(NOT CAFFE2_CMAKE_BUILDING_WITH_MAIN_REPO)
   57 #      find_package(Caffe2 REQUIRED)
   58 #    endif()
   59 set(CAFFE2_CMAKE_BUILDING_WITH_MAIN_REPO ON)
   60 
   61 # Googletest's cmake files are going to set it on once they are processed. Let's
   62 # set it at the very beginning so that the entire build is deterministic.
   63 set(THREADS_PREFER_PTHREAD_FLAG ON)
   64 
   65 if(NOT DEFINED BLAS_SET_BY_USER)
   66   if(DEFINED BLAS)
   67     set(BLAS_SET_BY_USER TRUE)
   68   else()
   69     message(STATUS "Not forcing any particular BLAS to be found")
   70     set(BLAS_SET_BY_USER FALSE)
   71   endif()
   72   set(BLAS_SET_BY_USER ${BLAS_SET_BY_USER} CACHE STRING "Marks whether BLAS was manually set by user or auto-detected")
   73 endif()
   74 
   75 # Apple specific
   76 if(APPLE)
   77   # These lines are an attempt to make find_package(cuda) pick up
   78   # libcuda.dylib, and not cuda.framework.  It doesn't work all
   79   # the time, but it seems to help for some users.
   80   # TODO: replace this with a more robust fix
   81   set(CMAKE_FIND_FRAMEWORK LAST)
   82   set(CMAKE_FIND_APPBUNDLE LAST)
   83 
   84   # Get clang version on macOS
   85   execute_process( COMMAND ${CMAKE_CXX_COMPILER} --version OUTPUT_VARIABLE clang_full_version_string )
   86   string(REGEX REPLACE "Apple LLVM version ([0-9]+\\.[0-9]+).*" "\\1" CLANG_VERSION_STRING ${clang_full_version_string})
   87   message( STATUS "CLANG_VERSION_STRING:         " ${CLANG_VERSION_STRING} )
   88 
   89 
   90   # RPATH stuff
   91   set(CMAKE_MACOSX_RPATH ON)
   92 endif()
   93 
   94 set(CPU_AARCH64 OFF)
   95 set(CPU_INTEL OFF)
   96 
   97 if(WIN32)
   98   # On Windows, CMAKE_HOST_SYSTEM_PROCESSOR is calculated through `PROCESSOR_ARCHITECTURE`,
   99   # which only has the value of `x86` or `AMD64`. We cannot infer whether it's a Intel CPU
  100   # or not. However, the environment variable `PROCESSOR_IDENTIFIER` could be used.
  101   if($ENV{PROCESSOR_IDENTIFIER} MATCHES "Intel")
  102     set(CPU_INTEL ON)
  103   else()
  104     set(CPU_INTEL OFF)
  105   endif()
  106 else()
  107  if(CMAKE_SYSTEM_PROCESSOR MATCHES "(x86_64|i[3-6]+86)")
  108     set(CPU_INTEL ON)
  109   elseif(CMAKE_SYSTEM_PROCESSOR MATCHES "^(aarch64|arm64)")
  110     set(CPU_AARCH64 ON)
  111   endif()
  112 endif()
  113 
  114 
  115 # For non-supported platforms, turn USE_DISTRIBUTED off by default.
  116 # It is not tested and likely won't work without additional changes.
  117 if(NOT LINUX AND NOT WIN32)
  118   set(USE_DISTRIBUTED OFF CACHE STRING "Use distributed")
  119   # On macOS, if USE_DISTRIBUTED is enabled (specified by the user),
  120   # then make Gloo build with the libuv transport.
  121   if(APPLE AND USE_DISTRIBUTED)
  122     set(USE_LIBUV ON CACHE STRING "")
  123   endif()
  124 endif()
  125 
  126 # ---[ Options.
  127 # Note to developers: if you add an option below, make sure you also add it to
  128 # cmake/Summary.cmake so that the summary prints out the option values.
  129 include(CMakeDependentOption)
  130 option(ATEN_NO_TEST "Do not build ATen test binaries" OFF)
  131 option(BUILD_BINARY "Build C++ binaries" OFF)
  132 option(BUILD_DOCS "Build Caffe2 documentation" OFF)
  133 option(BUILD_CUSTOM_PROTOBUF "Build and use Caffe2's own protobuf under third_party" ON)
  134 option(BUILD_PYTHON "Build Python binaries" ON)
  135 option(BUILD_CAFFE2 "Master flag to build Caffe2" ON)
  136 cmake_dependent_option(
  137     BUILD_CAFFE2_OPS "Build Caffe2 operators" ON
  138     "BUILD_CAFFE2" OFF)
  139 cmake_dependent_option(
  140     BUILD_CAFFE2_MOBILE "Build libcaffe2 for mobile (deprecating)" OFF
  141     "BUILD_CAFFE2" OFF)
  142 option(BUILD_SHARED_LIBS "Build libcaffe2.so" ON)
  143 cmake_dependent_option(
  144     CAFFE2_LINK_LOCAL_PROTOBUF "If set, build protobuf inside libcaffe2.so." ON
  145     "BUILD_SHARED_LIBS AND BUILD_CUSTOM_PROTOBUF" OFF)
  146 cmake_dependent_option(
  147     CAFFE2_USE_MSVC_STATIC_RUNTIME "Using MSVC static runtime libraries" ON
  148     "NOT BUILD_SHARED_LIBS" OFF)
  149 option(BUILD_TEST "Build C++ test binaries (need gtest and gbenchmark)" OFF)
  150 option(BUILD_STATIC_RUNTIME_BENCHMARK "Build C++ binaries for static runtime benchmarks (need gbenchmark)" OFF)
  151 option(BUILD_TENSOREXPR_BENCHMARK "Build C++ binaries for tensorexpr benchmarks (need gbenchmark)" OFF)
  152 option(BUILD_MOBILE_BENCHMARK "Build C++ test binaries for mobile (ARM) targets(need gtest and gbenchmark)" OFF)
  153 option(BUILD_MOBILE_TEST "Build C++ test binaries for mobile (ARM) targets(need gtest and gbenchmark)" OFF)
  154 option(BUILD_JNI "Build JNI bindings" OFF)
  155 option(BUILD_MOBILE_AUTOGRAD "Build autograd function in mobile build (in development)" OFF)
  156 cmake_dependent_option(
  157     INSTALL_TEST "Install test binaries if BUILD_TEST is on" ON
  158     "BUILD_TEST" OFF)
  159 option(USE_CPP_CODE_COVERAGE "Compile C/C++ with code coverage flags" OFF)
  160 option(COLORIZE_OUTPUT "Colorize output during compilation" ON)
  161 option(USE_ASAN "Use Address Sanitizer" OFF)
  162 option(USE_TSAN "Use Thread Sanitizer" OFF)
  163 option(USE_CUDA "Use CUDA" ON)
  164 # BUILD_SPLIT_CUDA must also be exported as an environment variable before building, with
  165 # `export BUILD_SPLIT_CUDA=1` because cpp_extension.py can only work properly if this variable
  166 # also exists in the environment.
  167 # This option is incompatible with CUDA_SEPARABLE_COMPILATION.
  168 cmake_dependent_option(
  169     BUILD_SPLIT_CUDA "Split torch_cuda library into torch_cuda_cu and torch_cuda_cpp" OFF
  170     "USE_CUDA AND NOT CUDA_SEPARABLE_COMPILATION" OFF)
  171 option(USE_FAST_NVCC "Use parallel NVCC build" OFF)
  172 option(USE_ROCM "Use ROCm" ON)
  173 option(CAFFE2_STATIC_LINK_CUDA "Statically link CUDA libraries" OFF)
  174 cmake_dependent_option(
  175     USE_CUDNN "Use cuDNN" ON
  176     "USE_CUDA" OFF)
  177 cmake_dependent_option(
  178     USE_STATIC_CUDNN "Use cuDNN static libraries" OFF
  179     "USE_CUDNN" OFF)
  180 option(USE_FBGEMM "Use FBGEMM (quantized 8-bit server operators)" ON)
  181 option(USE_KINETO "Use Kineto profiling library" ON)
  182 option(USE_CUPTI_SO "Use CUPTI as a shared library" OFF)
  183 option(USE_FAKELOWP "Use FakeLowp operators" OFF)
  184 option(USE_FFMPEG "Use ffmpeg" OFF)
  185 option(USE_GFLAGS "Use GFLAGS" OFF)
  186 option(USE_GLOG "Use GLOG" OFF)
  187 option(USE_LEVELDB "Use LEVELDB" OFF)
  188 option(USE_LITE_PROTO "Use lite protobuf instead of full." OFF)
  189 option(USE_LMDB "Use LMDB" OFF)
  190 option(USE_METAL "Use Metal for Caffe2 iOS build" ON)
  191 option(USE_PYTORCH_METAL "Use Metal for PyTorch iOS build" OFF)
  192 option(USE_NATIVE_ARCH "Use -march=native" OFF)
  193 cmake_dependent_option(
  194     USE_NCCL "Use NCCL" ON
  195     "USE_CUDA OR USE_ROCM;UNIX;NOT APPLE" OFF)
  196 cmake_dependent_option(USE_RCCL "Use RCCL" ON
  197     USE_NCCL OFF)
  198 cmake_dependent_option(
  199     USE_STATIC_NCCL "Use static NCCL" OFF
  200     "USE_NCCL" OFF)
  201 cmake_dependent_option(
  202     USE_SYSTEM_NCCL "Use system-wide NCCL" OFF
  203     "USE_NCCL" OFF)
  204 option(USE_NNAPI "Use NNAPI" OFF)
  205 option(USE_NNPACK "Use NNPACK" ON)
  206 cmake_dependent_option(
  207     USE_NUMA "Use NUMA. Only available on Linux." ON
  208     "LINUX" OFF)
  209 cmake_dependent_option(
  210     USE_NVRTC "Use NVRTC. Only available if USE_CUDA is on." OFF
  211     "USE_CUDA" OFF)
  212 option(USE_NUMPY "Use NumPy" ON)
  213 option(USE_OBSERVERS "Use observers module." OFF)
  214 option(USE_OPENCL "Use OpenCL" OFF)
  215 option(USE_OPENCV "Use OpenCV" OFF)
  216 option(USE_OPENMP "Use OpenMP for parallel code" ON)
  217 option(USE_PROF "Use profiling" OFF)
  218 option(USE_QNNPACK "Use QNNPACK (quantized 8-bit operators)" ON)
  219 option(USE_PYTORCH_QNNPACK "Use ATen/QNNPACK (quantized 8-bit operators)" ON)
  220 option(USE_REDIS "Use Redis" OFF)
  221 option(USE_ROCKSDB "Use RocksDB" OFF)
  222 option(USE_SNPE "Use Qualcomm's SNPE library" OFF)
  223 option(USE_SYSTEM_EIGEN_INSTALL
  224     "Use system Eigen instead of the one under third_party" OFF)
  225 option(USE_TENSORRT "Using Nvidia TensorRT library" OFF)
  226 cmake_dependent_option(
  227     USE_VALGRIND "Use Valgrind. Only available on Linux." ON
  228     "LINUX" OFF)
  229 
  230 if(NOT DEFINED USE_VULKAN)
  231   cmake_dependent_option(
  232       USE_VULKAN "Use Vulkan GPU backend" ON
  233       "ANDROID" OFF)
  234 endif()
  235 
  236 option(USE_VULKAN_FP16_INFERENCE "Vulkan - Use fp16 inference even on fp32 tensors" OFF)
  237 option(USE_VULKAN_RELAXED_PRECISION "Vulkan - Use relaxed precision math in the kernels (mediump)" OFF)
  238 option(USE_VULKAN_SHADERC_RUNTIME "Vulkan - Use runtime shader compilation (needs libshaderc)" OFF)
  239 option(USE_VULKAN_WRAPPER "Vulkan - Dynamically load Vulkan functions" ON)
  240 option(USE_XNNPACK "Use XNNPACK" ON)
  241 option(USE_ZMQ "Use ZMQ" OFF)
  242 option(USE_ZSTD "Use ZSTD" OFF)
  243 # Ensure that an MKLDNN build is the default for x86 CPUs
  244 # but optional for AArch64 (dependent on -DUSE_MKLDNN).
  245 cmake_dependent_option(
  246   USE_MKLDNN "Use MKLDNN. Only available on x86, x86_64, and AArch64." "${CPU_INTEL}"
  247   "CPU_INTEL OR CPU_AARCH64" OFF)
  248 set(MKLDNN_ENABLE_CONCURRENT_EXEC ${USE_MKLDNN})
  249 cmake_dependent_option(
  250     USE_MKLDNN_CBLAS "Use CBLAS in MKLDNN" OFF
  251     "USE_MKLDNN" OFF)
  252 option(USE_DISTRIBUTED "Use distributed" ON)
  253 cmake_dependent_option(
  254     USE_MPI "Use MPI for Caffe2. Only available if USE_DISTRIBUTED is on." ON
  255     "USE_DISTRIBUTED" OFF)
  256 cmake_dependent_option(
  257     USE_GLOO "Use Gloo. Only available if USE_DISTRIBUTED is on." ON
  258     "USE_DISTRIBUTED" OFF)
  259 cmake_dependent_option(
  260     USE_TENSORPIPE "Use TensorPipe. Only available if USE_DISTRIBUTED is on." ON
  261     "USE_DISTRIBUTED" OFF)
  262 option(USE_TBB "Use TBB" OFF)
  263 option(ONNX_ML "Enable traditional ONNX ML API." ON)
  264 option(HAVE_SOVERSION "Whether to add SOVERSION to the shared objects" OFF)
  265 cmake_dependent_option(
  266     USE_DEPLOY "Build embedded torch::deploy interpreter" OFF
  267     "BUILD_PYTHON" OFF)
  268 # Since TensorPipe does not support Windows, set it to OFF when WIN32 detected
  269 # On Windows platform, if user does not install libuv in build conda env and
  270 # does not set libuv_ROOT environment variable. Set USE_DISTRIBUTED to OFF.
  271 if(WIN32)
  272   set(USE_TENSORPIPE OFF)
  273   message(WARNING "TensorPipe cannot be used on Windows. Set it to OFF")
  274 
  275   if(USE_DISTRIBUTED AND NOT DEFINED ENV{libuv_ROOT})
  276     find_library(
  277       libuv_tmp_LIBRARY
  278       NAMES uv libuv
  279       HINTS $ENV{CONDA_PREFIX}\\Library $ENV{PREFIX}\\Library
  280       PATH_SUFFIXES lib
  281       NO_DEFAULT_PATH)
  282     if(NOT libuv_tmp_LIBRARY)
  283       set(USE_DISTRIBUTED OFF)
  284       set(USE_GLOO OFF)
  285       message(
  286         WARNING "Libuv is not installed in current conda env. Set USE_DISTRIBUTED to OFF. "
  287         "Please run command 'conda install -c conda-forge libuv=1.39' to install libuv.")
  288     else()
  289       set(ENV{libuv_ROOT} ${libuv_tmp_LIBRARY}/../../)
  290     endif()
  291   endif()
  292 endif()
  293 
  294 # Linux distributions do not want too many embedded sources, in that sense we
  295 # need to be able to build pytorch with an (almost) empty third_party
  296 # directory.
  297 # USE_SYSTEM_LIBS is a shortcut variable to toggle all the # USE_SYSTEM_*
  298 # variables on. Individual USE_SYSTEM_* variables can be toggled with
  299 # USE_SYSTEM_LIBS being "OFF".
  300 option(USE_SYSTEM_LIBS "Use all available system-provided libraries." OFF)
  301 option(USE_SYSTEM_CPUINFO "Use system-provided cpuinfo." OFF)
  302 option(USE_SYSTEM_SLEEF "Use system-provided sleef." OFF)
  303 option(USE_SYSTEM_GLOO "Use system-provided gloo." OFF)
  304 option(USE_SYSTEM_FP16 "Use system-provided fp16." OFF)
  305 option(USE_SYSTEM_PTHREADPOOL "Use system-provided pthreadpool." OFF)
  306 option(USE_SYSTEM_PSIMD "Use system-provided psimd." OFF)
  307 option(USE_SYSTEM_FXDIV "Use system-provided fxdiv." OFF)
  308 option(USE_SYSTEM_BENCHMARK "Use system-provided google benchmark." OFF)
  309 option(USE_SYSTEM_ONNX "Use system-provided onnx." OFF)
  310 option(USE_SYSTEM_XNNPACK "Use system-provided xnnpack." OFF)
  311 if(USE_SYSTEM_LIBS)
  312   set(USE_SYSTEM_CPUINFO ON)
  313   set(USE_SYSTEM_SLEEF ON)
  314   set(USE_SYSTEM_GLOO ON)
  315   set(BUILD_CUSTOM_PROTOBUF OFF)
  316   set(USE_SYSTEM_EIGEN_INSTALL ON)
  317   set(USE_SYSTEM_FP16 ON)
  318   set(USE_SYSTEM_PTHREADPOOL ON)
  319   set(USE_SYSTEM_PSIMD ON)
  320   set(USE_SYSTEM_FXDIV ON)
  321   set(USE_SYSTEM_BENCHMARK ON)
  322   set(USE_SYSTEM_ONNX ON)
  323   set(USE_SYSTEM_XNNPACK ON)
  324 endif()
  325 
  326 # Used when building Caffe2 through setup.py
  327 option(BUILDING_WITH_TORCH_LIBS "Tell cmake if Caffe2 is being built alongside torch libs" ON)
  328 
  329 # /Z7 override option
  330 # When generating debug symbols, CMake default to use the flag /Zi.
  331 # However, it is not compatible with sccache. So we rewrite it off.
  332 # But some users don't use sccache; this override is for them.
  333 cmake_dependent_option(
  334   MSVC_Z7_OVERRIDE "Work around sccache bug by replacing /Zi and /ZI with /Z7 when using MSVC (if you are not using sccache, you can turn this OFF)" ON
  335   "MSVC" OFF)
  336 
  337 if(NOT USE_SYSTEM_ONNX)
  338   set(ONNX_NAMESPACE "onnx_torch" CACHE STRING "A namespace for ONNX; needed to build with other frameworks that share ONNX.")
  339 elseif()
  340   set(ONNX_NAMESPACE "onnx" CACHE STRING "A namespace for ONNX; needed to build with other frameworks that share ONNX.")
  341 endif()
  342 set(SELECTED_OP_LIST "" CACHE STRING
  343     "Path to the yaml file that contains the list of operators to include for custom build. Include all operators by default.")
  344 set(OP_DEPENDENCY "" CACHE STRING
  345     "Path to the yaml file that contains the op dependency graph for custom build.")
  346 
  347 # This is a fix for a rare build issue on Ubuntu:
  348 # symbol lookup error: miniconda3/envs/pytorch-py3.7/lib/libmkl_intel_lp64.so: undefined symbol: mkl_blas_dsyrk
  349 # https://software.intel.com/en-us/articles/symbol-lookup-error-when-linking-intel-mkl-with-gcc-on-ubuntu
  350 if(LINUX)
  351   set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -Wl,--no-as-needed")
  352 endif()
  353 
  354 if(MSVC)
  355   foreach(flag_var
  356       CMAKE_C_FLAGS CMAKE_C_FLAGS_DEBUG CMAKE_C_FLAGS_RELEASE
  357       CMAKE_C_FLAGS_MINSIZEREL CMAKE_C_FLAGS_RELWITHDEBINFO
  358       CMAKE_CXX_FLAGS CMAKE_CXX_FLAGS_DEBUG CMAKE_CXX_FLAGS_RELEASE
  359       CMAKE_CXX_FLAGS_MINSIZEREL CMAKE_CXX_FLAGS_RELWITHDEBINFO)
  360     # Replace /Zi and /ZI with /Z7
  361     if(MSVC_Z7_OVERRIDE)
  362       if(${flag_var} MATCHES "/Z[iI]")
  363         string(REGEX REPLACE "/Z[iI]" "/Z7" ${flag_var} "${${flag_var}}")
  364       endif(${flag_var} MATCHES "/Z[iI]")
  365     endif(MSVC_Z7_OVERRIDE)
  366     # Turn off warnings on Windows.  In an ideal world we'd be warning
  367     # clean on Windows too, but this is too much work for our
  368     # non-Windows developers.
  369     #
  370     # NB: Technically, this is not necessary if CMP0092 was applied
  371     # properly, but only cmake >= 3.15 has this policy, so we nail
  372     # it one more time just be safe.
  373     #
  374     # NB2: This is NOT enough to prevent warnings from nvcc on MSVC.  At the
  375     # moment only CMP0092 is enough to prevent those warnings too.
  376     string(REPLACE "/W3" "" ${flag_var} "${${flag_var}}")
  377 
  378     # Turn off warnings (Windows build is currently is extremely warning
  379     # unclean and the warnings aren't telling us anything useful.)
  380     string(APPEND ${flag_var} " /w")
  381 
  382     if(${CAFFE2_USE_MSVC_STATIC_RUNTIME})
  383       if(${flag_var} MATCHES "/MD")
  384         string(REGEX REPLACE "/MD" "/MT" ${flag_var} "${${flag_var}}")
  385       endif(${flag_var} MATCHES "/MD")
  386     else()
  387       if(${flag_var} MATCHES "/MT")
  388         string(REGEX REPLACE "/MT" "/MD" ${flag_var} "${${flag_var}}")
  389       endif()
  390     endif()
  391 
  392     # /bigobj increases number of sections in .obj file, which is needed to link
  393     # against libraries in Python 2.7 under Windows
  394     # For Visual Studio generators, if /MP is not added, then we may need
  395     # to add /MP to the flags.
  396     # For other generators like ninja, we don't need to add /MP because it is
  397     # already handled by the generator itself.
  398     if(CMAKE_GENERATOR MATCHES "Visual Studio" AND NOT ${flag_var} MATCHES "/MP")
  399       set(${flag_var} "${${flag_var}} /MP /bigobj")
  400     else()
  401       set(${flag_var} "${${flag_var}} /bigobj")
  402     endif()
  403   endforeach(flag_var)
  404 
  405   foreach(flag_var
  406       CMAKE_C_FLAGS CMAKE_C_FLAGS_RELEASE CMAKE_C_FLAGS_MINSIZEREL
  407       CMAKE_CXX_FLAGS CMAKE_CXX_FLAGS_RELEASE CMAKE_CXX_FLAGS_MINSIZEREL)
  408     if(${flag_var} MATCHES "/Z[iI7]")
  409       string(REGEX REPLACE "/Z[iI7]" "" ${flag_var} "${${flag_var}}")
  410     endif()
  411   endforeach(flag_var)
  412 
  413   foreach(flag_var
  414       CMAKE_SHARED_LINKER_FLAGS_DEBUG CMAKE_STATIC_LINKER_FLAGS_DEBUG
  415       CMAKE_EXE_LINKER_FLAGS_DEBUG CMAKE_MODULE_LINKER_FLAGS_DEBUG)
  416     # Switch off incremental linking in debug builds
  417     if(${flag_var} MATCHES "/INCREMENTAL" AND NOT ${flag_var} MATCHES "/INCREMENTAL:NO")
  418       string(REGEX REPLACE "/INCREMENTAL" "/INCREMENTAL:NO" ${flag_var} "${${flag_var}}")
  419     endif()
  420   endforeach(flag_var)
  421 
  422   foreach(flag_var
  423       CMAKE_SHARED_LINKER_FLAGS CMAKE_STATIC_LINKER_FLAGS
  424       CMAKE_EXE_LINKER_FLAGS CMAKE_MODULE_LINKER_FLAGS)
  425     string(APPEND ${flag_var} " /ignore:4049 /ignore:4217")
  426   endforeach(flag_var)
  427 
  428   # Try harder
  429   list(APPEND CUDA_NVCC_FLAGS "-Xcompiler" "/w" "-w")
  430 endif(MSVC)
  431 
  432 list(APPEND CUDA_NVCC_FLAGS "-Xfatbin" "-compress-all")
  433 list(APPEND CUDA_NVCC_FLAGS_DEBUG "-Xfatbin" "-compress-all")
  434 list(APPEND CUDA_NVCC_FLAGS_RELWITHDEBINFO "-Xfatbin" "-compress-all")
  435 
  436 if(NOT MSVC)
  437   list(APPEND CUDA_NVCC_FLAGS_DEBUG "-g" "-lineinfo" "--source-in-ptx")
  438   list(APPEND CUDA_NVCC_FLAGS_RELWITHDEBINFO "-g" "-lineinfo" "--source-in-ptx")
  439 endif(NOT MSVC)
  440 
  441 # Set INTERN_BUILD_MOBILE for all mobile builds. Components that are not
  442 # applicable to mobile are disabled by this variable.
  443 # Setting `BUILD_PYTORCH_MOBILE_WITH_HOST_TOOLCHAIN` environment variable can
  444 # force it to do mobile build with host toolchain - which is useful for testing
  445 # purpose.
  446 if(ANDROID OR IOS OR DEFINED ENV{BUILD_PYTORCH_MOBILE_WITH_HOST_TOOLCHAIN})
  447   set(INTERN_BUILD_MOBILE ON)
  448 
  449   if(DEFINED ENV{BUILD_PYTORCH_MOBILE_WITH_HOST_TOOLCHAIN})
  450     # C10_MOBILE is derived from Android/iOS toolchain macros in
  451     # c10/macros/Macros.h, so it needs to be explicitly set here.
  452     string(APPEND CMAKE_CXX_FLAGS " -DC10_MOBILE")
  453   endif()
  454 endif()
  455 
  456 # INTERN_BUILD_ATEN_OPS is used to control whether to build ATen/TH operators.
  457 # It's disabled for caffe2 mobile library.
  458 if(INTERN_BUILD_MOBILE AND BUILD_CAFFE2_MOBILE)
  459   set(INTERN_BUILD_ATEN_OPS OFF)
  460 else()
  461   set(INTERN_BUILD_ATEN_OPS ON)
  462 endif()
  463 
  464 # BUILD_CAFFE2_MOBILE is the master switch to choose between libcaffe2 v.s. libtorch mobile build.
  465 # When it's enabled it builds original libcaffe2 mobile library without ATen/TH ops nor TorchScript support;
  466 # When it's disabled it builds libtorch mobile library, which contains ATen/TH ops and native support for
  467 # TorchScript model, but doesn't contain not-yet-unified caffe2 ops;
  468 if(INTERN_BUILD_MOBILE AND NOT BUILD_CAFFE2_MOBILE)
  469   if(NOT BUILD_SHARED_LIBS AND NOT "${SELECTED_OP_LIST}" STREQUAL "")
  470     string(APPEND CMAKE_CXX_FLAGS " -DNO_EXPORT")
  471   endif()
  472   if(BUILD_MOBILE_AUTOGRAD)
  473     set(INTERN_DISABLE_AUTOGRAD OFF)
  474   else()
  475     set(INTERN_DISABLE_AUTOGRAD ON)
  476   endif()
  477   set(BUILD_PYTHON OFF)
  478   set(BUILD_CAFFE2_OPS OFF)
  479   set(USE_DISTRIBUTED OFF)
  480   set(FEATURE_TORCH_MOBILE ON)
  481   set(NO_API ON)
  482   set(USE_FBGEMM OFF)
  483   set(USE_QNNPACK OFF)
  484   set(INTERN_DISABLE_ONNX ON)
  485   set(INTERN_USE_EIGEN_BLAS ON)
  486   # Disable developing mobile interpreter for actual mobile build.
  487   # Enable it elsewhere to capture build error.
  488   set(INTERN_DISABLE_MOBILE_INTERP ON)
  489 endif()
  490 
  491 # ---[ Utils
  492 include(cmake/public/utils.cmake)
  493 
  494 # ---[ Version numbers for generated libraries
  495 file(READ version.txt TORCH_DEFAULT_VERSION)
  496 # Strip trailing newline
  497 string(REGEX REPLACE "\n$" "" TORCH_DEFAULT_VERSION "${TORCH_DEFAULT_VERSION}")
  498 if("${TORCH_DEFAULT_VERSION} " STREQUAL " ")
  499   message(WARNING "Could not get version from base 'version.txt'")
  500   # If we can't get the version from the version file we should probably
  501   # set it to something non-sensical like 0.0.0
  502   set(TORCH_DEFAULT_VERSION, "0.0.0")
  503 endif()
  504 set(TORCH_BUILD_VERSION "${TORCH_DEFAULT_VERSION}" CACHE STRING "Torch build version")
  505 if(DEFINED ENV{PYTORCH_BUILD_VERSION})
  506   set(TORCH_BUILD_VERSION "$ENV{PYTORCH_BUILD_VERSION}"
  507     CACHE STRING "Torch build version" FORCE)
  508 endif()
  509 if(NOT TORCH_BUILD_VERSION)
  510   # An empty string was specified so force version to the default
  511   set(TORCH_BUILD_VERSION "${TORCH_DEFAULT_VERSION}"
  512     CACHE STRING "Torch build version" FORCE)
  513 endif()
  514 caffe2_parse_version_str(TORCH ${TORCH_BUILD_VERSION})
  515 caffe2_parse_version_str(CAFFE2 ${TORCH_BUILD_VERSION})
  516 set(TORCH_SOVERSION "${TORCH_VERSION_MAJOR}.${TORCH_VERSION_MINOR}")
  517 
  518 # ---[ CMake scripts + modules
  519 list(APPEND CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake/Modules)
  520 
  521 # ---[ CMake build directories
  522 set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
  523 set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
  524 set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
  525 
  526 enable_testing()
  527 
  528 # ---[ Build variables set within the cmake tree
  529 include(cmake/BuildVariables.cmake)
  530 set(CAFFE2_ALLOWLIST "" CACHE STRING "A allowlist file of files that one should build.")
  531 
  532 # Set default build type
  533 if(NOT CMAKE_BUILD_TYPE)
  534     message(STATUS "Build type not set - defaulting to Release")
  535     set(CMAKE_BUILD_TYPE "Release" CACHE STRING "Choose the type of build from: Debug Release RelWithDebInfo MinSizeRel Coverage." FORCE)
  536 endif()
  537 
  538 # The below means we are cross compiling for arm64 or x86_64 on MacOSX
  539 if(NOT IOS AND CMAKE_SYSTEM_NAME STREQUAL "Darwin" AND CMAKE_OSX_ARCHITECTURES MATCHES "^(x86_64|arm64)$")
  540   set(CROSS_COMPILING_MACOSX TRUE)
  541   # We need to compile a universal protoc to not fail protobuf build
  542   execute_process(COMMAND ./scripts/build_host_protoc.sh --other-flags "-DCMAKE_OSX_ARCHITECTURES=x86_64;arm64"
  543                   WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
  544                   RESULT_VARIABLE BUILD_HOST_PROTOC_RESULT)
  545   if(NOT BUILD_HOST_PROTOC_RESULT EQUAL "0")
  546     message(FATAL_ERROR "Could not compile universal protoc.")
  547   endif()
  548   set(PROTOBUF_PROTOC_EXECUTABLE "${PROJECT_SOURCE_DIR}/build_host_protoc/bin/protoc")
  549   set(CAFFE2_CUSTOM_PROTOC_EXECUTABLE "${PROJECT_SOURCE_DIR}/build_host_protoc/bin/protoc")
  550 endif()
  551 
  552 # ---[ Misc checks to cope with various compiler modes
  553 include(cmake/MiscCheck.cmake)
  554 
  555 # External projects
  556 include(ExternalProject)
  557 
  558 # ---[ Dependencies
  559 # ---[ FBGEMM doesn't work on x86 32bit and CMAKE_SYSTEM_PROCESSOR thinks its 64bit
  560 if(USE_FBGEMM AND ((CMAKE_SYSTEM_PROCESSOR STREQUAL "x86_64" AND CMAKE_SIZEOF_VOID_P EQUAL 4) OR CMAKE_SYSTEM_PROCESSOR STREQUAL "x86"))
  561   set(USE_FBGEMM OFF)
  562 endif()
  563 
  564 include(cmake/Dependencies.cmake)
  565 
  566 if(USE_FBGEMM)
  567   string(APPEND CMAKE_CXX_FLAGS " -DUSE_FBGEMM")
  568 endif()
  569 
  570 if(USE_QNNPACK)
  571   string(APPEND CMAKE_CXX_FLAGS " -DUSE_QNNPACK")
  572 endif()
  573 
  574 if(USE_PYTORCH_QNNPACK)
  575   string(APPEND CMAKE_CXX_FLAGS " -DUSE_PYTORCH_QNNPACK")
  576 endif()
  577 
  578 if(USE_XNNPACK)
  579   string(APPEND CMAKE_CXX_FLAGS " -DUSE_XNNPACK")
  580 endif()
  581 
  582 if(USE_VULKAN)
  583   string(APPEND CMAKE_CXX_FLAGS " -DUSE_VULKAN")
  584   string(APPEND CMAKE_CXX_FLAGS " -DUSE_VULKAN_API")
  585 
  586   if(USE_VULKAN_FP16_INFERENCE)
  587     string(APPEND CMAKE_CXX_FLAGS " -DUSE_VULKAN_FP16_INFERENCE")
  588   endif()
  589 
  590   if(USE_VULKAN_RELAXED_PRECISION)
  591     string(APPEND CMAKE_CXX_FLAGS " -DUSE_VULKAN_RELAXED_PRECISION")
  592   endif()
  593 
  594   if(USE_VULKAN_SHADERC_RUNTIME)
  595     string(APPEND CMAKE_CXX_FLAGS " -DUSE_VULKAN_SHADERC_RUNTIME")
  596   endif()
  597 
  598   if(USE_VULKAN_WRAPPER)
  599     string(APPEND CMAKE_CXX_FLAGS " -DUSE_VULKAN_WRAPPER")
  600   endif()
  601 endif()
  602 
  603 if(USE_PYTORCH_METAL)
  604   string(APPEND CMAKE_CXX_FLAGS " -DUSE_PYTORCH_METAL")
  605 endif()
  606 
  607 # ---[ Allowlist file if allowlist is specified
  608 include(cmake/Allowlist.cmake)
  609 
  610 # ---[ Set link flag, handle additional deps for gcc 4.8 and above
  611 if(CMAKE_COMPILER_IS_GNUCXX AND CMAKE_CXX_COMPILER_VERSION VERSION_GREATER 4.8.0 AND NOT ANDROID)
  612   message(STATUS "GCC ${CMAKE_CXX_COMPILER_VERSION}: Adding gcc and gcc_s libs to link line")
  613   list(APPEND Caffe2_DEPENDENCY_LIBS gcc_s gcc)
  614 endif()
  615 
  616 # ---[ Build flags
  617 set(CMAKE_C_STANDARD 11)
  618 set(CMAKE_CXX_STANDARD 14)
  619 if(NOT MSVC)
  620   string(APPEND CMAKE_CXX_FLAGS " -O2 -fPIC")
  621   string(APPEND CMAKE_CXX_FLAGS " -Wno-narrowing")
  622   # Eigen fails to build with some versions, so convert this to a warning
  623   # Details at http://eigen.tuxfamily.org/bz/show_bug.cgi?id=1459
  624   string(APPEND CMAKE_CXX_FLAGS " -Wall")
  625   string(APPEND CMAKE_CXX_FLAGS " -Wextra")
  626   string(APPEND CMAKE_CXX_FLAGS " -Werror=return-type")
  627   string(APPEND CMAKE_CXX_FLAGS " -Wno-missing-field-initializers")
  628   string(APPEND CMAKE_CXX_FLAGS " -Wno-type-limits")
  629   string(APPEND CMAKE_CXX_FLAGS " -Wno-array-bounds")
  630   string(APPEND CMAKE_CXX_FLAGS " -Wno-unknown-pragmas")
  631   string(APPEND CMAKE_CXX_FLAGS " -Wno-sign-compare")
  632   string(APPEND CMAKE_CXX_FLAGS " -Wno-unused-parameter")
  633   string(APPEND CMAKE_CXX_FLAGS " -Wno-unused-variable")
  634   string(APPEND CMAKE_CXX_FLAGS " -Wno-unused-function")
  635   string(APPEND CMAKE_CXX_FLAGS " -Wno-unused-result")
  636   string(APPEND CMAKE_CXX_FLAGS " -Wno-unused-local-typedefs")
  637   string(APPEND CMAKE_CXX_FLAGS " -Wno-strict-overflow")
  638   string(APPEND CMAKE_CXX_FLAGS " -Wno-strict-aliasing")
  639   string(APPEND CMAKE_CXX_FLAGS " -Wno-error=deprecated-declarations")
  640   if(CMAKE_COMPILER_IS_GNUCXX AND NOT (CMAKE_CXX_COMPILER_VERSION VERSION_LESS 7.0.0))
  641     string(APPEND CMAKE_CXX_FLAGS " -Wno-stringop-overflow")
  642   endif()
  643   if(CMAKE_COMPILER_IS_GNUCXX)
  644     # Suppress "The ABI for passing parameters with 64-byte alignment has changed in GCC 4.6"
  645     string(APPEND CMAKE_CXX_FLAGS " -Wno-psabi")
  646   endif()
  647   string(APPEND CMAKE_CXX_FLAGS " -Wno-error=pedantic")
  648   string(APPEND CMAKE_CXX_FLAGS " -Wno-error=redundant-decls")
  649   string(APPEND CMAKE_CXX_FLAGS " -Wno-error=old-style-cast")
  650   # These flags are not available in GCC-4.8.5. Set only when using clang.
  651   # Compared against https://gcc.gnu.org/onlinedocs/gcc-4.8.5/gcc/Option-Summary.html
  652   if("${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang")
  653     string(APPEND CMAKE_CXX_FLAGS " -Wno-invalid-partial-specialization")
  654     string(APPEND CMAKE_CXX_FLAGS " -Wno-typedef-redefinition")
  655     string(APPEND CMAKE_CXX_FLAGS " -Wno-unknown-warning-option")
  656     string(APPEND CMAKE_CXX_FLAGS " -Wno-unused-private-field")
  657     string(APPEND CMAKE_CXX_FLAGS " -Wno-inconsistent-missing-override")
  658     string(APPEND CMAKE_CXX_FLAGS " -Wno-aligned-allocation-unavailable")
  659     string(APPEND CMAKE_CXX_FLAGS " -Wno-c++14-extensions")
  660     string(APPEND CMAKE_CXX_FLAGS " -Wno-constexpr-not-const")
  661     string(APPEND CMAKE_CXX_FLAGS " -Wno-missing-braces")
  662     string(APPEND CMAKE_CXX_FLAGS " -Qunused-arguments")
  663     if(${COLORIZE_OUTPUT})
  664       string(APPEND CMAKE_CXX_FLAGS " -fcolor-diagnostics")
  665     endif()
  666   endif()
  667   if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU" AND CMAKE_CXX_COMPILER_VERSION VERSION_GREATER 4.9)
  668     if(${COLORIZE_OUTPUT})
  669       string(APPEND CMAKE_CXX_FLAGS " -fdiagnostics-color=always")
  670     endif()
  671   endif()
  672   if((APPLE AND (NOT ("${CLANG_VERSION_STRING}" VERSION_LESS "9.0")))
  673     OR(CMAKE_COMPILER_IS_GNUCXX
  674     AND(CMAKE_CXX_COMPILER_VERSION VERSION_GREATER 7.0 AND NOT APPLE)))
  675     string(APPEND CMAKE_CXX_FLAGS " -faligned-new")
  676   endif()
  677   if(WERROR)
  678     check_cxx_compiler_flag("-Werror" COMPILER_SUPPORT_WERROR)
  679     if(NOT COMPILER_SUPPORT_WERROR)
  680       set(WERROR FALSE)
  681     else()
  682       string(APPEND CMAKE_CXX_FLAGS " -Werror")
  683     endif()
  684   endif(WERROR)
  685   if(NOT APPLE)
  686     string(APPEND CMAKE_CXX_FLAGS " -Wno-unused-but-set-variable")
  687     string(APPEND CMAKE_CXX_FLAGS " -Wno-maybe-uninitialized")
  688   endif()
  689   string(APPEND CMAKE_CXX_FLAGS_DEBUG " -fno-omit-frame-pointer -O0")
  690   string(APPEND CMAKE_LINKER_FLAGS_DEBUG " -fno-omit-frame-pointer -O0")
  691   string(APPEND CMAKE_CXX_FLAGS " -fno-math-errno")
  692   string(APPEND CMAKE_CXX_FLAGS " -fno-trapping-math")
  693   check_cxx_compiler_flag("-Werror=format" HAS_WERROR_FORMAT)
  694   if(HAS_WERROR_FORMAT)
  695     string(APPEND CMAKE_CXX_FLAGS " -Werror=format")
  696   endif()
  697   check_cxx_compiler_flag("-Werror=cast-function-type" HAS_WERROR_CAST_FUNCTION_TYPE)
  698   if(HAS_WERROR_CAST_FUNCTION_TYPE)
  699     string(APPEND CMAKE_CXX_FLAGS " -Werror=cast-function-type")
  700   endif()
  701 endif()
  702 
  703 if(USE_ASAN)
  704     string(APPEND CMAKE_CXX_FLAGS_DEBUG " -fsanitize=address")
  705     string(APPEND CMAKE_LINKER_FLAGS_DEBUG " -fsanitize=address")
  706 endif()
  707 
  708 if(CMAKE_SYSTEM_PROCESSOR MATCHES "aarch64")
  709   include(CheckCSourceCompiles)
  710   check_c_source_compiles("#include <arm_neon.h>
  711 int main() {
  712   float a[] = {1.0, 1.0};
  713   float32x4x2_t v;
  714   v.val[0] = vcombine_f32 (vcreate_f32 (0UL), vcreate_f32 (0UL));
  715   v.val[1] = vcombine_f32 (vcreate_f32 (0UL), vcreate_f32 (0UL));
  716   vst1q_f32_x2(a, v);
  717   return 0;
  718 }" HAS_VST1)
  719 
  720   if(NOT HAS_VST1)
  721     string(APPEND CMAKE_CXX_FLAGS " -DMISSING_ARM_VST1")
  722   endif()
  723 endif()
  724 
  725 if(CMAKE_SYSTEM_PROCESSOR MATCHES "aarch64")
  726   include(CheckCSourceCompiles)
  727   check_c_source_compiles("#include <arm_neon.h>
  728 int main() {
  729   float a[] = {1.0, 1.0};
  730   vld1q_f32_x2(a);
  731   return 0;
  732 }" HAS_VLD1)
  733 
  734   if(NOT HAS_VLD1)
  735     string(APPEND CMAKE_CXX_FLAGS " -DMISSING_ARM_VLD1")
  736   endif()
  737 endif()
  738 
  739 
  740 # Add code coverage flags to supported compilers
  741 if(USE_CPP_CODE_COVERAGE)
  742   if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU")
  743     string(APPEND CMAKE_C_FLAGS  " --coverage -fprofile-abs-path")
  744     string(APPEND CMAKE_CXX_FLAGS  " --coverage -fprofile-abs-path")
  745   elseif("${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang")
  746     string(APPEND CMAKE_C_FLAGS  " -fprofile-instr-generate -fcoverage-mapping")
  747     string(APPEND CMAKE_CXX_FLAGS " -fprofile-instr-generate -fcoverage-mapping")
  748   else()
  749     message(ERROR "Code coverage for compiler ${CMAKE_CXX_COMPILER_ID} is unsupported")
  750   endif()
  751 
  752 endif()
  753 
  754 if(APPLE)
  755     string(APPEND CMAKE_CXX_FLAGS " -Wno-unused-private-field")
  756     string(APPEND CMAKE_CXX_FLAGS " -Wno-missing-braces")
  757     string(APPEND CMAKE_CXX_FLAGS " -Wno-c++14-extensions")
  758     string(APPEND CMAKE_CXX_FLAGS " -Wno-constexpr-not-const")
  759 endif()
  760 
  761 if(EMSCRIPTEN)
  762   string(APPEND CMAKE_CXX_FLAGS " -Wno-implicit-function-declaration -DEMSCRIPTEN -s DISABLE_EXCEPTION_CATCHING=0")
  763 endif()
  764 
  765 if(CMAKE_COMPILER_IS_GNUCXX AND CMAKE_CXX_COMPILER_VERSION VERSION_GREATER 7.0.0)
  766   string(APPEND CMAKE_CXX_FLAGS " -Wno-stringop-overflow")
  767 endif()
  768 
  769 if(ANDROID AND (NOT ANDROID_DEBUG_SYMBOLS))
  770   if(CMAKE_COMPILER_IS_GNUCXX)
  771     string(APPEND CMAKE_CXX_FLAGS " -s")
  772   elseif("${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang")
  773     string(APPEND CMAKE_CXX_FLAGS " -g0")
  774   else()
  775     string(APPEND CMAKE_EXE_LINKER_FLAGS " -s")
  776   endif()
  777 endif()
  778 
  779 if(NOT APPLE AND UNIX)
  780   list(APPEND Caffe2_DEPENDENCY_LIBS dl)
  781 endif()
  782 
  783 # Prefix path to Caffe2 headers.
  784 # If a directory containing installed Caffe2 headers was inadvertently
  785 # added to the list of include directories, prefixing
  786 # PROJECT_SOURCE_DIR means this source tree always takes precedence.
  787 include_directories(BEFORE ${PROJECT_SOURCE_DIR})
  788 
  789 # Prefix path to generated Caffe2 headers.
  790 # These need to take precedence over their empty counterparts located
  791 # in PROJECT_SOURCE_DIR.
  792 include_directories(BEFORE ${PROJECT_BINARY_DIR})
  793 
  794 include_directories(BEFORE ${PROJECT_SOURCE_DIR}/aten/src/)
  795 include_directories(BEFORE ${PROJECT_BINARY_DIR}/aten/src/)
  796 
  797 # ---[ Main build
  798 add_subdirectory(c10)
  799 add_subdirectory(caffe2)
  800 
  801 # --[ Documentation
  802 if(BUILD_DOCS)
  803   # check if Doxygen is installed
  804   find_package(Doxygen)
  805   if(DOXYGEN_FOUND)
  806     message("Generating documentation")
  807 
  808     set(DOXYGEN_C_IN ${CMAKE_CURRENT_SOURCE_DIR}/docs/caffe2/.Doxyfile-c)
  809     set(DOXYGEN_C_OUT ${CMAKE_CURRENT_SOURCE_DIR}/docs/caffe2/Doxyfile-c)
  810     set(DOXYGEN_P_IN ${CMAKE_CURRENT_SOURCE_DIR}/docs/caffe2/.Doxyfile-python)
  811     set(DOXYGEN_P_OUT ${CMAKE_CURRENT_SOURCE_DIR}/docs/caffe2/Doxyfile-python)
  812 
  813     if(EXISTS ${CMAKE_CURRENT_BINARY_DIR}/docs)
  814       file(REMOVE_RECURSE ${CMAKE_CURRENT_BINARY_DIR}/docs)
  815     endif()
  816 
  817     file(MAKE_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/docs)
  818     configure_file(${DOXYGEN_C_IN} ${DOXYGEN_C_OUT} @ONLY)
  819     configure_file(${DOXYGEN_P_IN} ${DOXYGEN_P_OUT} @ONLY)
  820 
  821     add_custom_target(doc_doxygen_c ALL
  822         COMMAND ${DOXYGEN_EXECUTABLE} ${DOXYGEN_C_OUT}
  823         WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
  824         COMMENT "Generating C++ API documentation with Doxygen"
  825         VERBATIM)
  826 
  827     add_custom_target(doc_doxygen_python ALL
  828         COMMAND ${DOXYGEN_EXECUTABLE} ${DOXYGEN_P_OUT}
  829         WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
  830         COMMENT "Generating Python API documentation with Doxygen"
  831         VERBATIM)
  832   else()
  833     message(FATAL_ERROR "Doxygen needs to be installed to generate the documentation")
  834   endif()
  835 endif()
  836 
  837 # ---[ CMake related files
  838 # Uninistall option.
  839 if(NOT TARGET caffe2_uninstall)
  840   configure_file(
  841       ${CMAKE_CURRENT_SOURCE_DIR}/cmake/cmake_uninstall.cmake.in
  842       ${CMAKE_CURRENT_BINARY_DIR}/cmake_uninstall.cmake
  843       IMMEDIATE @ONLY)
  844 
  845   add_custom_target(caffe2_uninstall
  846       COMMAND ${CMAKE_COMMAND} -P
  847       ${CMAKE_CURRENT_BINARY_DIR}/cmake_uninstall.cmake)
  848 endif()
  849 
  850 # ---[ Make configuration files for cmake to allow dependent libraries
  851 # easier access to Caffe2.
  852 
  853 if((NOT USE_GLOG) OR (NOT USE_GFLAGS) OR BUILD_CUSTOM_PROTOBUF)
  854   message(WARNING
  855       "Generated cmake files are only fully tested if one builds "
  856       "with system glog, gflags, and protobuf. Other settings may "
  857       "generate files that are not well tested.")
  858 endif()
  859 
  860 if(USE_CUDA OR USE_ROCM)
  861   # TODO: check if we should include other cuda dependency libraries
  862   # to the interface as well.
  863 
  864 endif()
  865 
  866 # Note(jiayq): when building static libraries, all PRIVATE dependencies
  867 # will also become interface libraries, and as a result if there are any
  868 # dependency libraries that are not exported, the following install export
  869 # script will fail. As a result, we will only provide the targets cmake
  870 # files for shared lib installation. For more info, read:
  871 # https://cmake.org/pipermail/cmake/2016-May/063400.html
  872 if(BUILD_SHARED_LIBS)
  873   configure_file(
  874       ${PROJECT_SOURCE_DIR}/cmake/Caffe2ConfigVersion.cmake.in
  875       ${PROJECT_BINARY_DIR}/Caffe2ConfigVersion.cmake
  876       @ONLY)
  877   configure_file(
  878       ${PROJECT_SOURCE_DIR}/cmake/Caffe2Config.cmake.in
  879       ${PROJECT_BINARY_DIR}/Caffe2Config.cmake
  880       @ONLY)
  881   install(FILES
  882       ${PROJECT_BINARY_DIR}/Caffe2ConfigVersion.cmake
  883       ${PROJECT_BINARY_DIR}/Caffe2Config.cmake
  884       DESTINATION share/cmake/Caffe2
  885       COMPONENT dev)
  886   install(FILES
  887       ${PROJECT_SOURCE_DIR}/cmake/public/cuda.cmake
  888       ${PROJECT_SOURCE_DIR}/cmake/public/glog.cmake
  889       ${PROJECT_SOURCE_DIR}/cmake/public/gflags.cmake
  890       ${PROJECT_SOURCE_DIR}/cmake/public/mkl.cmake
  891       ${PROJECT_SOURCE_DIR}/cmake/public/mkldnn.cmake
  892       ${PROJECT_SOURCE_DIR}/cmake/public/protobuf.cmake
  893       ${PROJECT_SOURCE_DIR}/cmake/public/threads.cmake
  894       ${PROJECT_SOURCE_DIR}/cmake/public/utils.cmake
  895       DESTINATION share/cmake/Caffe2/public
  896       COMPONENT dev)
  897   install(DIRECTORY
  898       ${PROJECT_SOURCE_DIR}/cmake/Modules_CUDA_fix
  899       DESTINATION share/cmake/Caffe2/
  900       COMPONENT dev)
  901 
  902   install(EXPORT Caffe2Targets DESTINATION share/cmake/Caffe2
  903       FILE Caffe2Targets.cmake
  904       COMPONENT dev)
  905 else()
  906   message(WARNING
  907       "Generated cmake files are only available when building "
  908       "shared libs.")
  909 endif()
  910 
  911 # ---[ Modules
  912 # If master flag for buildling Caffe2 is disabled, we also disable the
  913 # build for Caffe2 related operator modules.
  914 if(BUILD_CAFFE2)
  915   add_subdirectory(modules)
  916 endif()
  917 
  918 # ---[ Binaries
  919 # Binaries will be built after the Caffe2 main libraries and the modules
  920 # are built. For the binaries, they will be linked to the Caffe2 main
  921 # libraries, as well as all the modules that are built with Caffe2 (the ones
  922 # built in the previous Modules section above).
  923 if(BUILD_BINARY)
  924   add_subdirectory(binaries)
  925 endif()
  926 
  927 # ---[ JNI
  928 if(BUILD_JNI)
  929   set(BUILD_LIBTORCH_WITH_JNI 1)
  930   set(FBJNI_SKIP_TESTS 1)
  931   add_subdirectory(android/pytorch_android)
  932 endif()
  933 
  934 include(cmake/Summary.cmake)
  935 caffe2_print_configuration_summary()
  936 
  937 # ---[ Torch Deploy
  938 if(USE_DEPLOY)
  939   add_subdirectory(torch/csrc/deploy)
  940 endif()