Skip to content

Commit 8665055

Browse files
rcdaileycdunn2001
authored andcommitted
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.
1 parent aadd0b1 commit 8665055

File tree

9 files changed

+75
-62
lines changed

9 files changed

+75
-62
lines changed

CMakeLists.txt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,9 @@ OPTION(JSONCPP_WITH_POST_BUILD_UNITTEST "Automatically run unit-tests as a post
99
OPTION(JSONCPP_WITH_WARNING_AS_ERROR "Force compilation to fail if a warning occurs" OFF)
1010
OPTION(JSONCPP_WITH_PKGCONFIG_SUPPORT "Generate and install .pc files" ON)
1111
OPTION(JSONCPP_WITH_CMAKE_PACKAGE "Generate and install cmake package files" OFF)
12-
OPTION(BUILD_SHARED_LIBS "Build jsoncpp_lib as a shared library." OFF)
13-
OPTION(BUILD_STATIC_LIBS "Build jsoncpp_lib static library." ON)
12+
13+
SET(JSONCPP_LIBRARY_TYPE STATIC CACHE STRING "Build a static or shared library")
14+
SET_PROPERTY(CACHE JSONCPP_LIBRARY_TYPE PROPERTY STRINGS STATIC SHARED )
1415

1516
# Ensures that CMAKE_BUILD_TYPE is visible in cmake-gui on Unix
1617
IF(NOT WIN32)

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,16 +85,16 @@ Steps for generating solution/makefiles using `cmake-gui`:
8585
* Make "source code" point to the source directory.
8686
* Make "where to build the binary" point to the directory to use for the build.
8787
* Click on the "Grouped" check box.
88-
* Review JsonCpp build options (tick `BUILD_SHARED_LIBS` to build as a
89-
dynamic library).
88+
* Review JsonCpp build options (e.g. set `JSONCPP_LIBRARY_TYPE` to `SHARED` to
89+
build as a shared library).
9090
* Click the configure button at the bottom, then the generate button.
9191
* The generated solution/makefiles can be found in the binary directory.
9292

9393
Alternatively, from the command-line on Unix in the source directory:
9494

9595
mkdir -p build/debug
9696
cd build/debug
97-
cmake -DCMAKE_BUILD_TYPE=debug -DBUILD_STATIC_LIBS=ON -DBUILD_SHARED_LIBS=OFF -DARCHIVE_INSTALL_DIR=. -G "Unix Makefiles" ../..
97+
cmake -DCMAKE_BUILD_TYPE=debug -DJSONCPP_LIBRARY_TYPE=STATIC -DARCHIVE_INSTALL_DIR=. -G "Unix Makefiles" ../..
9898
make
9999

100100
Running `cmake -h` will display the list of available generators (passed using

dev.makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ dox:
1616
# Then 'git add -A' and 'git push' in jsoncpp-docs.
1717
build:
1818
mkdir -p build/debug
19-
cd build/debug; cmake -DCMAKE_BUILD_TYPE=debug -DBUILD_SHARED_LIBS=ON -G "Unix Makefiles" ../..
19+
cd build/debug; cmake -DCMAKE_BUILD_TYPE=debug -DJSONCPP_LIBRARY_TYPE=SHARED -G "Unix Makefiles" ../..
2020
make -C build/debug
2121

2222
# Currently, this depends on include/json/version.h generated

devtools/agent_vmw7.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,7 @@
1919
},
2020
{"name": "shared_dll",
2121
"variables": [
22-
["BUILD_SHARED_LIBS=true"],
23-
["BUILD_SHARED_LIBS=false"]
22+
["JSONCPP_LIBRARY_TYPE=SHARED"]
2423
]
2524
},
2625
{"name": "build_type",

devtools/agent_vmxp.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,7 @@
1212
},
1313
{"name": "shared_dll",
1414
"variables": [
15-
["BUILD_SHARED_LIBS=true"],
16-
["BUILD_SHARED_LIBS=false"]
15+
["JSONCPP_LIBRARY_TYPE=SHARED"]
1716
]
1817
},
1918
{"name": "build_type",

src/jsontestrunner/CMakeLists.txt

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,12 @@ ADD_EXECUTABLE(jsontestrunner_exe
44
main.cpp
55
)
66

7-
IF(BUILD_SHARED_LIBS)
7+
IF(JSONCPP_LIBRARY_TYPE STREQUAL "SHARED")
88
ADD_DEFINITIONS( -DJSON_DLL )
9-
TARGET_LINK_LIBRARIES(jsontestrunner_exe jsoncpp_lib)
10-
ELSE(BUILD_SHARED_LIBS)
11-
TARGET_LINK_LIBRARIES(jsontestrunner_exe jsoncpp_lib_static)
129
ENDIF()
1310

11+
TARGET_LINK_LIBRARIES(jsontestrunner_exe jsoncpp)
12+
1413
SET_TARGET_PROPERTIES(jsontestrunner_exe PROPERTIES OUTPUT_NAME jsontestrunner_exe)
1514

1615
IF(PYTHONINTERP_FOUND)

src/lib_json/CMakeLists.txt

Lines changed: 23 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -39,42 +39,33 @@ ELSE(JSONCPP_WITH_CMAKE_PACKAGE)
3939
SET(INSTALL_EXPORT)
4040
ENDIF()
4141

42-
IF(BUILD_SHARED_LIBS)
43-
ADD_DEFINITIONS( -DJSON_DLL_BUILD )
44-
ADD_LIBRARY(jsoncpp_lib SHARED ${PUBLIC_HEADERS} ${jsoncpp_sources})
45-
SET_TARGET_PROPERTIES( jsoncpp_lib PROPERTIES VERSION ${JSONCPP_VERSION} SOVERSION ${JSONCPP_VERSION_MAJOR})
46-
SET_TARGET_PROPERTIES( jsoncpp_lib PROPERTIES OUTPUT_NAME jsoncpp
47-
DEBUG_OUTPUT_NAME jsoncpp${DEBUG_LIBNAME_SUFFIX} )
48-
49-
INSTALL( TARGETS jsoncpp_lib ${INSTALL_EXPORT}
50-
RUNTIME DESTINATION ${RUNTIME_INSTALL_DIR}
51-
LIBRARY DESTINATION ${LIBRARY_INSTALL_DIR}
52-
ARCHIVE DESTINATION ${ARCHIVE_INSTALL_DIR})
53-
54-
IF(NOT CMAKE_VERSION VERSION_LESS 2.8.11)
55-
TARGET_INCLUDE_DIRECTORIES( jsoncpp_lib PUBLIC
56-
$<INSTALL_INTERFACE:${INCLUDE_INSTALL_DIR}>
57-
$<BUILD_INTERFACE:${CMAKE_CURRENT_LIST_DIR}/${JSONCPP_INCLUDE_DIR}>)
58-
ENDIF()
42+
set( project_name jsoncpp )
5943

44+
IF(JSONCPP_LIBRARY_TYPE STREQUAL "SHARED")
45+
ADD_DEFINITIONS( -DJSON_DLL_BUILD )
46+
SET(lib_type SHARED)
47+
ELSEIF(JSONCPP_LIBRARY_TYPE STREQUAL "STATIC")
48+
SET(lib_type STATIC)
6049
ENDIF()
6150

62-
IF(BUILD_STATIC_LIBS)
63-
ADD_LIBRARY(jsoncpp_lib_static STATIC ${PUBLIC_HEADERS} ${jsoncpp_sources})
64-
SET_TARGET_PROPERTIES( jsoncpp_lib_static PROPERTIES VERSION ${JSONCPP_VERSION} SOVERSION ${JSONCPP_VERSION_MAJOR})
65-
SET_TARGET_PROPERTIES( jsoncpp_lib_static PROPERTIES OUTPUT_NAME jsoncpp
66-
DEBUG_OUTPUT_NAME jsoncpp${DEBUG_LIBNAME_SUFFIX} )
51+
ADD_LIBRARY(${project_name} ${lib_type} ${PUBLIC_HEADERS} ${jsoncpp_sources})
6752

68-
INSTALL( TARGETS jsoncpp_lib_static ${INSTALL_EXPORT}
69-
RUNTIME DESTINATION ${RUNTIME_INSTALL_DIR}
70-
LIBRARY DESTINATION ${LIBRARY_INSTALL_DIR}
71-
ARCHIVE DESTINATION ${ARCHIVE_INSTALL_DIR})
53+
SET_TARGET_PROPERTIES(${project_name} PROPERTIES
54+
VERSION ${JSONCPP_VERSION}
55+
SOVERSION ${JSONCPP_VERSION_MAJOR}
56+
OUTPUT_NAME jsoncpp
57+
DEBUG_OUTPUT_NAME jsoncpp${DEBUG_LIBNAME_SUFFIX}
58+
)
7259

73-
IF(NOT CMAKE_VERSION VERSION_LESS 2.8.11)
74-
TARGET_INCLUDE_DIRECTORIES( jsoncpp_lib_static PUBLIC
75-
$<INSTALL_INTERFACE:${INCLUDE_INSTALL_DIR}>
76-
$<BUILD_INTERFACE:${CMAKE_CURRENT_LIST_DIR}/${JSONCPP_INCLUDE_DIR}>
77-
)
78-
ENDIF()
60+
INSTALL(TARGETS ${project_name} ${INSTALL_EXPORT}
61+
RUNTIME DESTINATION ${RUNTIME_INSTALL_DIR}
62+
LIBRARY DESTINATION ${LIBRARY_INSTALL_DIR}
63+
ARCHIVE DESTINATION ${ARCHIVE_INSTALL_DIR}
64+
)
7965

66+
IF(NOT CMAKE_VERSION VERSION_LESS 2.8.11)
67+
TARGET_INCLUDE_DIRECTORIES( ${project_name} PUBLIC
68+
$<INSTALL_INTERFACE:${INCLUDE_INSTALL_DIR}>
69+
$<BUILD_INTERFACE:${CMAKE_CURRENT_LIST_DIR}/${JSONCPP_INCLUDE_DIR}>
70+
)
8071
ENDIF()

src/test_lib_json/CMakeLists.txt

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,27 +7,26 @@ ADD_EXECUTABLE( jsoncpp_test
77
)
88

99

10-
IF(BUILD_SHARED_LIBS)
10+
IF(JSONCPP_LIBRARY_TYPE STREQUAL "SHARED")
1111
ADD_DEFINITIONS( -DJSON_DLL )
12-
TARGET_LINK_LIBRARIES(jsoncpp_test jsoncpp_lib)
13-
ELSE(BUILD_SHARED_LIBS)
14-
TARGET_LINK_LIBRARIES(jsoncpp_test jsoncpp_lib_static)
1512
ENDIF()
1613

14+
TARGET_LINK_LIBRARIES(jsoncpp_test jsoncpp)
15+
1716
# another way to solve issue #90
1817
#set_target_properties(jsoncpp_test PROPERTIES COMPILE_FLAGS -ffloat-store)
1918

2019
# Run unit tests in post-build
2120
# (default cmake workflow hides away the test result into a file, resulting in poor dev workflow?!?)
2221
IF(JSONCPP_WITH_POST_BUILD_UNITTEST)
23-
IF(BUILD_SHARED_LIBS)
22+
IF(JSONCPP_LIBRARY_TYPE STREQUAL "SHARED")
2423
# First, copy the shared lib, for Microsoft.
2524
# Then, run the test executable.
2625
ADD_CUSTOM_COMMAND( TARGET jsoncpp_test
2726
POST_BUILD
28-
COMMAND ${CMAKE_COMMAND} -E copy_if_different $<TARGET_FILE:jsoncpp_lib> $<TARGET_FILE_DIR:jsoncpp_test>
27+
COMMAND ${CMAKE_COMMAND} -E copy_if_different $<TARGET_FILE:jsoncpp> $<TARGET_FILE_DIR:jsoncpp_test>
2928
COMMAND $<TARGET_FILE:jsoncpp_test>)
30-
ELSE(BUILD_SHARED_LIBS)
29+
ELSE(JSONCPP_LIBRARY_TYPE STREQUAL "STATIC")
3130
# Just run the test executable.
3231
ADD_CUSTOM_COMMAND( TARGET jsoncpp_test
3332
POST_BUILD

travis.sh

Lines changed: 34 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,38 @@ set -vex
1717

1818
env | sort
1919

20-
cmake -DJSONCPP_WITH_CMAKE_PACKAGE=$CMAKE_PKG -DBUILD_SHARED_LIBS=$SHARED_LIB -DCMAKE_BUILD_TYPE=$BUILD_TYPE -DCMAKE_VERBOSE_MAKEFILE=$VERBOSE_MAKE .
21-
make
22-
23-
# Python is not available in Travis for osx.
24-
# https://github.com/travis-ci/travis-ci/issues/2320
25-
if [ "$TRAVIS_OS_NAME" != "osx" ]
26-
then
27-
make jsoncpp_check
28-
valgrind --error-exitcode=42 --leak-check=full ./src/test_lib_json/jsoncpp_test
20+
# $1 = Binary directory name
21+
# $2 = Value for JSONCPP_LIBRARY_TYPE (SHARED or STATIC)
22+
cmakebuild()
23+
{
24+
if [ ! -d "$1" ]; then
25+
mkdir "$1"
26+
fi
27+
28+
cd "$1"
29+
30+
cmake .. -G "Unix Makefiles" \
31+
-DJSONCPP_WITH_CMAKE_PACKAGE="$CMAKE_PKG" \
32+
-DJSONCPP_LIBRARY_TYPE="$2" \
33+
-DCMAKE_BUILD_TYPE="$BUILD_TYPE" \
34+
-DCMAKE_VERBOSE_MAKEFILE="$VERBOSE_MAKE"
35+
36+
make
37+
38+
# Python is not available in Travis for osx.
39+
# https://github.com/travis-ci/travis-ci/issues/2320
40+
if [ "$TRAVIS_OS_NAME" != "osx" ]; then
41+
make jsoncpp_check
42+
valgrind --error-exitcode=42 --leak-check=full ./src/test_lib_json/jsoncpp_test
43+
fi
44+
45+
cd -
46+
}
47+
48+
if [ "$SHARED_LIB" = "ON" ]; then
49+
cmakebuild build_shared SHARED
50+
fi
51+
52+
if [ "$STATIC_LIB" = "ON" ]; then
53+
cmakebuild build_static STATIC
2954
fi

0 commit comments

Comments
 (0)