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>
6 # Redistribution and use is allowed according to the terms of the BSD license.
7 # For details see the accompanying COPYING-CMAKE-SCRIPTS file.
13 # This file provides a function to add a test
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]
28 # Required, expects the name of the test which will be used to define a target
31 # Required, expects one or more source files names
33 # ``COMPILE_OPTIONS``:
34 # Optional, expects one or more options to be passed to the compiler
37 # Optional, expects one or more libraries to be linked with the test
41 # Optional, expects one or more options to be passed to the linker
46 # .. code-block:: cmake
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
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.
64 if (CMAKE_CROSSCOMPILING)
66 find_program(WINE_EXECUTABLE
68 set(TARGET_SYSTEM_EMULATOR ${WINE_EXECUTABLE} CACHE INTERNAL "")
72 function(ADD_CMOCKA_TEST _TARGET_NAME)
74 set(one_value_arguments
77 set(multi_value_arguments
84 cmake_parse_arguments(_add_cmocka_test
86 "${one_value_arguments}"
87 "${multi_value_arguments}"
91 if (NOT DEFINED _add_cmocka_test_SOURCES)
92 message(FATAL_ERROR "No sources provided for target ${_TARGET_NAME}")
95 add_executable(${_TARGET_NAME} ${_add_cmocka_test_SOURCES})
97 if (DEFINED _add_cmocka_test_COMPILE_OPTIONS)
98 target_compile_options(${_TARGET_NAME}
99 PRIVATE ${_add_cmocka_test_COMPILE_OPTIONS}
103 if (DEFINED _add_cmocka_test_LINK_LIBRARIES)
104 target_link_libraries(${_TARGET_NAME}
105 PRIVATE ${_add_cmocka_test_LINK_LIBRARIES}
109 if (DEFINED _add_cmocka_test_LINK_OPTIONS)
110 set_target_properties(${_TARGET_NAME}
111 PROPERTIES LINK_FLAGS
112 ${_add_cmocka_test_LINK_OPTIONS}
116 add_test(${_TARGET_NAME}
117 ${TARGET_SYSTEM_EMULATOR} ${_TARGET_NAME}
120 endfunction (ADD_CMOCKA_TEST)