CMakeLists.txt 8.5 KB

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