Skip to content

Commit 8ae1c97

Browse files
committed
Add initial CMake support
* Support for both in-source and out-of-source builds * Set library version to 0.12 to map Debian package * Add separate options to build tests, examples and documentation * Add pkgconfig lookup support (if installed with `make install`) * Add CMake lookup support (if isntalled with `make install`) * Add Google Test Source lookup * Add CTest support for running tests (use `make test` or `ctest -V`)
1 parent 8d4405c commit 8ae1c97

11 files changed

+212
-1
lines changed

CMakeLists.txt

+85
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
CMAKE_MINIMUM_REQUIRED(VERSION 2.8.11)
2+
SET(CMAKE_MODULE_PATH ${CMAKE_SOURCE_DIR}/CMakeModules)
3+
4+
PROJECT(RapidJSON CXX)
5+
6+
set(LIB_MAJOR_VERSION "0")
7+
set(LIB_MINOR_VERSION "12")
8+
set(LIB_PATCH_VERSION "0")
9+
set(LIB_VERSION_STRING "${LIB_MAJOR_VERSION}.${LIB_MINOR_VERSION}.${LIB_PATCH_VERSION}")
10+
11+
# compile in release with debug info mode by default
12+
SET(CMAKE_BUILD_TYPE RelWithDebInfo CACHE STRING "Build Type")
13+
14+
option(RAPIDJSON_BUILD_DOC "Build rapidjson documentation." ON)
15+
option(RAPIDJSON_BUILD_EXAMPLES "Build rapidjson examples." ON)
16+
option(RAPIDJSON_BUILD_TESTS "Build rapidjson perftests and unittests." ON)
17+
18+
#add extra search paths for libraries and includes
19+
SET(INCLUDE_INSTALL_DIR "${CMAKE_INSTALL_PREFIX}/include" CACHE PATH "The directory the headers are installed in")
20+
SET(LIB_INSTALL_DIR "${CMAKE_INSTALL_PREFIX}/lib" CACHE STRING "Directory where lib will install")
21+
SET(DOC_INSTALL_DIR "${CMAKE_INSTALL_PREFIX}/share/doc/${PROJECT_NAME}" CACHE PATH "Path to the documentation")
22+
23+
IF(UNIX OR CYGWIN)
24+
SET(_CMAKE_INSTALL_DIR "${LIB_INSTALL_DIR}/cmake/${PROJECT_NAME}")
25+
ELSEIF(WIN32)
26+
SET(_CMAKE_INSTALL_DIR "${CMAKE_INSTALL_PREFIX}/cmake")
27+
ENDIF()
28+
SET(CMAKE_INSTALL_DIR "${_CMAKE_INSTALL_DIR}" CACHE PATH "The directory cmake fiels are installed in")
29+
30+
31+
include_directories(${CMAKE_SOURCE_DIR}/include)
32+
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${RAPIDJSON_CXX_FLAGS}")
33+
34+
if(RAPIDJSON_BUILD_DOC)
35+
add_subdirectory(doc)
36+
endif()
37+
38+
if(RAPIDJSON_BUILD_EXAMPLES)
39+
add_subdirectory(example)
40+
endif()
41+
42+
if(RAPIDJSON_BUILD_TESTS)
43+
add_subdirectory(test)
44+
include(CTest)
45+
endif()
46+
47+
# pkg-config
48+
IF (UNIX OR CYGWIN)
49+
CONFIGURE_FILE (${CMAKE_CURRENT_SOURCE_DIR}/${PROJECT_NAME}.pc.in
50+
${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}.pc
51+
@ONLY)
52+
INSTALL (FILES ${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}.pc
53+
DESTINATION "${LIB_INSTALL_DIR}/pkgconfig"
54+
COMPONENT pkgconfig)
55+
ENDIF()
56+
57+
install(FILES readme.md
58+
DESTINATION "${DOC_INSTALL_DIR}"
59+
COMPONENT doc)
60+
61+
install(DIRECTORY include/rapidjson
62+
DESTINATION "${INCLUDE_INSTALL_DIR}"
63+
COMPONENT dev)
64+
65+
install(DIRECTORY example/
66+
DESTINATION "${DOC_INSTALL_DIR}/examples"
67+
COMPONENT examples)
68+
69+
# Provide config and version files to be used by other applications
70+
# ===============================
71+
72+
export(PACKAGE ${PROJECT_NAME})
73+
74+
# cmake-modules
75+
CONFIGURE_FILE(${PROJECT_NAME}Config.cmake.in
76+
${PROJECT_NAME}Config.cmake
77+
@ONLY)
78+
CONFIGURE_FILE(${PROJECT_NAME}ConfigVersion.cmake.in
79+
${PROJECT_NAME}ConfigVersion.cmake
80+
@ONLY)
81+
INSTALL(FILES
82+
${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}Config.cmake
83+
${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}ConfigVersion.cmake
84+
DESTINATION "${CMAKE_INSTALL_DIR}"
85+
COMPONENT dev)

