1CMAKE_MINIMUM_REQUIRED(VERSION 2.8.5)
2PROJECT(jsoncpp)
3ENABLE_TESTING()
4
5OPTION(JSONCPP_WITH_TESTS "Compile and run JsonCpp test executables" ON)
6OPTION(JSONCPP_WITH_POST_BUILD_UNITTEST "Automatically run unit-tests as a post build step" ON)
7OPTION(JSONCPP_WITH_WARNING_AS_ERROR "Force compilation to fail if a warning occurs" OFF)
8OPTION(JSONCPP_WITH_PKGCONFIG_SUPPORT "Generate and install .pc files" ON)
9OPTION(JSONCPP_WITH_CMAKE_PACKAGE "Generate and install cmake package files" OFF)
10
11# Ensures that CMAKE_BUILD_TYPE is visible in cmake-gui on Unix
12IF(NOT WIN32)
13    IF(NOT CMAKE_BUILD_TYPE)
14        SET(CMAKE_BUILD_TYPE Release CACHE STRING
15            "Choose the type of build, options are: None Debug Release RelWithDebInfo MinSizeRel Coverage."
16            FORCE)
17    ENDIF(NOT CMAKE_BUILD_TYPE)
18ENDIF(NOT WIN32)
19
20SET(RUNTIME_INSTALL_DIR bin
21    CACHE PATH "Install dir for executables and dlls")
22SET(ARCHIVE_INSTALL_DIR lib
23    CACHE PATH "Install dir for static libraries")
24SET(LIBRARY_INSTALL_DIR lib
25    CACHE PATH "Install dir for shared libraries")
26SET(INCLUDE_INSTALL_DIR include
27    CACHE PATH "Install dir for headers")
28SET(PACKAGE_INSTALL_DIR lib/cmake
29    CACHE PATH "Install dir for cmake package config files")
30MARK_AS_ADVANCED( RUNTIME_INSTALL_DIR ARCHIVE_INSTALL_DIR INCLUDE_INSTALL_DIR PACKAGE_INSTALL_DIR )
31
32# This ensures shared DLL are in the same dir as executable on Windows.
33# Put all executables / libraries are in a project global directory.
34SET(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR}/lib
35    CACHE PATH "Single directory for all static libraries.")
36SET(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR}/lib
37    CACHE PATH "Single directory for all dynamic libraries on Unix.")
38SET(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR}/bin
39    CACHE PATH "Single directory for all executable and dynamic libraries on Windows.")
40MARK_AS_ADVANCED( CMAKE_RUNTIME_OUTPUT_DIRECTORY CMAKE_LIBRARY_OUTPUT_DIRECTORY CMAKE_ARCHIVE_OUTPUT_DIRECTORY )
41
42# Set variable named ${VAR_NAME} to value ${VALUE}
43FUNCTION(set_using_dynamic_name VAR_NAME VALUE)
44    SET( "${VAR_NAME}" "${VALUE}" PARENT_SCOPE)
45ENDFUNCTION(set_using_dynamic_name)
46
47# Extract major, minor, patch from version text
48# Parse a version string "X.Y.Z" and outputs
49# version parts in ${OUPUT_PREFIX}_MAJOR, _MINOR, _PATCH.
50# If parse succeeds then ${OUPUT_PREFIX}_FOUND is TRUE.
51MACRO(jsoncpp_parse_version VERSION_TEXT OUPUT_PREFIX)
52    SET(VERSION_REGEX "[0-9]+\\.[0-9]+\\.[0-9]+(-[a-zA-Z0-9_]+)?")
53    IF( ${VERSION_TEXT} MATCHES ${VERSION_REGEX} )
54        STRING(REGEX MATCHALL "[0-9]+|-([A-Za-z0-9_]+)" VERSION_PARTS ${VERSION_TEXT})
55        LIST(GET VERSION_PARTS 0 ${OUPUT_PREFIX}_MAJOR)
56        LIST(GET VERSION_PARTS 1 ${OUPUT_PREFIX}_MINOR)
57        LIST(GET VERSION_PARTS 2 ${OUPUT_PREFIX}_PATCH)
58        set_using_dynamic_name( "${OUPUT_PREFIX}_FOUND" TRUE )
59    ELSE( ${VERSION_TEXT} MATCHES ${VERSION_REGEX} )
60        set_using_dynamic_name( "${OUPUT_PREFIX}_FOUND" FALSE )
61    ENDIF( ${VERSION_TEXT} MATCHES ${VERSION_REGEX} )
62ENDMACRO(jsoncpp_parse_version)
63
64# Read out version from "version" file
65FILE(STRINGS "version" JSONCPP_VERSION)
66
67jsoncpp_parse_version( ${JSONCPP_VERSION} JSONCPP_VERSION )
68IF(NOT JSONCPP_VERSION_FOUND)
69    MESSAGE(FATAL_ERROR "Failed to parse version string properly. Expect X.Y.Z")
70ENDIF(NOT JSONCPP_VERSION_FOUND)
71
72MESSAGE(STATUS "JsonCpp Version: ${JSONCPP_VERSION_MAJOR}.${JSONCPP_VERSION_MINOR}.${JSONCPP_VERSION_PATCH}")
73# File version.h is only regenerated on CMake configure step
74CONFIGURE_FILE( "${PROJECT_SOURCE_DIR}/src/lib_json/version.h.in"
75                "${PROJECT_SOURCE_DIR}/include/json/version.h" )
76
77macro(UseCompilationWarningAsError)
78	if ( MSVC )
79        # Only enabled in debug because some old versions of VS STL generate
80        # warnings when compiled in release configuration.
81		set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} /WX ")
82	endif( MSVC )
83endmacro()
84
85# Include our configuration header
86INCLUDE_DIRECTORIES( ${jsoncpp_SOURCE_DIR}/include )
87
88if ( MSVC )
89    # Only enabled in debug because some old versions of VS STL generate
90    # unreachable code warning when compiled in release configuration.
91    set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} /W4 ")
92endif( MSVC )
93
94IF(JSONCPP_WITH_WARNING_AS_ERROR)
95    UseCompilationWarningAsError()
96ENDIF(JSONCPP_WITH_WARNING_AS_ERROR)
97
98IF(JSONCPP_WITH_PKGCONFIG_SUPPORT)
99	CONFIGURE_FILE(
100		"pkg-config/jsoncpp.pc.in"
101		"pkg-config/jsoncpp.pc"
102		@ONLY)
103	INSTALL(FILES "${CMAKE_BINARY_DIR}/pkg-config/jsoncpp.pc"
104		DESTINATION "${CMAKE_INSTALL_PREFIX}/lib/pkgconfig")
105ENDIF(JSONCPP_WITH_PKGCONFIG_SUPPORT)
106
107IF(JSONCPP_WITH_CMAKE_PACKAGE)
108        INSTALL(EXPORT jsoncpp
109                DESTINATION ${PACKAGE_INSTALL_DIR}/jsoncpp
110                FILE        jsoncppConfig.cmake)
111ENDIF(JSONCPP_WITH_CMAKE_PACKAGE)
112
113# Build the different applications
114ADD_SUBDIRECTORY( src )
115
116#install the includes
117ADD_SUBDIRECTORY( include )
118