CMakeLists.txt 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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://downloads.sourceforge.net/project/check/check/${CIVETWEB_CHECK_VERSION}/check-${CIVETWEB_CHECK_VERSION}.tar.gz"
  12. #URL_MD5 ${CIVETWEB_CHECK_MD5_HASH}
  13. URL "https://github.com/libcheck/check/archive/master.zip"
  14. PREFIX "${CIVETWEB_THIRD_PARTY_DIR}"
  15. BUILD_IN_SOURCE 1
  16. PATCH_COMMAND ${CMAKE_COMMAND}
  17. -DSOURCE_DIR=<SOURCE_DIR>
  18. -DBINARY_DIR=<BINARY_DIR>
  19. -DINSTALL_DIR=<INSTALL_DIR>
  20. -DVERSION=${CIVETWEB_CHECK_VERSION}
  21. -P ${CMAKE_SOURCE_DIR}/cmake/check/patch.cmake
  22. CMAKE_ARGS
  23. "-DCMAKE_BUILD_TYPE=Release"
  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)
  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 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 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(exe-c-unit-tests STATIC private_exe.c)
  84. if (BUILD_SHARED_LIBS)
  85. target_compile_definitions(exe-c-unit-tests PRIVATE)
  86. endif()
  87. target_include_directories(
  88. exe-c-unit-tests PUBLIC
  89. ${PROJECT_SOURCE_DIR}/include)
  90. target_link_libraries(exe-c-unit-tests c-library ${CHECK_LIBRARIES})
  91. add_dependencies(exe-c-unit-tests check-unit-test-framework)
  92. add_executable(civetweb-c-unit-test main.c)
  93. target_link_libraries(civetweb-c-unit-test
  94. shared-c-unit-tests
  95. public-func-c-unit-tests
  96. public-server-c-unit-tests
  97. private-c-unit-tests
  98. exe-c-unit-tests
  99. ${CHECK_LIBRARIES})
  100. add_dependencies(civetweb-c-unit-test check-unit-test-framework)
  101. # Add a check command that builds the dependent test program
  102. add_custom_target(check
  103. COMMAND ${CMAKE_CTEST_COMMAND}
  104. DEPENDS civetweb-c-unit-test)
  105. # A macro for adding tests
  106. macro(civetweb_add_test suite test_case)
  107. set(test "test-${suite}-${test_case}")
  108. string(TOLOWER "${test}" test)
  109. string(REGEX REPLACE "[^-A-Za-z0-9]" "-" test "${test}")
  110. add_test(
  111. NAME ${test}
  112. COMMAND civetweb-c-unit-test "--test-dir=${CMAKE_CURRENT_SOURCE_DIR}" "--suite=${suite}" "--test-case=${test_case}")
  113. if (WIN32)
  114. string(REPLACE ";" "\\;" test_path "$ENV{PATH}")
  115. set_tests_properties(${test} PROPERTIES
  116. ENVIRONMENT "PATH=${test_path}\\;$<TARGET_FILE_DIR:c-library>")
  117. endif()
  118. endmacro(civetweb_add_test)
  119. # Tests of private functions
  120. civetweb_add_test(Private "HTTP Message")
  121. civetweb_add_test(Private "URL Parsing")
  122. civetweb_add_test(Private "Internal Parsing")
  123. civetweb_add_test(Private "Encode Decode")
  124. civetweb_add_test(Private "Mask Data")
  125. # Public API function tests
  126. civetweb_add_test(PublicFunc "Version")
  127. civetweb_add_test(PublicFunc "Options")
  128. civetweb_add_test(PublicFunc "MIME types")
  129. civetweb_add_test(PublicFunc "strcasecmp")
  130. civetweb_add_test(PublicFunc "URL encoding decoding")
  131. civetweb_add_test(PublicFunc "Cookies and variables")
  132. civetweb_add_test(PublicFunc "MD5")
  133. # Public API server tests
  134. civetweb_add_test(PublicServer "Check test environment")
  135. civetweb_add_test(PublicServer "Start threads")
  136. civetweb_add_test(PublicServer "Start Stop HTTP Server")
  137. civetweb_add_test(PublicServer "Start Stop HTTPS Server")
  138. civetweb_add_test(PublicServer "TLS Server Client")
  139. civetweb_add_test(PublicServer "Server Requests")
  140. # Tests with main.c
  141. civetweb_add_test(EXE "Helper funcs")
  142. # Add the coverage command(s)
  143. if (${CMAKE_BUILD_TYPE} MATCHES "[Cc]overage")
  144. find_program(GCOV_EXECUTABLE gcov)
  145. find_program(LCOV_EXECUTABLE lcov)
  146. find_program(GENHTML_EXECUTABLE genhtml)
  147. find_program(CTEST_EXECUTABLE ctest)
  148. if (GCOV_EXECUTABLE AND LCOV_EXECUTABLE AND GENHTML_EXECUTABLE AND CTEST_EXECUTABLE AND HAVE_C_FLAG_COVERAGE)
  149. add_custom_command(
  150. OUTPUT ${CMAKE_BINARY_DIR}/lcov/index.html
  151. COMMAND ${LCOV_EXECUTABLE} -q -z -d .
  152. COMMAND ${LCOV_EXECUTABLE} -q --no-external -c -b "${CMAKE_SOURCE_DIR}" -d . -o before.lcov -i
  153. COMMAND ${CTEST_EXECUTABLE} --force-new-ctest-process
  154. COMMAND ${LCOV_EXECUTABLE} -q --no-external -c -b "${CMAKE_SOURCE_DIR}" -d . -o after.lcov
  155. COMMAND ${LCOV_EXECUTABLE} -q -a before.lcov -a after.lcov --output-file final.lcov
  156. COMMAND ${LCOV_EXECUTABLE} -q -r final.lcov "'${CMAKE_SOURCE_DIR}/test/*'" -o final.lcov
  157. COMMAND ${GENHTML_EXECUTABLE} final.lcov -o lcov --demangle-cpp --sort -p "${CMAKE_SOURCE_DIR}" -t benchmark
  158. DEPENDS civetweb-c-unit-test
  159. WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
  160. COMMENT "Running LCOV"
  161. )
  162. add_custom_target(coverage
  163. DEPENDS ${CMAKE_BINARY_DIR}/lcov/index.html
  164. COMMENT "LCOV report at lcov/index.html"
  165. )
  166. message(STATUS "Coverage command added")
  167. else()
  168. if (HAVE_C_FLAG_COVERAGE)
  169. set(C_FLAG_COVERAGE_MESSAGE supported)
  170. else()
  171. set(C_FLAG_COVERAGE_MESSAGE unavailable)
  172. endif()
  173. message(WARNING
  174. "Coverage command not available:\n"
  175. " gcov: ${GCOV_EXECUTABLE}\n"
  176. " lcov: ${LCOV_EXECUTABLE}\n"
  177. " genhtml: ${GENHTML_EXECUTABLE}\n"
  178. " ctest: ${CTEST_EXECUTABLE}\n"
  179. " --coverage flag: ${C_FLAG_COVERAGE_MESSAGE}")
  180. endif()
  181. endif()