From c0cadd92953d905313f9f3188867f7b790898263 Mon Sep 17 00:00:00 2001 From: Andres6936 Date: Sun, 26 Dec 2021 09:10:34 -0500 Subject: [PATCH] Better support to CMake --- CMakeLists.txt | 27 +---- example/readFromStream/CMakeLists.txt | 5 +- example/readFromString/CMakeLists.txt | 5 +- example/streamWrite/CMakeLists.txt | 5 +- example/stringWrite/CMakeLists.txt | 5 +- include/CMakeLists.txt | 5 +- src/lib_json/CMakeLists.txt | 149 ++++++-------------------- 7 files changed, 45 insertions(+), 156 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index fd8bcf2..51bbe44 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -78,6 +78,9 @@ project(jsoncpp message(STATUS "JsonCpp Version: ${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}.${PROJECT_VERSION_PATCH}") set(PROJECT_SOVERSION 25) +# Defines the project root to be used throughout all scripts. +SET(JSONCPP_ROOT_DIR ${CMAKE_CURRENT_SOURCE_DIR}) + include(${CMAKE_CURRENT_SOURCE_DIR}/include/PreventInSourceBuilds.cmake) include(${CMAKE_CURRENT_SOURCE_DIR}/include/PreventInBuildInstalls.cmake) @@ -89,9 +92,8 @@ option(JSONCPP_WITH_PKGCONFIG_SUPPORT "Generate and install .pc files" ON) option(JSONCPP_WITH_CMAKE_PACKAGE "Generate and install cmake package files" ON) option(JSONCPP_WITH_EXAMPLE "Compile JsonCpp example" OFF) option(JSONCPP_STATIC_WINDOWS_RUNTIME "Use static (MT/MTd) Windows runtime" OFF) -option(BUILD_SHARED_LIBS "Build jsoncpp_lib as a shared library." ON) -option(BUILD_STATIC_LIBS "Build jsoncpp_lib as a static library." ON) -option(BUILD_OBJECT_LIBS "Build jsoncpp_lib as a object library." ON) + +option(JSONCPP_BUILD_SHARED_LIBS "Build jsoncpp_lib as a shared library." OFF) # Adhere to GNU filesystem layout conventions include(GNUInstallDirs) @@ -179,22 +181,6 @@ if(JSONCPP_WITH_PKGCONFIG_SUPPORT) DESTINATION "${CMAKE_INSTALL_LIBDIR}/pkgconfig") endif() -if(JSONCPP_WITH_CMAKE_PACKAGE) - include(CMakePackageConfigHelpers) - install(EXPORT jsoncpp - DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/jsoncpp - FILE jsoncpp-targets.cmake) - configure_package_config_file(jsoncppConfig.cmake.in ${CMAKE_CURRENT_BINARY_DIR}/jsoncppConfig.cmake - INSTALL_DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/jsoncpp) - - write_basic_package_version_file("${CMAKE_CURRENT_BINARY_DIR}/jsoncppConfigVersion.cmake" - VERSION ${PROJECT_VERSION} - COMPATIBILITY SameMajorVersion) - install(FILES - ${CMAKE_CURRENT_BINARY_DIR}/jsoncppConfigVersion.cmake ${CMAKE_CURRENT_BINARY_DIR}/jsoncppConfig.cmake - ${CMAKE_CURRENT_SOURCE_DIR}/jsoncpp-namespaced-targets.cmake - DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/jsoncpp) -endif() if(JSONCPP_WITH_TESTS) enable_testing() @@ -204,9 +190,6 @@ endif() # Build the different applications add_subdirectory(src) -#install the includes -add_subdirectory(include) - #install the example if(JSONCPP_WITH_EXAMPLE) add_subdirectory(example) diff --git a/example/readFromStream/CMakeLists.txt b/example/readFromStream/CMakeLists.txt index e779db6..e947dcd 100644 --- a/example/readFromStream/CMakeLists.txt +++ b/example/readFromStream/CMakeLists.txt @@ -1,3 +1,2 @@ -add_executable(readFromStream readFromStream.cpp) -target_include_directories(readFromStream PUBLIC ${CMAKE_SOURCE_DIR}/include) -target_link_libraries(readFromStream jsoncpp_lib) \ No newline at end of file +ADD_EXECUTABLE(readFromStream readFromStream.cpp) +TARGET_LINK_LIBRARIES(readFromStream PRIVATE jsoncpp::framework) \ No newline at end of file diff --git a/example/readFromString/CMakeLists.txt b/example/readFromString/CMakeLists.txt index 5c55ae2..d076651 100644 --- a/example/readFromString/CMakeLists.txt +++ b/example/readFromString/CMakeLists.txt @@ -1,3 +1,2 @@ -add_executable(readFromString readFromString.cpp) -target_include_directories(readFromString PUBLIC ${CMAKE_SOURCE_DIR}/include) -target_link_libraries(readFromString jsoncpp_lib) \ No newline at end of file +ADD_EXECUTABLE(readFromString readFromString.cpp) +TARGET_LINK_LIBRARIES(readFromString PRIVATE jsoncpp::framework) \ No newline at end of file diff --git a/example/streamWrite/CMakeLists.txt b/example/streamWrite/CMakeLists.txt index 8bdb061..f5f9e63 100644 --- a/example/streamWrite/CMakeLists.txt +++ b/example/streamWrite/CMakeLists.txt @@ -1,3 +1,2 @@ -add_executable(streamWrite streamWrite.cpp) -target_include_directories(streamWrite PUBLIC ${CMAKE_SOURCE_DIR}/include) -target_link_libraries(streamWrite jsoncpp_lib) \ No newline at end of file +ADD_EXECUTABLE(streamWrite streamWrite.cpp) +TARGET_LINK_LIBRARIES(streamWrite PRIVATE jsoncpp::framework) \ No newline at end of file diff --git a/example/stringWrite/CMakeLists.txt b/example/stringWrite/CMakeLists.txt index 6676b02..28c3ce7 100644 --- a/example/stringWrite/CMakeLists.txt +++ b/example/stringWrite/CMakeLists.txt @@ -1,3 +1,2 @@ -add_executable(stringWrite stringWrite.cpp) -target_include_directories(stringWrite PUBLIC ${CMAKE_SOURCE_DIR}/include) -target_link_libraries(stringWrite jsoncpp_lib) \ No newline at end of file +ADD_EXECUTABLE(stringWrite stringWrite.cpp) +TARGET_LINK_LIBRARIES(stringWrite PRIVATE jsoncpp::framework) \ No newline at end of file diff --git a/include/CMakeLists.txt b/include/CMakeLists.txt index dc40d95..139597f 100644 --- a/include/CMakeLists.txt +++ b/include/CMakeLists.txt @@ -1,5 +1,2 @@ -file(GLOB INCLUDE_FILES "json/*.h") -install(FILES - ${INCLUDE_FILES} - DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/json) + diff --git a/src/lib_json/CMakeLists.txt b/src/lib_json/CMakeLists.txt index 3cf66eb..d24d273 100644 --- a/src/lib_json/CMakeLists.txt +++ b/src/lib_json/CMakeLists.txt @@ -25,41 +25,11 @@ if(NOT (HAVE_CLOCALE AND HAVE_LCONV_SIZE AND HAVE_DECIMAL_POINT AND HAVE_LOCALEC endif() endif() -set(JSONCPP_INCLUDE_DIR ../../include) - -set(PUBLIC_HEADERS - ${JSONCPP_INCLUDE_DIR}/json/config.h - ${JSONCPP_INCLUDE_DIR}/json/forwards.h - ${JSONCPP_INCLUDE_DIR}/json/json_features.h - ${JSONCPP_INCLUDE_DIR}/json/value.h - ${JSONCPP_INCLUDE_DIR}/json/reader.h - ${JSONCPP_INCLUDE_DIR}/json/version.h - ${JSONCPP_INCLUDE_DIR}/json/writer.h - ${JSONCPP_INCLUDE_DIR}/json/assertions.h -) - -source_group("Public API" FILES ${PUBLIC_HEADERS}) - -set(JSONCPP_SOURCES - json_tool.h - json_reader.cpp - json_valueiterator.inl - json_value.cpp - json_writer.cpp -) - -# Install instructions for this target -if(JSONCPP_WITH_CMAKE_PACKAGE) - set(INSTALL_EXPORT EXPORT jsoncpp) -else() - set(INSTALL_EXPORT) -endif() # Specify compiler features required when compiling a given target. # See https://cmake.org/cmake/help/v3.1/prop_gbl/CMAKE_CXX_KNOWN_FEATURES.html#prop_gbl:CMAKE_CXX_KNOWN_FEATURES # for complete list of features available list(APPEND REQUIRED_FEATURES - cxx_std_11 # Compiler mode is aware of C++ 11. #MSVC 1900 cxx_alignas # Alignment control alignas, as defined in N2341. #MSVC 1900 cxx_alignof # Alignment control alignof, as defined in N2341. #MSVC 1900 cxx_attributes # Generic attributes, as defined in N2761. @@ -106,103 +76,46 @@ list(APPEND REQUIRED_FEATURES ) -if(BUILD_SHARED_LIBS) - if(CMAKE_VERSION VERSION_GREATER_EQUAL 3.12.0) - add_compile_definitions(JSON_DLL_BUILD) - else() - add_definitions(-DJSON_DLL_BUILD) - endif() +ADD_LIBRARY(jsoncpp.framework.object OBJECT + json_tool.h + json_reader.cpp + json_valueiterator.inl + json_value.cpp + json_writer.cpp + ) - 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} - SOVERSION ${PROJECT_SOVERSION} - POSITION_INDEPENDENT_CODE ${BUILD_SHARED_LIBS} - ) +SET_TARGET_PROPERTIES(jsoncpp.framework.object PROPERTIES CXX_STANDARD 11) +SET_TARGET_PROPERTIES(jsoncpp.framework.object PROPERTIES VERSION ${PROJECT_VERSION}) +SET_TARGET_PROPERTIES(jsoncpp.framework.object PROPERTIES SOVERSION ${PROJECT_SOVERSION}) - # Set library's runtime search path on OSX - if(APPLE) - set_target_properties(${SHARED_LIB} PROPERTIES INSTALL_RPATH "@loader_path/.") - endif() +TARGET_COMPILE_FEATURES(jsoncpp.framework.object PUBLIC ${REQUIRED_FEATURES}) +TARGET_INCLUDE_DIRECTORIES(jsoncpp.framework.object PUBLIC $) - target_compile_features(${SHARED_LIB} PUBLIC ${REQUIRED_FEATURES}) +# Set library's runtime search path on OSX +IF (APPLE) + SET_TARGET_PROPERTIES(jsoncpp.framework.object PROPERTIES INSTALL_RPATH "@loader_path/.") +ENDIF () - target_include_directories(${SHARED_LIB} PUBLIC - $ - $ - $ - ) - list(APPEND CMAKE_TARGETS ${SHARED_LIB}) -endif() +IF (JSONCPP_BUILD_SHARED_LIBS) -if(BUILD_STATIC_LIBS) - set(STATIC_LIB ${PROJECT_NAME}_static) - add_library(${STATIC_LIB} STATIC ${PUBLIC_HEADERS} ${JSONCPP_SOURCES}) + # CMake link shared library on Windows + # Ref: https://stackoverflow.com/a/41618677 + SET(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS TRUE) - # avoid name clashes on windows as the shared import lib is also named jsoncpp.lib - if(NOT DEFINED STATIC_SUFFIX AND BUILD_SHARED_LIBS) - if (MSVC) - set(STATIC_SUFFIX "_static") - else() - set(STATIC_SUFFIX "") - endif() - endif() + ADD_LIBRARY(jsoncpp.framework SHARED $) + TARGET_COMPILE_DEFINITIONS(jsoncpp.framework PUBLIC JSON_DLL_BUILD) - set_target_properties(${STATIC_LIB} PROPERTIES - OUTPUT_NAME jsoncpp${STATIC_SUFFIX} - VERSION ${PROJECT_VERSION} - ) +ELSE () - # Set library's runtime search path on OSX - if(APPLE) - set_target_properties(${STATIC_LIB} PROPERTIES INSTALL_RPATH "@loader_path/.") - endif() + ADD_LIBRARY(jsoncpp.framework STATIC $) - target_compile_features(${STATIC_LIB} PUBLIC ${REQUIRED_FEATURES}) +ENDIF () - target_include_directories(${STATIC_LIB} PUBLIC - $ - $ - $ - ) - - list(APPEND CMAKE_TARGETS ${STATIC_LIB}) -endif() - -if(BUILD_OBJECT_LIBS) - set(OBJECT_LIB ${PROJECT_NAME}_object) - add_library(${OBJECT_LIB} OBJECT ${PUBLIC_HEADERS} ${JSONCPP_SOURCES}) - - set_target_properties(${OBJECT_LIB} PROPERTIES - OUTPUT_NAME jsoncpp - VERSION ${PROJECT_VERSION} - SOVERSION ${PROJECT_SOVERSION} - POSITION_INDEPENDENT_CODE ${BUILD_SHARED_LIBS} - ) - - # Set library's runtime search path on OSX - if(APPLE) - set_target_properties(${OBJECT_LIB} PROPERTIES INSTALL_RPATH "@loader_path/.") - endif() - - target_compile_features(${OBJECT_LIB} PUBLIC ${REQUIRED_FEATURES}) - - target_include_directories(${OBJECT_LIB} PUBLIC - $ - $ - $ - ) - - list(APPEND CMAKE_TARGETS ${OBJECT_LIB}) -endif() - -install(TARGETS ${CMAKE_TARGETS} ${INSTALL_EXPORT} - RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR} - LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} - ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR} - OBJECTS DESTINATION ${CMAKE_INSTALL_LIBDIR} -) +ADD_LIBRARY(jsoncpp::framework ALIAS jsoncpp.framework) +SET_TARGET_PROPERTIES(jsoncpp.framework PROPERTIES CXX_STANDARD 11) +SET_TARGET_PROPERTIES(jsoncpp.framework PROPERTIES VERSION ${PROJECT_VERSION}) +SET_TARGET_PROPERTIES(jsoncpp.framework PROPERTIES SOVERSION ${PROJECT_SOVERSION}) +TARGET_COMPILE_FEATURES(jsoncpp.framework PUBLIC ${REQUIRED_FEATURES}) +TARGET_INCLUDE_DIRECTORIES(jsoncpp.framework PUBLIC $) \ No newline at end of file