File: FindGperftools.cmake

package info (click to toggle)
madness 0.10.1~gite4aa500e-10
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 33,452 kB
  • ctags: 30,300
  • sloc: cpp: 267,232; ansic: 12,308; python: 4,961; fortran: 4,245; xml: 1,053; makefile: 717; perl: 244; yacc: 227; lex: 188; asm: 141; sh: 139; csh: 55
file content (143 lines) | stat: -rw-r--r-- 6,548 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# - Try to find Google performance tools (gperftools)
# Input variables:
#   GPERFTOOLS_ROOT_DIR    - The gperftools install directory
#   GPERFTOOLS_INCLUDE_DIR - The gperftools include directory
#   GPERFTOOLS_LIBRARY     - The gperftools library directory
# Components: profiler, and tcmalloc or tcmalloc_minimal
# Output variables:
#   GPERFTOOLS_FOUND        - System has gperftools
#   GPERFTOOLS_INCLUDE_DIRS - The gperftools include directories
#   GPERFTOOLS_LIBRARIES    - The libraries needed to use gperftools
#   GPERFTOOLS_VERSION      - The version string for gperftools

include(FindPackageHandleStandardArgs)
  
if(NOT DEFINED GPERFTOOLS_FOUND)

  # Check to see if libunwind is required
  set(GPERFTOOLS_DISABLE_PROFILER FALSE)
  if((";${Gperftools_FIND_COMPONENTS};" MATCHES ";profiler;") AND 
      (CMAKE_SYSTEM_NAME MATCHES "Linux" OR 
       CMAKE_SYSTEM_NAME MATCHES "BlueGeneQ" OR
       CMAKE_SYSTEM_NAME MATCHES "BlueGeneP") AND
       (CMAKE_SIZEOF_VOID_P EQUAL 8))
       
    # Libunwind is required by profiler on this platform
    if(Gperftools_FIND_REQUIRED_profiler OR Gperftools_FIND_REQUIRED_tcmalloc_and_profiler)
      find_package(Libunwind 0.99 REQUIRED)
    else()
      find_package(Libunwind)
      if(NOT LIBUNWIND_FOUND OR LIBUNWIND_VERSION VERSION_LESS 0.99)
        set(GPERFTOOLS_DISABLE_PROFILER TRUE)
      endif()
    endif()
  endif()

  # Check for invalid components
  foreach(_comp ${Gperftools_FIND_COMPONENTS})
    if((NOT _comp STREQUAL "tcmalloc_and_profiler") AND
       (NOT _comp STREQUAL "tcmalloc") AND
       (NOT _comp STREQUAL "tcmalloc_minimal") AND
       (NOT _comp STREQUAL "profiler"))
      message(FATAL_ERROR "Invalid component specified for Gperftools: ${_comp}")
    endif()
  endforeach()

  # Check for valid component combinations
  if(";${Gperftools_FIND_COMPONENTS};" MATCHES ";tcmalloc_and_profiler;" AND 
      (";${Gperftools_FIND_COMPONENTS};" MATCHES ";tcmalloc;" OR 
       ";${Gperftools_FIND_COMPONENTS};" MATCHES ";tcmalloc_minimal;" OR
       ";${Gperftools_FIND_COMPONENTS};" MATCHES ";profiler;"))
    message("ERROR: Invalid component selection for Gperftools: ${Gperftools_FIND_COMPONENTS}")
    message("ERROR: Gperftools cannot link both tcmalloc_and_profiler with the tcmalloc, tcmalloc_minimal, or profiler libraries")
    message(FATAL_ERROR "Gperftools component list is invalid")
  endif()
  if(";${Gperftools_FIND_COMPONENTS};" MATCHES ";tcmalloc;" AND ";${Gperftools_FIND_COMPONENTS};" MATCHES ";tcmalloc_minimal;")
    message("ERROR: Invalid component selection for Gperftools: ${Gperftools_FIND_COMPONENTS}")
    message("ERROR: Gperftools cannot link both tcmalloc and tcmalloc_minimal")
    message(FATAL_ERROR "Gperftools component list is invalid")
  endif()

  # Set default sarch paths for gperftools
  if(GPERFTOOLS_ROOT_DIR)
    set(GPERFTOOLS_INCLUDE_DIR ${GPERFTOOLS_ROOT_DIR}/include CACHE PATH "The include directory for gperftools")
    if(CMAKE_SIZEOF_VOID_P EQUAL 8 AND CMAKE_SYSTEM_NAME STREQUAL "Linux")
      set(GPERFTOOLS_LIBRARY ${GPERFTOOLS_ROOT_DIR}/lib64;${GPERFTOOLS_ROOT_DIR}/lib CACHE PATH "The library directory for gperftools")
    else()
      set(GPERFTOOLS_LIBRARY ${GPERFTOOLS_ROOT_DIR}/lib CACHE PATH "The library directory for gperftools")
    endif()
  endif()
  
  find_path(GPERFTOOLS_INCLUDE_DIRS NAMES gperftools/malloc_extension.h
      HINTS ${GPERFTOOLS_INCLUDE_DIR})

  # Search for component libraries
  foreach(_comp tcmalloc_and_profiler tcmalloc tcmalloc_minimal profiler)
    find_library(GPERFTOOLS_${_comp}_LIBRARY ${_comp} 
        HINTS ${GPERFTOOLS_LIBRARY})
    if(GPERFTOOLS_${_comp}_LIBRARY)
      set(GPERFTOOLS_${_comp}_FOUND TRUE)
    else()
      set(GPERFTOOLS_${_comp}_FOUND FALSE)
    endif()
    
    # Exclude profiler from the found list if libunwind is required but not found
    if(GPERFTOOLS_${_comp}_FOUND AND ${_comp} MATCHES "profiler" AND GPERFTOOLS_DISABLE_PROFILER)
      set(GPERFTOOLS_${_comp}_FOUND FALSE)
      set(GPERFTOOLS_${_comp}_LIBRARY "GPERFTOOLS_${_comp}_LIBRARY-NOTFOUND")
      message("WARNING: Gperftools '${_comp}' requires libunwind 0.99 or later.")
      message("WARNING: Gperftools '${_comp}' will be disabled.")
    endif()
    
    if(";${Gperftools_FIND_COMPONENTS};" MATCHES ";${_comp};" AND GPERFTOOLS_${_comp}_FOUND)
      list(APPEND GPERFTOOLS_LIBRARIES "${GPERFTOOLS_${_comp}_LIBRARY}")
    endif()
  endforeach()
  
  # Set gperftools libraries if not set based on component list
  if(NOT GPERFTOOLS_LIBRARIES)
    if(GPERFTOOLS_tcmalloc_and_profiler_FOUND)
      set(GPERFTOOLS_LIBRARIES "${GPERFTOOLS_tcmalloc_and_profiler_LIBRARY}")
    elseif(GPERFTOOLS_tcmalloc_FOUND AND GPERFTOOLS_profiler_FOUND)
      set(GPERFTOOLS_LIBRARIES "${GPERFTOOLS_tcmalloc_LIBRARY}" "${GPERFTOOLS_profiler_LIBRARY}")
    elseif(GPERFTOOLS_profiler_FOUND)
      set(GPERFTOOLS_LIBRARIES "${GPERFTOOLS_profiler_LIBRARY}")
    elseif(GPERFTOOLS_tcmalloc_FOUND)
      set(GPERFTOOLS_LIBRARIES "${GPERFTOOLS_tcmalloc_LIBRARY}")
    elseif(GPERFTOOLS_tcmalloc_minimal_FOUND)
      set(GPERFTOOLS_LIBRARIES "${GPERFTOOLS_tcmalloc_minimal_LIBRARY}")
    endif()
  endif()

  # handle the QUIETLY and REQUIRED arguments and set GPERFTOOLS_FOUND to TRUE
  # if all listed variables are TRUE
  find_package_handle_standard_args(GPERFTOOLS
      FOUND_VAR GPERFTOOLS_FOUND
      REQUIRED_VARS GPERFTOOLS_LIBRARIES GPERFTOOLS_INCLUDE_DIRS
      HANDLE_COMPONENTS)

  mark_as_advanced(GPERFTOOLS_INCLUDE_DIR GPERFTOOLS_LIBRARY 
      GPERFTOOLS_INCLUDE_DIRS GPERFTOOLS_LIBRARIES)

  # Add linker flags that instruct the compiler to exclude built in memory
  # allocation functions. This works for GNU, Intel, and Clang. Other compilers
  # may need to be added in the future.
  if(GPERFTOOLS_LIBRARIES MATCHES "tcmalloc")
    if((CMAKE_CXX_COMPILER_ID MATCHES "GNU") OR
       (CMAKE_CXX_COMPILER_ID MATCHES "AppleClang") OR 
       (CMAKE_CXX_COMPILER_ID MATCHES "Clang") OR
      ((CMAKE_CXX_COMPILER_ID MATCHES "Intel") AND (NOT CMAKE_CXX_PLATFORM_ID MATCHES "Windows"))) 
      list(APPEND GPERFTOOLS_LIBRARIES "-fno-builtin-malloc"
          "-fno-builtin-calloc" "-fno-builtin-realloc" "-fno-builtin-free")
    endif()
  endif()

  # Add libunwind flags to gperftools if the profiler is being used
  if(GPERFTOOLS_LIBRARIES MATCHES "profiler" AND LIBUNWIND_FOUND)
    list(APPEND GPERFTOOLS_INCLUDE_DIRS "${LIBUNWIND_INCLUDE_DIRS}")
    list(APPEND GPERFTOOLS_LIBRARIES "${LIBUNWIND_LIBRARIES}")
  endif()
  
  unset(GPERFTOOLS_DISABLE_PROFILER)

endif()