Skip to content

Commit eafd702

Browse files
committed
- New CMake based build system. Based in part on contribution from
Igor Okulist and Damien Buhl (Patch #14). Added support for running tests and building with DLL on Windows. - added missing JSON_API - Visual Studio DLL: suppressed warning "C4251: <data member>: <type> needs to have dll-interface to be used by..." via pragma push/pop in json-cpp headers. - New header json/version.h now contains version number macros (JSONCPP_VERSION_MAJOR, JSONCPP_VERSION_MINOR, JSONCPP_VERSION_PATCH and JSONCPP_VERSION_HEXA). While this header is generated by CMake, it is committed to ease build with alternate build system (CMake only update the file when it changes avoid issues with VCS).
1 parent a8afdd4 commit eafd702

File tree

15 files changed

+314
-21
lines changed

15 files changed

+314
-21
lines changed

CMakeLists.txt

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
CMAKE_MINIMUM_REQUIRED(VERSION 2.6)
2+
PROJECT(jsoncpp)
3+
ENABLE_TESTING()
4+
5+
OPTION(JSONCPP_WITH_TESTS "Compile and run JsonCpp test executables" ON)
6+
OPTION(JSONCPP_WITH_POST_BUILD_UNITTEST "Automatically run unit-tests as a post build step" ON)
7+
8+
# Ensures that CMAKE_BUILD_TYPE is visible in cmake-gui on Unix
9+
IF(NOT WIN32)
10+
IF(NOT CMAKE_BUILD_TYPE)
11+
SET(CMAKE_BUILD_TYPE Release CACHE STRING
12+
"Choose the type of build, options are: None Debug Release RelWithDebInfo MinSizeRel Coverage."
13+
FORCE)
14+
ENDIF(NOT CMAKE_BUILD_TYPE)
15+
ENDIF(NOT WIN32)
16+
17+
# This ensures shared DLL are in the same dir as executable on Windows.
18+
# Put all executables / libraries are in a project global directory.
19+
SET(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR}/lib
20+
CACHE PATH "Single directory for all static libraries.")
21+
SET(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR}/lib
22+
CACHE PATH "Single directory for all dynamic libraries on Unix.")
23+
SET(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR}/bin
24+
CACHE PATH "Single directory for all executable and dynamic libraries on Windows.")
25+
MARK_AS_ADVANCED( CMAKE_RUNTIME_OUTPUT_DIRECTORY CMAKE_LIBRARY_OUTPUT_DIRECTORY CMAKE_ARCHIVE_OUTPUT_DIRECTORY )
26+
27+
# Set variable named ${VAR_NAME} to value ${VALUE}
28+
FUNCTION(set_using_dynamic_name VAR_NAME VALUE)
29+
SET( "${VAR_NAME}" "${VALUE}" PARENT_SCOPE)
30+
ENDFUNCTION(set_using_dynamic_name)
31+
32+
# Extract major, minor, patch and qualifier from version text
33+
# Parse a version string "X.Y.Z[-qualifier]" and outputs
34+
# version parts in ${OUPUT_PREFIX}_MAJOR, _MINOR, _PATCH, _QUALIFIER.
35+
# If parse succed then ${OUPUT_PREFIX}_FOUND is TRUE.
36+
MACRO(jsoncpp_parse_version VERSION_TEXT OUPUT_PREFIX)
37+
SET(VERSION_REGEX "[0-9]+\\.[0-9]+\\.[0-9]+(-[a-zA-Z0-9_]+)?")
38+
IF( ${VERSION_TEXT} MATCHES ${VERSION_REGEX} )
39+
STRING(REGEX MATCHALL "[0-9]+|-([A-Za-z0-9_]+)" VERSION_PARTS ${VERSION_TEXT})
40+
list(APPEND VERSION_PARTS "") # empty qualifier to handle no qualifier case
41+
LIST(GET VERSION_PARTS 0 ${OUPUT_PREFIX}_MAJOR)
42+
LIST(GET VERSION_PARTS 1 ${OUPUT_PREFIX}_MINOR)
43+
LIST(GET VERSION_PARTS 2 ${OUPUT_PREFIX}_PATCH)
44+
LIST(GET VERSION_PARTS 3 ${OUPUT_PREFIX}_QUALIFIER)
45+
set_using_dynamic_name( "${OUPUT_PREFIX}_FOUND" TRUE )
46+
ELSE( ${VERSION_TEXT} MATCHES ${VERSION_REGEX} )
47+
set_using_dynamic_name( "${OUPUT_PREFIX}_FOUND" FALSE )
48+
ENDIF( ${VERSION_TEXT} MATCHES ${VERSION_REGEX} )
49+
ENDMACRO(jsoncpp_parse_version)
50+
51+
# Read out version from "version" file
52+
FILE(STRINGS "version" JSONCPP_VERSION)
53+
54+
jsoncpp_parse_version( ${JSONCPP_VERSION} JSONCPP_VERSION )
55+
IF(NOT JSONCPP_VERSION_FOUND)
56+
MESSAGE(FATAL_ERROR "Failed to parse version string properly. Expect X.Y.Z[-qualifier]")
57+
ENDIF(NOT JSONCPP_VERSION_FOUND)
58+
59+
MESSAGE(STATUS "JsonCpp Version: ${JSONCPP_VERSION_MAJOR}.${JSONCPP_VERSION_MINOR}.${JSONCPP_VERSION_PATCH}${JSONCPP_VERSION_QUALIFIER}")
60+
# File version.h is only regenerated on CMake configure step
61+
CONFIGURE_FILE( "${PROJECT_SOURCE_DIR}/src/lib_json/version.h.in"
62+
"${PROJECT_SOURCE_DIR}/include/json/version.h" )
63+
64+
# Include our configuration header
65+
INCLUDE_DIRECTORIES( ${CMAKE_SOURCE_DIR}/include )
66+
67+
# Build the different applications
68+
ADD_SUBDIRECTORY( src )
69+
70+
#install the includes
71+
ADD_SUBDIRECTORY( include )

