Merge pull request #1165 from mrjoel/mrjoel/cmake-updates

Improve CMake correctness and handling
This commit is contained in:
Christopher Dunn 2020-04-29 08:27:43 -05:00 committed by GitHub
commit d517d598a7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 28 additions and 97 deletions

View File

@ -37,27 +37,14 @@ foreach(pold "") # Currently Empty
endif() endif()
endforeach() endforeach()
# ==== Define language standard configurations requiring at least c++11 standard # Build the library with C++11 standard support, independent from other including
if(CMAKE_CXX_STANDARD EQUAL "98") # software which may use a different CXX_STANDARD or CMAKE_CXX_STANDARD.
message(FATAL_ERROR "CMAKE_CXX_STANDARD:STRING=98 is not supported.") set(CMAKE_CXX_STANDARD 11)
endif() set(CMAKE_CXX_EXTENSIONS OFF)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
##### # Ensure that CMAKE_BUILD_TYPE has a value specified for single configuration generators.
## Set the default target properties if(NOT DEFINED CMAKE_BUILD_TYPE AND NOT DEFINED CMAKE_CONFIGURATION_TYPES)
if(NOT CMAKE_CXX_STANDARD)
set(CMAKE_CXX_STANDARD 11) # Supported values are ``11``, ``14``, and ``17``.
endif()
if(NOT CMAKE_CXX_STANDARD_REQUIRED)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
endif()
if(NOT CMAKE_CXX_EXTENSIONS)
set(CMAKE_CXX_EXTENSIONS OFF)
endif()
# ====
# Ensures that CMAKE_BUILD_TYPE has a default value
if(NOT DEFINED CMAKE_BUILD_TYPE)
set(CMAKE_BUILD_TYPE Release CACHE STRING set(CMAKE_BUILD_TYPE Release CACHE STRING
"Choose the type of build, options are: None Debug Release RelWithDebInfo MinSizeRel Coverage.") "Choose the type of build, options are: None Debug Release RelWithDebInfo MinSizeRel Coverage.")
endif() endif()
@ -95,16 +82,9 @@ option(JSONCPP_WITH_CMAKE_PACKAGE "Generate and install cmake package files" ON)
option(JSONCPP_WITH_EXAMPLE "Compile JsonCpp example" OFF) option(JSONCPP_WITH_EXAMPLE "Compile JsonCpp example" OFF)
option(BUILD_SHARED_LIBS "Build jsoncpp_lib as a shared library." OFF) option(BUILD_SHARED_LIBS "Build jsoncpp_lib as a shared library." OFF)
# Enable runtime search path support for dynamic libraries on OSX
if(APPLE)
set(CMAKE_MACOSX_RPATH 1)
endif()
# Adhere to GNU filesystem layout conventions # Adhere to GNU filesystem layout conventions
include(GNUInstallDirs) include(GNUInstallDirs)
set(DEBUG_LIBNAME_SUFFIX "" CACHE STRING "Optional suffix to append to the library name for a debug build")
set(JSONCPP_USE_SECURE_MEMORY "0" CACHE STRING "-D...=1 to use memory-wiping allocator for STL") set(JSONCPP_USE_SECURE_MEMORY "0" CACHE STRING "-D...=1 to use memory-wiping allocator for STL")
configure_file("${PROJECT_SOURCE_DIR}/version.in" configure_file("${PROJECT_SOURCE_DIR}/version.in"
@ -115,23 +95,11 @@ macro(use_compilation_warning_as_error)
if(MSVC) if(MSVC)
# Only enabled in debug because some old versions of VS STL generate # Only enabled in debug because some old versions of VS STL generate
# warnings when compiled in release configuration. # warnings when compiled in release configuration.
if(CMAKE_VERSION VERSION_GREATER_EQUAL 3.12.0) add_compile_options($<$<CONFIG:Debug>:/WX>)
add_compile_options($<$<CONFIG:Debug>:/WX>)
else()
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} /WX ")
endif()
elseif(CMAKE_CXX_COMPILER_ID STREQUAL "GNU") elseif(CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
if(CMAKE_VERSION VERSION_GREATER_EQUAL 3.12.0) add_compile_options(-Werror)
add_compile_options(-Werror)
else()
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Werror")
endif()
if(JSONCPP_WITH_STRICT_ISO) if(JSONCPP_WITH_STRICT_ISO)
if(CMAKE_VERSION VERSION_GREATER_EQUAL 3.12.0) add_compile_options(-pedantic-errors)
add_compile_options(-pedantic-errors)
else()
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -pedantic-errors")
endif()
endif() endif()
endif() endif()
endmacro() endmacro()
@ -142,57 +110,29 @@ include_directories(${jsoncpp_SOURCE_DIR}/include)
if(MSVC) if(MSVC)
# Only enabled in debug because some old versions of VS STL generate # Only enabled in debug because some old versions of VS STL generate
# unreachable code warning when compiled in release configuration. # unreachable code warning when compiled in release configuration.
if(CMAKE_VERSION VERSION_GREATER_EQUAL 3.12.0) add_compile_options($<$<CONFIG:Debug>:/W4>)
add_compile_options($<$<CONFIG:Debug>:/W4>)
else()
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} /W4 ")
endif()
endif() endif()
if(CMAKE_CXX_COMPILER_ID MATCHES "Clang") if(CMAKE_CXX_COMPILER_ID MATCHES "Clang")
# using regular Clang or AppleClang # using regular Clang or AppleClang
if(CMAKE_VERSION VERSION_GREATER_EQUAL 3.12.0) add_compile_options(-Wall -Wconversion -Wshadow -Werror=conversion -Werror=sign-compare)
add_compile_options(-Wall -Wconversion -Wshadow -Werror=conversion -Werror=sign-compare)
else()
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wconversion -Wshadow -Werror=conversion -Werror=sign-compare")
endif()
elseif(CMAKE_CXX_COMPILER_ID STREQUAL "GNU") elseif(CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
# using GCC # using GCC
if(CMAKE_VERSION VERSION_GREATER_EQUAL 3.12.0) add_compile_options(-Wall -Wconversion -Wshadow -Wextra)
add_compile_options(-Wall -Wconversion -Wshadow -Wextra)
else()
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wconversion -Wshadow -Wextra")
endif()
# not yet ready for -Wsign-conversion # not yet ready for -Wsign-conversion
if(JSONCPP_WITH_STRICT_ISO) if(JSONCPP_WITH_STRICT_ISO)
if(CMAKE_VERSION VERSION_GREATER_EQUAL 3.12.0) add_compile_options(-Wpedantic)
add_compile_options(-Wpedantic)
else()
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wpedantic")
endif()
endif() endif()
if(JSONCPP_WITH_WARNING_AS_ERROR) if(JSONCPP_WITH_WARNING_AS_ERROR)
if(CMAKE_VERSION VERSION_GREATER_EQUAL 3.12.0) add_compile_options(-Werror=conversion)
add_compile_options(-Werror=conversion)
else()
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Werror=conversion")
endif()
endif() endif()
elseif(CMAKE_CXX_COMPILER_ID STREQUAL "Intel") elseif(CMAKE_CXX_COMPILER_ID STREQUAL "Intel")
# using Intel compiler # using Intel compiler
if(CMAKE_VERSION VERSION_GREATER_EQUAL 3.12.0) add_compile_options(-Wall -Wconversion -Wshadow -Wextra -Werror=conversion)
add_compile_options(-Wall -Wconversion -Wshadow -Wextra -Werror=conversion)
else()
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wconversion -Wshadow -Wextra -Werror=conversion")
endif()
if(JSONCPP_WITH_STRICT_ISO AND NOT JSONCPP_WITH_WARNING_AS_ERROR) if(JSONCPP_WITH_STRICT_ISO AND NOT JSONCPP_WITH_WARNING_AS_ERROR)
if(CMAKE_VERSION VERSION_GREATER_EQUAL 3.12.0) add_compile_options(-Wpedantic)
add_compile_options(-Wpedantic)
else()
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wpedantic")
endif()
endif() endif()
endif() endif()

View File

@ -1,6 +1,4 @@
#vim: et ts =4 sts = 4 sw = 4 tw = 0 #vim: et ts =4 sts = 4 sw = 4 tw = 0
cmake_minimum_required(VERSION 3.1)
set(EXAMPLES set(EXAMPLES
readFromString readFromString
readFromStream readFromStream
@ -8,11 +6,10 @@ set(EXAMPLES
streamWrite streamWrite
) )
add_definitions(-D_GLIBCXX_USE_CXX11_ABI) add_definitions(-D_GLIBCXX_USE_CXX11_ABI)
set_property(DIRECTORY PROPERTY COMPILE_OPTIONS ${EXTRA_CXX_FLAGS})
if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU") if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU" OR CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wextra ") add_compile_options(-Wall -Wextra)
else() elseif(CMAKE_CXX_COMPILER_ID STREQUAL "MSVC")
add_definitions( add_definitions(
-D_SCL_SECURE_NO_WARNINGS -D_SCL_SECURE_NO_WARNINGS
-D_CRT_SECURE_NO_WARNINGS -D_CRT_SECURE_NO_WARNINGS

View File

@ -1,13 +1,6 @@
if(CMAKE_COMPILER_IS_GNUCXX) if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU" AND CMAKE_CXX_COMPILER_VERSION VERSION_GREATER 4.1.2)
#Get compiler version.
execute_process(COMMAND ${CMAKE_CXX_COMPILER} -dumpversion
OUTPUT_VARIABLE GNUCXX_VERSION
)
#-Werror=* was introduced -after- GCC 4.1.2 #-Werror=* was introduced -after- GCC 4.1.2
if(GNUCXX_VERSION VERSION_GREATER 4.1.2) add_compile_options("-Werror=strict-aliasing")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Werror=strict-aliasing")
endif()
endif() endif()
include(CheckIncludeFileCXX) include(CheckIncludeFileCXX)
@ -81,12 +74,13 @@ if(BUILD_SHARED_LIBS)
endif() endif()
endif() endif()
add_library(jsoncpp_lib ${PUBLIC_HEADERS} ${jsoncpp_sources}) add_library(jsoncpp_lib ${PUBLIC_HEADERS} ${jsoncpp_sources})
set_target_properties(jsoncpp_lib PROPERTIES VERSION ${JSONCPP_VERSION} SOVERSION ${JSONCPP_SOVERSION}) set_target_properties( jsoncpp_lib PROPERTIES
set_target_properties(jsoncpp_lib PROPERTIES OUTPUT_NAME jsoncpp OUTPUT_NAME jsoncpp
DEBUG_OUTPUT_NAME jsoncpp${DEBUG_LIBNAME_SUFFIX}) VERSION ${JSONCPP_VERSION}
set_target_properties(jsoncpp_lib PROPERTIES POSITION_INDEPENDENT_CODE ON) SOVERSION ${JSONCPP_SOVERSION}
POSITION_INDEPENDENT_CODE ON
)
# Set library's runtime search path on OSX # Set library's runtime search path on OSX
if(APPLE) if(APPLE)