cmake_minimum_required (VERSION 2.8)

# include CMake modules
INCLUDE (CheckLibraryExists)
INCLUDE (CheckIncludeFiles)
INCLUDE (CheckFunctionExists)

project (EZTrace C Fortran)
#set (INSTALL_DIR "${CMAKE_INSTALL_PREFIX}/lib")
list(APPEND CMAKE_INSTALL_RPATH  "${CMAKE_CURRENT_BINARY_DIR}/extlib/lib")
# use, i.e. don't skip the full RPATH for the build tree
set(CMAKE_SKIP_BUILD_RPATH  FALSE)

# when building, don't use the install RPATH already
# (but later on when installing)
set(CMAKE_BUILD_WITH_INSTALL_RPATH FALSE)

# the RPATH to be used when installing
set(CMAKE_INSTALL_RPATH "${CMAKE_INSTALL_PREFIX}/lib")

# CMakeFind repository
list (APPEND CMAKE_MODULE_PATH ${CMAKE_SOURCE_DIR}/cmake_modules)

# Check libraries and functions
find_library(BFD_LIBRARY bfd)
find_library(IBERTY_LIBRARY iberty)

CHECK_LIBRARY_EXISTS(bfd bfd_demangle "" bfd_demangle_exist)
if (NOT bfd_demangle_exist)
  message(FATAL_ERROR "libbfd: bfd_demangle not found")
endif()

option(EZTRACE_ENABLE_IBERTY      "Enable libiberty"    OFF)
CHECK_LIBRARY_EXISTS(iberty cplus_demangle "" cplus_demangle_exist)
if (NOT cplus_demangle_exist)
  message(FATAL_ERROR "libiberty: cplus_demangle not found")
else ()
  message(WARNING " libiberty detected, you can try to use it with -DHAVE_DEMANGLE")
endif()

#TRY_COMPILE(COMPILE_IBERTY ${CMAKE_CURRENT_BINARY_DIR}  ${CMAKE_SOURCE_DIR}/cmake_modules/tetIberty.c COMPILE_DEFINITIONS "${CMAKE_C_FLAGS} " OUTPUT_VARIABLE COMPILE_IBERTY_OUTPUT)

CHECK_LIBRARY_EXISTS(dl dlsym "" dlsym_exist)
if (NOT dlsym_exist)
  message(FATAL_ERROR "libdl: dlsym not found")
endif()

CHECK_LIBRARY_EXISTS(m cos "" cos_exist)
if (NOT cos_exist)
  message(WARNING "libm: cos not found")
endif()

CHECK_LIBRARY_EXISTS(pthread pthread_create "" pthread_create_exist)
CHECK_LIBRARY_EXISTS(pthread pthread_spin_lock "" full_pthread)
if (NOT pthread_create_exist)
  message(FATAL_ERROR "libpthread: pthread_create not found")
endif()

CHECK_LIBRARY_EXISTS(z deflate "" z_exist)
if (NOT z_exist)
  message(FATAL_ERROR "libz: deflate not found")
endif()

CHECK_LIBRARY_EXISTS(m cos "" cos_exist)
if (NOT cos_exist)
  message(WARNING "libm: cos not found")
endif()

set (HAVE_LIBOPCODE "1")
find_package (LibOpcodes)
CHECK_LIBRARY_EXISTS(opcodes disassembler "" disassembler_exist)
if (NOT disassembler_exist)
  set (HAVE_LIBOPCODE "0")
  message(WARNING "libopcodes: disassembler not found")
  message(WARNING "EZtrace will work better with LibOpcodes")
endif()

set (LIBRARY_PATH "${CMAKE_INSTALL_PREFIX}/lib")

# Internals stuff
if ("${APPLE}")
  set (CFLAGS "$CFLAGS -DHAVE_DARWIN")
  set (DYNLIB_EXT ".dylib")
  set (LD_PRELOAD_NAME "DYLD_INSERT_LIBRARIES")
  set (LD_LIBRARY_PATH_NAME "DYLD_LIBRARY_PATH")
else() # By default -> Linux
  set (CLFAGS "-DHAVE_LINUX -Wno-unused-parameter -Wno-unused-value")
  set (DYNLIB_EXT ".so")
  set (LD_PRELOAD_NAME "LD_PRELOAD")
  set (LD_LIBRARY_PATH_NAME "LD_LIBRARY_PATH")
endif()

set (PPTRACE_BINARY_TYPE "1")
set (__PPTRACE_BINARY_TYPE "1")
set (BINARY_LIB ${LIBBFD_LIBRARIES})
CHECK_INCLUDE_FILES ("sys/prctl.h" HAVE_PRCTL_H)
set (abs_top_builddir ${CMAKE_BINARY_DIR})

# Look for library

#find_package (OpenMP)

# Options to enable/disable modules
option(EZTRACE_ENABLE_MPI      "Enable MPI module"     ON)
option(EZTRACE_ENABLE_CUDA     "Enable CUDA module"    OFF)
option(EZTRACE_ENABLE_STARPU   "Enable StarPU module"  OFF)
option(EZTRACE_ENABLE_OPENMP   "Enable OpenMP module"  OFF)
option(EZTRACE_ENABLE_PAPI     "Enable PAPI module"    OFF)

if (EZTRACE_ENABLE_MPI)
  find_package (MPI REQUIRED)
endif()

if (EZTRACE_ENABLE_STARPU)
  find_package (STARPU REQUIRED COMPONENTS HWLOC MPI)
  if (NOT STARPU_FOUND)
    message(ERROR "StarPU not FOUND")
  endif()
  list (APPEND CMAKE_INSTALL_RPATH ${STARPU_LIBRARY_DIRS})
endif()

if (EZTRACE_ENABLE_PAPI)
  find_package (LibPapi REQUIRED)
endif()

if (EZTRACE_ENABLE_CUDA)
  find_package (CUDA REQUIRED)
endif()

if (EZTRACE_ENABLE_OPENMP)
  find_package (OpenMP REQUIRED)
endif()

# Some flags for compilation
set (USE_LITL "1")
set (prefix "${CMAKE_INSTALL_PREFIX}")

# Files to convert
configure_file (${PROJECT_SOURCE_DIR}/src/core/eztrace_config.h.in ${PROJECT_SOURCE_DIR}/src/core/eztrace_config.h)
configure_file (${PROJECT_SOURCE_DIR}/src/core/eztrace.c.in ${PROJECT_SOURCE_DIR}/src/core/eztrace.c)
configure_file (${PROJECT_SOURCE_DIR}/test/automake/testcommon.h.in ${PROJECT_SOURCE_DIR}/test/automake/testcommon.h)

# Subdirectory
add_subdirectory (extlib)
add_subdirectory (src)

# Test section
#add_subdirectory (test)
#enable_testing ()

include(PrintOpts)
