Fix several CMake Issues (#294)

* Restructure Build.cmake to use "modern" Cmake

Since it is deprecated to modify the global compiler flags and similar options, the Build.cmake is converted to use only per target operations.

Additionally, the checks for backtrace and Pthread lib is converted to use the in cmake included functions. Also the check for the cxa_demangle function should be more robust now.

* fixed option for performance test

* use CMAKE_CURRENT_SOURCE_DIR in git cmd-lines to get current version, for better integration as cmake subdirectory

* bump required cmake version to 3.2 and use target_compile_feature to fix OSX compiler recognition and c++14 compiler flags
This commit is contained in:
Benjamin Beichler 2019-02-18 20:43:04 +01:00 committed by Kjell Hedström
parent e08e933f3e
commit e8a07f25b5
3 changed files with 129 additions and 129 deletions

View File

@ -8,116 +8,38 @@
# and no restrictions or obligations.
# ===================================================================
# GENERIC STEPS
SET(LOG_SRC ${g3log_SOURCE_DIR}/src)
file(GLOB SRC_FILES ${LOG_SRC}/*.cpp ${LOG_SRC}/*.ipp)
file(GLOB HEADER_FILES ${LOG_SRC}/g3log/*.hpp)
list( APPEND HEADER_FILES ${GENERATED_G3_DEFINITIONS} )
list( APPEND SRC_FILES ${GENERATED_G3_DEFINITIONS} )
SET(LOG_SRC ${g3log_SOURCE_DIR}/src)
include_directories(${LOG_SRC})
include_directories("${CMAKE_CURRENT_BINARY_DIR}/include")
SET(ACTIVE_CPP0xx_DIR "Release")
IF (MSVC OR MINGW)
list(REMOVE_ITEM SRC_FILES ${LOG_SRC}/crashhandler_unix.cpp)
ELSE()
list(REMOVE_ITEM SRC_FILES ${LOG_SRC}/crashhandler_windows.cpp ${LOG_SRC}/g3log/stacktrace_windows.hpp ${LOG_SRC}/stacktrace_windows.cpp)
ENDIF (MSVC OR MINGW)
#cmake -DCMAKE_CXX_COMPILER=clang++ ..
# WARNING: If Clang for Linux does not work with full c++14 support it might be your
# installation that is faulty. When I tested Clang on Ubuntu I followed the following
# description
# 1) http://kjellkod.wordpress.com/2013/09/23/experimental-g3log-with-clang/
# 2) https://github.com/maidsafe/MaidSafe/wiki/Hacking-with-Clang-llvm-abi-and-llvm-libc
IF (${CMAKE_CXX_COMPILER_ID} MATCHES ".*Clang")
message( STATUS "" )
message( STATUS "cmake for Clang " )
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -std=c++14 -Wunused -D_GLIBCXX_USE_NANOSLEEP")
IF (${CMAKE_SYSTEM_NAME} STREQUAL "Linux")
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -stdlib=libstdc++ -pthread")
ELSE()
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -stdlib=libc++")
ENDIF()
IF (${CMAKE_SYSTEM} MATCHES "FreeBSD-([0-9]*)\\.(.*)")
IF (${CMAKE_MATCH_1} GREATER 9)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -pthread")
set(PLATFORM_LINK_LIBRIES execinfo)
set(SRC_FILES ${SRC_FILES} ${SRC_PLATFORM_SPECIFIC})
# Create the g3log library
SET(G3LOG_LIBRARY g3logger)
IF( G3_SHARED_LIB )
IF( WIN32 )
IF(NOT(${CMAKE_VERSION} VERSION_LESS "3.4"))
set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS ON)
ELSE()
message( FATAL_ERROR "Need CMake version >=3.4 to build shared windows library!" )
ENDIF()
ELSEIF (APPLE)
set(PLATFORM_LINK_LIBRIES c++abi)
ELSEIF (NOT (${CMAKE_SYSTEM_NAME} STREQUAL "Linux"))
set(PLATFORM_LINK_LIBRIES rt c++abi)
ELSE()
set(PLATFORM_LINK_LIBRIES rt)
ENDIF()
ELSEIF(${CMAKE_CXX_COMPILER_ID} STREQUAL "GNU")
message( STATUS "cmake for GCC " )
IF (APPLE)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wunused -std=c++14 -pthread -D_GLIBCXX_USE_NANOSLEEP")
ELSEIF (MINGW)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wunused -std=c++14 -pthread -D_GLIBCXX_USE_NANOSLEEP -D_GLIBCXX_USE_SCHED_YIELD")
set(PLATFORM_LINK_LIBRIES dbghelp)
# deal with ERROR level conflicts with windows.h
ADD_DEFINITIONS (-DNOGDI)
ELSE()
set(PLATFORM_LINK_LIBRIES rt)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -rdynamic -Wunused -std=c++14 -pthread -lrt -D_GLIBCXX_USE_NANOSLEEP -D_GLIBCXX_USE_SCHED_YIELD")
ENDIF()
ELSEIF(MSVC)
set(PLATFORM_LINK_LIBRIES dbghelp)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /utf-8") # source code already in utf-8, force it for compilers in non-utf8_windows_locale
# ERROR level conflicts with windows.h
ADD_DEFINITIONS (-DNOGDI)
# support AMD proc on vc2015
if(${CMAKE_CL_64} STREQUAL "0")
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} /arch:IA32")
endif()
endif()
IF (MSVC OR MINGW)
# VC11 bug: http://code.google.com/p/googletest/issues/detail?id=408
# add_definition(-D_VARIADIC_MAX=10)
# https://github.com/anhstudios/swganh/pull/186/files
ADD_DEFINITIONS (/D_VARIADIC_MAX=10)
MESSAGE(STATUS "- MSVC: Set variadic max to 10 for MSVC compatibility")
# Remember to set set target properties if using GTEST similar to done below on target "unit_test"
# "set_target_properties(unit_test PROPERTIES COMPILE_DEFINITIONS "GTEST_USE_OWN_TR1_TUPLE=0")
message( STATUS "" )
message( STATUS "Windows: Run cmake with the appropriate Visual Studio generator" )
message( STATUS "The generator is one number below the official version number. I.e. VS2013 -> Generator 'Visual Studio 12'" )
MESSAGE( STATUS "I.e. if VS2013: Please run the command [cmake -DCMAKE_BUILD_TYPE=Release -G \"Visual Studio 12\" ..]")
message( STATUS "if cmake finishes OK, do 'msbuild g3log.sln /p:Configuration=Release'" )
message( STATUS "then run 'Release\\g3log-FATAL-*' examples" )
message( STATUS "" )
ENDIF()
# GENERIC STEPS
file(GLOB SRC_FILES ${LOG_SRC}/g3log/*.h ${LOG_SRC}/g3log/*.hpp ${LOG_SRC}/*.cpp ${LOG_SRC}/*.ipp)
file(GLOB HEADER_FILES ${LOG_SRC}/g3log/*.hpp ${LOG_SRC}/*.hpp)
list( APPEND HEADER_FILES ${GENERATED_G3_DEFINITIONS} )
list( APPEND SRC_FILES ${GENERATED_G3_DEFINITIONS} )
IF (MSVC OR MINGW)
list(REMOVE_ITEM SRC_FILES ${LOG_SRC}/crashhandler_unix.cpp)
ELSE()
list(REMOVE_ITEM SRC_FILES ${LOG_SRC}/crashhandler_windows.cpp ${LOG_SRC}/g3log/stacktrace_windows.hpp ${LOG_SRC}/stacktrace_windows.cpp)
ENDIF (MSVC OR MINGW)
set(SRC_FILES ${SRC_FILES} ${SRC_PLATFORM_SPECIFIC})
# Create the g3log library
INCLUDE_DIRECTORIES(${LOG_SRC})
SET(G3LOG_LIBRARY g3logger)
IF( G3_SHARED_LIB )
IF( WIN32 )
IF(NOT(${CMAKE_VERSION} VERSION_LESS "3.4"))
set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS ON)
ELSE()
message( FATAL_ERROR "Need CMake version >=3.4 to build shared windows library!" )
ENDIF()
ENDIF()
ADD_LIBRARY(${G3LOG_LIBRARY} SHARED ${SRC_FILES})
ELSE()
IF(MSVC)
IF(NOT G3_SHARED_RUNTIME)
ENDIF()
ADD_LIBRARY(${G3LOG_LIBRARY} SHARED ${SRC_FILES})
ELSE()
IF(MSVC)
IF(NOT G3_SHARED_RUNTIME)
SET(CompilerFlags
CMAKE_CXX_FLAGS
CMAKE_CXX_FLAGS_DEBUG
@ -131,27 +53,105 @@ ENDIF()
string(REPLACE "/MD" "/MT" ${CompilerFlag} "${${CompilerFlag}}")
endforeach()
ENDIF()
ENDIF()
ADD_LIBRARY(${G3LOG_LIBRARY} STATIC ${SRC_FILES})
ENDIF()
ENDIF()
ADD_LIBRARY(${G3LOG_LIBRARY} STATIC ${SRC_FILES})
ENDIF()
SET(${G3LOG_LIBRARY}_VERSION_STRING ${VERSION})
MESSAGE( STATUS "Creating ${G3LOG_LIBRARY} VERSION: ${VERSION}" )
SET_TARGET_PROPERTIES(${G3LOG_LIBRARY} PROPERTIES
LINKER_LANGUAGE CXX
OUTPUT_NAME g3logger
CLEAN_DIRECT_OUTPUT 1
CXX_STANDARD 14
CXX_STANDARD_REQUIRED YES
CXX_EXTENSIONS NO
SOVERSION ${VERSION}
)
IF(APPLE)
SET_TARGET_PROPERTIES(${G3LOG_LIBRARY} PROPERTIES MACOSX_RPATH TRUE)
ENDIF()
# require here some proxy for c++14 standard to avoid problems TARGET_PROPERTY CXX_STANDARD
TARGET_COMPILE_FEATURES(${G3LOG_LIBRARY} PUBLIC cxx_variable_templates)
TARGET_INCLUDE_DIRECTORIES(${G3LOG_LIBRARY} PUBLIC ${LOG_SRC} "${CMAKE_CURRENT_BINARY_DIR}/include")
SET(ACTIVE_CPP0xx_DIR "Release")
# find corresponding thread lib (e.g. whether -lpthread is needed or not)
FIND_PACKAGE(Threads REQUIRED)
TARGET_LINK_LIBRARIES(${G3LOG_LIBRARY} Threads::Threads )
# check for backtrace and cxa_demangle only in non-Windows dev environments
IF(NOT(MSVC OR MINGW))
# the backtrace module does not provide a modern cmake target
FIND_PACKAGE(Backtrace REQUIRED)
if(Backtrace_FOUND)
TARGET_INCLUDE_DIRECTORIES(${G3LOG_LIBRARY} PRIVATE ${Backtrace_INCLUDE_DIRS})
TARGET_LINK_LIBRARIES(${G3LOG_LIBRARY} ${Backtrace_LIBRARIES})
else()
message( FATAL_ERROR "Could not find Library to create backtraces")
endif()
SET(${G3LOG_LIBRARY}_VERSION_STRING ${VERSION})
MESSAGE( STATUS "Creating ${G3LOG_LIBRARY} VERSION: ${VERSION}" )
SET_TARGET_PROPERTIES(g3logger PROPERTIES LINKER_LANGUAGE CXX SOVERSION ${VERSION})
INCLUDE(CheckLibraryExists)
INCLUDE(CheckCXXSymbolExists)
set_target_properties(${G3LOG_LIBRARY} PROPERTIES
LINKER_LANGUAGE CXX
OUTPUT_NAME g3logger
CLEAN_DIRECT_OUTPUT 1)
#if demangle is in c++ runtime lib
CHECK_CXX_SYMBOL_EXISTS(abi::__cxa_demangle "cxxabi.h" DEMANGLE_EXISTS)
IF( NOT (DEMANGLE_EXISTS))
#try to link against c++abi to get demangle
CHECK_LIBRARY_EXISTS(c++abi abi::__cxa_demangle "cxxabi.h" NEED_C++ABI)
IF( NEED_C++ABI)
TARGET_LINK_LIBRARIES(${G3LOG_LIBRARY} c++abi)
ELSE()
message( FATAL_ERROR "Could not find function abi::__cxa_demangle")
ENDIF()
endif()
ENDIF()
# add Warnings
target_compile_options(${G3LOG_LIBRARY} PRIVATE
# clang/GCC warnings
$<$<OR:$<CXX_COMPILER_ID:Clang>,$<CXX_COMPILER_ID:GNU>>:-Wall -Wunused>
# MSVC warnings
$<$<CXX_COMPILER_ID:MSVC>:/W4>)
# add GCC specific stuff
target_compile_options(${G3LOG_LIBRARY} PRIVATE
# clang/GCC warnings
$<$<CXX_COMPILER_ID:GNU>:-rdynamic>
)
IF(APPLE)
set_target_properties(${G3LOG_LIBRARY} PROPERTIES MACOSX_RPATH TRUE)
ENDIF()
TARGET_LINK_LIBRARIES(${G3LOG_LIBRARY} ${PLATFORM_LINK_LIBRIES})
# Kjell: This is likely not necessary, except for Windows?
TARGET_INCLUDE_DIRECTORIES(${G3LOG_LIBRARY} PUBLIC ${LOG_SRC} "${CMAKE_CURRENT_BINARY_DIR}/include")
#cmake -DCMAKE_CXX_COMPILER=clang++ ..
# WARNING: If Clang for Linux does not work with full c++14 support it might be your
# installation that is faulty. When I tested Clang on Ubuntu I followed the following
# description
# 1) http://kjellkod.wordpress.com/2013/09/23/experimental-g3log-with-clang/
# 2) https://github.com/maidsafe/MaidSafe/wiki/Hacking-with-Clang-llvm-abi-and-llvm-libc
# Windows Stuff
IF(MSVC OR MINGW)
TARGET_COMPILE_DEFINITIONS(${G3LOG_LIBRARY} PRIVATE NOGDI)
TARGET_LINK_LIBRARIES(${G3LOG_LIBRARY} dbghelp)
# VC11 bug: http://code.google.com/p/googletest/issues/detail?id=408
# add_definition(-D_VARIADIC_MAX=10)
# https://github.com/anhstudios/swganh/pull/186/files
TARGET_COMPILE_DEFINITIONS(${G3LOG_LIBRARY} PRIVATE _VARIADIC_MAX=10)
MESSAGE(STATUS "- MSVC: Set variadic max to 10 for MSVC compatibility")
# Remember to set set target properties if using GTEST similar to done below on target "unit_test"
# "set_target_properties(unit_test PROPERTIES COMPILE_DEFINITIONS "GTEST_USE_OWN_TR1_TUPLE=0")
message( STATUS "" )
message( STATUS "Windows: Run cmake with the appropriate Visual Studio generator" )
message( STATUS "The generator is one number below the official version number. I.e. VS2013 -> Generator 'Visual Studio 12'" )
MESSAGE( STATUS "I.e. if VS2013: Please run the command [cmake -DCMAKE_BUILD_TYPE=Release -G \"Visual Studio 12\" ..]")
message( STATUS "if cmake finishes OK, do 'msbuild g3log.sln /p:Configuration=Release'" )
message( STATUS "then run 'Release\\g3log-FATAL-*' examples" )
message( STATUS "" )
ENDIF()
TARGET_COMPILE_OPTIONS(${G3LOG_LIBRARY} PRIVATE
$<$<CXX_COMPILER_ID:MSVC>:/utf-8> # source code already in utf-8, force it for compilers in non-utf8_windows_locale
$<$<CXX_COMPILER_ID:MSVC>:$<$<EQUAL:4,${CMAKE_SIZEOF_VOID_P}>:/arch:IA32>>
)

View File

@ -53,7 +53,7 @@
#
# ============================================================================
cmake_minimum_required (VERSION 3.1)
cmake_minimum_required (VERSION 3.2)
ENABLE_LANGUAGE(CXX)
if(NOT CMAKE_BUILD_TYPE AND NOT (MSVC_IDE OR XCODE))
@ -81,14 +81,14 @@ SET(MAJOR_VERSION 1)
IF ( NOT VERSION )
IF ( "${CMAKE_HOST_SYSTEM_NAME}" STREQUAL "Windows" )
message("windows: Extracting git software version")
execute_process(COMMAND cmd /c "git rev-list --branches HEAD | find /v " " /c" OUTPUT_VARIABLE GIT_VERSION WORKING_DIRECTORY ${CMAKE_SOURCE_DIR})
execute_process(COMMAND cmd /c "git rev-list --branches HEAD | find /v " " /c" OUTPUT_VARIABLE GIT_VERSION WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR})
ELSE()
IF(UNIX OR ${CMAKE_SYSTEM_NAME} MATCHES "Darwin")
message( STATUS "nix: Extracting git software version" )
ELSE()
message( STATUS "unknown platform: extracting git software version" )
ENDIF()
execute_process(COMMAND bash "-c" "git rev-list --branches HEAD | wc -l | tr -d ' ' | tr -d '\n'" OUTPUT_VARIABLE GIT_VERSION WORKING_DIRECTORY ${CMAKE_SOURCE_DIR})
execute_process(COMMAND bash "-c" "git rev-list --branches HEAD | wc -l | tr -d ' ' | tr -d '\n'" OUTPUT_VARIABLE GIT_VERSION WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR})
ENDIF()
SET(MINOR_VERSION 3)

View File

@ -12,8 +12,8 @@
# . performance test (average + worst case) for KjellKod's g3log
# Do 'cmake -DUSE_G3LOG_PERFORMANCE=ON' to enable this
option (ADD_G3LOG_PERFORMANCE "g3log performance test" OFF)
# Do 'cmake -DADD_G3LOG_BENCH_PERFORMANCE=ON' to enable this
option (ADD_G3LOG_BENCH_PERFORMANCE "g3log performance test" OFF)