comparison cmake/function/MalikaniaDefineTest.cmake @ 49:2804ae55c70f

CMake: big cleanup, closes #598
author David Demelier <markand@malikania.fr>
date Fri, 09 Dec 2016 13:28:45 +0100
parents
children 0edaba9986ba
comparison
equal deleted inserted replaced
48:3be179ba3226 49:2804ae55c70f
1 #
2 # MalikaniaDefineTest.cmake -- CMake build system for malikania
3 #
4 # Copyright (c) 2013-2016 Malikania Authors
5 #
6 # Permission to use, copy, modify, and/or distribute this software for any
7 # purpose with or without fee is hereby granted, provided that the above
8 # copyright notice and this permission notice appear in all copies.
9 #
10 # THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 # WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 # MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 # ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 # WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 # ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 # OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 #
18
19 #
20 # malikania_create_test
21 # ---------------------
22 #
23 # malikania_create_test(
24 # NAME Test name (must be lowercase)
25 # SOURCES Test sources files
26 # LIBRARIES (Optional) Libraries to link to
27 # RESOURCES (Optional) Resources files to copy verbatim
28 # )
29 #
30 # This will generate a target named test-<name> where name is the parameter NAME. The test is created
31 # under CMAKE_BINARY_DIR/test/<NAME> and resources are copied there with the same hierarchy.
32 #
33
34 include(CMakeParseArguments)
35
36 function(malikania_create_test)
37 set(singleArgs NAME)
38 set(multiArgs LIBRARIES SOURCES RESOURCES)
39
40 cmake_parse_arguments(TEST "" "${singleArgs}" "${multiArgs}" ${ARGN})
41
42 if (NOT TEST_NAME)
43 message(FATAL_ERROR "Missing NAME parameter")
44 endif ()
45 if (NOT TEST_SOURCES)
46 message(FATAL_ERROR "Missing SOURCES parameter")
47 endif ()
48
49 file(MAKE_DIRECTORY ${CMAKE_BINARY_DIR}/test/${TEST_NAME})
50
51 if (UNIX)
52 list(APPEND TEST_LIBRARIES pthread)
53 endif ()
54
55 # Resources files added before as custom output
56 foreach (f ${TEST_RESOURCES})
57 get_filename_component(absolute ${f} ABSOLUTE)
58 file(RELATIVE_PATH basename ${CMAKE_CURRENT_SOURCE_DIR} ${absolute})
59 set(output ${CMAKE_BINARY_DIR}/test/${TEST_NAME}/${basename})
60
61 add_custom_command(
62 OUTPUT ${output}
63 COMMAND ${CMAKE_COMMAND} -E copy ${absolute} ${output}
64 DEPENDS ${absolute}
65 )
66
67 list(APPEND TEST_SOURCES ${absolute})
68 list(APPEND outputs ${output})
69 endforeach ()
70
71 add_executable(test-${TEST_NAME} ${TEST_SOURCES} ${outputs})
72 source_group(private\\Resources FILES ${outputs})
73 target_compile_definitions(
74 test-${TEST_NAME}
75 PRIVATE
76 BOOST_TEST_DYN_LINK
77 SOURCE_DIRECTORY=\"${CMAKE_BINARY_DIR}/test/${TEST_NAME}\"
78 )
79 set_target_properties(
80 test-${TEST_NAME}
81 PROPERTIES
82 RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/test/${TEST_NAME}
83 RUNTIME_OUTPUT_DIRECTORY_DEBUG ${CMAKE_BINARY_DIR}/test/${TEST_NAME}
84 RUNTIME_OUTPUT_DIRECTORY_RELEASE ${CMAKE_BINARY_DIR}/test/${TEST_NAME}
85 RUNTIME_OUTPUT_DIRECTORY_RELWITHDEBINFO ${CMAKE_BINARY_DIR}/test/${TEST_NAME}
86 RUNTIME_OUTPUT_DIRECTORY_MINSIZEREL ${CMAKE_BINARY_DIR}/test/${TEST_NAME}
87 )
88 add_test(
89 NAME ${TEST_NAME}
90 COMMAND $<TARGET_FILE:test-${TEST_NAME}>
91 WORKING_DIRECTORY ${CMAKE_BINARY_DIR}/test/${TEST_NAME}
92 )
93
94 target_link_libraries(
95 test-${TEST_NAME}
96 ${TEST_LIBRARIES}
97 ${Boost_unit_test_framework_LIBRARY}
98 )
99
100 add_dependencies(tests test-${TEST_NAME})
101 endfunction()