CMakeModules/FindGTestSrc.cmake

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
SET(GTEST_SEARCH_PATH
2+
"${GTEST_SOURCE_DIR}"
3+
"${CMAKE_SOURCE_DIR}/thirdparty/gtest")
4+
5+
IF(UNIX)
6+
LIST(INSERT GTEST_SEARCH_PATH 1 "/usr/src/gtest")
7+
ENDIF()
8+
9+
FIND_PATH(GTEST_SOURCE_DIR
10+
NAMES CMakeLists.txt src/gtest_main.cc
11+
PATHS ${GTEST_SEARCH_PATH})
12+
13+
# Debian installs gtest include directory in /usr/include, thus need to look
14+
# for include directory separately from source directory.
15+
FIND_PATH(GTEST_INCLUDE_DIR
16+
NAMES gtest/gtest.h
17+
PATH_SUFFIXES include
18+
PATHS ${GTEST_SEARCH_PATH})
19+
20+
find_package_handle_standard_args(GTestSrc DEFAULT_MSG
21+
GTEST_SOURCE_DIR
22+
GTEST_INCLUDE_DIR)

RapidJSON.pc.in

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Name: @PROJECT_NAME@
2+
Description: RapidJSON is a JSON parser and generator for C++ inspired by RapidXml.
3+
Version: @LIB_VERSION_STRING@
4+
Cflags: -I${includedir}

RapidJSONConfig.cmake.in

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
get_filename_component(RAPIDJSON_CMAKE_DIR "${CMAKE_CURRENT_LIST_FILE}" PATH)
2+
set(RAPIDJSON_INCLUDE_DIRS "@INCLUDE_INSTALL_DIR@")
3+
message(STATUS "RapidJSON found. Headers: ${RAPIDJSON_INCLUDE_DIRS}")

RapidJSONConfigVersion.cmake.in

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
SET(PACKAGE_VERSION "@LIB_VERSION_STRING@")
2+
3+
IF (PACKAGE_FIND_VERSION VERSION_EQUAL PACKAGE_VERSION)
4+
SET(PACKAGE_VERSION_EXACT "true")
5+
ENDIF (PACKAGE_FIND_VERSION VERSION_EQUAL PACKAGE_VERSION)
6+
IF (NOT PACKAGE_FIND_VERSION VERSION_GREATER PACKAGE_VERSION)
7+
SET(PACKAGE_VERSION_COMPATIBLE "true")
8+
ELSE (NOT PACKAGE_FIND_VERSION VERSION_GREATER PACKAGE_VERSION)
9+
SET(PACKAGE_VERSION_UNSUITABLE "true")
10+
ENDIF (NOT PACKAGE_FIND_VERSION VERSION_GREATER PACKAGE_VERSION)

