# Find the google-perftools library.
#
# This module defines
#  GOOGLE_PERFTOOLS_FOUND                  - True if google-perftools was found.
#  GOOGLE_PERFTOOLS_LIB_DIR                - Directory containing perftools libraries.
#  GOOGLE_PERFTOOLS_TCMALLOC_LIBRARIES     - Libraries for just using tcmalloc.
#  GOOGLE_PERFTOOLS_HEAPPROFILE_LIBRARIES  - Libraries for heap checking and profiling
#  GOOGLE_PERFTOOLS_CPUPROFILE_LIBRARIES   - Libraries for cpu profiling
# To specify an additional directory to search, set GOOGLE_PERFTOOLS_ROOT.
#
# Copyright (c) 2011, Stanford University
# Based on FindSQLite3.cmake by:
#  Copyright (c) 2006, Jaroslaw Staniek, <js@iidea.pl>
#  Extended by Siddhartha Chaudhuri, 2008.
#
# Redistribution and use is allowed according to the terms of the BSD license.
#

SET(GOOGLE_PERFTOOLS_FOUND FALSE)
SET(GOOGLE_PERFTOOLS_INCLUDE_DIRS)
SET(GOOGLE_PERFTOOLS_LIBRARIES)

SET(GOOGLE_PERFTOOLS_LIBRARY_DIRS)
IF(GOOGLE_PERFTOOLS_ROOT)
  SET(GOOGLE_PERFTOOLS_LIBRARY_DIRS ${GOOGLE_PERFTOOLS_ROOT})
  IF(EXISTS "${GOOGLE_PERFTOOLS_ROOT}/lib")
    SET(GOOGLE_PERFTOOLS_LIBRARY_DIRS ${GOOGLE_PERFTOOLS_LIBRARY_DIRS} ${GOOGLE_PERFTOOLS_ROOT}/lib)
  ENDIF()
ENDIF()

# tcmalloc
# Without system dirs
FIND_LIBRARY(GOOGLE_PERFTOOLS_TCMALLOC_LIBRARY
  NAMES tcmalloc
  PATHS ${GOOGLE_PERFTOOLS_LIBRARY_DIRS}
  NO_DEFAULT_PATH
  )
IF(NOT GOOGLE_PERFTOOLS_TCMALLOC_LIBRARY)  # now look in system locations
  FIND_LIBRARY(GOOGLE_PERFTOOLS_TCMALLOC_LIBRARY NAMES tcmalloc)
ENDIF()

# profiler
# Without system dirs
FIND_LIBRARY(GOOGLE_PERFTOOLS_PROFILER_LIBRARY
  NAMES profiler
  PATHS ${GOOGLE_PERFTOOLS_LIBRARY_DIRS}
  NO_DEFAULT_PATH
  )
IF(NOT GOOGLE_PERFTOOLS_PROFILER_LIBRARY)  # now look in system locations
  FIND_LIBRARY(GOOGLE_PERFTOOLS_PROFILER_LIBRARY NAMES profiler)
ENDIF()

# Note that we don't build a GOOGLE_PERFTOOLS_LIBRARIES since you
# don't always want to link everything in. Instead, build for
# different scenarios: TCMALLOC, HEAPPROFILE (or check), and
# CPUPROFILE.

IF(GOOGLE_PERFTOOLS_TCMALLOC_LIBRARY AND GOOGLE_PERFTOOLS_PROFILER_LIBRARY)
  SET(GOOGLE_PERFTOOLS_FOUND TRUE)

  GET_FILENAME_COMPONENT(GOOGLE_PERFTOOLS_LIB_DIR ${GOOGLE_PERFTOOLS_TCMALLOC_LIBRARY} PATH)

  SET(GOOGLE_PERFTOOLS_TCMALLOC_LIBRARIES ${GOOGLE_PERFTOOLS_TCMALLOC_LIBRARY})
  SET(GOOGLE_PERFTOOLS_HEAPPROFILE_LIBRARIES ${GOOGLE_PERFTOOLS_TCMALLOC_LIBRARY})
  SET(GOOGLE_PERFTOOLS_CPUPROFILE_LIBRARIES ${GOOGLE_PERFTOOLS_TCMALLOC_LIBRARY} ${GOOGLE_PERFTOOLS_PROFILER_LIBRARY})

  IF(NOT GOOGLE_PERFTOOLS_FIND_QUIETLY)
    MESSAGE(STATUS "Found google-perftools: libraries at ${GOOGLE_PERFTOOLS_LIBRARY_DIRS} :: ${GOOGLE_PERFTOOLS_LIBRARIES}")
  ENDIF()
ELSE()
  SET(GOOGLE_PERFTOOLS_FOUND FALSE)
  IF(GOOGLE_PERFTOOLS_FIND_REQUIRED)
    MESSAGE(STATUS "google-perftools not found")
  ENDIF()
ENDIF()

MARK_AS_ADVANCED(GOOGLE_PERFTOOLS_LIBRARIES)
