cmake: Update AddCMockaTest.cmake
[resolv_wrapper.git] / cmake / Modules / AddCMockaTest.cmake
1 #
2 # Copyright (c) 2007      Daniel Gollub <dgollub@suse.de>
3 # Copyright (c) 2007-2018 Andreas Schneider <asn@cryptomilk.org>
4 # Copyright (c) 2018      Anderson Toshiyuki Sasaki <ansasaki@redhat.com>
5 #
6 # Redistribution and use is allowed according to the terms of the BSD license.
7 # For details see the accompanying COPYING-CMAKE-SCRIPTS file.
8
9 #.rst:
10 # AddCMockaTest
11 # -------------
12 #
13 # This file provides a function to add a test
14 #
15 # Functions provided
16 # ------------------
17 #
18 # ::
19 #
20 #   add_cmocka_test(target_name
21 #                   SOURCES src1 src2 ... srcN
22 #                   [COMPILE_OPTIONS opt1 opt2 ... optN]
23 #                   [LINK_LIBRARIES lib1 lib2 ... libN]
24 #                   [LINK_OPTIONS lopt1 lop2 .. loptN]
25 #                  )
26 #
27 # ``target_name``:
28 #   Required, expects the name of the test which will be used to define a target
29 #
30 # ``SOURCES``:
31 #   Required, expects one or more source files names
32 #
33 # ``COMPILE_OPTIONS``:
34 #   Optional, expects one or more options to be passed to the compiler
35 #
36 # ``LINK_LIBRARIES``:
37 #   Optional, expects one or more libraries to be linked with the test
38 #   executable.
39 #
40 # ``LINK_OPTIONS``:
41 #   Optional, expects one or more options to be passed to the linker
42 #
43 #
44 # Example:
45 #
46 # .. code-block:: cmake
47 #
48 #   add_cmocka_test(my_test
49 #                   SOURCES my_test.c other_source.c
50 #                   COMPILE_OPTIONS -g -Wall
51 #                   LINK_LIBRARIES mylib
52 #                   LINK_OPTIONS -Wl,--enable-syscall-fixup
53 #                  )
54 #
55 # Where ``my_test`` is the name of the test, ``my_test.c`` and
56 # ``other_source.c`` are sources for the binary, ``-g -Wall`` are compiler
57 # options to be used, ``mylib`` is a target of a library to be linked, and
58 # ``-Wl,--enable-syscall-fixup`` is an option passed to the linker.
59 #
60
61 enable_testing()
62 include(CTest)
63
64 if (CMAKE_CROSSCOMPILING)
65     if (WIN32)
66         find_program(WINE_EXECUTABLE
67                      NAMES wine)
68         set(TARGET_SYSTEM_EMULATOR ${WINE_EXECUTABLE} CACHE INTERNAL "")
69     endif()
70 endif()
71
72 function(ADD_CMOCKA_TEST _TARGET_NAME)
73
74     set(one_value_arguments
75     )
76
77     set(multi_value_arguments
78         SOURCES
79         COMPILE_OPTIONS
80         LINK_LIBRARIES
81         LINK_OPTIONS
82     )
83
84     cmake_parse_arguments(_add_cmocka_test
85         ""
86         "${one_value_arguments}"
87         "${multi_value_arguments}"
88         ${ARGN}
89     )
90
91     if (NOT DEFINED _add_cmocka_test_SOURCES)
92         message(FATAL_ERROR "No sources provided for target ${_TARGET_NAME}")
93     endif()
94
95     add_executable(${_TARGET_NAME} ${_add_cmocka_test_SOURCES})
96
97     if (DEFINED _add_cmocka_test_COMPILE_OPTIONS)
98         target_compile_options(${_TARGET_NAME}
99             PRIVATE ${_add_cmocka_test_COMPILE_OPTIONS}
100         )
101     endif()
102
103     if (DEFINED _add_cmocka_test_LINK_LIBRARIES)
104         target_link_libraries(${_TARGET_NAME}
105             PRIVATE ${_add_cmocka_test_LINK_LIBRARIES}
106         )
107     endif()
108
109     if (DEFINED _add_cmocka_test_LINK_OPTIONS)
110         set_target_properties(${_TARGET_NAME}
111             PROPERTIES LINK_FLAGS
112             ${_add_cmocka_test_LINK_OPTIONS}
113         )
114     endif()
115
116     add_test(${_TARGET_NAME}
117         ${TARGET_SYSTEM_EMULATOR} ${_TARGET_NAME}
118     )
119
120 endfunction (ADD_CMOCKA_TEST)