blob: 9a89f6e0e5a3a8003ce9f8151eb75b5db98c8f71 [file] [log] [blame]
Christopher Dunn295e73f2015-03-05 18:14:58 -06001# vim: et ts=4 sts=4 sw=4 tw=0
2
sergzubae5a56f2014-10-03 16:40:58 +04003CMAKE_MINIMUM_REQUIRED(VERSION 2.8.5)
Baptiste Lepilleureafd7022013-05-08 20:21:11 +00004PROJECT(jsoncpp)
5ENABLE_TESTING()
6
Christopher Dunn99980942015-02-26 09:39:40 -06007OPTION(JSONCPP_WITH_TESTS "Compile and (for jsoncpp_check) run JsonCpp test executables" ON)
Baptiste Lepilleureafd7022013-05-08 20:21:11 +00008OPTION(JSONCPP_WITH_POST_BUILD_UNITTEST "Automatically run unit-tests as a post build step" ON)
Baptiste Lepilleur700b3802013-05-09 18:42:33 +00009OPTION(JSONCPP_WITH_WARNING_AS_ERROR "Force compilation to fail if a warning occurs" OFF)
Jörg Krause48bfe912015-10-27 22:45:17 +010010OPTION(JSONCPP_WITH_STRICT_ISO "Issue all the warnings demanded by strict ISO C and ISO C++" ON)
Jonas Platte62708582014-09-14 15:45:07 +020011OPTION(JSONCPP_WITH_PKGCONFIG_SUPPORT "Generate and install .pc files" ON)
Yu Xiaoleidc84d962014-11-04 16:33:25 +080012OPTION(JSONCPP_WITH_CMAKE_PACKAGE "Generate and install cmake package files" OFF)
Gaurav30bb4cc2015-04-23 18:39:00 +053013OPTION(BUILD_SHARED_LIBS "Build jsoncpp_lib as a shared library." OFF)
14OPTION(BUILD_STATIC_LIBS "Build jsoncpp_lib static library." ON)
Baptiste Lepilleureafd7022013-05-08 20:21:11 +000015
16# Ensures that CMAKE_BUILD_TYPE is visible in cmake-gui on Unix
17IF(NOT WIN32)
18 IF(NOT CMAKE_BUILD_TYPE)
19 SET(CMAKE_BUILD_TYPE Release CACHE STRING
20 "Choose the type of build, options are: None Debug Release RelWithDebInfo MinSizeRel Coverage."
21 FORCE)
Robert Dailey37aaaec2015-08-06 10:17:09 -050022 ENDIF()
23ENDIF()
Baptiste Lepilleureafd7022013-05-08 20:21:11 +000024
Martyn Gigg717c7912015-06-29 19:20:08 +010025SET(DEBUG_LIBNAME_SUFFIX "" CACHE STRING "Optional suffix to append to the library name for a debug build")
Rémi Verscheldef8a3a592014-12-01 23:44:08 +010026SET(LIB_SUFFIX "" CACHE STRING "Optional arch-dependent suffix for the library installation directory")
27
Yu Xiaolei72e52232014-11-05 13:18:16 +080028SET(RUNTIME_INSTALL_DIR bin
Yu Xiaolei1c3a20d2014-11-04 12:44:48 +080029 CACHE PATH "Install dir for executables and dlls")
Michał Górnye6f1cff2015-05-19 17:32:31 +020030SET(ARCHIVE_INSTALL_DIR ${CMAKE_INSTALL_PREFIX}/lib${LIB_SUFFIX}
Yu Xiaolei1c3a20d2014-11-04 12:44:48 +080031 CACHE PATH "Install dir for static libraries")
Michał Górnye6f1cff2015-05-19 17:32:31 +020032SET(LIBRARY_INSTALL_DIR ${CMAKE_INSTALL_PREFIX}/lib${LIB_SUFFIX}
Yu Xiaolei1c3a20d2014-11-04 12:44:48 +080033 CACHE PATH "Install dir for shared libraries")
Michał Górnye6f1cff2015-05-19 17:32:31 +020034SET(INCLUDE_INSTALL_DIR ${CMAKE_INSTALL_PREFIX}/include
Yu Xiaolei1c3a20d2014-11-04 12:44:48 +080035 CACHE PATH "Install dir for headers")
Rémi Verscheldef8a3a592014-12-01 23:44:08 +010036SET(PACKAGE_INSTALL_DIR lib${LIB_SUFFIX}/cmake
Yu Xiaoleidc84d962014-11-04 16:33:25 +080037 CACHE PATH "Install dir for cmake package config files")
38MARK_AS_ADVANCED( RUNTIME_INSTALL_DIR ARCHIVE_INSTALL_DIR INCLUDE_INSTALL_DIR PACKAGE_INSTALL_DIR )
Yu Xiaolei1c3a20d2014-11-04 12:44:48 +080039
Baptiste Lepilleureafd7022013-05-08 20:21:11 +000040# Set variable named ${VAR_NAME} to value ${VALUE}
41FUNCTION(set_using_dynamic_name VAR_NAME VALUE)
42 SET( "${VAR_NAME}" "${VALUE}" PARENT_SCOPE)
Robert Dailey37aaaec2015-08-06 10:17:09 -050043ENDFUNCTION()
Baptiste Lepilleureafd7022013-05-08 20:21:11 +000044
Christopher Dunn0375af22014-09-18 10:06:14 -070045# Extract major, minor, patch from version text
46# Parse a version string "X.Y.Z" and outputs
47# version parts in ${OUPUT_PREFIX}_MAJOR, _MINOR, _PATCH.
Jonas Platte69c324e2014-09-17 20:37:59 +020048# If parse succeeds then ${OUPUT_PREFIX}_FOUND is TRUE.
Christopher Dunn9aa46812014-09-16 12:26:50 -070049MACRO(jsoncpp_parse_version VERSION_TEXT OUPUT_PREFIX)
50 SET(VERSION_REGEX "[0-9]+\\.[0-9]+\\.[0-9]+(-[a-zA-Z0-9_]+)?")
51 IF( ${VERSION_TEXT} MATCHES ${VERSION_REGEX} )
52 STRING(REGEX MATCHALL "[0-9]+|-([A-Za-z0-9_]+)" VERSION_PARTS ${VERSION_TEXT})
Christopher Dunn9aa46812014-09-16 12:26:50 -070053 LIST(GET VERSION_PARTS 0 ${OUPUT_PREFIX}_MAJOR)
54 LIST(GET VERSION_PARTS 1 ${OUPUT_PREFIX}_MINOR)
55 LIST(GET VERSION_PARTS 2 ${OUPUT_PREFIX}_PATCH)
Jonas Platte69c324e2014-09-17 20:37:59 +020056 set_using_dynamic_name( "${OUPUT_PREFIX}_FOUND" TRUE )
Christopher Dunn9aa46812014-09-16 12:26:50 -070057 ELSE( ${VERSION_TEXT} MATCHES ${VERSION_REGEX} )
Jonas Platte69c324e2014-09-17 20:37:59 +020058 set_using_dynamic_name( "${OUPUT_PREFIX}_FOUND" FALSE )
Robert Dailey37aaaec2015-08-06 10:17:09 -050059 ENDIF()
60ENDMACRO()
Christopher Dunn9aa46812014-09-16 12:26:50 -070061
62# Read out version from "version" file
Christopher Dunn295e73f2015-03-05 18:14:58 -060063#FILE(STRINGS "version" JSONCPP_VERSION)
64#SET( JSONCPP_VERSION_MAJOR X )
65#SET( JSONCPP_VERSION_MINOR Y )
66#SET( JSONCPP_VERSION_PATCH Z )
Christopher Dunn9a4b1e32016-02-06 10:26:59 -060067SET( JSONCPP_VERSION 1.7.0 )
Christopher Dunn9aa46812014-09-16 12:26:50 -070068jsoncpp_parse_version( ${JSONCPP_VERSION} JSONCPP_VERSION )
Christopher Dunn295e73f2015-03-05 18:14:58 -060069#IF(NOT JSONCPP_VERSION_FOUND)
70# MESSAGE(FATAL_ERROR "Failed to parse version string properly. Expect X.Y.Z")
71#ENDIF(NOT JSONCPP_VERSION_FOUND)
dawescae564652016-03-14 19:11:02 -050072SET( JSONCPP_USE_SECURE_MEMORY "0" CACHE STRING "-D...=1 to use memory-wiping allocator for STL" )
Christopher Dunn9aa46812014-09-16 12:26:50 -070073
Christopher Dunn0375af22014-09-18 10:06:14 -070074MESSAGE(STATUS "JsonCpp Version: ${JSONCPP_VERSION_MAJOR}.${JSONCPP_VERSION_MINOR}.${JSONCPP_VERSION_PATCH}")
Christopher Dunn9aa46812014-09-16 12:26:50 -070075# File version.h is only regenerated on CMake configure step
76CONFIGURE_FILE( "${PROJECT_SOURCE_DIR}/src/lib_json/version.h.in"
Christopher Dunn295e73f2015-03-05 18:14:58 -060077 "${PROJECT_SOURCE_DIR}/include/json/version.h"
78 NEWLINE_STYLE UNIX )
79CONFIGURE_FILE( "${PROJECT_SOURCE_DIR}/version.in"
80 "${PROJECT_SOURCE_DIR}/version"
81 NEWLINE_STYLE UNIX )
Christopher Dunn9aa46812014-09-16 12:26:50 -070082
Baptiste Lepilleur700b3802013-05-09 18:42:33 +000083macro(UseCompilationWarningAsError)
84 if ( MSVC )
85 # Only enabled in debug because some old versions of VS STL generate
86 # warnings when compiled in release configuration.
87 set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} /WX ")
Jörg Krause48bfe912015-10-27 22:45:17 +010088 elseif (CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
Jörg Krause7c930312015-10-29 09:19:41 +010089 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Werror")
Jörg Krause48bfe912015-10-27 22:45:17 +010090 if (JSONCPP_WITH_STRICT_ISO)
91 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -pedantic-errors")
92 endif ()
Robert Dailey37aaaec2015-08-06 10:17:09 -050093 endif()
Jonas Platte69c324e2014-09-17 20:37:59 +020094endmacro()
Baptiste Lepilleur700b3802013-05-09 18:42:33 +000095
Baptiste Lepilleureafd7022013-05-08 20:21:11 +000096# Include our configuration header
Aleksandr Derbenevb3deb612014-08-09 22:21:33 +040097INCLUDE_DIRECTORIES( ${jsoncpp_SOURCE_DIR}/include )
Baptiste Lepilleureafd7022013-05-08 20:21:11 +000098
Baptiste Lepilleur700b3802013-05-09 18:42:33 +000099if ( MSVC )
100 # Only enabled in debug because some old versions of VS STL generate
101 # unreachable code warning when compiled in release configuration.
102 set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} /W4 ")
Robert Dailey37aaaec2015-08-06 10:17:09 -0500103endif()
Baptiste Lepilleur700b3802013-05-09 18:42:33 +0000104
Christopher Dunnec727e22015-01-20 13:32:38 -0600105if (CMAKE_CXX_COMPILER_ID MATCHES "Clang")
106 # using regular Clang or AppleClang
Christopher Dunn6b562c82016-02-07 11:19:04 -0600107 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -Wall -Wconversion -Wshadow -Werror=conversion -Werror=sign-compare")
Robert Dailey37aaaec2015-08-06 10:17:09 -0500108elseif (CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
Christopher Dunnec727e22015-01-20 13:32:38 -0600109 # using GCC
Christopher Dunnd4513fc2016-02-06 09:25:20 -0600110 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -Wall -Wconversion -Wshadow -Wextra -Werror=conversion")
Christopher Dunn7275e3c2015-07-12 12:49:57 -0500111 # not yet ready for -Wsign-conversion
Jörg Krause48bfe912015-10-27 22:45:17 +0100112
113 if (JSONCPP_WITH_STRICT_ISO AND NOT JSONCPP_WITH_WARNING_AS_ERROR)
114 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -pedantic")
115 endif ()
Christopher Dunnec727e22015-01-20 13:32:38 -0600116endif()
117
Christopher Dunnd1a2b942015-10-19 12:10:58 -0500118find_program(CCACHE_FOUND ccache)
119if(CCACHE_FOUND)
120 set_property(GLOBAL PROPERTY RULE_LAUNCH_COMPILE ccache)
121 set_property(GLOBAL PROPERTY RULE_LAUNCH_LINK ccache)
122endif(CCACHE_FOUND)
123
Baptiste Lepilleur700b3802013-05-09 18:42:33 +0000124IF(JSONCPP_WITH_WARNING_AS_ERROR)
125 UseCompilationWarningAsError()
Robert Dailey37aaaec2015-08-06 10:17:09 -0500126ENDIF()
Baptiste Lepilleur700b3802013-05-09 18:42:33 +0000127
Jonas Platte62708582014-09-14 15:45:07 +0200128IF(JSONCPP_WITH_PKGCONFIG_SUPPORT)
129 CONFIGURE_FILE(
130 "pkg-config/jsoncpp.pc.in"
131 "pkg-config/jsoncpp.pc"
132 @ONLY)
ds2832cca1cd2015-12-05 12:18:12 +0000133 INSTALL(FILES "${CMAKE_CURRENT_BINARY_DIR}/pkg-config/jsoncpp.pc"
Rémi Verscheldef8a3a592014-12-01 23:44:08 +0100134 DESTINATION "${CMAKE_INSTALL_PREFIX}/lib${LIB_SUFFIX}/pkgconfig")
Robert Dailey37aaaec2015-08-06 10:17:09 -0500135ENDIF()
Jonas Platte62708582014-09-14 15:45:07 +0200136
Yu Xiaoleidc84d962014-11-04 16:33:25 +0800137IF(JSONCPP_WITH_CMAKE_PACKAGE)
138 INSTALL(EXPORT jsoncpp
139 DESTINATION ${PACKAGE_INSTALL_DIR}/jsoncpp
140 FILE jsoncppConfig.cmake)
Robert Dailey37aaaec2015-08-06 10:17:09 -0500141ENDIF()
Yu Xiaoleidc84d962014-11-04 16:33:25 +0800142
Baptiste Lepilleureafd7022013-05-08 20:21:11 +0000143# Build the different applications
144ADD_SUBDIRECTORY( src )
145
146#install the includes
147ADD_SUBDIRECTORY( include )