# cmake -G "Unix Makefiles"
# cmake -G "Unix Makefiles" -DCMAKE_BUILD_TYPE=Profile
# cmake -G "Unix Makefiles" -DCMAKE_BUILD_TYPE=Release
# make CXX=$(which g++) CC=$(which gcc)
# make clean
# make
# make VERBOSE=1
# sudo apt-get install cmake-curses-gui

cmake_minimum_required(VERSION 2.6)

# set version of the programs and librariries used
set(VERSION "v1_0")

set(CMAKE_C_COMPILER "/usr/bin/gcc")
set(CMAKE_CXX_COMPILER "/usr/bin/g++")

# -------------------------------------------------------------------
# define compile flags
# -------------------------------------------------------------------
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -std=c++11 ")
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS} -Ofast -std=c++11")
set(CMAKE_CXX_FLAGS_DEBUG   "${CMAKE_CXX_FLAGS} -ggdb  -std=c++11")
set(CMAKE_CXX_FLAGS_PROFILE "${CMAKE_CXX_FLAGS} -Ofast -g -std=c++11")

message( STATUS	"cxx flags" ${CMAKE_CXX_FLAGS})    

set(CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/cmake/modules")

set(CMAKE_FIND_LIBRARY_PREFIXES "lib")
set(CMAKE_FIND_LIBRARY_SUFFIXES "so")

INCLUDE(FindGooglePerfTools)

message(STATUS "Google PerfTools ${GOOGLE_PERFTOOLS_FOUND}")

IF(GPERFTOOLS_FOUND)
	set(CMAKE_CXX_FLAGS_PROFILE "${CMAKE_CXX_FLAGS_PROFILE} -DPROFILE")
	set(CMAKE_INCLUDE_PATH "${CMAKE_INCLUDE_PATH} ${GPERFTOOLS_INCLUDE_DIR}") 	
	set(CMAKE_LIBRARY_PATH "${CMAKE_LIBRARY_PATH} ${PROFILER_LIBRARY}")
ENDIF()

# -------------------------------------------------------------------
# library base
# -------------------------------------------------------------------
project(base)
set(LIBRARY_OUTPUT_PATH lib/${CMAKE_BUILD_TYPE})
include_directories(src/${VERSION})
file(
        GLOB_RECURSE
        source_files
        src/${VERSION}/base/*
)

add_library(
        base
        SHARED
        ${source_files}
)

# -------------------------------------------------------------------
# library maths
# -------------------------------------------------------------------
project(maths)
set(LIBRARY_OUTPUT_PATH lib/${CMAKE_BUILD_TYPE})
include_directories(
	src/${VERSION}
)
file(
        GLOB_RECURSE
        source_files
		src/${VERSION}/base/*.h
        src/${VERSION}/maths/*
)

add_library(
        maths
        SHARED
        ${source_files}
)

# -------------------------------------------------------------------
# program prog1
# -------------------------------------------------------------------
project(prog1)

set(EXECUTABLE_OUTPUT_PATH build/${CMAKE_BUILD_TYPE})
file(
        GLOB_RECURSE
        source_files
        src/${VERSION}/prog/prog1.cpp
)

add_executable(
	prog1
    ${source_files}
)

target_link_libraries(
        prog1
        base
		maths
		profiler
)

# -------------------------------------------------------------------
# program prog2
# -------------------------------------------------------------------
project(prog2)

set(EXECUTABLE_OUTPUT_PATH build/${CMAKE_BUILD_TYPE})
file(
        GLOB_RECURSE
        source_files
        src/${VERSION}/prog/prog2.cpp
)

add_executable(
	prog2
    ${source_files}
)

target_link_libraries(
        prog2
        base
		maths
		profiler
)

# -------------------------------------------------------------------
# tests
# -------------------------------------------------------------------

find_package(CppUnit REQUIRED)

include_directories(src/${VERSION} ${CPPUNIT_INCLUDE_DIR})

LIST(APPEND UnitTestLibs ${CPPUNIT_LIBRARY})

#Include the "unit-tests" directory 
include_directories("tst")

#Find all source files in unit test
file(GLOB_RECURSE UNIT_TEST "tst/*.cpp")

message(STATUS " FILES =  ${UNIT_TEST}" )

foreach(a_file ${UNIT_TEST})
    get_filename_component(a_target ${a_file} NAME_WE)
    project(${a_target})
	set(CMAKE_BUILD_TYPE release)
	set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -Wall -ftree-vectorize -msse2 -std=c++11")
   	add_executable(${a_target} ${a_file})
	target_link_libraries (${a_target} ${UnitTestLibs} base maths)
endforeach(a_file ${UNIT_TEST})



