CMakeLists.txt 6.7 KB

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