CMakeLists.txt 8.6 KB

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