doc/CMakeLists.txt

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
find_package(Doxygen)
2+
3+
IF(NOT DOXYGEN_FOUND)
4+
MESSAGE(STATUS "No Doxygen found. Documentation won't be built")
5+
ELSE()
6+
file(GLOB SOURCES ${CMAKE_SOURCE_DIR}/include/*)
7+
file(GLOB MARKDOWN_DOC ${CMAKE_SOURCE_DIR}/doc/*.md)
8+
list(APPEND MARKDOWN_DOC ${CMAKE_SOURCE_DIR}/readme.md)
9+
10+
CONFIGURE_FILE(Doxyfile.in Doxyfile @ONLY)
11+
12+
add_custom_command(OUTPUT html
13+
COMMAND ${DOXYGEN_EXECUTABLE} ${CMAKE_CURRENT_BINARY_DIR}/Doxyfile
14+
COMMAND ${CMAKE_COMMAND} -E touch ${CMAKE_CURRENT_BINARY_DIR}/html
15+
DEPENDS ${MARKDOWN_DOC} ${SOURCES} ${CMAKE_CURRENT_BINARY_DIR}/Doxyfile
16+
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
17+
)
18+
19+
add_custom_target(doc ALL DEPENDS html)
20+
install(DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/html
21+
DESTINATION ${DOC_INSTALL_DIR}
22+
COMPONENT doc)
23+
ENDIF()

build/Doxyfile doc/Doxyfile.in

+1-1
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ PROJECT_LOGO =
5858
# entered, it will be relative to the location where doxygen was started. If
5959
# left blank the current directory will be used.
6060

61-
OUTPUT_DIRECTORY = ./doc
61+
OUTPUT_DIRECTORY = @CMAKE_CURRENT_BINARY_DIR@
6262

6363
# If the CREATE_SUBDIRS tag is set to YES, then doxygen will create 4096 sub-
6464
# directories (in 2 levels) under the output directory of each output format and

example/CMakeLists.txt

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Copyright (c) 2011 Milo Yip ([email protected])
2+
# Copyright (c) 2013 Rafal Jeczalik ([email protected])
3+
# Distributed under the MIT License (see license.txt file)
4+
5+
set(EXAMPLES
6+
capitalize
7+
condense
8+
messagereader
9+
pretty
10+
prettyauto
11+
serialize
12+
simpledom
13+
simplereader
14+
simplewriter
15+
tutorial)
16+
17+
foreach (example ${EXAMPLES})
18+
add_executable(${example}_ ${example}/${example}.cpp)
19+
endforeach()

test/CMakeLists.txt

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
find_package(GTestSrc)
2+
3+
IF(GTESTSRC_FOUND)
4+
enable_testing()
5+
6+
if (WIN32 AND (NOT CYGWIN) AND (NOT MINGW))
7+
set(gtest_disable_pthreads ON)
8+
endif()
9+
10+
add_subdirectory(${GTEST_SOURCE_DIR} ${CMAKE_BINARY_DIR}/googletest)
11+
include_directories(${GTEST_INCLUDE_DIR})
12+
13+
set(TEST_LIBRARIES gtest gtest_main)
14+
15+
add_subdirectory(perftest)
16+
add_subdirectory(unittest)
17+
ENDIF(GTESTSRC_FOUND)

test/perftest/CMakeLists.txt

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
set(PERFTEST_SOURCES
2+
misctest.cpp
3+
perftest.cpp
4+
platformtest.cpp
5+
rapidjsontest.cpp)
6+
7+
add_executable(perftest ${PERFTEST_SOURCES})
8+
target_link_libraries(perftest ${TEST_LIBRARIES})
9+
add_test(NAME perftest
10+
COMMAND ${CMAKE_CURRENT_BINARY_DIR}/perftest
11+
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}/bin)

test/unittest/CMakeLists.txt

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
set(UNITTEST_SOURCES
2+
documenttest.cpp
3+
encodedstreamtest.cpp
4+
encodingstest.cpp
5+
filestreamtest.cpp
6+
jsoncheckertest.cpp
7+
readertest.cpp
8+
unittest.cpp
9+
unittest.h
10+
valuetest.cpp
11+
writertest.cpp)
12+
13+
add_executable(unittest ${UNITTEST_SOURCES})
14+
target_link_libraries(unittest ${TEST_LIBRARIES})
15+
add_test(NAME unittest
16+
COMMAND ${CMAKE_CURRENT_BINARY_DIR}/unittest
17+
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}/bin)

0 commit comments

Comments
 (0)