cmake: Improve checks for compiler flags
[resolv_wrapper.git] / tests / CMakeLists.txt
1 project(tests C)
2
3 include_directories(
4   ${CMAKE_BINARY_DIR}
5   ${CMAKE_CURRENT_SOURCE_DIR}
6   ${CMAKE_SOURCE_DIR}/src
7   ${CMOCKA_INCLUDE_DIR}
8 )
9
10 set(TORTURE_LIBRARY torture)
11
12 # A simple DNS server for testing
13 add_executable(dns_srv dns_srv.c)
14 target_compile_options(dns_srv PRIVATE ${DEFAULT_C_COMPILE_FLAGS})
15 target_link_libraries(dns_srv ${RWRAP_REQUIRED_LIBRARIES})
16
17 add_executable(test_real_res_query test_real_res_query.c)
18 target_compile_options(test_real_res_query PRIVATE ${DEFAULT_C_COMPILE_FLAGS})
19 target_link_libraries(test_real_res_query ${RWRAP_REQUIRED_LIBRARIES} ${CMOCKA_LIBRARY})
20
21 configure_file(fake_hosts.in ${CMAKE_CURRENT_BINARY_DIR}/fake_hosts @ONLY)
22
23 add_library(${TORTURE_LIBRARY} STATIC torture.c)
24 target_compile_options(${TORTURE_LIBRARY} PRIVATE ${DEFAULT_C_COMPILE_FLAGS})
25 target_link_libraries(${TORTURE_LIBRARY}
26     ${CMOCKA_LIBRARY}
27     ${SWRAP_REQUIRED_LIBRARIES})
28
29
30 set(TESTSUITE_LIBRARIES ${RWRAP_REQUIRED_LIBRARIES} ${CMOCKA_LIBRARY})
31
32 set(RWRAP_TESTS
33     test_res_init)
34
35 set(PRELOAD_LIBS ${RESOLV_WRAPPER_LOCATION})
36
37 # Some tests require socket_wrapper as well.
38 find_package(socket_wrapper REQUIRED)
39
40 # On Solaris the socket functions are compiled into libresolv.so so we can't preload
41 # socket_wrapper. Only faking will work!
42 if (HAVE_LIBRESOLV AND SOCKET_WRAPPER_LIBRARY AND NOT SOLARIS)
43     set(RWRAP_TESTS ${RWRAP_TESTS} test_res_query_search)
44     set(PRELOAD_LIBS ${RESOLV_WRAPPER_LOCATION}:${SOCKET_WRAPPER_LIBRARY})
45 endif()
46
47 foreach(_RWRAP_TEST ${RWRAP_TESTS})
48     add_cmocka_test(${_RWRAP_TEST} ${_RWRAP_TEST}.c ${TORTURE_LIBRARY} ${TESTSUITE_LIBRARIES})
49
50     if (OSX)
51         set_property(
52             TEST
53                 ${_RWRAP_TEST}
54             PROPERTY
55                 ENVIRONMENT DYLD_FORCE_FLAT_NAMESPACE=1;DYLD_INSERT_LIBRARIES=${PRELOAD_LIBS})
56     else ()
57         set_property(
58             TEST
59                 ${_RWRAP_TEST}
60             PROPERTY
61                 ENVIRONMENT LD_PRELOAD=${PRELOAD_LIBS})
62     endif()
63 endforeach()
64
65 add_cmocka_test(test_dns_fake test_dns_fake.c ${TORTURE_LIBRARY} ${TESTSUITE_LIBRARIES})
66 if (OSX)
67     set_property(
68         TEST
69             test_dns_fake
70         PROPERTY
71         ENVIRONMENT DYLD_FORCE_FLAT_NAMESPACE=1;DYLD_INSERT_LIBRARIES=${PRELOAD_LIBS};RESOLV_WRAPPER_HOSTS=${CMAKE_CURRENT_BINARY_DIR}/fake_hosts)
72 else ()
73     set_property(
74         TEST
75             test_dns_fake
76         PROPERTY
77             ENVIRONMENT LD_PRELOAD=${PRELOAD_LIBS};RESOLV_WRAPPER_HOSTS=${CMAKE_CURRENT_BINARY_DIR}/fake_hosts)
78 endif ()