diff --git a/CMakeLists.txt b/CMakeLists.txt index 8920544a3..6c47338cf 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -12,8 +12,8 @@ # CMake versions greater than the JSONCPP_NEWEST_VALIDATED_POLICIES_VERSION policies will # continue to generate policy warnings "CMake Warning (dev)...Policy CMP0XXX is not set:" # -set(JSONCPP_OLDEST_VALIDATED_POLICIES_VERSION "3.8.0") -set(JSONCPP_NEWEST_VALIDATED_POLICIES_VERSION "3.13.2") +set(JSONCPP_OLDEST_VALIDATED_POLICIES_VERSION "3.21..") +set(JSONCPP_NEWEST_VALIDATED_POLICIES_VERSION "3.29") cmake_minimum_required(VERSION ${JSONCPP_OLDEST_VALIDATED_POLICIES_VERSION}) if("${CMAKE_VERSION}" VERSION_LESS "${JSONCPP_NEWEST_VALIDATED_POLICIES_VERSION}") #Set and use the newest available cmake policies that are validated to work @@ -40,12 +40,6 @@ foreach(pold "") # Currently Empty endif() endforeach() -# Build the library with C++11 standard support, independent from other including -# software which may use a different CXX_STANDARD or CMAKE_CXX_STANDARD. -set(CMAKE_CXX_STANDARD 11) -set(CMAKE_CXX_EXTENSIONS OFF) -set(CMAKE_CXX_STANDARD_REQUIRED ON) - # Ensure that CMAKE_BUILD_TYPE has a value specified for single configuration generators. if(NOT DEFINED CMAKE_BUILD_TYPE AND NOT DEFINED CMAKE_CONFIGURATION_TYPES) set(CMAKE_BUILD_TYPE Release CACHE STRING @@ -75,6 +69,15 @@ project(jsoncpp VERSION 1.9.5 # [.[.[.]]] LANGUAGES CXX) + +if (PROJECT_IS_TOP_LEVEL) + # Build the library with C++11 standard support, independent from other including + # software which may use a different CXX_STANDARD or CMAKE_CXX_STANDARD. + set(CMAKE_CXX_STANDARD 11) + set(CMAKE_CXX_EXTENSIONS OFF) + set(CMAKE_CXX_STANDARD_REQUIRED ON) +endif() + message(STATUS "JsonCpp Version: ${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}.${PROJECT_VERSION_PATCH}") set(PROJECT_SOVERSION 25) @@ -105,6 +108,8 @@ endif() set(JSONCPP_USE_SECURE_MEMORY "0" CACHE STRING "-D...=1 to use memory-wiping allocator for STL") +add_library(project_options INTERFACE) + configure_file("${PROJECT_SOURCE_DIR}/version.in" "${PROJECT_BINARY_DIR}/version" NEWLINE_STYLE UNIX) @@ -113,22 +118,22 @@ macro(use_compilation_warning_as_error) if(MSVC) # Only enabled in debug because some old versions of VS STL generate # warnings when compiled in release configuration. - add_compile_options($<$:/WX>) + target_compile_options(project_options INTERFACE $<$:/WX>) elseif(CMAKE_CXX_COMPILER_ID STREQUAL "GNU") - add_compile_options(-Werror) + target_compile_options(project_options INTERFACE -Werror) if(JSONCPP_WITH_STRICT_ISO) - add_compile_options(-pedantic-errors) + target_compile_options(project_options INTETFACE -pedantic-errors) endif() endif() endmacro() # Include our configuration header -include_directories(${jsoncpp_SOURCE_DIR}/include) +target_include_directories(project_options INTERFACE ${jsoncpp_SOURCE_DIR}/include) if(MSVC) # Only enabled in debug because some old versions of VS STL generate # unreachable code warning when compiled in release configuration. - add_compile_options($<$:/W4>) + target_compile_options(project_options INTERFACE $<$:/W4>) if (JSONCPP_STATIC_WINDOWS_RUNTIME) set(CMAKE_MSVC_RUNTIME_LIBRARY "MultiThreaded$<$:Debug>") endif() @@ -136,30 +141,30 @@ endif() if(CMAKE_CXX_COMPILER_ID MATCHES "Clang") # using regular Clang or AppleClang - add_compile_options(-Wall -Wconversion -Wshadow) + target_compile_options(project_options INTERFACE -Wall -Wconversion -Wshadow) if(JSONCPP_WITH_WARNING_AS_ERROR) - add_compile_options(-Werror=conversion -Werror=sign-compare) + target_compile_options(project_options INTERFACE -Werror=conversion -Werror=sign-compare) endif() elseif(CMAKE_CXX_COMPILER_ID STREQUAL "GNU") # using GCC - add_compile_options(-Wall -Wconversion -Wshadow -Wextra) + target_compile_options(project_options INTERFACE -Wall -Wconversion -Wshadow -Wextra) # not yet ready for -Wsign-conversion if(JSONCPP_WITH_STRICT_ISO) - add_compile_options(-Wpedantic) + target_compile_options(project_options INTERFACE -Wpedantic) endif() if(JSONCPP_WITH_WARNING_AS_ERROR) - add_compile_options(-Werror=conversion) + target_compile_options(project_options INTERFACE -Werror=conversion) endif() elseif(CMAKE_CXX_COMPILER_ID STREQUAL "Intel") # using Intel compiler - add_compile_options(-Wall -Wconversion -Wshadow -Wextra) + target_compile_options(project_options INTERFACE -Wall -Wconversion -Wshadow -Wextra) if(JSONCPP_WITH_WARNING_AS_ERROR) - add_compile_options(-Werror=conversion) + target_compile_options(project_options INTERFACE -Werror=conversion) elseif(JSONCPP_WITH_STRICT_ISO) - add_compile_options(-Wpedantic) + target_compile_options(project_options INTERFACE -Wpedantic) endif() endif() diff --git a/example/CMakeLists.txt b/example/CMakeLists.txt index 230d1bd7b..1af78a6d3 100644 --- a/example/CMakeLists.txt +++ b/example/CMakeLists.txt @@ -5,21 +5,27 @@ set(EXAMPLES stringWrite streamWrite ) -add_definitions(-D_GLIBCXX_USE_CXX11_ABI) -if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU" OR CMAKE_CXX_COMPILER_ID STREQUAL "Clang") - add_compile_options(-Wall -Wextra) -elseif(CMAKE_CXX_COMPILER_ID STREQUAL "MSVC") - add_definitions( - -D_SCL_SECURE_NO_WARNINGS - -D_CRT_SECURE_NO_WARNINGS - -D_WIN32_WINNT=0x601 - -D_WINSOCK_DEPRECATED_NO_WARNINGS - ) +macro(add_example_options target) + + target_compile_definitions(${target} -D_GLIBCXX_USE_CXX11_ABI) + + if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU" OR CMAKE_CXX_COMPILER_ID STREQUAL "Clang") + target_compile_options(${target} PRIVATE -Wall -Wextra) + elseif(CMAKE_CXX_COMPILER_ID STREQUAL "MSVC") + target_compile_definitions(${target} + PRIVATE + -D_SCL_SECURE_NO_WARNINGS + -D_CRT_SECURE_NO_WARNINGS + -D_WIN32_WINNT=0x601 + -D_WINSOCK_DEPRECATED_NO_WARNINGS + ) + endif() endif() foreach(example ${EXAMPLES}) add_executable(${example} ${example}/${example}.cpp) + add_example_options(${example}) target_include_directories(${example} PUBLIC ${CMAKE_SOURCE_DIR}/include) target_link_libraries(${example} jsoncpp_lib) endforeach() diff --git a/src/jsontestrunner/CMakeLists.txt b/src/jsontestrunner/CMakeLists.txt index 1fc71ea87..70aefb3de 100644 --- a/src/jsontestrunner/CMakeLists.txt +++ b/src/jsontestrunner/CMakeLists.txt @@ -15,7 +15,7 @@ add_executable(jsontestrunner_exe if(BUILD_SHARED_LIBS) if(CMAKE_VERSION VERSION_GREATER_EQUAL 3.12.0) - add_compile_definitions( JSON_DLL ) + target_compile_definitions(jsontestrunner_exe PUBLIC JSON_DLL ) else() add_definitions(-DJSON_DLL) endif() diff --git a/src/lib_json/CMakeLists.txt b/src/lib_json/CMakeLists.txt index 3cf66eb34..4e1bffe3d 100644 --- a/src/lib_json/CMakeLists.txt +++ b/src/lib_json/CMakeLists.txt @@ -1,6 +1,7 @@ +add_library(jsonlib_options INTERFACE) if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU" AND CMAKE_CXX_COMPILER_VERSION VERSION_GREATER 4.1.2) #-Werror=* was introduced -after- GCC 4.1.2 - add_compile_options("-Werror=strict-aliasing") + target_compile_options(jsonlib_options INTERFACE "-Werror=strict-aliasing") endif() include(CheckIncludeFileCXX) @@ -8,6 +9,8 @@ include(CheckTypeSize) include(CheckStructHasMember) include(CheckCXXSymbolExists) +# might have to do this: list(APPEND CMAKE_REQUIRED_INCLUDES ${ASIO_PATH}) <-- with some path that is missing...? + check_include_file_cxx(clocale HAVE_CLOCALE) check_cxx_symbol_exists(localeconv clocale HAVE_LOCALECONV) @@ -19,7 +22,7 @@ check_struct_has_member(lconv decimal_point clocale HAVE_DECIMAL_POINT LANGUAGE if(NOT (HAVE_CLOCALE AND HAVE_LCONV_SIZE AND HAVE_DECIMAL_POINT AND HAVE_LOCALECONV)) message(WARNING "Locale functionality is not supported") if(CMAKE_VERSION VERSION_GREATER_EQUAL 3.12.0) - add_compile_definitions(JSONCPP_NO_LOCALE_SUPPORT) + target_compile_definitions(jsonlib_options PUBLIC JSONCPP_NO_LOCALE_SUPPORT) else() add_definitions(-DJSONCPP_NO_LOCALE_SUPPORT) endif() @@ -107,14 +110,13 @@ list(APPEND REQUIRED_FEATURES if(BUILD_SHARED_LIBS) + set(SHARED_LIB ${PROJECT_NAME}_lib) + add_library(${SHARED_LIB} SHARED ${PUBLIC_HEADERS} ${JSONCPP_SOURCES}) if(CMAKE_VERSION VERSION_GREATER_EQUAL 3.12.0) - add_compile_definitions(JSON_DLL_BUILD) + target_compile_definitions(${SHARED_LIB} PUBLIC JSON_DLL_BUILD) else() add_definitions(-DJSON_DLL_BUILD) endif() - - set(SHARED_LIB ${PROJECT_NAME}_lib) - add_library(${SHARED_LIB} SHARED ${PUBLIC_HEADERS} ${JSONCPP_SOURCES}) set_target_properties(${SHARED_LIB} PROPERTIES OUTPUT_NAME jsoncpp VERSION ${PROJECT_VERSION} @@ -128,6 +130,10 @@ if(BUILD_SHARED_LIBS) endif() target_compile_features(${SHARED_LIB} PUBLIC ${REQUIRED_FEATURES}) + target_link_libraries(${SHARED_LIB} PUBLIC + $ + $ + ) target_include_directories(${SHARED_LIB} PUBLIC $ @@ -162,6 +168,10 @@ if(BUILD_STATIC_LIBS) endif() target_compile_features(${STATIC_LIB} PUBLIC ${REQUIRED_FEATURES}) + target_link_libraries(${STATIC_LIB} PUBLIC + $ + $ + ) target_include_directories(${STATIC_LIB} PUBLIC $ @@ -189,6 +199,10 @@ if(BUILD_OBJECT_LIBS) endif() target_compile_features(${OBJECT_LIB} PUBLIC ${REQUIRED_FEATURES}) + target_link_libraries(${OBJECT_LIB} PUBLIC + $ + $ + ) target_include_directories(${OBJECT_LIB} PUBLIC $ diff --git a/src/test_lib_json/CMakeLists.txt b/src/test_lib_json/CMakeLists.txt index 1c3fce913..c459e3b30 100644 --- a/src/test_lib_json/CMakeLists.txt +++ b/src/test_lib_json/CMakeLists.txt @@ -11,13 +11,15 @@ add_executable(jsoncpp_test if(BUILD_SHARED_LIBS) if(CMAKE_VERSION VERSION_GREATER_EQUAL 3.12.0) - add_compile_definitions( JSON_DLL ) + target_compile_definitions(jsoncpp_test PRIVATE JSON_DLL ) else() - add_definitions( -DJSON_DLL ) + target_compile_definitions(jsoncpp_test PRIVATE -DJSON_DLL ) endif() - target_link_libraries(jsoncpp_test jsoncpp_lib) -else() - target_link_libraries(jsoncpp_test jsoncpp_static) + target_link_libraries(jsoncpp_test jsoncpp_lib + $ + ) + else() + target_link_libraries(jsoncpp_test jsoncpp_static $) endif() # another way to solve issue #90