cmake_minimum_required(VERSION 2.6)

project(FlyingHttpServer)


add_subdirectory(contrib/gmock-1.6.0)

OPTION(COVERAGE_ENABLED "Set to ON if you would like to enable coverage: Default=OFF")

INCLUDE_DIRECTORIES(
  inc
  contrib/gmock-1.6.0/include
  contrib/gmock-1.6.0/gtest/include
)

# function include_parent_directories
function(include_parent_directories)
  get_directory_property(incdirs DIR ${CMAKE_CURRENT_SOURCE_DIR}/.. INCLUDE_DIRECTORIES)
  INCLUDE_DIRECTORIES("${CMAKE_CURRENT_SOURCE_DIR} ${incdirs}")
endfunction (include_parent_directories)

function(add_include_directories)
  get_directory_property(incdirs DIR ${CMAKE_CURRENT_SOURCE_DIR} INCLUDE_DIRECTORIES)
  INCLUDE_DIRECTORIES("${incdirs} ${ARGN}")
endfunction (add_include_directories)

# Platform dependent system header files checking
include(cmake/header_files_checker.cmake)

# CXX_BASE_FLAGS inclusion
include(cmake/compiler_base_setting.cmake)

enable_testing()

# function cxx_library_static(<libname> "lib1;lib2;lib3;..."
#                             followed by source files)
function(cxx_library_static libname link_libs)
  add_library(${libname} STATIC ${ARGN})
  target_link_libraries(${libname} ${link_libs})
  set_property(DIRECTORY APPEND PROPERTY DIR_LIB_TARGETS "${libname}")
endfunction(cxx_library_static)

# function gmock_executable(<executable_name> "lib1;lib2;..."
#                           followed by source files)
function(gmock_executable exename link_libs)
  add_executable(${exename} ${ARGN})
  target_link_libraries(${exename} "gmock_main")
  target_link_libraries(${exename} "stdc++")
  foreach(li ${link_libs})
    target_link_libraries(${exename} ${li})
  endforeach(li)
  set_property(DIRECTORY APPEND PROPERTY DIR_TEST_TARGETS "${exename}")
  add_test(${exename} ${CMAKE_CURRENT_BINARY_DIR}/${exename})
endfunction(gmock_executable)

# function gtest_executable(<executable_name> "lib1;lib2;..."
#                           followed by source files
function(gtest_executable exename link_libs)
  add_executable(${exename} ${ARGN})
  target_link_libraries(${exename} "gmock_main")
  target_link_libraries(${exename} "stdc++")
  foreach(li ${link_libs})
    target_link_libraries(${exename} ${li})
  endforeach(li)
  set_property(DIRECTORY APPEND PROPERTY DIR_TEST_TARGETS "${exename}")
  add_test(${exename} ${CMAKE_CURRENT_BINARY_DIR}/${exename})
endfunction(gtest_executable)

function(enable_coverage)
  if (COVERAGE_ENABLED)
    get_property(libtargets DIRECTORY PROPERTY DIR_LIB_TARGETS)
    foreach(tgt ${libtargets})
      MESSAGE("[coverage] processing library target ${tgt}")
      set_target_properties(${tgt} PROPERTIES COMPILE_FLAGS "${CXX_CUSTOMIZED_TARGET_FLAGS} ${CXX_CUSTOMIZED_OPTIMIZATION_FLAGS_COVERAGE}")
      set_target_properties(${tgt} PROPERTIES LINK_FLAGS "${CXX_CUSTOMIZED_LINK_FLAGS_COVERAGE}")
    endforeach(tgt)
    get_property(testtargets DIRECTORY PROPERTY DIR_TEST_TARGETS)
    foreach(tgt ${testtargets})
      MESSAGE("[coverage] processing test target ${tgt}")
      set_target_properties(${tgt} PROPERTIES COMPILE_FLAGS "${CXX_CUSTOMIZED_TEST_FLAGS} ${CXX_CUSTOMIZED_OPTIMIZATION_FLAGS_COVERAGE}")
      set_target_properties(${tgt} PROPERTIES LINK_FLAGS "${CXX_CUSTOMIZED_LINK_FLAGS_COVERAGE}")
    endforeach(tgt)
  else (COVERAGE_ENABLED)
    get_property(libtargets DIRECTORY PROPERTY DIR_LIB_TARGETS)
    foreach(tgt ${libtargets})
      MESSAGE("[no coverage] processing library target ${tgt}")
      set_target_properties(${tgt} PROPERTIES COMPILE_FLAGS "${CXX_CUSTOMIZED_TARGET_FLAGS} ${CXX_CUSTOMIZED_OPTIMIZATION_FLAGS}")
    endforeach(tgt)
    get_property(testtargets DIRECTORY PROPERTY DIR_TEST_TARGETS)
    foreach(tgt ${testtargets})
      MESSAGE("[no coverage] processing test target ${tgt}")
      set_target_properties(${tgt} PROPERTIES COMPILE_FLAGS "${CXX_CUSTOMIZED_TEST_FLAGS} ${CXX_CUSTOMIZED_OPTIMIZATION_FLAGS}")
    endforeach(tgt)
  endif (COVERAGE_ENABLED)
endfunction(enable_coverage)

add_subdirectory(inc)
add_subdirectory(cm)

add_custom_target(coverage-info-org
  lcov --capture --directory ${CMAKE_CURRENT_BINARY_DIR}/. -o coverage-org.info
)
add_custom_target(coverage-info-stdlib-excluded
  lcov --remove coverage-org.info \"/usr*\" -o coverage-stdlib-excluded.info
)
add_custom_target(coverage-info
  lcov --remove coverage-stdlib-excluded.info \"contrib*\" -o coverage.info
)
add_custom_target(coverage genhtml ${CMAKE_CURRENT_BINARY_DIR}/coverage.info)
add_dependencies(coverage coverage-info)
add_dependencies(coverage-info coverage-info-stdlib-excluded)
add_dependencies(coverage-info-stdlib-excluded coverage-info-org)

