From f1337dac2ebdd356e8b6cd4762368b0f6a1f94db Mon Sep 17 00:00:00 2001
From: Danilo Piparo <danilo.piparo@cern.ch>
Date: Tue, 30 Dec 2014 15:50:32 +0100
Subject: [PATCH] Add the -Dtcmalloc option.

---
 CMakeLists.txt                              |  6 ++++
 cmake/modules/Findtcmalloc.cmake            | 36 +++++++++++++++++++++
 cmake/modules/RootBuildOptions.cmake        |  1 +
 cmake/modules/SearchInstalledSoftware.cmake | 10 ++++++
 4 files changed, 53 insertions(+)
 create mode 100644 cmake/modules/Findtcmalloc.cmake

diff --git a/CMakeLists.txt b/CMakeLists.txt
index 63b41ce62f7..fb014814e05 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -46,6 +46,12 @@ ROOT_CHECK_OUT_OF_SOURCE_BUILD()
 include(SearchInstalledSoftware)
 ROOT_SHOW_OPTIONS()
 
+#---Here we add tcmalloc to the linker flags if needed------------------------------------------
+if (TCMALLOC_FOUND)
+  set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -ltcmalloc")
+  set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -ltcmalloc")
+endif()
+
 #---Populate the configure arguments returned by 'root-config --config'-------------------------
 get_cmake_property(variables CACHE_VARIABLES)
 foreach(var ${variables})
diff --git a/cmake/modules/Findtcmalloc.cmake b/cmake/modules/Findtcmalloc.cmake
new file mode 100644
index 00000000000..247227cff9e
--- /dev/null
+++ b/cmake/modules/Findtcmalloc.cmake
@@ -0,0 +1,36 @@
+# - Locate tcmalloc library
+# Defines:
+#
+#  TCMALLOC_FOUND
+#  TCMALLOC_INCLUDE_DIR
+#  TCMALLOC_INCLUDE_DIRS (not cached)
+#  TCMALLOC_tcmalloc_LIBRARY
+#  TCMALLOC_profiler_LIBRARY
+#  TCMALLOC_LIBRARIES (not cached)
+#  TCMALLOC_LIBRARY_DIRS (not cached)
+#  PPROF_EXECUTABLE
+
+find_path(TCMALLOC_INCLUDE_DIR google/tcmalloc.h)
+foreach(component tcmalloc profiler)
+  find_library(TCMALLOC_${component}_LIBRARY NAMES ${component})
+  mark_as_advanced(TCMALLOC_${component}_LIBRARY)
+endforeach()
+
+find_program(PPROF_EXECUTABLE NAMES pprof
+             HINTS ${TCMALLOC_INCLUDE_DIR}/../bin)
+
+set(TCMALLOC_INCLUDE_DIRS ${TCMALLOC_INCLUDE_DIR})
+set(TCMALLOC_LIBRARIES ${TCMALLOC_tcmalloc_LIBRARY} ${TCMALLOC_profiler_LIBRARY})
+
+# handle the QUIETLY and REQUIRED arguments and set TCMALLOC_FOUND to TRUE if
+# all listed variables are TRUE
+INCLUDE(FindPackageHandleStandardArgs)
+FIND_PACKAGE_HANDLE_STANDARD_ARGS(tcmalloc DEFAULT_MSG TCMALLOC_INCLUDE_DIR TCMALLOC_LIBRARIES)
+
+mark_as_advanced(TCMALLOC_FOUND TCMALLOC_INCLUDE_DIR PPROF_EXECUTABLE)
+
+if(TCMALLOC_tcmalloc_LIBRARY)
+  get_filename_component(TCMALLOC_LIBRARY_DIRS ${TCMALLOC_tcmalloc_LIBRARY} PATH)
+elseif(TCMALLOC_profiler_LIBRARY)
+  get_filename_component(TCMALLOC_LIBRARY_DIRS ${TCMALLOC_profiler_LIBRARY} PATH)
+endif()
diff --git a/cmake/modules/RootBuildOptions.cmake b/cmake/modules/RootBuildOptions.cmake
index 5beca704e2a..c85b5b8bf4b 100644
--- a/cmake/modules/RootBuildOptions.cmake
+++ b/cmake/modules/RootBuildOptions.cmake
@@ -163,6 +163,7 @@ ROOT_BUILD_OPTION(srp ON "SRP support, requires SRP source tree")
 ROOT_BUILD_OPTION(ssl ON "SSL encryption support, requires openssl")
 ROOT_BUILD_OPTION(gnuinstall OFF "Perform installation following the GNU guidelines")
 ROOT_BUILD_OPTION(table ${table_defvalue} "Build libTable contrib library")
+ROOT_BUILD_OPTION(tcmalloc OFF "Using the tcmalloc allocator")
 ROOT_BUILD_OPTION(thread ON "Using thread library (cannot be disabled)")
 ROOT_BUILD_OPTION(tmva ON "Build TMVA multi variate analysis library")
 ROOT_BUILD_OPTION(unuran ${unuran_defvalue} "UNURAN - package for generating non-uniform random numbers")
diff --git a/cmake/modules/SearchInstalledSoftware.cmake b/cmake/modules/SearchInstalledSoftware.cmake
index ed9970a6646..63b5bb1d516 100644
--- a/cmake/modules/SearchInstalledSoftware.cmake
+++ b/cmake/modules/SearchInstalledSoftware.cmake
@@ -843,6 +843,16 @@ if(vc)
   endif()
 endif()
 
+#---Check for TCMalloc---------------------------------------------------------------
+
+if (tcmalloc)
+  message(STATUS "Looking for tcmalloc")
+  find_package(tcmalloc)
+  if(NOT TCMALLOC_FOUND)
+    message(STATUS "TCMalloc not found.")
+  endif()
+endif()
+
 #---Report non implemented options---------------------------------------------------
 foreach(opt afs clarens glite pch peac sapdb srp geocad)
   if(${opt})
-- 
GitLab