Description: The elegant patching work is based on the master branch
    https://github.com/pytorch/pytorch/issues/14699
  And we will be able to use that solution in the next upstream release.
  I don't want to rebase my patches back to this version, so let's go with a fast, yet dirty hack.
Author: Mo Zhou
Index: pytorch/CMakeLists.txt
===================================================================
--- pytorch.orig/CMakeLists.txt
+++ pytorch/CMakeLists.txt
@@ -155,10 +155,11 @@ endif()
 # Note to developers: if you add an option below, make sure you also add it to
 # cmake/Summary.cmake so that the summary prints out the option values.
 include(CMakeDependentOption)
+set(CMAKE_VERBOSE_MAKEFILE ON)
 option(ATEN_NO_TEST "Do not build ATen test binaries" OFF)
 option(BUILD_BINARY "Build C++ binaries" OFF)
 option(BUILD_DOCS "Build Caffe2 documentation" OFF)
-option(BUILD_CUSTOM_PROTOBUF "Build and use Caffe2's own protobuf under third_party" ON)
+option(BUILD_CUSTOM_PROTOBUF "Build and use Caffe2's own protobuf under third_party" OFF)
 option(BUILD_PYTHON "Build Python binaries" ON)
 option(BUILD_CAFFE2 "Master flag to build Caffe2" OFF)
 option(BUILD_LITE_INTERPRETER "Master flag to build Lite Interpreter" OFF)
@@ -241,7 +242,7 @@ cmake_dependent_option(
     USE_SYSTEM_NCCL "Use system-wide NCCL" OFF
     "USE_NCCL" OFF)
 option(USE_NNAPI "Use NNAPI" OFF)
-option(USE_NNPACK "Use NNPACK" ON)
+option(USE_NNPACK "Use NNPACK" OFF)
 cmake_dependent_option(
     USE_NUMA "Use NUMA. Only available on Linux." ON
     "LINUX" OFF)
@@ -258,13 +259,13 @@ cmake_dependent_option(
     "CMAKE_VERSION VERSION_GREATER_EQUAL \"3.16\"" OFF)
 
 option(USE_PROF "Use profiling" OFF)
