CMakeLists.txt 8.9 KB

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