CMakeLists.txt 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. if(CMAKE_SYSTEM_NAME STREQUAL Windows)
  2. execute_process(COMMAND
  3. "${CMAKE_COMMAND}" -E copy_directory "${PROJECT_SOURCE_DIR}/private"
  4. "${CMAKE_CURRENT_BINARY_DIR}/dispatch")
  5. execute_process(COMMAND
  6. "${CMAKE_COMMAND}" -E copy "${CMAKE_CURRENT_SOURCE_DIR}/leaks-wrapper.sh"
  7. "${CMAKE_CURRENT_BINARY_DIR}/leaks-wrapper")
  8. else()
  9. execute_process(COMMAND
  10. "${CMAKE_COMMAND}" -E create_symlink "${PROJECT_SOURCE_DIR}/private"
  11. "${CMAKE_CURRENT_BINARY_DIR}/dispatch")
  12. execute_process(COMMAND
  13. "${CMAKE_COMMAND}" -E create_symlink "${CMAKE_CURRENT_SOURCE_DIR}/leaks-wrapper.sh"
  14. "${CMAKE_CURRENT_BINARY_DIR}/leaks-wrapper")
  15. endif()
  16. if(CMAKE_SYSTEM_NAME STREQUAL Linux)
  17. set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -lrt")
  18. endif()
  19. add_library(bsdtests
  20. STATIC
  21. bsdtests.c
  22. dispatch_test.c)
  23. target_include_directories(bsdtests
  24. PRIVATE
  25. ${CMAKE_CURRENT_BINARY_DIR}
  26. ${CMAKE_CURRENT_SOURCE_DIR}
  27. ${PROJECT_SOURCE_DIR}
  28. PUBLIC
  29. # bsdtests.h needs config_ac.h
  30. ${PROJECT_BINARY_DIR})
  31. if (WIN32)
  32. target_sources(bsdtests
  33. PRIVATE
  34. generic_win_port.c)
  35. target_compile_definitions(bsdtests
  36. PUBLIC
  37. _CRT_NONSTDC_NO_WARNINGS
  38. _CRT_SECURE_NO_WARNINGS
  39. _USE_MATH_DEFINES)
  40. target_link_libraries(bsdtests
  41. PUBLIC
  42. bcrypt)
  43. endif ()
  44. add_executable(bsdtestharness
  45. bsdtestharness.c)
  46. target_include_directories(bsdtestharness
  47. PRIVATE
  48. ${CMAKE_CURRENT_BINARY_DIR}
  49. ${CMAKE_CURRENT_SOURCE_DIR}
  50. ${PROJECT_SOURCE_DIR})
  51. target_link_libraries(bsdtestharness
  52. PRIVATE
  53. bsdtests
  54. dispatch)
  55. function(add_unit_test name)
  56. set(options DISABLED_TEST)
  57. set(single_value_args)
  58. set(multiple_value_args SOURCES)
  59. cmake_parse_arguments(AUT "${options}" "${single_value_args}" "${multiple_value_args}" ${ARGN})
  60. if(AUT_DISABLED_TEST)
  61. return()
  62. endif()
  63. add_executable(${name} ${AUT_SOURCES})
  64. target_include_directories(${name}
  65. PRIVATE
  66. ${CMAKE_CURRENT_BINARY_DIR}
  67. ${CMAKE_CURRENT_SOURCE_DIR}
  68. ${PROJECT_SOURCE_DIR})
  69. if(ENABLE_SWIFT)
  70. # For testing in swift.org CI system; make deadlines lenient by default
  71. # to reduce probability of test failures due to machine load.
  72. target_compile_options(${name} PRIVATE -DLENIENT_DEADLINES=1)
  73. endif()
  74. target_include_directories(${name}
  75. SYSTEM BEFORE PRIVATE
  76. "${BlocksRuntime_INCLUDE_DIR}")
  77. if("${CMAKE_C_SIMULATE_ID}" STREQUAL "MSVC")
  78. target_compile_options(${name} PRIVATE -Xclang -fblocks)
  79. target_compile_options(${name} PRIVATE /W3 -Wno-deprecated-declarations)
  80. else()
  81. target_compile_options(${name} PRIVATE -fblocks)
  82. target_compile_options(${name} PRIVATE -Wall -Wno-deprecated-declarations)
  83. endif()
  84. # Without this flag, cross-compiling static test executables for Android armv7
  85. # fails with the multiple definition errors seen in android/ndk#176, so I
  86. # pulled in this workaround noted there. The tests build and run with this
  87. # flag applied.
  88. if(NOT BUILD_SHARED_LIBS AND CMAKE_SYSTEM_NAME STREQUAL Android AND
  89. CMAKE_SYSTEM_PROCESSOR STREQUAL armv7-a)
  90. target_link_options(${name} PRIVATE "LINKER:--allow-multiple-definition")
  91. endif()
  92. target_link_libraries(${name}
  93. PRIVATE
  94. dispatch
  95. Threads::Threads
  96. BlocksRuntime::BlocksRuntime)
  97. target_link_libraries(${name} PRIVATE bsdtests)
  98. add_test(NAME ${name}
  99. COMMAND bsdtestharness $<TARGET_FILE:${name}>)
  100. set_tests_properties(${name}
  101. PROPERTIES
  102. TIMEOUT 120
  103. DEPENDS bsdtestharness
  104. WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})
  105. if(NOT leaks_EXECUTABLE)
  106. set_tests_properties(${name}
  107. PROPERTIES
  108. ENVIRONMENT NOLEAKS=1)
  109. endif()
  110. endfunction()
  111. # Tests that reliably pass on all platforms
  112. set(DISPATCH_C_TESTS
  113. apply
  114. api
  115. debug
  116. queue_finalizer
  117. overcommit
  118. context_for_key
  119. after
  120. timer
  121. timer_short
  122. timer_timeout
  123. sema
  124. timer_bit31
  125. timer_bit63
  126. timer_set_time
  127. data
  128. io_muxed
  129. io_net
  130. io_pipe
  131. io_pipe_close
  132. select)
  133. # Tests that usually pass, but occasionally fail.
  134. # Excluded by default for purposes of Swift CI
  135. if(EXTENDED_TEST_SUITE)
  136. # When dispatch_group is reenabled here, also remove the if(EXTENDED_TEST_SUITE) condition below
  137. list(APPEND DISPATCH_C_TESTS
  138. priority
  139. concur
  140. group
  141. read
  142. read2
  143. starfish
  144. suspend_timer
  145. pingpong
  146. drift
  147. readsync
  148. cascade
  149. io)
  150. # an oddball; dispatch_priority.c compiled with -DUSE_SET_TARGET_QUEUE=1
  151. add_unit_test(dispatch_priority2 SOURCES dispatch_priority.c)
  152. target_compile_options(dispatch_priority2 PRIVATE -DUSE_SET_TARGET_QUEUE=1)
  153. endif()
  154. # add C tests for platform-specific functionality when applicable
  155. if(CMAKE_SYSTEM_NAME STREQUAL Darwin)
  156. list(APPEND DISPATCH_C_TESTS
  157. deadname
  158. proc
  159. vm
  160. vnode)
  161. endif()
  162. foreach(test ${DISPATCH_C_TESTS})
  163. add_unit_test(dispatch_${test}
  164. SOURCES
  165. dispatch_${test}.c)
  166. endforeach()
  167. set_tests_properties(dispatch_io_pipe PROPERTIES TIMEOUT 15)
  168. set_tests_properties(dispatch_io_pipe_close PROPERTIES TIMEOUT 5)
  169. # test dispatch API for various C/CXX language variants
  170. add_unit_test(dispatch_c99 SOURCES dispatch_c99.c)
  171. add_unit_test(dispatch_plusplus SOURCES dispatch_plusplus.cpp)
  172. # test-specific link options
  173. if(WIN32)
  174. target_link_libraries(dispatch_io_muxed PRIVATE WS2_32)
  175. target_link_libraries(dispatch_io_net PRIVATE WS2_32)
  176. else()
  177. # When dispatch_group is reenabled above, remove this
  178. if(EXTENDED_TEST_SUITE)
  179. target_link_libraries(dispatch_group PRIVATE m)
  180. endif()
  181. target_link_libraries(dispatch_timer_short PRIVATE m)
  182. endif()
  183. # test-specific compile options
  184. set_target_properties(dispatch_c99 PROPERTIES C_STANDARD 99)