diff options
| author | Andreas Schneider <asn@samba.org> | 2018-09-27 17:22:57 +0200 |
|---|---|---|
| committer | Andreas Schneider <asn@samba.org> | 2018-10-23 15:38:50 +0200 |
| commit | b976977a5355b67bd563e363abb3d2cb7fa6d3c9 (patch) | |
| tree | a16b065bacb044f149abfecbb98a561f3ea38362 | |
| parent | b91dae57e472cafe1c2014b84f79a9552d53579c (diff) | |
| download | socket_wrapper-b976977a5355b67bd563e363abb3d2cb7fa6d3c9.tar.gz socket_wrapper-b976977a5355b67bd563e363abb3d2cb7fa6d3c9.tar.xz socket_wrapper-b976977a5355b67bd563e363abb3d2cb7fa6d3c9.zip | |
cmake: Update AddCMockaTest
Signed-off-by: Andreas Schneider <asn@samba.org>
Reviewed-by: Ralph Boehme <slow@samba.org>
| -rw-r--r-- | cmake/Modules/AddCMockaTest.cmake | 118 | ||||
| -rw-r--r-- | tests/CMakeLists.txt | 83 |
2 files changed, 152 insertions, 49 deletions
diff --git a/cmake/Modules/AddCMockaTest.cmake b/cmake/Modules/AddCMockaTest.cmake index 6a34e76..2f45e1a 100644 --- a/cmake/Modules/AddCMockaTest.cmake +++ b/cmake/Modules/AddCMockaTest.cmake @@ -1,16 +1,120 @@ -# - ADD_CHECK_TEST(test_name test_source linklib1 ... linklibN) - +# # Copyright (c) 2007 Daniel Gollub <dgollub@suse.de> -# Copyright (c) 2007-2010 Andreas Schneider <asn@cynapses.org> +# Copyright (c) 2007-2018 Andreas Schneider <asn@cryptomilk.org> +# Copyright (c) 2018 Anderson Toshiyuki Sasaki <ansasaki@redhat.com> # # Redistribution and use is allowed according to the terms of the BSD license. # For details see the accompanying COPYING-CMAKE-SCRIPTS file. +#.rst: +# AddCMockaTest +# ------------- +# +# This file provides a function to add a test +# +# Functions provided +# ------------------ +# +# :: +# +# add_cmocka_test(target_name +# SOURCES src1 src2 ... srcN +# [COMPILE_OPTIONS opt1 opt2 ... optN] +# [LINK_LIBRARIES lib1 lib2 ... libN] +# [LINK_OPTIONS lopt1 lop2 .. loptN] +# ) +# +# ``target_name``: +# Required, expects the name of the test which will be used to define a target +# +# ``SOURCES``: +# Required, expects one or more source files names +# +# ``COMPILE_OPTIONS``: +# Optional, expects one or more options to be passed to the compiler +# +# ``LINK_LIBRARIES``: +# Optional, expects one or more libraries to be linked with the test +# executable. +# +# ``LINK_OPTIONS``: +# Optional, expects one or more options to be passed to the linker +# +# +# Example: +# +# .. code-block:: cmake +# +# add_cmocka_test(my_test +# SOURCES my_test.c other_source.c +# COMPILE_OPTIONS -g -Wall +# LINK_LIBRARIES mylib +# LINK_OPTIONS -Wl,--enable-syscall-fixup +# ) +# +# Where ``my_test`` is the name of the test, ``my_test.c`` and +# ``other_source.c`` are sources for the binary, ``-g -Wall`` are compiler +# options to be used, ``mylib`` is a target of a library to be linked, and +# ``-Wl,--enable-syscall-fixup`` is an option passed to the linker. +# + enable_testing() include(CTest) -function (ADD_CMOCKA_TEST _testName _testSource) - add_executable(${_testName} ${_testSource}) - target_link_libraries(${_testName} ${ARGN}) - add_test(${_testName} ${CMAKE_CURRENT_BINARY_DIR}/${_testName}) +if (CMAKE_CROSSCOMPILING) + if (WIN32) + find_program(WINE_EXECUTABLE + NAMES wine) + set(TARGET_SYSTEM_EMULATOR ${WINE_EXECUTABLE} CACHE INTERNAL "") + endif() +endif() + +function(ADD_CMOCKA_TEST _TARGET_NAME) + + set(one_value_arguments + ) + + set(multi_value_arguments + SOURCES + COMPILE_OPTIONS + LINK_LIBRARIES + LINK_OPTIONS + ) + + cmake_parse_arguments(_add_cmocka_test + "" + "${one_value_arguments}" + "${multi_value_arguments}" + ${ARGN} + ) + + if (NOT DEFINED _add_cmocka_test_SOURCES) + message(FATAL_ERROR "No sources provided for target ${_TARGET_NAME}") + endif() + + add_executable(${_TARGET_NAME} ${_add_cmocka_test_SOURCES}) + + if (DEFINED _add_cmocka_test_COMPILE_OPTIONS) + target_compile_options(${_TARGET_NAME} + PRIVATE ${_add_cmocka_test_COMPILE_OPTIONS} + ) + endif() + + if (DEFINED _add_cmocka_test_LINK_LIBRARIES) + target_link_libraries(${_TARGET_NAME} + PRIVATE ${_add_cmocka_test_LINK_LIBRARIES} + ) + endif() + + if (DEFINED _add_cmocka_test_LINK_OPTIONS) + set_target_properties(${_TARGET_NAME} + PROPERTIES LINK_FLAGS + ${_add_cmocka_test_LINK_OPTIONS} + ) + endif() + + add_test(${_TARGET_NAME} + ${TARGET_SYSTEM_EMULATOR} ${_TARGET_NAME} + ) + endfunction (ADD_CMOCKA_TEST) diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 464ac9b..3578f76 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -52,7 +52,6 @@ set(SWRAP_TESTS test_swrap_unit test_max_sockets test_close_failure - test_fork_thread_deadlock test_tcp_socket_overwrite ${SWRAP_THREADED_TESTS}) @@ -60,52 +59,47 @@ if (HAVE_STRUCT_MSGHDR_MSG_CONTROL) set(SWRAP_TESTS ${SWRAP_TESTS} test_sendmsg_recvmsg_fd) endif (HAVE_STRUCT_MSGHDR_MSG_CONTROL) -if (CMAKE_BUILD_TYPE) - string(TOLOWER "${CMAKE_BUILD_TYPE}" CMAKE_BUILD_TYPE_LOWER) - if (CMAKE_BUILD_TYPE_LOWER STREQUAL "addresssanitizer") - find_library(ASAN_LIBRARY - NAMES asan) - if (NOT ASAN_LIBRARY) - foreach(version RANGE 10 1) - if (NOT ASAN_LIBRARY) - find_library(ASAN_LIBRARY libasan.so.${version}) - endif() - endforeach() +function(ADD_CMOCKA_TEST_ENVIRONMENT _TEST_NAME) + if (CMAKE_BUILD_TYPE) + string(TOLOWER "${CMAKE_BUILD_TYPE}" CMAKE_BUILD_TYPE_LOWER) + if (CMAKE_BUILD_TYPE_LOWER STREQUAL "addresssanitizer") + find_library(ASAN_LIBRARY + NAMES asan) + if (NOT ASAN_LIBRARY) + foreach(version RANGE 10 1) + if (NOT ASAN_LIBRARY) + find_library(ASAN_LIBRARY libasan.so.${version}) + endif() + endforeach() + endif() endif() endif() -endif() - -if (ASAN_LIBRARY) - list(APPEND PRELOAD_LIBRARIES ${ASAN_LIBRARY}) -endif() - -list(APPEND PRELOAD_LIBRARIES ${SOCKET_WRAPPER_LOCATION}) - -string(REPLACE ";" ":" TORTURE_ENVIRONMENT "${PRELOAD_LIBRARIES}") -string(PREPEND TORTURE_ENVIRONMENT "LD_PRELOAD=") - -message(STATUS "TORTURE_ENVIRONMENT=${TORTURE_ENVIRONMENT}") -foreach(_SWRAP_TEST ${SWRAP_TESTS}) - add_cmocka_test(${_SWRAP_TEST} ${_SWRAP_TEST}.c ${TORTURE_LIBRARY}) - target_compile_options(${_SWRAP_TEST} - PRIVATE - ${DEFAULT_C_COMPILE_FLAGS} - -D_GNU_SOURCE) + if (ASAN_LIBRARY) + list(APPEND PRELOAD_LIBRARIES ${ASAN_LIBRARY}) + endif() + list(APPEND PRELOAD_LIBRARIES ${SOCKET_WRAPPER_LOCATION}) if (OSX) - set_property( - TEST - ${_SWRAP_TEST} - PROPERTY - ENVIRONMENT DYLD_FORCE_FLAT_NAMESPACE=1;DYLD_INSERT_LIBRARIES=${SOCKET_WRAPPER_LOCATION}) + set(TORTURE_ENVIRONMENT "DYLD_FORCE_FLAT_NAMESPACE=1;DYLD_INSERT_LIBRARIES=${SOCKET_WRAPPER_LOCATION}") else () - set_property( - TEST - ${_SWRAP_TEST} - PROPERTY - ENVIRONMENT ${TORTURE_ENVIRONMENT}) + string(REPLACE ";" ":" TORTURE_ENVIRONMENT "${PRELOAD_LIBRARIES}") + string(PREPEND TORTURE_ENVIRONMENT "LD_PRELOAD=") endif() + + set_property(TEST + ${_TEST_NAME} + PROPERTY + ENVIRONMENT "${TORTURE_ENVIRONMENT}") +endfunction() + +foreach(_SWRAP_TEST ${SWRAP_TESTS}) + add_cmocka_test(${_SWRAP_TEST} + SOURCES ${_SWRAP_TEST}.c + COMPILE_OPTIONS ${DEFAULT_C_COMPILE_FLAGS} -D_GNU_SOURCE + LINK_LIBRARIES ${TORTURE_LIBRARY} + LINK_OPTIONS ${DEFAULT_LINK_FLAGS}) + add_cmocka_test_environment(${_SWRAP_TEST}) endforeach() if (HELGRIND_TESTING) @@ -138,5 +132,10 @@ endif() add_library(thread_deadlock SHARED thread_deadlock.c) target_link_libraries(thread_deadlock ${CMAKE_THREAD_LIBS_INIT}) target_compile_options(thread_deadlock PRIVATE ${DEFAULT_C_COMPILE_FLAGS}) -target_link_libraries(test_fork_thread_deadlock thread_deadlock) -target_compile_options(test_fork_thread_deadlock PRIVATE ${DEFAULT_C_COMPILE_FLAGS}) + +add_cmocka_test(test_fork_thread_deadlock + SOURCES test_fork_thread_deadlock.c + COMPILE_OPTIONS ${DEFAULT_C_COMPILE_FLAGS} -D_GNU_SOURCE + LINK_LIBRARIES ${TORTURE_LIBRARY} thread_deadlock + LINK_OPTIONS ${DEFAULT_LINK_FLAGS}) +add_cmocka_test_environment(test_fork_thread_deadlock) |
