Skip to content

Commit

Permalink
Merge pull request #220 from mikkoi/so-for-visual-studio
Browse files Browse the repository at this point in the history
Shared Library for Visual Studio
  • Loading branch information
brarcher authored Sep 15, 2019
2 parents 4cd6b30 + 517bf26 commit ecf337c
Show file tree
Hide file tree
Showing 6 changed files with 191 additions and 64 deletions.
78 changes: 44 additions & 34 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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
Expand All @@ -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")
Expand Down Expand Up @@ -421,14 +430,15 @@ unset(LIBS)
unset(PTHREAD_LIBS)
unset(GCOV_LIBS)
unset(LIBSUBUNIT_PC)
unset(VERSION)
unset(includedir)
unset(libdir)
unset(exec_prefix)
set(PREFIX "${prefix_save}")

install(
FILES ${CMAKE_CURRENT_BINARY_DIR}/check.pc
DESTINATION lib/pkgconfig
DESTINATION ${CMAKE_INSTALL_LIBDIR}/pkgconfig
)

###############################################################################
Expand All @@ -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)

Expand All @@ -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
)

Expand All @@ -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}
)

5 changes: 2 additions & 3 deletions checkmk/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,15 @@ 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
DESTINATION ${CMAKE_INSTALL_BINDIR}
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)

Expand Down
6 changes: 0 additions & 6 deletions lib/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
139 changes: 124 additions & 15 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
$<BUILD_INTERFACE:${CMAKE_CURRENT_BINARY_DIR}>
$<INSTALL_INTERFACE:include>)

# 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
$<BUILD_INTERFACE:${CMAKE_CURRENT_BINARY_DIR}>
$<INSTALL_INTERFACE:include>
)
target_include_directories(checkShared
PUBLIC
$<BUILD_INTERFACE:${CMAKE_CURRENT_BINARY_DIR}>
$<INSTALL_INTERFACE:include>
)

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}
)

9 changes: 9 additions & 0 deletions src/check.h.in
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading

0 comments on commit ecf337c

Please sign in to comment.