From 63a961a752dc5f6f256c8498f85179c18f8f37b6 Mon Sep 17 00:00:00 2001 From: Robert Dailey Date: Thu, 6 Aug 2015 10:17:09 -0500 Subject: [PATCH 1/2] Clean up cmake END* (again) (I missed a couple. ~cd) --- src/test_lib_json/CMakeLists.txt | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/test_lib_json/CMakeLists.txt b/src/test_lib_json/CMakeLists.txt index f54c85f68..7000264a7 100644 --- a/src/test_lib_json/CMakeLists.txt +++ b/src/test_lib_json/CMakeLists.txt @@ -12,7 +12,7 @@ IF(BUILD_SHARED_LIBS) TARGET_LINK_LIBRARIES(jsoncpp_test jsoncpp_lib) ELSE(BUILD_SHARED_LIBS) TARGET_LINK_LIBRARIES(jsoncpp_test jsoncpp_lib_static) -ENDIF(BUILD_SHARED_LIBS) +ENDIF() # another way to solve issue #90 #set_target_properties(jsoncpp_test PROPERTIES COMPILE_FLAGS -ffloat-store) @@ -32,7 +32,7 @@ IF(JSONCPP_WITH_POST_BUILD_UNITTEST) ADD_CUSTOM_COMMAND( TARGET jsoncpp_test POST_BUILD COMMAND $) - ENDIF(BUILD_SHARED_LIBS) -ENDIF(JSONCPP_WITH_POST_BUILD_UNITTEST) + ENDIF() +ENDIF() SET_TARGET_PROPERTIES(jsoncpp_test PROPERTIES OUTPUT_NAME jsoncpp_test) From 6e3042840fdf7167230bd8f57462bd9582a91d29 Mon Sep 17 00:00:00 2001 From: Robert Dailey Date: Thu, 6 Aug 2015 10:17:09 -0500 Subject: [PATCH 2/2] Use a consistent target name for jsoncpp in CMake scripts The target name for the jsoncpp library would change based on whether we were building a static or shared library. This inconsistency made it difficult and unintuitive to pull in jsoncpp as a submodule in another repository and link to it directly via other CMake scripts. Having a consistent target name will allow libraries with their own CMake scripts to reliably refer to jsoncpp as a dependency. Other Changes: * `BUILD_SHARED_LIBS` and `BUILD_STATIC_LIBS` removed in favor of `JSONCPP_LIBRARY_TYPE`, which allows you to pick either `SHARED` or `STATIC` library variations. This change was made to prevent both shared and static libraries being built at the same time. This isn't allowed anymore since we only generate 1 target for the jsoncpp library now. * `travis.sh` build script updated to perform CMake generation in an out-of-source binary directory. This will prevent the temporary generated output files from intermixing into the source tree and allow for multiple generations with different configurations using the same source tree. --- CMakeLists.txt | 5 +-- README.md | 6 ++-- dev.makefile | 2 +- devtools/agent_vmw7.json | 3 +- devtools/agent_vmxp.json | 3 +- src/jsontestrunner/CMakeLists.txt | 7 ++-- src/lib_json/CMakeLists.txt | 55 +++++++++++++------------------ src/test_lib_json/CMakeLists.txt | 13 ++++---- travis.sh | 43 +++++++++++++++++++----- 9 files changed, 75 insertions(+), 62 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 7d459fd60..313de9584 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -9,8 +9,9 @@ OPTION(JSONCPP_WITH_POST_BUILD_UNITTEST "Automatically run unit-tests as a post OPTION(JSONCPP_WITH_WARNING_AS_ERROR "Force compilation to fail if a warning occurs" OFF) OPTION(JSONCPP_WITH_PKGCONFIG_SUPPORT "Generate and install .pc files" ON) OPTION(JSONCPP_WITH_CMAKE_PACKAGE "Generate and install cmake package files" OFF) -OPTION(BUILD_SHARED_LIBS "Build jsoncpp_lib as a shared library." OFF) -OPTION(BUILD_STATIC_LIBS "Build jsoncpp_lib static library." ON) + +SET(JSONCPP_LIBRARY_TYPE STATIC CACHE STRING "Build a static or shared library") +SET_PROPERTY(CACHE JSONCPP_LIBRARY_TYPE PROPERTY STRINGS STATIC SHARED ) # Ensures that CMAKE_BUILD_TYPE is visible in cmake-gui on Unix IF(NOT WIN32) diff --git a/README.md b/README.md index 93c8d1f59..7e8dc0d9f 100644 --- a/README.md +++ b/README.md @@ -85,8 +85,8 @@ Steps for generating solution/makefiles using `cmake-gui`: * Make "source code" point to the source directory. * Make "where to build the binary" point to the directory to use for the build. * Click on the "Grouped" check box. -* Review JsonCpp build options (tick `BUILD_SHARED_LIBS` to build as a - dynamic library). +* Review JsonCpp build options (e.g. set `JSONCPP_LIBRARY_TYPE` to `SHARED` to + build as a shared library). * Click the configure button at the bottom, then the generate button. * The generated solution/makefiles can be found in the binary directory. @@ -94,7 +94,7 @@ Alternatively, from the command-line on Unix in the source directory: mkdir -p build/debug cd build/debug - cmake -DCMAKE_BUILD_TYPE=debug -DBUILD_STATIC_LIBS=ON -DBUILD_SHARED_LIBS=OFF -DARCHIVE_INSTALL_DIR=. -G "Unix Makefiles" ../.. + cmake -DCMAKE_BUILD_TYPE=debug -DJSONCPP_LIBRARY_TYPE=STATIC -DARCHIVE_INSTALL_DIR=. -G "Unix Makefiles" ../.. make Running `cmake -h` will display the list of available generators (passed using diff --git a/dev.makefile b/dev.makefile index d288b1665..c3c9c7ba3 100644 --- a/dev.makefile +++ b/dev.makefile @@ -16,7 +16,7 @@ dox: # Then 'git add -A' and 'git push' in jsoncpp-docs. build: mkdir -p build/debug - cd build/debug; cmake -DCMAKE_BUILD_TYPE=debug -DBUILD_SHARED_LIBS=ON -G "Unix Makefiles" ../.. + cd build/debug; cmake -DCMAKE_BUILD_TYPE=debug -DJSONCPP_LIBRARY_TYPE=SHARED -G "Unix Makefiles" ../.. make -C build/debug # Currently, this depends on include/json/version.h generated diff --git a/devtools/agent_vmw7.json b/devtools/agent_vmw7.json index cd7b777fe..c1fba831f 100644 --- a/devtools/agent_vmw7.json +++ b/devtools/agent_vmw7.json @@ -19,8 +19,7 @@ }, {"name": "shared_dll", "variables": [ - ["BUILD_SHARED_LIBS=true"], - ["BUILD_SHARED_LIBS=false"] + ["JSONCPP_LIBRARY_TYPE=SHARED"] ] }, {"name": "build_type", diff --git a/devtools/agent_vmxp.json b/devtools/agent_vmxp.json index f82a0773a..735e26310 100644 --- a/devtools/agent_vmxp.json +++ b/devtools/agent_vmxp.json @@ -12,8 +12,7 @@ }, {"name": "shared_dll", "variables": [ - ["BUILD_SHARED_LIBS=true"], - ["BUILD_SHARED_LIBS=false"] + ["JSONCPP_LIBRARY_TYPE=SHARED"] ] }, {"name": "build_type", diff --git a/src/jsontestrunner/CMakeLists.txt b/src/jsontestrunner/CMakeLists.txt index 20d01e626..9501d9c01 100644 --- a/src/jsontestrunner/CMakeLists.txt +++ b/src/jsontestrunner/CMakeLists.txt @@ -4,13 +4,12 @@ ADD_EXECUTABLE(jsontestrunner_exe main.cpp ) -IF(BUILD_SHARED_LIBS) +IF(JSONCPP_LIBRARY_TYPE STREQUAL "SHARED") ADD_DEFINITIONS( -DJSON_DLL ) - TARGET_LINK_LIBRARIES(jsontestrunner_exe jsoncpp_lib) -ELSE(BUILD_SHARED_LIBS) - TARGET_LINK_LIBRARIES(jsontestrunner_exe jsoncpp_lib_static) ENDIF() +TARGET_LINK_LIBRARIES(jsontestrunner_exe jsoncpp) + SET_TARGET_PROPERTIES(jsontestrunner_exe PROPERTIES OUTPUT_NAME jsontestrunner_exe) IF(PYTHONINTERP_FOUND) diff --git a/src/lib_json/CMakeLists.txt b/src/lib_json/CMakeLists.txt index 99ddc7f8b..c17cdd13d 100644 --- a/src/lib_json/CMakeLists.txt +++ b/src/lib_json/CMakeLists.txt @@ -39,42 +39,33 @@ ELSE(JSONCPP_WITH_CMAKE_PACKAGE) SET(INSTALL_EXPORT) ENDIF() -IF(BUILD_SHARED_LIBS) - ADD_DEFINITIONS( -DJSON_DLL_BUILD ) - ADD_LIBRARY(jsoncpp_lib SHARED ${PUBLIC_HEADERS} ${jsoncpp_sources}) - SET_TARGET_PROPERTIES( jsoncpp_lib PROPERTIES VERSION ${JSONCPP_VERSION} SOVERSION ${JSONCPP_VERSION_MAJOR}) - SET_TARGET_PROPERTIES( jsoncpp_lib PROPERTIES OUTPUT_NAME jsoncpp - DEBUG_OUTPUT_NAME jsoncpp${DEBUG_LIBNAME_SUFFIX} ) - - INSTALL( TARGETS jsoncpp_lib ${INSTALL_EXPORT} - RUNTIME DESTINATION ${RUNTIME_INSTALL_DIR} - LIBRARY DESTINATION ${LIBRARY_INSTALL_DIR} - ARCHIVE DESTINATION ${ARCHIVE_INSTALL_DIR}) - - IF(NOT CMAKE_VERSION VERSION_LESS 2.8.11) - TARGET_INCLUDE_DIRECTORIES( jsoncpp_lib PUBLIC - $ - $) - ENDIF() +set( project_name jsoncpp ) +IF(JSONCPP_LIBRARY_TYPE STREQUAL "SHARED") + ADD_DEFINITIONS( -DJSON_DLL_BUILD ) + SET(lib_type SHARED) +ELSEIF(JSONCPP_LIBRARY_TYPE STREQUAL "STATIC") + SET(lib_type STATIC) ENDIF() -IF(BUILD_STATIC_LIBS) - ADD_LIBRARY(jsoncpp_lib_static STATIC ${PUBLIC_HEADERS} ${jsoncpp_sources}) - SET_TARGET_PROPERTIES( jsoncpp_lib_static PROPERTIES VERSION ${JSONCPP_VERSION} SOVERSION ${JSONCPP_VERSION_MAJOR}) - SET_TARGET_PROPERTIES( jsoncpp_lib_static PROPERTIES OUTPUT_NAME jsoncpp - DEBUG_OUTPUT_NAME jsoncpp${DEBUG_LIBNAME_SUFFIX} ) +ADD_LIBRARY(${project_name} ${lib_type} ${PUBLIC_HEADERS} ${jsoncpp_sources}) - INSTALL( TARGETS jsoncpp_lib_static ${INSTALL_EXPORT} - RUNTIME DESTINATION ${RUNTIME_INSTALL_DIR} - LIBRARY DESTINATION ${LIBRARY_INSTALL_DIR} - ARCHIVE DESTINATION ${ARCHIVE_INSTALL_DIR}) +SET_TARGET_PROPERTIES(${project_name} PROPERTIES + VERSION ${JSONCPP_VERSION} + SOVERSION ${JSONCPP_VERSION_MAJOR} + OUTPUT_NAME jsoncpp + DEBUG_OUTPUT_NAME jsoncpp${DEBUG_LIBNAME_SUFFIX} +) - IF(NOT CMAKE_VERSION VERSION_LESS 2.8.11) - TARGET_INCLUDE_DIRECTORIES( jsoncpp_lib_static PUBLIC - $ - $ - ) - ENDIF() +INSTALL(TARGETS ${project_name} ${INSTALL_EXPORT} + RUNTIME DESTINATION ${RUNTIME_INSTALL_DIR} + LIBRARY DESTINATION ${LIBRARY_INSTALL_DIR} + ARCHIVE DESTINATION ${ARCHIVE_INSTALL_DIR} +) +IF(NOT CMAKE_VERSION VERSION_LESS 2.8.11) + TARGET_INCLUDE_DIRECTORIES( ${project_name} PUBLIC + $ + $ + ) ENDIF() diff --git a/src/test_lib_json/CMakeLists.txt b/src/test_lib_json/CMakeLists.txt index 7000264a7..8bb597530 100644 --- a/src/test_lib_json/CMakeLists.txt +++ b/src/test_lib_json/CMakeLists.txt @@ -7,27 +7,26 @@ ADD_EXECUTABLE( jsoncpp_test ) -IF(BUILD_SHARED_LIBS) +IF(JSONCPP_LIBRARY_TYPE STREQUAL "SHARED") ADD_DEFINITIONS( -DJSON_DLL ) - TARGET_LINK_LIBRARIES(jsoncpp_test jsoncpp_lib) -ELSE(BUILD_SHARED_LIBS) - TARGET_LINK_LIBRARIES(jsoncpp_test jsoncpp_lib_static) ENDIF() +TARGET_LINK_LIBRARIES(jsoncpp_test jsoncpp) + # another way to solve issue #90 #set_target_properties(jsoncpp_test PROPERTIES COMPILE_FLAGS -ffloat-store) # Run unit tests in post-build # (default cmake workflow hides away the test result into a file, resulting in poor dev workflow?!?) IF(JSONCPP_WITH_POST_BUILD_UNITTEST) - IF(BUILD_SHARED_LIBS) + IF(JSONCPP_LIBRARY_TYPE STREQUAL "SHARED") # First, copy the shared lib, for Microsoft. # Then, run the test executable. ADD_CUSTOM_COMMAND( TARGET jsoncpp_test POST_BUILD - COMMAND ${CMAKE_COMMAND} -E copy_if_different $ $ + COMMAND ${CMAKE_COMMAND} -E copy_if_different $ $ COMMAND $) - ELSE(BUILD_SHARED_LIBS) + ELSE(JSONCPP_LIBRARY_TYPE STREQUAL "STATIC") # Just run the test executable. ADD_CUSTOM_COMMAND( TARGET jsoncpp_test POST_BUILD diff --git a/travis.sh b/travis.sh index 2b25f4766..47eb99986 100755 --- a/travis.sh +++ b/travis.sh @@ -17,13 +17,38 @@ set -vex env | sort -cmake -DJSONCPP_WITH_CMAKE_PACKAGE=$CMAKE_PKG -DBUILD_SHARED_LIBS=$SHARED_LIB -DCMAKE_BUILD_TYPE=$BUILD_TYPE -DCMAKE_VERBOSE_MAKEFILE=$VERBOSE_MAKE . -make - -# Python is not available in Travis for osx. -# https://github.com/travis-ci/travis-ci/issues/2320 -if [ "$TRAVIS_OS_NAME" != "osx" ] -then - make jsoncpp_check - valgrind --error-exitcode=42 --leak-check=full ./src/test_lib_json/jsoncpp_test +# $1 = Binary directory name +# $2 = Value for JSONCPP_LIBRARY_TYPE (SHARED or STATIC) +cmakebuild() +{ + if [ ! -d "$1" ]; then + mkdir "$1" + fi + + cd "$1" + + cmake .. -G "Unix Makefiles" \ + -DJSONCPP_WITH_CMAKE_PACKAGE="$CMAKE_PKG" \ + -DJSONCPP_LIBRARY_TYPE="$2" \ + -DCMAKE_BUILD_TYPE="$BUILD_TYPE" \ + -DCMAKE_VERBOSE_MAKEFILE="$VERBOSE_MAKE" + + make + + # Python is not available in Travis for osx. + # https://github.com/travis-ci/travis-ci/issues/2320 + if [ "$TRAVIS_OS_NAME" != "osx" ]; then + make jsoncpp_check + valgrind --error-exitcode=42 --leak-check=full ./src/test_lib_json/jsoncpp_test + fi + + cd - +} + +if [ "$SHARED_LIB" = "ON" ]; then + cmakebuild build_shared SHARED +fi + +if [ "$STATIC_LIB" = "ON" ]; then + cmakebuild build_static STATIC fi