CMakeLists.txt 7.9 KB

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