diff --git a/CMakeLists.txt b/CMakeLists.txt index 0edfd006..12b01768 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -20,6 +20,10 @@ # Boston, MA 02111-1307, USA. # cmake_minimum_required(VERSION 3.9 FATAL_ERROR) +if(POLICY CMP0076) + # target_sources() leaves relative source file paths unmodified. (OLD) + cmake_policy(SET CMP0076 OLD) +endif() project(check DESCRIPTION "Unit Testing Framework for C" LANGUAGES C) @@ -43,18 +47,16 @@ extract_version(configure.ac CHECK_MICRO_VERSION) set(PROJECT_VERSION_MAJOR ${CHECK_MAJOR_VERSION}) set(PROJECT_VERSION_MINOR ${CHECK_MINOR_VERSION}) set(PROJECT_VERSION_PATCH ${CHECK_MICRO_VERSION}) -set(PROJECT_VERSION_TWEAK 0) -set(PROJECT_VERSION "${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}.${PROJECT_VERSION_PATCH}.${PROJECT_VERSION_TWEAK}") - -set(check_VERSION - "${CHECK_MAJOR_VERSION}.${CHECK_MINOR_VERSION}.${CHECK_MICRO_VERSION}") - -set(MEMORY_LEAKING_TESTS_ENABLED 1) +set(PROJECT_VERSION "${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}.${PROJECT_VERSION_PATCH}") ############################################################################### # Set build features set(CMAKE_BUILD_TYPE Debug) +############################################################################### +# Provides install directory variables as defined by the GNU Coding Standards. +include(GNUInstallDirs) + ############################################################################### # Adhere strictly to old ANSI C89 / ISO C90 standard set(CMAKE_C_STANDARD 90) @@ -69,12 +71,16 @@ if(NOT CHECK_ENABLE_TESTS) message(DEPRECATION "The option CHECK_ENABLE_TESTS is deprecated. Use option BUILD_TESTING.") # TODO Remove this option by Check 0.15.0! endif(NOT CHECK_ENABLE_TESTS) + option(CHECK_ENABLE_GCOV "Turn on test coverage" OFF) if (CHECK_ENABLE_GCOV AND NOT ${CMAKE_C_COMPILER_ID} MATCHES "GNU") message(FATAL_ERROR "Code Coverage (gcov) only works if GNU compiler is used!") endif (CHECK_ENABLE_GCOV AND NOT ${CMAKE_C_COMPILER_ID} MATCHES "GNU") +option(ENABLE_MEMORY_LEAKING_TESTS + "Enable certain memory leaking tests only if valgrind is not used in testing" ON) + ############################################################################### # Check system and architecture if(WIN32) @@ -376,10 +382,12 @@ set(CONFIG_HEADER ${CMAKE_CURRENT_BINARY_DIR}/config.h) # # When converting to CMake we also want to abandon the m4 macros. # -set(PROJECT_VERSION "${check_VERSION}") configure_file(${CMAKE_CURRENT_SOURCE_DIR}/cmake/check_stdint.h.in ${CMAKE_CURRENT_BINARY_DIR}/check_stdint.h @ONLY) -install(FILES ${CMAKE_CURRENT_BINARY_DIR}/check_stdint.h DESTINATION include) +install( + FILES ${CMAKE_CURRENT_BINARY_DIR}/check_stdint.h + DESTINATION ${CMAKE_INSTALL_INCLUDEDIR} +) ############################################################################### # Generate "check.pc", the package config (pkgconfig) file for libtool @@ -388,6 +396,7 @@ set(prefix "${CMAKE_INSTALL_PREFIX}") set(exec_prefix "\${prefix}") set(libdir "\${exec_prefix}/lib") set(includedir "\${prefix}/include") +set(VERSION "${PROJECT_VERSION}") if (HAVE_SUBUNIT) set(LIBSUBUNIT_PC "libsubunit") @@ -421,6 +430,7 @@ unset(LIBS) unset(PTHREAD_LIBS) unset(GCOV_LIBS) unset(LIBSUBUNIT_PC) +unset(VERSION) unset(includedir) unset(libdir) unset(exec_prefix) @@ -428,7 +438,7 @@ set(PREFIX "${prefix_save}") install( FILES ${CMAKE_CURRENT_BINARY_DIR}/check.pc - DESTINATION lib/pkgconfig + DESTINATION ${CMAKE_INSTALL_LIBDIR}/pkgconfig ) ############################################################################### @@ -446,27 +456,27 @@ if (BUILD_TESTING) # Only offer to run shell scripts if we may have a working interpreter if(UNIX OR MINGW OR MSYS) - add_test(NAME test_output.sh - WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/tests - COMMAND sh ${CMAKE_CURRENT_SOURCE_DIR}/tests/test_output.sh) - add_test(NAME test_log_output.sh - WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/tests - COMMAND sh ${CMAKE_CURRENT_SOURCE_DIR}/tests/test_log_output.sh) - add_test(NAME test_xml_output.sh - WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/tests - COMMAND sh ${CMAKE_CURRENT_SOURCE_DIR}/tests/test_xml_output.sh) - add_test(NAME test_tap_output.sh - WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/tests - COMMAND sh ${CMAKE_CURRENT_SOURCE_DIR}/tests/test_tap_output.sh) - add_test(NAME test_check_nofork.sh - WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/tests - COMMAND sh ${CMAKE_CURRENT_SOURCE_DIR}/tests/test_check_nofork.sh) - add_test(NAME test_check_nofork_teardown.sh - WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/tests - COMMAND sh ${CMAKE_CURRENT_SOURCE_DIR}/tests/test_check_nofork_teardown.sh) - add_test(NAME test_set_max_msg_size.sh - WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/tests - COMMAND sh ${CMAKE_CURRENT_SOURCE_DIR}/tests/test_set_max_msg_size.sh) + add_test(NAME test_output.sh + WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/tests + COMMAND sh ${CMAKE_CURRENT_SOURCE_DIR}/tests/test_output.sh) + add_test(NAME test_log_output.sh + WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/tests + COMMAND sh ${CMAKE_CURRENT_SOURCE_DIR}/tests/test_log_output.sh) + add_test(NAME test_xml_output.sh + WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/tests + COMMAND sh ${CMAKE_CURRENT_SOURCE_DIR}/tests/test_xml_output.sh) + add_test(NAME test_tap_output.sh + WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/tests + COMMAND sh ${CMAKE_CURRENT_SOURCE_DIR}/tests/test_tap_output.sh) + add_test(NAME test_check_nofork.sh + WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/tests + COMMAND sh ${CMAKE_CURRENT_SOURCE_DIR}/tests/test_check_nofork.sh) + add_test(NAME test_check_nofork_teardown.sh + WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/tests + COMMAND sh ${CMAKE_CURRENT_SOURCE_DIR}/tests/test_check_nofork_teardown.sh) + add_test(NAME test_set_max_msg_size.sh + WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/tests + COMMAND sh ${CMAKE_CURRENT_SOURCE_DIR}/tests/test_set_max_msg_size.sh) endif(UNIX OR MINGW OR MSYS) endif (BUILD_TESTING) @@ -482,7 +492,7 @@ configure_package_config_file( ) write_basic_package_version_file( ${CMAKE_CURRENT_BINARY_DIR}/${EXPORT_NAME}-config-version.cmake - VERSION ${check_VERSION} + VERSION ${PROJECT_VERSION} COMPATIBILITY AnyNewerVersion ) @@ -494,12 +504,12 @@ export(EXPORT check-targets install(EXPORT check-targets NAMESPACE Check:: FILE check-targets.cmake - DESTINATION lib/cmake/${EXPORT_NAME} + DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/${EXPORT_NAME} ) install( FILES "${CMAKE_CURRENT_BINARY_DIR}/${EXPORT_NAME}-config.cmake" "${CMAKE_CURRENT_BINARY_DIR}/${EXPORT_NAME}-config-version.cmake" - DESTINATION lib/cmake/${EXPORT_NAME} + DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/${EXPORT_NAME} ) diff --git a/checkmk/CMakeLists.txt b/checkmk/CMakeLists.txt index 1254ec2c..4634b8de 100644 --- a/checkmk/CMakeLists.txt +++ b/checkmk/CMakeLists.txt @@ -7,7 +7,6 @@ configure_file(checkmk.in checkmk @ONLY) file(COPY doc/checkmk.1 DESTINATION man/man1) option(INSTALL_CHECKMK "Install checkmk" ON) -include(GNUInstallDirs) if(INSTALL_CHECKMK) install( FILES ${CMAKE_CURRENT_BINARY_DIR}/checkmk @@ -15,8 +14,8 @@ if(INSTALL_CHECKMK) PERMISSIONS OWNER_READ OWNER_WRITE OWNER_EXECUTE GROUP_READ GROUP_EXECUTE WORLD_READ WORLD_EXECUTE ) install( - DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/man/man1 - DESTINATION share/man + DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/man + DESTINATION ${CMAKE_INSTALL_DATAROOTDIR} ) endif(INSTALL_CHECKMK) diff --git a/lib/CMakeLists.txt b/lib/CMakeLists.txt index c4813ce6..e6346dd5 100644 --- a/lib/CMakeLists.txt +++ b/lib/CMakeLists.txt @@ -70,9 +70,3 @@ set(HEADERS libcompat.h) add_library(compat STATIC ${SOURCES} ${HEADERS}) -install(TARGETS compat - RUNTIME DESTINATION bin - LIBRARY DESTINATION lib - ARCHIVE DESTINATION lib) - -install(FILES ${CMAKE_CURRENT_SOURCE_DIR}/libcompat.h DESTINATION include) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 8b126671..437ea2a6 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -30,7 +30,7 @@ set(SOURCES check_run.c check_str.c) -set(HEADERS +set(HEADERS ${CONFIG_HEADER} ${CMAKE_CURRENT_BINARY_DIR}/check.h check.h.in @@ -46,23 +46,132 @@ set(HEADERS configure_file(check.h.in check.h @ONLY) add_library(check STATIC ${SOURCES} ${HEADERS}) -target_link_libraries(check ${LIBM} ${LIBRT} ${SUBUNIT}) -# Enable finding check.h -target_include_directories(check - PUBLIC - $ - $) + +# We would like to create an OBJECT library but currently they are +# too unreliable and cumbersome, +# especially with target_link_libraries and install(EXPORT... +# https://stackoverflow.com/questions/38832528/transitive-target-include-directories-on-object-libraries +# So we instead do the work twice. +add_library(checkShared SHARED ${SOURCES} ${HEADERS}) + +# Add parts of libcompat as required +target_sources(check PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/../lib/fpclassify.c) +target_sources(checkShared PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/../lib/fpclassify.c) + +if (NOT HAVE_LIBRT) + target_sources(check PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/../lib/clock_gettime.c) + target_sources(check PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/../lib/timer_create.c) + target_sources(check PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/../lib/timer_delete.c) + target_sources(check PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/../lib/timer_settime.c) + target_sources(checkShared PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/../lib/clock_gettime.c) + target_sources(checkShared PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/../lib/timer_create.c) + target_sources(checkShared PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/../lib/timer_delete.c) + target_sources(checkShared PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/../lib/timer_settime.c) +endif(NOT HAVE_LIBRT) + +if(NOT HAVE_GETLINE) + target_sources(check PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/../lib/getline.c) + target_sources(checkShared PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/../lib/getline.c) +endif(NOT HAVE_GETLINE) + +if(NOT HAVE_GETTIMEOFDAY) + target_sources(check PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/../lib/gettimeofday.c) + target_sources(checkShared PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/../lib/gettimeofday.c) +endif(NOT HAVE_GETTIMEOFDAY) + +if(NOT HAVE_DECL_LOCALTIME_R) + target_sources(check PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/../lib/localtime_r.c) + target_sources(checkShared PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/../lib/localtime_r.c) +endif(NOT HAVE_DECL_LOCALTIME_R) + +if(NOT HAVE_MALLOC) + target_sources(check PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/../lib/malloc.c) + target_sources(checkShared PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/../lib/malloc.c) +endif(NOT HAVE_MALLOC) + +if(NOT HAVE_REALLOC) + target_sources(check PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/../lib/realloc.c) + target_sources(checkShared PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/../lib/realloc.c) +endif(NOT HAVE_REALLOC) + +if(NOT HAVE_SNPRINTF) + target_sources(check PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/../lib/snprintf.c) + target_sources(checkShared PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/../lib/snprintf.c) +endif(NOT HAVE_SNPRINTF) + +if(NOT HAVE_DECL_STRDUP AND NOT HAVE__STRDUP) + target_sources(check PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/../lib/strdup.c) + target_sources(checkShared PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/../lib/snprintf.c) +endif(NOT HAVE_DECL_STRDUP AND NOT HAVE__STRDUP) + +if(NOT HAVE_DECL_STRSIGNAL) + target_sources(check PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/../lib/strsignal.c) + target_sources(checkShared PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/../lib/strsignal.c) +endif(NOT HAVE_DECL_STRSIGNAL) + +if(NOT HAVE_DECL_ALARM) + target_sources(check PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/../lib/alarm.c) + target_sources(checkShared PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/../lib/alarm.c) +endif(NOT HAVE_DECL_ALARM) + +# Include libraries if available +if (HAVE_LIBM) + target_link_libraries(check PUBLIC m) + target_link_libraries(checkShared PUBLIC m) +endif (HAVE_LIBM) +if (HAVE_LIBRT) + target_link_libraries(check PUBLIC rt) + target_link_libraries(checkShared PUBLIC rt) +endif (HAVE_LIBRT) +if (HAVE_SUBUNIT) + target_link_libraries(check PUBLIC subunit) + target_link_libraries(checkShared PUBLIC subunit) +endif (HAVE_SUBUNIT) if(MSVC) - add_definitions(-DCK_DLL_EXP=_declspec\(dllexport\)) + target_compile_definitions(checkShared + PRIVATE "CK_DLL_EXP=_declspec(dllexport)" + INTERFACE "CK_DLL_EXP=_declspec(dllimport)" + ) endif (MSVC) -install(TARGETS check - EXPORT check-targets - INCLUDES DESTINATION include - RUNTIME DESTINATION bin - LIBRARY DESTINATION lib - ARCHIVE DESTINATION lib) +# More configuration for exporting + +set(LIBRARY_OUTPUT_NAME "check") -install(FILES ${CMAKE_BINARY_DIR}/src/check.h DESTINATION include) +set_target_properties(check PROPERTIES + OUTPUT_NAME ${LIBRARY_OUTPUT_NAME} + PUBLIC_HEADER ${CMAKE_CURRENT_BINARY_DIR}/check.h +) + +if (MSVC) + # "On Windows you should probably give each library a different name, + # since there is a ".lib" file for both shared and static". + # https://stackoverflow.com/a/2152157/4716395 + set(LIBRARY_OUTPUT_NAME "checkStatic") +endif (MSVC) +set_target_properties(checkShared PROPERTIES + OUTPUT_NAME ${LIBRARY_OUTPUT_NAME} + VERSION ${PROJECT_VERSION} + SOVERSION ${PROJECT_VERSION_MAJOR} + PUBLIC_HEADER ${CMAKE_CURRENT_BINARY_DIR}/check.h +) +target_include_directories(check + PUBLIC + $ + $ +) +target_include_directories(checkShared + PUBLIC + $ + $ +) + +install(TARGETS check checkShared + EXPORT check-targets + ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR} + LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} + RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR} + PUBLIC_HEADER DESTINATION ${CMAKE_INSTALL_INCLUDEDIR} +) diff --git a/src/check.h.in b/src/check.h.in index ac86d9bb..0c6aa2c7 100644 --- a/src/check.h.in +++ b/src/check.h.in @@ -93,6 +93,15 @@ CK_CPPSTART * Used for MSVC to create the export attribute * CK_DLL_EXP is defined during the compilation of the library * on the command line. + * + * This definition is only used when building or linking to + * the shared library, i.e. libcheck.so. When building the library + * the value must be "_declspec(dllexport)". + * When linking with the library, the value must be "_declspec(dllimport)" + * + * This is only used with Microsoft Visual C. In other systems + * the value is empty. In MSVC the value is empty when linking with + * a static library. */ #ifndef CK_DLL_EXP #define CK_DLL_EXP diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 82e3b439..5a925c25 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -40,6 +40,12 @@ endif(WIN32) configure_file(${CMAKE_CURRENT_SOURCE_DIR}/test_vars.in ${CMAKE_CURRENT_BINARY_DIR}/test_vars @ONLY) +if(ENABLE_MEMORY_LEAKING_TESTS) + add_definitions(-DMEMORY_LEAKING_TESTS_ENABLED=1) +else(ENABLE_MEMORY_LEAKING_TESTS) + add_definitions(-DMEMORY_LEAKING_TESTS_ENABLED=0) +endif(ENABLE_MEMORY_LEAKING_TESTS) + set(CHECK_CHECK_SOURCES check_check_exit.c check_check_fixture.c @@ -57,7 +63,7 @@ set(CHECK_CHECK_SOURCES check_list.c) set(CHECK_CHECK_HEADERS check_check.h) add_executable(check_check ${CHECK_CHECK_HEADERS} ${CHECK_CHECK_SOURCES}) -target_link_libraries(check_check check compat) +target_link_libraries(check_check check) set(CHECK_CHECK_EXPORT_SOURCES check_check_sub.c @@ -70,20 +76,20 @@ set(CHECK_CHECK_EXPORT_HEADERS check_check.h) add_executable(check_check_export ${CHECK_CHECK_EXPORT_HEADERS} ${CHECK_CHECK_EXPORT_SOURCES}) -target_link_libraries(check_check_export check compat) +target_link_libraries(check_check_export check) set(EX_OUTPUT_SOURCES ex_output.c) add_executable(ex_output ${EX_OUTPUT_SOURCES}) -target_link_libraries(ex_output check compat) +target_link_libraries(ex_output check) set(CHECK_NOFORK_SOURCES check_nofork.c) add_executable(check_nofork ${CHECK_NOFORK_SOURCES}) -target_link_libraries(check_nofork check compat) +target_link_libraries(check_nofork check) set(CHECK_NOFORK_TEARDOWN_SOURCES check_nofork_teardown.c) add_executable(check_nofork_teardown ${CHECK_NOFORK_TEARDOWN_SOURCES}) -target_link_libraries(check_nofork_teardown check compat) +target_link_libraries(check_nofork_teardown check) set(CHECK_SET_MAX_MSG_SIZE_SOURCES check_set_max_msg_size.c) add_executable(check_set_max_msg_size ${CHECK_SET_MAX_MSG_SIZE_SOURCES}) -target_link_libraries(check_set_max_msg_size check compat) +target_link_libraries(check_set_max_msg_size check)