-option(USE_QNNPACK "Use QNNPACK (quantized 8-bit operators)" ON)
+option(USE_QNNPACK "Use QNNPACK (quantized 8-bit operators)" OFF)
 option(USE_PYTORCH_QNNPACK "Use ATen/QNNPACK (quantized 8-bit operators)" ON)
 option(USE_REDIS "Use Redis" OFF)
 option(USE_ROCKSDB "Use RocksDB" OFF)
 option(USE_SNPE "Use Qualcomm's SNPE library" OFF)
 option(USE_SYSTEM_EIGEN_INSTALL
-    "Use system Eigen instead of the one under third_party" OFF)
+	"Use system Eigen instead of the one under third_party" ON)
 option(USE_TENSORRT "Using Nvidia TensorRT library" OFF)
 cmake_dependent_option(
     USE_VALGRIND "Use Valgrind. Only available on Linux." ON
@@ -343,7 +344,7 @@ option(USE_TBB "Use TBB (Deprecated)" OF
 cmake_dependent_option(
     USE_SYSTEM_TBB "Use system-provided Intel TBB." OFF "USE_TBB" OFF)
 option(ONNX_ML "Enable traditional ONNX ML API." ON)
-option(HAVE_SOVERSION "Whether to add SOVERSION to the shared objects" OFF)
+option(HAVE_SOVERSION "Whether to add SOVERSION to the shared objects" ON)
 option(BUILD_LIBTORCH_CPU_WITH_DEBUG "Enable RelWithDebInfo for libtorch_cpu target only" OFF)
 cmake_dependent_option(USE_CCACHE "Attempt using CCache to wrap the compilation" ON "UNIX" OFF)
 option(WERROR "Build with -Werror supported by the compiler" OFF)
@@ -415,6 +416,7 @@ option(USE_SYSTEM_FXDIV "Use system-prov
 option(USE_SYSTEM_BENCHMARK "Use system-provided google benchmark." OFF)
 option(USE_SYSTEM_ONNX "Use system-provided onnx." OFF)
 option(USE_SYSTEM_XNNPACK "Use system-provided xnnpack." OFF)
+option(USE_SYSTEM_TENSORPIPE "Use system-provided tensorpipe." OFF)
 option(USE_GOLD_LINKER "Use ld.gold to link" OFF)
 if(USE_SYSTEM_LIBS)
   set(USE_SYSTEM_CPUINFO ON)
@@ -429,6 +431,7 @@ if(USE_SYSTEM_LIBS)
   set(USE_SYSTEM_BENCHMARK ON)
   set(USE_SYSTEM_ONNX ON)
   set(USE_SYSTEM_XNNPACK ON)
+  set(USE_SYSTEM_TENSORPIPE ON)
   set(USE_SYSTEM_PYBIND11 ON)
   if(USE_TBB)
     set(USE_SYSTEM_TBB ON)
@@ -438,6 +441,8 @@ endif()
 # Used when building Caffe2 through setup.py
 option(BUILDING_WITH_TORCH_LIBS "Tell cmake if Caffe2 is being built alongside torch libs" ON)
 
+set(GOOGLETEST_SOURCE_DIR "/usr/src/googletest")
+
 # /Z7 override option
 # When generating debug symbols, CMake default to use the flag /Zi.
 # However, it is not compatible with sccache. So we rewrite it off.
Index: pytorch/cmake/Dependencies.cmake
===================================================================
--- pytorch.orig/cmake/Dependencies.cmake
+++ pytorch/cmake/Dependencies.cmake
@@ -10,10 +10,10 @@ endif(APPLE)
 set(CMAKE_SKIP_BUILD_RPATH  FALSE)
 # Don't use the install-rpath during the build phase
 set(CMAKE_BUILD_WITH_INSTALL_RPATH FALSE)
-set(CMAKE_INSTALL_RPATH "${_rpath_portable_origin}")
+set(CMAKE_INSTALL_RPATH "")
 # Automatically add all linked folders that are NOT in the build directory to
 # the rpath (per library?)
-set(CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE)
+set(CMAKE_INSTALL_RPATH_USE_LINK_PATH FALSE)
 
  # UBSAN triggers when compiling protobuf, so we need to disable it.
 set(UBSAN_FLAG "-fsanitize=undefined")
@@ -298,7 +298,7 @@ endif()
 # --- [ PocketFFT
 set(AT_POCKETFFT_ENABLED 0)
 if(NOT AT_MKL_ENABLED)
-  set(POCKETFFT_INCLUDE_DIR "${Torch_SOURCE_DIR}/third_party/pocketfft/")
+  set(POCKETFFT_INCLUDE_DIR "${CMAKE_SOURCE_DIR}/debian/pocketfft/")
   if(NOT EXISTS "${POCKETFFT_INCLUDE_DIR}")
     message(FATAL_ERROR "pocketfft directory not found, expected ${POCKETFFT_INCLUDE_DIR}")
   elif(NOT EXISTS "${POCKETFFT_INCLUDE_DIR}/pocketfft_hdronly.h")
@@ -644,7 +644,7 @@ if(USE_XNNPACK AND NOT USE_SYSTEM_XNNPAC
 
   include_directories(SYSTEM ${XNNPACK_INCLUDE_DIR})
   list(APPEND Caffe2_DEPENDENCY_LIBS XNNPACK)
-elseif(NOT TARGET XNNPACK AND USE_SYSTEM_XNNPACK)
+elseif(USE_XNNPACK AND NOT TARGET XNNPACK AND USE_SYSTEM_XNNPACK)
   add_library(XNNPACK SHARED IMPORTED)
   find_library(XNNPACK_LIBRARY XNNPACK)
   set_property(TARGET XNNPACK PROPERTY IMPORTED_LOCATION "${XNNPACK_LIBRARY}")
@@ -1391,7 +1391,12 @@ if(USE_CUDA)
   endif()
 endif()
 
-if(USE_DISTRIBUTED AND USE_TENSORPIPE)
+if(USE_DISTRIBUTED AND USE_TENSORPIPE AND USE_SYSTEM_TENSORPIPE)
+	add_library(tensorpipe SHARED IMPORTED)
+	find_library(TENSORPIPE_LIBRARY tensorpipe)
+	set_property(TARGET tensorpipe PROPERTY IMPORTED_LOCATION "${TENSORPIPE_LIBRARY}")
+	list(APPEND Caffe2_DEPENDENCY_LIBS tensorpipe)
+elseif(USE_DISTRIBUTED AND USE_TENSORPIPE)
   if(MSVC)
     message(WARNING "Tensorpipe cannot be used on Windows.")
   else()
@@ -1557,7 +1562,7 @@ if(CAFFE2_CMAKE_BUILDING_WITH_MAIN_REPO
   if(NOT USE_SYSTEM_ONNX)
     add_subdirectory(${CMAKE_CURRENT_LIST_DIR}/../third_party/onnx EXCLUDE_FROM_ALL)
   endif()
-  add_subdirectory(${CMAKE_CURRENT_LIST_DIR}/../third_party/foxi EXCLUDE_FROM_ALL)
+  add_subdirectory(${CMAKE_CURRENT_LIST_DIR}/../debian/foxi EXCLUDE_FROM_ALL)
 
   add_definitions(-DONNX_NAMESPACE=${ONNX_NAMESPACE})
   if(NOT USE_SYSTEM_ONNX)
@@ -1850,18 +1855,8 @@ endif()
 #
 set(TEMP_BUILD_SHARED_LIBS ${BUILD_SHARED_LIBS})
 set(BUILD_SHARED_LIBS OFF CACHE BOOL "Build shared libs" FORCE)
-add_subdirectory(${PROJECT_SOURCE_DIR}/third_party/fmt)
 
-# Disable compiler feature checks for `fmt`.
-#
-# CMake compiles a little program to check compiler features. Some of our build
-# configurations (notably the mobile build analyzer) will populate
-# CMAKE_CXX_FLAGS in ways that break feature checks. Since we already know
-# `fmt` is compatible with a superset of the compilers that PyTorch is, it
-# shouldn't be too bad to just disable the checks.
-set_target_properties(fmt-header-only PROPERTIES INTERFACE_COMPILE_FEATURES "")
-
-list(APPEND Caffe2_DEPENDENCY_LIBS fmt::fmt-header-only)
+find_package(fmt REQUIRED)
 set(BUILD_SHARED_LIBS ${TEMP_BUILD_SHARED_LIBS} CACHE BOOL "Build shared libs" FORCE)
 
 # ---[ Kineto
@@ -1896,7 +1891,7 @@ if(USE_KINETO)
   endif()
 
   set(CAFFE2_THIRD_PARTY_ROOT "${PROJECT_SOURCE_DIR}/third_party" CACHE STRING "")
-  set(KINETO_SOURCE_DIR "${CAFFE2_THIRD_PARTY_ROOT}/kineto/libkineto" CACHE STRING "")
+  set(KINETO_SOURCE_DIR "${CMAKE_SOURCE_DIR}/debian/kineto/" CACHE STRING "")
   set(KINETO_BUILD_TESTS OFF CACHE BOOL "")
   set(KINETO_LIBRARY_TYPE "static" CACHE STRING "")
 
Index: pytorch/aten/src/ATen/native/quantized/cpu/qnnpack/CMakeLists.txt
===================================================================
--- pytorch.orig/aten/src/ATen/native/quantized/cpu/qnnpack/CMakeLists.txt
+++ pytorch/aten/src/ATen/native/quantized/cpu/qnnpack/CMakeLists.txt
@@ -324,7 +324,7 @@ set_target_properties(pytorch_qnnpack PR
 set_target_properties(pytorch_qnnpack PROPERTIES PUBLIC_HEADER include/qnnpack_func.h)
 
 # ---[ Configure clog
-if(NOT TARGET clog)
+if(NOT TARGET clog AND NOT USE_SYSTEM_CLOG)
   set(CLOG_BUILD_TESTS OFF CACHE BOOL "")
   set(CLOG_RUNTIME_TYPE "${CPUINFO_RUNTIME_TYPE}" CACHE STRING "")
   add_subdirectory(
@@ -332,6 +332,9 @@ if(NOT TARGET clog)
     "${CONFU_DEPENDENCIES_BINARY_DIR}/clog")
   # We build static version of clog but a dynamic library may indirectly depend on it
   set_property(TARGET clog PROPERTY POSITION_INDEPENDENT_CODE ON)
+elseif(NOT TARGET clog and USE_SYSTEM_CLOG)
+  add_library(clog STATIC ${CMAKE_SOURCE_DIR}/debian/clog/src/clog.c
+	${CMAKE_SOURCE_DIR}/debian/clog/include/clog.h)
 endif()
 target_link_libraries(pytorch_qnnpack PUBLIC clog)
 
@@ -371,7 +374,7 @@ elseif(NOT TARGET pthreadpool AND USE_SY
   message("-- Found pthreadpool: ${PTHREADPOOL_LIBRARY}")
   set_target_properties(pthreadpool PROPERTIES
     IMPORTED_LOCATION "${PTHREADPOOL_LIBRARY}")
-  add_library(pthreadpool_interface INTERFACE)
+  add_library(pthreadpool_interface ALIAS pthreadpool)
 endif()
 if(PYTORCH_QNNPACK_CUSTOM_THREADPOOL)
   # Depend on pthreadpool interface, but not on implementation.
Index: pytorch/tools/setup_helpers/cmake.py
===================================================================
--- pytorch.orig/tools/setup_helpers/cmake.py
+++ pytorch/tools/setup_helpers/cmake.py
@@ -394,5 +394,5 @@ class CMake:
                 # We are likely using msbuild here
                 build_args += ["/p:CL_MPCount={}".format(max_jobs)]
             else:
-                build_args += ["-j", max_jobs]
+                build_args += ["-j", max_jobs, '-v']
         self.run(build_args, my_env)
Index: pytorch/modules/module_test/CMakeLists.txt
===================================================================
--- pytorch.orig/modules/module_test/CMakeLists.txt
+++ pytorch/modules/module_test/CMakeLists.txt
@@ -10,6 +10,7 @@ if(BUILD_TEST AND NOT BUILD_LITE_INTERPR
   add_library(
       caffe2_module_test_dynamic
       ${CMAKE_CURRENT_SOURCE_DIR}/module_test_dynamic.cc)
+  set_target_properties(caffe2_module_test_dynamic PROPERTIES VERSION 1 SOVERSION 1)
 
   if(HAVE_SOVERSION)
     set_target_properties(caffe2_module_test_dynamic PROPERTIES
Index: pytorch/caffe2/CMakeLists.txt
===================================================================
--- pytorch.orig/caffe2/CMakeLists.txt
+++ pytorch/caffe2/CMakeLists.txt
@@ -809,6 +809,13 @@ if(HAVE_SOVERSION)
   set_target_properties(torch_cpu PROPERTIES
       VERSION ${TORCH_VERSION} SOVERSION ${TORCH_SOVERSION})
 endif()
+if(NOT FMT_LIBRARY)
+    add_library(fmt STATIC IMPORTED)
+    find_library(FMT_LIBRARY fmt)
+    set_property(TARGET fmt PROPERTY IMPORTED_LOCATION "${FMT_LIBRARY}")
+endif()
+target_link_libraries(torch_cpu PRIVATE fmt)
+
 torch_compile_options(torch_cpu)  # see cmake/public/utils.cmake
 if(HAS_WERROR_SIGN_COMPARE AND WERROR)
   # target_compile_options(torch_cpu PRIVATE "-Werror=sign-compare")
@@ -1010,10 +1017,6 @@ if(USE_CUDA OR USE_ROCM)
   include(${TORCH_SRC_DIR}/csrc/jit/codegen/cuda/nvfuser.cmake)
 endif()
 
-if(NOT MSVC AND USE_XNNPACK)
-  TARGET_LINK_LIBRARIES(torch_cpu PRIVATE fxdiv)
-endif()
-
 # ==========================================================
 # formerly-libtorch flags
 # ==========================================================
@@ -1137,11 +1140,11 @@ target_include_directories(torch_cpu PRI
   ${TORCH_ROOT}/third_party/miniz-2.1.0)
 
 target_include_directories(torch_cpu PRIVATE
-  ${TORCH_ROOT}/third_party/kineto/libkineto/include)
+  ${CMAKE_SOURCE_DIR}/debian/kineto/libkineto/include)
 
 if(USE_KINETO)
   target_include_directories(torch_cpu PRIVATE
-    ${TORCH_ROOT}/third_party/kineto/libkineto/src)
+  ${CMAKE_SOURCE_DIR}/debian/kineto/libkineto/src)
 endif()
 
 install(DIRECTORY "${TORCH_SRC_DIR}/csrc"
Index: pytorch/torch/CMakeLists.txt
===================================================================
--- pytorch.orig/torch/CMakeLists.txt
+++ pytorch/torch/CMakeLists.txt
@@ -74,7 +74,7 @@ set(TORCH_PYTHON_INCLUDE_DIRECTORIES
     ${TORCH_ROOT}/third_party/gloo
     ${TORCH_ROOT}/third_party/onnx
     ${TORCH_ROOT}/third_party/flatbuffers/include
-    ${TORCH_ROOT}/third_party/kineto/libkineto/include
+    ${CMAKE_SOURCE_DIR}/debian/kineto/libkineto/include
 
     ${TORCH_SRC_DIR}/csrc
     ${TORCH_SRC_DIR}/csrc/api/include
@@ -83,11 +83,17 @@ set(TORCH_PYTHON_INCLUDE_DIRECTORIES
 
 list(APPEND TORCH_PYTHON_INCLUDE_DIRECTORIES ${LIBSHM_SRCDIR})
 
+if(NOT FMT_LIBRARY)
+    add_library(fmt STATIC IMPORTED)
+    find_library(FMT_LIBRARY fmt)
+    set_property(TARGET fmt PROPERTY IMPORTED_LOCATION "${FMT_LIBRARY}")
+endif()
+
 set(TORCH_PYTHON_LINK_LIBRARIES
     python::python
     pybind::pybind11
     shm
-    fmt::fmt-header-only
+    fmt
     ATEN_CPU_FILES_GEN_LIB)
 
 set(TORCH_PYTHON_COMPILE_DEFINITIONS)
Index: pytorch/test/cpp/tensorexpr/CMakeLists.txt
===================================================================
--- pytorch.orig/test/cpp/tensorexpr/CMakeLists.txt
+++ pytorch/test/cpp/tensorexpr/CMakeLists.txt
@@ -54,7 +54,7 @@ target_include_directories(tutorial_tens
 # pthreadpool header. For some build environment we need add the dependency
 # explicitly.
 if(USE_PTHREADPOOL)
-  target_link_libraries(test_tensorexpr PRIVATE pthreadpool_interface)
+  target_link_libraries(test_tensorexpr PRIVATE pthreadpool)
 endif()
 if(USE_CUDA)
   target_link_libraries(test_tensorexpr PRIVATE
