CMakeLists.txt 7.2 KB

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