Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor/cmake buildsystem configuration #314

Draft
wants to merge 10 commits into
base: master
Choose a base branch
from
72 changes: 31 additions & 41 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -77,12 +77,6 @@ set(PROJECT_VERSION "${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}.${PROJECT
# Provides install directory variables as defined by the GNU Coding Standards.
include(GNUInstallDirs)

###############################################################################
# Follow ISO C99 standard
set(CMAKE_C_STANDARD 99)
set(CMAKE_C_STANDARD_REQUIRED ON)
set(CMAKE_C_EXTENSIONS ON) # Use GNU extensions and POSIX standard

###############################################################################
# Option
option(CHECK_ENABLE_TESTS
Expand All @@ -108,22 +102,6 @@ if(CHECK_ENABLE_TIMEOUT_TESTS)
else(CHECK_ENABLE_TIMEOUT_TESTS)
add_definitions(-DTIMEOUT_TESTS_ENABLED=0)
endif(CHECK_ENABLE_TIMEOUT_TESTS)
###############################################################################
# Check system and architecture
if(WIN32)
if(MSVC60)
set(WINVER 0x0400)
else()
set(WINVER 0x0600)
endif()
set(_WIN32_WINNT ${WINVER})
endif(WIN32)

if(MSVC)
add_definitions(-D_CRT_SECURE_NO_DEPRECATE)
add_definitions(-D_CRT_SECURE_NO_WARNINGS)
add_definitions(-D_CRT_NONSTDC_NO_WARNINGS)
endif(MSVC)

###############################################################################
include(CheckCSourceCompiles)
Expand Down Expand Up @@ -167,7 +145,9 @@ ck_check_include_file("unistd.h" HAVE_UNISTD_H)
ck_check_include_file("pthread.h" HAVE_PTHREAD)

# check if we have windows.h on native windows environments
ck_check_include_file("windows.h" HAVE_WINDOWS_H)
IF(WIN32)
ck_check_include_file("windows.h" HAVE_WINDOWS_H)
ENDIF()

###############################################################################
# Check functions
Expand Down Expand Up @@ -244,14 +224,22 @@ if(HAVE_REGEX_H AND HAVE_REGCOMP AND HAVE_REGEXEC)
set(ENABLE_REGEX 1)
endif()

if (HAVE_PTHREAD)
check_c_compiler_flag("-pthread" HAVE_PTHREADS_FLAG)
if (HAVE_PTHREADS_FLAG)
add_definitions("-pthread")
add_link_options("-pthread")
endif()
endif()
INCLUDE(FindThreads)

SET(THREADS_PREFER_PTHREAD_FLAG ON)
MESSAGE(STATUS "Prefer -pthread: ${THREADS_PREFER_PTHREAD_FLAG}")

FIND_PACKAGE(Threads
REQUIRED
)

IF(Threads_FOUND AND CMAKE_USE_PTHREADS_INIT)
SET(HAVE_PTHREAD 1)
ENDIF()

MESSAGE(STATUS "Threads library found: ${Threads_FOUND}")
MESSAGE(STATUS "PThreads compatible: ${CMAKE_USE_PTHREADS_INIT}")
MESSAGE(STATUS "Win32 Threads: ${CMAKE_USE_WIN32_THREADS_INIT}")

###############################################################################
# Check defines
Expand Down Expand Up @@ -405,9 +393,9 @@ endif (HAVE_SUBUNIT)

###############################################################################
# Generate "config.h" from "cmake/config.h.in"
configure_file(${CMAKE_CURRENT_SOURCE_DIR}/cmake/config.h.in
${CMAKE_CURRENT_BINARY_DIR}/config.h)
# Param @ONLY not used on purpose!
# Param @ONLY not used on purpose!
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good idea to change the comment row above the code. Comments should precede the code. Otherwise it is misleading.

CONFIGURE_FILE(${CMAKE_SOURCE_DIR}/cmake/config.h.in config.h)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CMake Variable CMAKE_SOURCE_DIR is not always the same as CMAKE_CURRENT_SOURCE_DIR. CMAKE_CURRENT_SOURCE_DIR is always the dir where the current CMakeLists.txt file is located but CMAKE_SOURCE_DIR can be different if, e.g. this project becomes a sub project of another project (for this same reason we also have the variable THIS_IS_SUBPROJECT; being a sub project complicates things).

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, I completely forgot the library was built as a subproject. Thanks for pointing this out, sorry about that

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not everybody builds Check as a subproject. Many - if not most - build the Check separately and install it system wide (for all users to access). But CMake allows the inclusion of one project into another. This way of using CMake projects will probably become more popular in the future because it is easier and also more ad hoc and therefore allows for quicker testing.


include_directories(BEFORE ${CMAKE_CURRENT_BINARY_DIR})
add_definitions(-DHAVE_CONFIG_H)
set(CONFIG_HEADER ${CMAKE_CURRENT_BINARY_DIR}/config.h)
Expand All @@ -425,13 +413,16 @@ set(CONFIG_HEADER ${CMAKE_CURRENT_BINARY_DIR}/config.h)
#
# When converting to CMake we also want to abandon the m4 macros.
#
configure_file(${CMAKE_CURRENT_SOURCE_DIR}/cmake/check_stdint.h.in
${CMAKE_CURRENT_BINARY_DIR}/check_stdint.h @ONLY)
if(NOT THIS_IS_SUBPROJECT)
install(
FILES ${CMAKE_CURRENT_BINARY_DIR}/check_stdint.h
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR})
endif()
CONFIGURE_FILE(${CMAKE_SOURCE_DIR}/cmake/check_stdint.h.in check_stdint.h @ONLY)

IF(NOT THIS_IS_SUBPROJECT)
INSTALL(
FILES
check_stdint.h
DESTINATION
${CMAKE_INSTALL_INCLUDEDIR}
)
ENDIF()

###############################################################################
# Generate "check.pc", the package config (pkgconfig) file for libtool
Expand Down Expand Up @@ -578,4 +569,3 @@ if(NOT THIS_IS_SUBPROJECT)
endif()

# vim: shiftwidth=2:softtabstop=2:tabstop=2:expandtab:autoindent

136 changes: 78 additions & 58 deletions lib/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,61 +19,81 @@
# Boston, MA 02111-1307, USA.
#


set(SOURCES libcompat.c)

set(SOURCES ${SOURCES} fpclassify.c)

if (NOT HAVE_LIBRT)
set(SOURCES ${SOURCES} clock_gettime.c)
set(SOURCES ${SOURCES} timer_create.c)
set(SOURCES ${SOURCES} timer_delete.c)
set(SOURCES ${SOURCES} timer_settime.c)
endif(NOT HAVE_LIBRT)

if(NOT HAVE_GETLINE)
set(SOURCES ${SOURCES} getline.c)
endif(NOT HAVE_GETLINE)

if(NOT HAVE_GETTIMEOFDAY)
set(SOURCES ${SOURCES} gettimeofday.c)
endif(NOT HAVE_GETTIMEOFDAY)

if(NOT HAVE_DECL_LOCALTIME_R)
set(SOURCES ${SOURCES} localtime_r.c)
endif(NOT HAVE_DECL_LOCALTIME_R)

if(NOT HAVE_MALLOC)
set(SOURCES ${SOURCES} malloc.c)
endif(NOT HAVE_MALLOC)

if(NOT HAVE_REALLOC)
set(SOURCES ${SOURCES} realloc.c)
endif(NOT HAVE_REALLOC)

if(NOT HAVE_SNPRINTF)
set(SOURCES ${SOURCES} snprintf.c)
endif(NOT HAVE_SNPRINTF)

if(NOT HAVE_DECL_STRDUP AND NOT HAVE__STRDUP)
set(SOURCES ${SOURCES} strdup.c)
endif(NOT HAVE_DECL_STRDUP AND NOT HAVE__STRDUP)

if(NOT HAVE_DECL_STRSIGNAL)
set(SOURCES ${SOURCES} strsignal.c)
endif(NOT HAVE_DECL_STRSIGNAL)

if(NOT HAVE_DECL_ALARM)
set(SOURCES ${SOURCES} alarm.c)
endif(NOT HAVE_DECL_ALARM)

if (NOT HAVE_PTHREAD)
set(SOURCES ${SOURCES} pthread_mutex.c)
endif()

set(HEADERS libcompat.h)

add_library(compat STATIC ${SOURCES} ${HEADERS})

# vim: shiftwidth=2:softtabstop=2:tabstop=2:expandtab:autoindent

ADD_LIBRARY(compat STATIC
libcompat.c
$<$<NOT:$<BOOL:${HAVE_LIBRT}>>:clock_gettime.c timer_create.c timer_delete.c timer_settime.c>
$<$<NOT:$<BOOL:${HAVE_GETLINE}>>:getline.c>
$<$<NOT:$<BOOL:${HAVE_GETTIMEOFDAY}>>:gettimeofday.c>
$<$<NOT:$<BOOL:${HAVE_DECL_LOCALTIME_R}>>:localtime_r.c>
$<$<NOT:$<BOOL:${HAVE_MALLOC}>>:malloc.c>
$<$<NOT:$<BOOL:${HAVE_REALLOC}>>:realloc.c>
$<$<NOT:$<BOOL:${HAVE_SNPRINTF}>>:snprintf.c>
$<$<AND:$<NOT:$<BOOL:${HAVE_DECL_STRDUP}>>,$<NOT:$<BOOL:${HAVE__STRDUP}>>>:strdup.c>
$<$<NOT:$<BOOL:${HAVE_STRSIGNAL}>>:strsignal.c>
$<$<NOT:$<BOOL:${HAVE_DECL_ALARM}>>:alarm.c>
$<$<NOT:$<BOOL:${HAVE_PTHREAD}>>:pthread_mutex.c>
)

SET_TARGET_PROPERTIES(compat
PROPERTIES
LANGUAGE C
C_STANDARD 99
C_STANDARD_REQUIRED ON
C_EXTENSIONS ON
)

TARGET_COMPILE_FEATURES(compat
PUBLIC
c_function_prototypes
c_variadic_macros
c_std_99
)

TARGET_COMPILE_OPTIONS(compat
PUBLIC
####################################################
# Clang Options
####################################################
#
# Enable the standard diagnostic options if we
# detect that the current C compiler is clang.
#
$<$<C_COMPILER_ID:Clang>:-Wall -Wextra>

####################################################
# GCC Options
####################################################
#
# Enable the standard diagnostic options if we
# detect that the current C compiler is GCC.
#
$<$<C_COMPILER_ID:GNU>:-Wall -Wextra>
#
# If the C compiler is GCC, we are building a Debug
# build, and the version of our compiler is at least
# 10.1.0, enable the static analyzer.
#
# This option was implemented in version 10.1.0, so
# this is the minimum version required for this
# option.
#
$<$<AND:$<C_COMPILER_ID:GNU>,$<VERSION_GREATER_EQUAL:$<C_COMPILER_VERSION>,10.1.0>,$<STREQUAL:${CMAKE_BUILD_TYPE},Debug>>:-fanalyzer>

####################################################
# MSVC Options
####################################################
#
$<$<AND:$<C_COMPILER_ID:MSVC>,$<STREQUAL:${CMAKE_BUILD_TYPE},Debug>>:/Od /RTC1 /GS /Gy /Zi /analyze:512 /sdl /TC /Wall>
$<$<AND:$<C_COMPILER_ID:MSVC>,$<STREQUAL:${CMAKE_BUILD_TYPE},Release>>:/O2 /GF /Gy /Qpar /Qfast_transcendentals /TC>
)

TARGET_COMPILE_DEFINITIONS(compat
PUBLIC
$<$<BOOL:${MSVC}>:_CRT_SECURE_NO_DEPRECATE _CRT_SECURE_NO_WARNINGS _CRT_NONSTDC_NO_WARNINGS>
)

TARGET_INCLUDE_DIRECTORIES(compat
PUBLIC
${CMAKE_BINARY_DIR}
${CMAKE_CURRENT_BINARY_DIR}
)
4 changes: 2 additions & 2 deletions lib/libcompat.h
Original file line number Diff line number Diff line change
Expand Up @@ -295,9 +295,9 @@ CK_DLL_EXP int timer_delete(timer_t timerid);
* snprintf (or its variants) should be replaced with
* the C99 compliant version in libcompat.
*/
#if HAVE_CONFIG_H
//#if HAVE_CONFIG_H
#include <config.h>
#endif
//#endif
#if HAVE_STDARG_H
#include <stdarg.h>

Expand Down
Loading