find backtrace by cmake module (#36017)

Summary: Pull Request resolved: https://github.com/pytorch/pytorch/pull/36017

Reviewed By: zou3519, albanD

Differential Revision: D20870233

Pulled By: ezyang

fbshipit-source-id: b11daf22a900e47b5b72272fb2f096d78c075bf8
diff --git a/c10/CMakeLists.txt b/c10/CMakeLists.txt
index e9e5cf9..2204de1 100644
--- a/c10/CMakeLists.txt
+++ b/c10/CMakeLists.txt
@@ -54,6 +54,15 @@
     target_link_libraries(c10 PUBLIC glog::glog)
 endif()
 
+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})
diff --git a/c10/util/Backtrace.cpp b/c10/util/Backtrace.cpp
index 094da95..09e41b9 100644
--- a/c10/util/Backtrace.cpp
+++ b/c10/util/Backtrace.cpp
@@ -18,14 +18,7 @@
 #pragma comment(lib, "Dbghelp.lib")
 #endif
 
-#if (defined(__ANDROID__)) ||                                                 \
-    (defined(__APPLE__) &&                                                    \
-     (TARGET_IPHONE_SIMULATOR || TARGET_OS_SIMULATOR || TARGET_OS_IPHONE)) || \
-    defined(_WIN32) || defined(__EMSCRIPTEN__)
-// No backtrace on mobile, windows and emscripten platforms.
-#define SUPPORTS_BACKTRACE 0
-#else
-#define SUPPORTS_BACKTRACE 1
+#if SUPPORTS_BACKTRACE
 #include <cxxabi.h>
 #include <execinfo.h>
 #endif