cmake_minimum_required(VERSION 2.6)
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/CMakeModules/")
#set(CMAKE_BUILD_TYPE Debug)
#set(CMAKE_BUILD_TYPE Production)
#set (CMAKE_BUILD_TYPE Debug CACHE STRING "Debug" FORCE)

project(libharkio3)
set(PROJECT_VERSION "2.3.0")
enable_testing()

add_subdirectory(src)

if(${CMAKE_BUILD_TYPE} MATCHES Debug)
    add_subdirectory(tests)
    #file(COPY testdata DESTINATION ${CMAKE_CURRENT_BINARY_DIR})
endif()

set(CPACK_GENERATOR "DEB")
set(CPACK_DEBIAN_PACKAGE_MAINTAINER "Takeshi Mizumoto")
include(CPack)

add_custom_target(--help
	COMMAND echo "make targets:"
	COMMAND echo "    all:      build libharkio3."
	COMMAND echo "    disclean: remove build targets."	
	COMMAND echo "    unittest: execute the unit tests of this project."
	COMMAND echo "    coverage: execute the coverage tests of this project."	
	COMMAND echo "    valgrind: execute the valgrind memory leak tests of this project."		
)

add_custom_target(distclean
	rm -rf `find . -name CMakeFiles`
	COMMAND rm -f `find . -name CMakeCache.txt`
	COMMAND rm -f `find . -name Makefile`
	COMMAND rm -f `find . -name cmake_install.cmake`
	COMMAND rm -f `find . -name \"*.a\"` 
        COMMAND rm -f `find . -name \"*.gcda\"`
)

add_custom_target(copysources ALL
	COMMAND echo "copying targets ..."
	COMMAND cp -f ${CMAKE_SOURCE_DIR}/DartConfiguration.tcl .
	COMMAND cp -rf ${CMAKE_SOURCE_DIR}/testdata .
)

add_custom_target(unittest
	COMMAND echo "executing unit test ..."
	COMMAND ctest -D ExperimentalTest --output-on-failure .
)

add_custom_target(coverage
	COMMAND echo "executing coverage test ..."
	COMMAND rm -rf cov
	COMMAND mkdir cov
	COMMAND lcov -c -d . -o cov.info
	COMMAND genhtml -o cov cov.info
	COMMAND gcovr --xml --output=gcovr.xml .
)

add_custom_target(valgrind
	COMMAND for f in `ls tests/test_*`\; do valgrind --leak-check=full --show-reachable=yes --xml=yes --xml-file=valglind-`basename $$f`.xml $$f 2> /dev/null\; done 
)
