-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CMakeLists.txt_z_Utils.cmake
76 lines (68 loc) · 3.27 KB
/
CMakeLists.txt_z_Utils.cmake
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#------------------------------------------------------------------------------------------------------
#------------------------------------------------------------------------------------------------------
# Scan project's sources folders.
set(SOURCE_MASK *.c *.cc *.cpp *.cxx *.c++ *.h *.hh *.hpp *.hxx *.h++ *.tcc *.tpp *.ipp *.qrc)
function(get_sources_from_dir DEST_SRCs_VAR DEST_INCs_VAR SRC_DIR)
unset(DIR_SRCs_FOUND)
foreach(SRC_MASK ${SOURCE_MASK})
file(GLOB DIR_SRCs "${SRC_DIR}/${SRC_MASK}")
if(NOT "${DIR_SRCs}" STREQUAL "")
set(DIR_SRCs_FOUND true)
get_filename_component(DIR_SRCs "${DIR_SRCs}" ABSOLUTE)
if(NOT "${DIR_SRCs}" IN_LIST ${DEST_SRCs_VAR})
list(APPEND ${DEST_SRCs_VAR} ${DIR_SRCs})
set(${DEST_SRCs_VAR} ${${DEST_SRCs_VAR}} PARENT_SCOPE)
endif()
endif()
endforeach()
if(${DIR_SRCs_FOUND})
get_filename_component(SRC_DIR "${SRC_DIR}" ABSOLUTE)
if(NOT "${SRC_DIR}" IN_LIST ${DEST_INCs_VAR})
list(APPEND ${DEST_INCs_VAR} ${SRC_DIR})
set(${DEST_INCs_VAR} ${${DEST_INCs_VAR}} PARENT_SCOPE)
endif()
endif()
endfunction()
foreach(SRC_FOLDER ${SCAN_SOURCES_FOLDER})
get_sources_from_dir(${PROJECT_NAME}_SOURCES ${PROJECT_NAME}_INCLUDES ${SRC_FOLDER})
endforeach()
#------------------------------------------------------------------------------------------------------
# Scan project's unit tests sources folder.
set(IS_IN_CONANFILE FALSE)
file(READ "conanfile.txt" CONAN_PACKAGES)
function(IS_CONANFILE_CONTAINS EXT_PKG_NAME IS_IN_CONANFILE)
set(IS_IN_CONANFILE FALSE)
if("${CONAN_PACKAGES}" MATCHES "${EXT_PKG_NAME}")
set(IS_IN_CONANFILE TRUE)
endif()
return (PROPAGATE IS_IN_CONANFILE)
endfunction()
enable_testing()
set(SOURCE_MASK_TEST *.c *.cc *.cpp *.cxx *.c++)
function(target_add_tests_from_folder TARGET_NAME FOLDER_PATH)
set(CMAKE_CXX_FLAGS "--coverage")
foreach(SRC_MASK_TEST ${SOURCE_MASK_TEST})
file(GLOB TEST_SRCs "${FOLDER_PATH}/${SRC_MASK_TEST}")
foreach(TEST_SRC ${TEST_SRCs})
get_filename_component(TEST_NAME ${TEST_SRC} NAME_WE)
set(TEST_TARGET_NAME ${TEST_NAME})
add_executable(${TEST_TARGET_NAME} "${TEST_SRC}")
target_include_directories(${TEST_TARGET_NAME} PUBLIC ${${TARGET_NAME}_INCLUDES})
IS_CONANFILE_CONTAINS("gtest" IS_IN_CONANFILE)
if(${IS_IN_CONANFILE})
find_package(GTest REQUIRED)
target_link_libraries(${TEST_TARGET_NAME} PUBLIC gtest::gtest)
endif()
IS_CONANFILE_CONTAINS("boost" IS_IN_CONANFILE)
if(${IS_IN_CONANFILE})
find_package(Boost REQUIRED COMPONENTS unit_test_framework)
target_link_libraries(${TEST_TARGET_NAME} PUBLIC Boost::unit_test_framework)
endif()
get_target_property(target_type ${TARGET_NAME} TYPE)
if (NOT target_type STREQUAL "EXECUTABLE")
target_link_libraries(${TEST_TARGET_NAME} PUBLIC ${TARGET_NAME})
endif ()
add_test(${TEST_TARGET_NAME} ${TEST_TARGET_NAME})
endforeach()
endforeach()
endfunction()