NEWS.txt

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,21 +32,32 @@ New in SVN
3232
maxInt64 to figure out whether it is approximately representable.
3333

3434
* Value
35-
- Patch #3393345: BOOST_FOREACH compatibility. Made Json::iterator more
35+
- Patch #10: BOOST_FOREACH compatibility. Made Json::iterator more
3636
standard compliant, added missing iterator_category and value_type
3737
typedefs (contribued by Robert A. Iannucci).
3838

3939
* Compilation
4040

41-
- Patch #3474563: added missing JSON_API on some classes causing link issues
41+
- New CMake based build system. Based in part on contribution from
42+
Igor Okulist and Damien Buhl (Patch #14).
43+
44+
- New header json/version.h now contains version number macros
45+
(JSONCPP_VERSION_MAJOR, JSONCPP_VERSION_MINOR, JSONCPP_VERSION_PATCH
46+
and JSONCPP_VERSION_HEXA).
47+
48+
- Patch #11: added missing JSON_API on some classes causing link issues
4249
when building as a dynamic library on Windows
4350
(contributed by Francis Bolduc).
51+
52+
- Visual Studio DLL: suppressed warning "C4251: <data member>: <type>
53+
needs to have dll-interface to be used by..." via pragma push/pop
54+
in json-cpp headers.
4455

4556
* Bug fixes
46-
- Patch #3539678: Copy constructor does not initialize allocated_ for stringValue
57+
- Patch #15: Copy constructor does not initialize allocated_ for stringValue
4758
(contributed by rmongia).
4859

49-
- Patch #3600941: Missing field copy in Json::Value::iterator causing infinite
60+
- Patch #16: Missing field copy in Json::Value::iterator causing infinite
5061
loop when using experimental internal map (#define JSON_VALUE_USE_INTERNAL_MAP)
5162
(contributed by Ming-Lin Kao).
5263

README.txt

Lines changed: 58 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,65 @@ making it a convenient format to store user input files.
1313

1414
Unserialization parsing is user friendly and provides precise error reports.
1515

16+
* Using json-cpp in your project:
17+
===============================
18+
19+
The recommended approach to integrate json-cpp in your project is to
20+
build the the amalgamated source (a single .cpp) with your own build
21+
system. This ensures compilation flags consistency and ABI compatibility.
22+
23+
See section "Generating amalgamated source and header" to generate them
24+
from the source distribution.
25+
26+
Directory include/ should be added to your compiler include path.
27+
json-cpp headers should be included as follow:
28+
29+
#include <json/json.h>
30+
31+
If json-cpp was build as a dynamic library on Windows, then your project
32+
need to define macro "JSON_DLL" to JSON_API should import exported symbols.
33+
34+
* Building/Testing with new CMake build system:
35+
=============================================
36+
37+
CMake is a C++ Makefiles/Solution generator that can be downloaded from:
38+
http://www.cmake.org
39+
40+
It is usually available on most Linux system as package. On Ubuntu:
41+
sudo apt-get install cmake
42+
43+
Notes that python is also required to run JSON reader/writer tests. If
44+
missing, the build will skip running those tests.
45+
46+
When running CMake, a few parameters are required:
47+
- a build directory where the makefiles/solution are generated. It is
48+
also used to store objects, libraries and executables files.
49+
- the generator to use: makefiles or Visual Studio solution? What version
50+
or Visual Studio, 32 or 64 bits solution?
51+
52+
Generating solution/makefiles using cmake-gui:
53+
- Makes "source code" points the source directory
54+
- Makes "where to build the binary" points to the directory to use for
55+
the build.
56+
- Click on the "Grouped" check box
57+
- Review JsonCpp build option (tick JSONCPP_LIB_BUILD_SHARED to build as
58+
a dynamic library)
59+
- Click configure button at the bottom, then the generate button.
60+
- The generated solution/makefiles can be found in the binary directory.
61+
62+
Alternatively, from the command-line on Unix in the source directory:
63+
64+
mkdir -p ../build/debug
65+
cmake -DCMAKE_BUILD_TYPE=debug -DJSONCPP_LIB_BUILD_SHARED=OFF -G "Unix Makefiles" ../build/debug
66+
(cd ../build/debug && make)
67+
68+
Running "cmake -h" will display the list of available generators (passed as -G option).
69+
70+
By default CMake hides compilation command-line. This can be modified by specifying:
71+
-DCMAKE_VERBOSE_MAKEFILE=true when generating makefiles.
1672

17-
* Building/Testing:
18-
=================
73+
* Building/Testing with the legacy build system based on SCons:
74+
=============================================================
1975

2076
JsonCpp uses Scons (http://www.scons.org) as a build system. Scons requires
2177
python to be installed (http://www.python.org).
@@ -47,7 +103,6 @@ to do so.
47103

48104
and TARGET may be:
49105
check: build library and run unit tests.
50-
51106

52107
* Running the test manually:
53108
==========================
@@ -115,15 +170,6 @@ The amalgamated sources are generated by concatenating JsonCpp source in the
115170
correct order and defining macro JSON_IS_AMALGAMATION to prevent inclusion
116171
of other headers.
117172

118-
* Using json-cpp in your project:
119-
===============================
120-
121-
include/ should be added to your compiler include path. jsoncpp headers
122-
should be included as follow:
123-
124-
#include <json/json.h>
125-
126-
127173
* Adding a reader/writer test:
128174
============================
129175

amalgamate.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ def amalgamate_source( source_top_dir=None,
6666
header.add_text( '/// If defined, indicates that the source file is amalgated' )
6767
header.add_text( '/// to prevent private header inclusion.' )
6868
header.add_text( '#define JSON_IS_AMALGAMATION' )
69+
header.add_file( 'include/json/version.h' )
6970
header.add_file( 'include/json/config.h' )
7071
header.add_file( 'include/json/forwards.h' )
7172
header.add_file( 'include/json/features.h' )

include/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
FILE(GLOB INCLUDE_FILES "json/*.h")
2+
INSTALL(FILES ${INCLUDE_FILES} DESTINATION include/json)

include/json/config.h

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,17 @@
4646
# ifdef JSON_IN_CPPTL
4747
# define JSON_API CPPTL_API
4848
# elif defined(JSON_DLL_BUILD)
49-
# define JSON_API __declspec(dllexport)
49+
# if defined(_MSC_VER)
50+
# define JSON_API __declspec(dllexport)
51+
# define JSONCPP_DISABLE_DLL_INTERFACE_WARNING
52+
# endif // if defined(_MSC_VER)
5053
# elif defined(JSON_DLL)
51-
# define JSON_API __declspec(dllimport)
52-
# else
54+
# if defined(_MSC_VER)
55+
# define JSON_API __declspec(dllimport)
56+
# define JSONCPP_DISABLE_DLL_INTERFACE_WARNING
57+
# endif // if defined(_MSC_VER)
58+
# endif // ifdef JSON_IN_CPPTL
59+
# if !defined(JSON_API)
5360
# define JSON_API
5461
# endif
5562

include/json/reader.h

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,13 @@
1414
# include <stack>
1515
# include <string>
1616

17+
// Disable warning C4251: <data member>: <type> needs to have dll-interface to be used by...
18+
#if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING)
19+
# pragma warning(push)
20+
# pragma warning(disable:4251)
21+
#endif // if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING)
22+
23+
1724
namespace Json {
1825

1926
/** \brief Unserialize a <a HREF="http://www.json.org">JSON</a> document into a Value.
@@ -206,8 +213,13 @@ namespace Json {
206213
\throw std::exception on parse error.
207214
\see Json::operator<<()
208215
*/
209-
std::istream& operator>>( std::istream&, Value& );
216+
JSON_API std::istream& operator>>( std::istream&, Value& );
210217

211218
} // namespace Json
212219

220+
#if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING)
221+
# pragma warning(pop)
222+
#endif // if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING)
223+
224+
213225
#endif // CPPTL_JSON_READER_H_INCLUDED

include/json/value.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,13 @@
2121
# include <cpptl/forwards.h>
2222
# endif
2323

24+
// Disable warning C4251: <data member>: <type> needs to have dll-interface to be used by...
25+
#if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING)
26+
# pragma warning(push)
27+
# pragma warning(disable:4251)
28+
#endif // if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING)
29+
30+
2431
/** \brief JSON (JavaScript Object Notation).
2532
*/
2633
namespace Json {
@@ -1109,4 +1116,9 @@ class DefaultValueArrayAllocator : public ValueArrayAllocator
11091116
} // namespace Json
11101117

11111118

1119+
#if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING)
1120+
# pragma warning(pop)
1121+
#endif // if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING)
1122+
1123+
11121124
#endif // CPPTL_JSON_H_INCLUDED

include/json/version.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// DO NOT EDIT. This file is generated by CMake from "version"
2+
// and "version.h.in" files.
3+
// Run CMake configure step to update it.
4+
#ifndef JSON_VERSION_H_INCLUDED
5+
# define JSON_VERSION_H_INCLUDED
6+
7+
# define JSONCPP_VERSION_STRING "0.6.0-dev"
8+
# define JSONCPP_VERSION_MAJOR 0
9+
# define JSONCPP_VERSION_MINOR 6
10+
# define JSONCPP_VERSION_PATCH 0
11+
# define JSONCPP_VERSION_QUALIFIER -dev
12+
# define JSONCPP_VERSION_HEXA ((JSONCPP_VERSION_MAJOR << 24) | (JSONCPP_VERSION_MINOR << 16) | (JSONCPP_VERSION_PATCH << 8))
13+
14+
#endif // JSON_VERSION_H_INCLUDED

include/json/writer.h

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,13 @@
1212
# include <vector>
1313
# include <string>
1414

15+
// Disable warning C4251: <data member>: <type> needs to have dll-interface to be used by...
16+
#if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING)
17+
# pragma warning(push)
18+
# pragma warning(disable:4251)
19+
#endif // if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING)
20+
21+
1522
namespace Json {
1623

1724
class Value;
@@ -183,10 +190,14 @@ namespace Json {
183190

184191
/// \brief Output using the StyledStreamWriter.
185192
/// \see Json::operator>>()
186-
std::ostream& operator<<( std::ostream&, const Value &root );
193+
JSON_API std::ostream& operator<<( std::ostream&, const Value &root );
187194

188195
} // namespace Json
189196

190197

198+
#if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING)
199+
# pragma warning(pop)
200+
#endif // if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING)
201+
191202

192203
#endif // JSON_WRITER_H_INCLUDED

src/CMakeLists.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
ADD_SUBDIRECTORY(lib_json)
2+
IF(JSONCPP_WITH_TESTS)
3+
ADD_SUBDIRECTORY(jsontestrunner)
4+
ADD_SUBDIRECTORY(test_lib_json)
5+
ENDIF(JSONCPP_WITH_TESTS)

src/jsontestrunner/CMakeLists.txt

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
FIND_PACKAGE(PythonInterp 2.6 REQUIRED)
2+
3+
IF(JSONCPP_LIB_BUILD_SHARED)
4+
ADD_DEFINITIONS( -DJSON_DLL )
5+
ENDIF(JSONCPP_LIB_BUILD_SHARED)
6+
7+
ADD_EXECUTABLE(jsontestrunner_exe
8+
main.cpp
9+
)
10+
TARGET_LINK_LIBRARIES(jsontestrunner_exe jsoncpp_lib)
11+
SET_TARGET_PROPERTIES(jsontestrunner_exe PROPERTIES OUTPUT_NAME jsontestrunner_exe)
12+
13+
IF(PYTHONINTERP_FOUND)
14+
# Run end to end parser/writer tests
15+
GET_PROPERTY(JSONTESTRUNNER_EXE_PATH TARGET jsontestrunner_exe PROPERTY LOCATION)
16+
SET(TEST_DIR ${CMAKE_CURRENT_SOURCE_DIR}/../../test)
17+
SET(RUNJSONTESTS_PATH ${TEST_DIR}/runjsontests.py)
18+
ADD_CUSTOM_TARGET(jsoncpp_readerwriter_tests ALL
19+
"${PYTHON_EXECUTABLE}" -B "${RUNJSONTESTS_PATH}" "${JSONTESTRUNNER_EXE_PATH}" "${TEST_DIR}/data"
20+
DEPENDS jsontestrunner_exe jsoncpp_test
21+
)
22+
ADD_CUSTOM_TARGET(jsoncpp_check DEPENDS jsoncpp_readerwriter_tests)
23+
ENDIF(PYTHONINTERP_FOUND)

0 commit comments

Comments
 (0)