CMakeLists.txt 9.0 KB

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