CMakeLists.txt 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. # Determine if we should print to the output
  2. if (CIVETWEB_ENABLE_THIRD_PARTY_OUTPUT)
  3. set(THIRD_PARTY_LOGGING 0)
  4. else()
  5. set(THIRD_PARTY_LOGGING 1)
  6. endif()
  7. # We use the check unit testing framework for our C unit tests
  8. include(ExternalProject)
  9. #if(NOT WIN32)
  10. # # Apply the patch to check to fix CMake building on OS X
  11. # set(CHECK_PATCH_COMMAND patch
  12. # ${CIVETWEB_THIRD_PARTY_DIR}/src/check-unit-test-framework/CMakeLists.txt
  13. # ${CMAKE_SOURCE_DIR}/cmake/check/c82fe8888aacfe784476112edd3878256d2e30bc.patch
  14. # )
  15. #else()
  16. # set(CHECK_PATCH_COMMAND "")
  17. #endif()
  18. ExternalProject_Add(check-unit-test-framework
  19. DEPENDS civetweb-c-library
  20. ## Use an official, released check version:
  21. # URL "https://codeload.github.com/libcheck/check/zip/${CIVETWEB_CHECK_VERSION}"
  22. # DOWNLOAD_NAME "${CIVETWEB_CHECK_VERSION}.zip"
  23. # URL_MD5 ${CIVETWEB_CHECK_MD5_HASH}
  24. ## Use a civetweb specific patched version
  25. URL "https://github.com/civetweb/check/archive/master.zip"
  26. DOWNLOAD_NAME "master.zip"
  27. # <Edit this file to flush AppVeyor build cache and force reloading check>
  28. PREFIX "${CIVETWEB_THIRD_PARTY_DIR}"
  29. BUILD_IN_SOURCE 1
  30. PATCH_COMMAND ${CHECK_PATCH_COMMAND}
  31. CMAKE_ARGS
  32. "-DCMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE}"
  33. "-DCMAKE_C_COMPILER=${CMAKE_C_COMPILER}"
  34. "-DCMAKE_INSTALL_PREFIX=<INSTALL_DIR>"
  35. LOG_DOWNLOAD ${THIRD_PARTY_LOGGING}
  36. LOG_UPDATE ${THIRD_PARTY_LOGGING}
  37. LOG_CONFIGURE ${THIRD_PARTY_LOGGING}
  38. LOG_BUILD ${THIRD_PARTY_LOGGING}
  39. LOG_TEST ${THIRD_PARTY_LOGGING}
  40. LOG_INSTALL ${THIRD_PARTY_LOGGING})
  41. ExternalProject_Get_Property(check-unit-test-framework INSTALL_DIR)
  42. set(CHECK_INSTALL_DIR ${INSTALL_DIR})
  43. unset(INSTALL_DIR)
  44. link_directories("${CHECK_INSTALL_DIR}/lib")
  45. include_directories("${CHECK_INSTALL_DIR}/include")
  46. if ((WIN32 AND MINGW) OR APPLE)
  47. set(CHECK_LIBRARIES "${CHECK_LIBRARIES};${CHECK_INSTALL_DIR}/lib/libcheck.a")
  48. set(CHECK_LIBRARIES "${CHECK_LIBRARIES};${CHECK_INSTALL_DIR}/lib/libcompat.a")
  49. elseif (WIN32)
  50. set(CHECK_LIBRARIES "${CHECK_LIBRARIES};${CHECK_INSTALL_DIR}/lib/check.lib")
  51. set(CHECK_LIBRARIES "${CHECK_LIBRARIES};${CHECK_INSTALL_DIR}/lib/compat.lib")
  52. else()
  53. set(CHECK_LIBRARIES "${CHECK_INSTALL_DIR}/lib/libcheck.a")
  54. endif()
  55. find_package(LibM)
  56. if (LIBM_FOUND)
  57. set(CHECK_LIBRARIES "${CHECK_LIBRARIES};LIBM::LIBM")
  58. endif()
  59. find_package(LibRt)
  60. if (LIBRT_FOUND)
  61. set(CHECK_LIBRARIES "${CHECK_LIBRARIES};LIBRT::LIBRT")
  62. endif()
  63. # Build the C unit tests
  64. add_library(shared-c-unit-tests STATIC shared.c)
  65. target_include_directories(
  66. shared-c-unit-tests PUBLIC
  67. ${PROJECT_SOURCE_DIR}/include)
  68. add_library(public-func-c-unit-tests STATIC public_func.c)
  69. if (BUILD_SHARED_LIBS)
  70. target_compile_definitions(public-func-c-unit-tests PRIVATE CIVETWEB_DLL_IMPORTS)
  71. endif()
  72. target_include_directories(
  73. public-func-c-unit-tests PUBLIC
  74. ${PROJECT_SOURCE_DIR}/include)
  75. target_link_libraries(public-func-c-unit-tests civetweb-c-library ${CHECK_LIBRARIES})
  76. add_dependencies(public-func-c-unit-tests check-unit-test-framework)
  77. add_library(public-server-c-unit-tests STATIC public_server.c)
  78. if (BUILD_SHARED_LIBS)
  79. target_compile_definitions(public-server-c-unit-tests PRIVATE CIVETWEB_DLL_IMPORTS)
  80. endif()
  81. target_include_directories(
  82. public-server-c-unit-tests PUBLIC
  83. ${PROJECT_SOURCE_DIR}/include)
  84. target_link_libraries(public-server-c-unit-tests civetweb-c-library ${CHECK_LIBRARIES})
  85. add_dependencies(public-server-c-unit-tests check-unit-test-framework)
  86. add_library(private-c-unit-tests STATIC private.c)
  87. target_include_directories(
  88. private-c-unit-tests PUBLIC
  89. ${PROJECT_SOURCE_DIR}/include)
  90. target_link_libraries(private-c-unit-tests ${CHECK_LIBRARIES})
  91. add_dependencies(private-c-unit-tests check-unit-test-framework)
  92. add_library(timer-c-unit-tests STATIC timertest.c)
  93. target_include_directories(
  94. timer-c-unit-tests PUBLIC
  95. ${PROJECT_SOURCE_DIR}/include)
  96. target_link_libraries(timer-c-unit-tests ${CHECK_LIBRARIES})
  97. add_dependencies(timer-c-unit-tests check-unit-test-framework)
  98. add_library(exe-c-unit-tests STATIC private_exe.c)
  99. if (BUILD_SHARED_LIBS)
  100. target_compile_definitions(exe-c-unit-tests PRIVATE)
  101. endif()
  102. target_include_directories(
  103. exe-c-unit-tests PUBLIC
  104. ${PROJECT_SOURCE_DIR}/include)
  105. target_link_libraries(exe-c-unit-tests civetweb-c-library ${CHECK_LIBRARIES})
  106. add_dependencies(exe-c-unit-tests check-unit-test-framework)
  107. add_executable(main-c-unit-test main.c)
  108. target_link_libraries(main-c-unit-test
  109. shared-c-unit-tests
  110. public-func-c-unit-tests
  111. public-server-c-unit-tests
  112. private-c-unit-tests
  113. timer-c-unit-tests
  114. exe-c-unit-tests
  115. ${CHECK_LIBRARIES})
  116. add_dependencies(main-c-unit-test check-unit-test-framework)
  117. # Add a check command that builds the dependent test program
  118. add_custom_target(check
  119. COMMAND ${CMAKE_CTEST_COMMAND}
  120. DEPENDS main-c-unit-test)
  121. # A macro for adding tests
  122. macro(civetweb_add_test suite test_case)
  123. set(test "test-${suite}-${test_case}")
  124. string(TOLOWER "${test}" test)
  125. string(REGEX REPLACE "[^-A-Za-z0-9]" "-" test "${test}")
  126. add_test(
  127. NAME ${test}
  128. COMMAND main-c-unit-test "--test-dir=${CMAKE_CURRENT_SOURCE_DIR}" "--suite=${suite}" "--test-case=${test_case}")
  129. if (WIN32)
  130. string(REPLACE ";" "\\;" test_path "$ENV{PATH}")
  131. set_tests_properties(${test} PROPERTIES
  132. ENVIRONMENT "PATH=${test_path}\\;$<TARGET_FILE_DIR:civetweb-c-library>")
  133. endif()
  134. endmacro(civetweb_add_test)
  135. # Tests of private functions
  136. civetweb_add_test(Private "HTTP Message")
  137. civetweb_add_test(Private "HTTP Keep Alive")
  138. civetweb_add_test(Private "URL Parsing 1")
  139. civetweb_add_test(Private "URL Parsing 2")
  140. civetweb_add_test(Private "URL Parsing 3")
  141. civetweb_add_test(Private "Internal Parsing 1")
  142. civetweb_add_test(Private "Internal Parsing 2")
  143. civetweb_add_test(Private "Internal Parsing 3")
  144. civetweb_add_test(Private "Internal Parsing 4")
  145. civetweb_add_test(Private "Internal Parsing 5")
  146. civetweb_add_test(Private "Internal Parsing 6")
  147. civetweb_add_test(Private "Encode Decode")
  148. civetweb_add_test(Private "Mask Data")
  149. civetweb_add_test(Private "Date Parsing")
  150. civetweb_add_test(Private "SHA1")
  151. civetweb_add_test(Private "Config Options")
  152. # Public API function tests
  153. civetweb_add_test(PublicFunc "Version")
  154. civetweb_add_test(PublicFunc "Options")
  155. civetweb_add_test(PublicFunc "MIME types")
  156. civetweb_add_test(PublicFunc "strcasecmp")
  157. civetweb_add_test(PublicFunc "URL encoding decoding")
  158. civetweb_add_test(PublicFunc "Cookies and variables")
  159. civetweb_add_test(PublicFunc "MD5")
  160. civetweb_add_test(PublicFunc "Aux functions")
  161. # Public API server tests
  162. civetweb_add_test(PublicServer "Check test environment")
  163. civetweb_add_test(PublicServer "Init library")
  164. civetweb_add_test(PublicServer "Start threads")
  165. civetweb_add_test(PublicServer "Minimal HTTP Server")
  166. civetweb_add_test(PublicServer "Minimal HTTPS Server")
  167. civetweb_add_test(PublicServer "Minimal HTTP Client")
  168. civetweb_add_test(PublicServer "Minimal HTTPS Client")
  169. civetweb_add_test(PublicServer "Start Stop HTTP Server")
  170. civetweb_add_test(PublicServer "Start Stop HTTP Server IPv6")
  171. civetweb_add_test(PublicServer "Start Stop HTTPS Server")
  172. civetweb_add_test(PublicServer "TLS Server Client")
  173. civetweb_add_test(PublicServer "Server Requests")
  174. civetweb_add_test(PublicServer "Store Body")
  175. civetweb_add_test(PublicServer "Handle Form")
  176. civetweb_add_test(PublicServer "HTTP Authentication")
  177. civetweb_add_test(PublicServer "HTTP Keep Alive")
  178. civetweb_add_test(PublicServer "Error handling")
  179. civetweb_add_test(PublicServer "Error logging")
  180. civetweb_add_test(PublicServer "Limit speed")
  181. civetweb_add_test(PublicServer "Large file")
  182. civetweb_add_test(PublicServer "File in memory")
  183. # Timer tests
  184. civetweb_add_test(Timer "Timer Single Shot")
  185. civetweb_add_test(Timer "Timer Periodic")
  186. civetweb_add_test(Timer "Timer Mixed")
  187. # Tests with main.c
  188. #civetweb_add_test(EXE "Helper funcs")
  189. # Add the coverage command(s)
  190. if (${CMAKE_BUILD_TYPE} MATCHES "[Cc]overage")
  191. find_program(GCOV_EXECUTABLE gcov)
  192. find_program(LCOV_EXECUTABLE lcov)
  193. find_program(GENHTML_EXECUTABLE genhtml)
  194. find_program(CTEST_EXECUTABLE ctest)
  195. if (GCOV_EXECUTABLE AND LCOV_EXECUTABLE AND GENHTML_EXECUTABLE AND CTEST_EXECUTABLE AND HAVE_C_FLAG_COVERAGE)
  196. add_custom_command(
  197. OUTPUT ${CMAKE_BINARY_DIR}/lcov/index.html
  198. COMMAND ${LCOV_EXECUTABLE} -q -z -d .
  199. COMMAND ${LCOV_EXECUTABLE} -q --no-external -c -b "${CMAKE_SOURCE_DIR}" -d . -o before.lcov -i
  200. COMMAND ${CTEST_EXECUTABLE} --force-new-ctest-process
  201. COMMAND ${LCOV_EXECUTABLE} -q --no-external -c -b "${CMAKE_SOURCE_DIR}" -d . -o after.lcov
  202. COMMAND ${LCOV_EXECUTABLE} -q -a before.lcov -a after.lcov --output-file final.lcov
  203. COMMAND ${LCOV_EXECUTABLE} -q -r final.lcov "'${CMAKE_SOURCE_DIR}/unittest/*'" -o final.lcov
  204. COMMAND ${GENHTML_EXECUTABLE} final.lcov -o lcov --demangle-cpp --sort -p "${CMAKE_SOURCE_DIR}" -t benchmark
  205. DEPENDS main-c-unit-test
  206. WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
  207. COMMENT "Running LCOV"
  208. )
  209. add_custom_target(coverage
  210. DEPENDS ${CMAKE_BINARY_DIR}/lcov/index.html
  211. COMMENT "LCOV report at lcov/index.html"
  212. )
  213. message(STATUS "Coverage command added")
  214. else()
  215. if (HAVE_C_FLAG_COVERAGE)
  216. set(C_FLAG_COVERAGE_MESSAGE supported)
  217. else()
  218. set(C_FLAG_COVERAGE_MESSAGE unavailable)
  219. endif()
  220. message(WARNING
  221. "Coverage command not available:\n"
  222. " gcov: ${GCOV_EXECUTABLE}\n"
  223. " lcov: ${LCOV_EXECUTABLE}\n"
  224. " genhtml: ${GENHTML_EXECUTABLE}\n"
  225. " ctest: ${CTEST_EXECUTABLE}\n"
  226. " --coverage flag: ${C_FLAG_COVERAGE_MESSAGE}")
  227. endif()
  228. endif()