1# vim: et ts=4 sts=4 sw=4 tw=0
2
3# ==== Define cmake build policies that affect compilation and linkage default behaviors
4#
5# Set the JSONCPP_NEWEST_VALIDATED_POLICIES_VERSION string to the newest cmake version
6# policies that provide successful builds. By setting JSONCPP_NEWEST_VALIDATED_POLICIES_VERSION
7# to a value greater than the oldest policies, all policies between
8# JSONCPP_OLDEST_VALIDATED_POLICIES_VERSION and CMAKE_VERSION (used for this build)
9# are set to their NEW behaivor, thereby suppressing policy warnings related to policies
10# between the JSONCPP_OLDEST_VALIDATED_POLICIES_VERSION and CMAKE_VERSION.
11#
12# CMake versions greater than the JSONCPP_NEWEST_VALIDATED_POLICIES_VERSION policies will
13# continue to generate policy warnings "CMake Warning (dev)...Policy CMP0XXX is not set:"
14#
15set(JSONCPP_OLDEST_VALIDATED_POLICIES_VERSION "3.8.0")
16set(JSONCPP_NEWEST_VALIDATED_POLICIES_VERSION "3.13.2")
17cmake_minimum_required(VERSION ${JSONCPP_OLDEST_VALIDATED_POLICIES_VERSION})
18if("${CMAKE_VERSION}" VERSION_LESS "${JSONCPP_NEWEST_VALIDATED_POLICIES_VERSION}")
19    #Set and use the newest available cmake policies that are validated to work
20    set(JSONCPP_CMAKE_POLICY_VERSION "${CMAKE_VERSION}")
21else()
22    set(JSONCPP_CMAKE_POLICY_VERSION "${JSONCPP_NEWEST_VALIDATED_POLICIES_VERSION}")
23endif()
24cmake_policy(VERSION ${JSONCPP_CMAKE_POLICY_VERSION})
25#
26# Now enumerate specific policies newer than JSONCPP_NEWEST_VALIDATED_POLICIES_VERSION
27# that may need to be individually set to NEW/OLD
28#
29foreach(pnew "") # Currently Empty
30    if(POLICY ${pnew})
31        cmake_policy(SET ${pnew} NEW)
32    endif()
33endforeach()
34foreach(pold "") # Currently Empty
35    if(POLICY ${pold})
36        cmake_policy(SET ${pold} OLD)
37    endif()
38endforeach()
39
40# Build the library with C++11 standard support, independent from other including
41# software which may use a different CXX_STANDARD or CMAKE_CXX_STANDARD.
42set(CMAKE_CXX_STANDARD 11)
43set(CMAKE_CXX_EXTENSIONS OFF)
44set(CMAKE_CXX_STANDARD_REQUIRED ON)
45
46# Ensure that CMAKE_BUILD_TYPE has a value specified for single configuration generators.
47if(NOT DEFINED CMAKE_BUILD_TYPE AND NOT DEFINED CMAKE_CONFIGURATION_TYPES)
48    set(CMAKE_BUILD_TYPE Release CACHE STRING
49        "Choose the type of build, options are: None Debug Release RelWithDebInfo MinSizeRel Coverage.")
50endif()
51
52set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_CURRENT_SOURCE_DIR}/cmake")
53
54# ---------------------------------------------------------------------------
55# use ccache if found, has to be done before project()
56# ---------------------------------------------------------------------------
57find_program(CCACHE_EXECUTABLE "ccache" HINTS /usr/local/bin /opt/local/bin)
58if(CCACHE_EXECUTABLE)
59    message(STATUS "use ccache")
60    set(CMAKE_CXX_COMPILER_LAUNCHER "${CCACHE_EXECUTABLE}" CACHE PATH "ccache" FORCE)
61    set(CMAKE_C_COMPILER_LAUNCHER "${CCACHE_EXECUTABLE}" CACHE PATH "ccache" FORCE)
62endif()
63
64project(jsoncpp
65        # Note: version must be updated in three places when doing a release. This
66        # annoying process ensures that amalgamate, CMake, and meson all report the
67        # correct version.
68        # 1. ./meson.build
69        # 2. ./include/json/version.h
70        # 3. ./CMakeLists.txt
71        # IMPORTANT: also update the PROJECT_SOVERSION!!
72        VERSION 1.9.4 # <major>[.<minor>[.<patch>[.<tweak>]]]
73        LANGUAGES CXX)
74
75message(STATUS "JsonCpp Version: ${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}.${PROJECT_VERSION_PATCH}")
76set(PROJECT_SOVERSION 24)
77
78include(${CMAKE_CURRENT_SOURCE_DIR}/include/PreventInSourceBuilds.cmake)
79include(${CMAKE_CURRENT_SOURCE_DIR}/include/PreventInBuildInstalls.cmake)
80
81option(JSONCPP_WITH_TESTS "Compile and (for jsoncpp_check) run JsonCpp test executables" ON)
82option(JSONCPP_WITH_POST_BUILD_UNITTEST "Automatically run unit-tests as a post build step" ON)
83option(JSONCPP_WITH_WARNING_AS_ERROR "Force compilation to fail if a warning occurs" OFF)
84option(JSONCPP_WITH_STRICT_ISO "Issue all the warnings demanded by strict ISO C and ISO C++" ON)
85option(JSONCPP_WITH_PKGCONFIG_SUPPORT "Generate and install .pc files" ON)
86option(JSONCPP_WITH_CMAKE_PACKAGE "Generate and install cmake package files" ON)
87option(JSONCPP_WITH_EXAMPLE "Compile JsonCpp example" OFF)
88option(BUILD_SHARED_LIBS "Build jsoncpp_lib as a shared library." ON)
89option(BUILD_STATIC_LIBS "Build jsoncpp_lib as a static library." ON)
90option(BUILD_OBJECT_LIBS "Build jsoncpp_lib as a object library." ON)
91
92# Adhere to GNU filesystem layout conventions
93include(GNUInstallDirs)
94
95set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/lib" CACHE PATH "Archive output dir.")
96set(CMAKE_LIBRARY_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/lib" CACHE PATH "Library output dir.")
97set(CMAKE_PDB_OUTPUT_DIRECTORY     "${CMAKE_BINARY_DIR}/bin" CACHE PATH "PDB (MSVC debug symbol)output dir.")
98set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin" CACHE PATH "Executable/dll output dir.")
99
100set(JSONCPP_USE_SECURE_MEMORY "0" CACHE STRING "-D...=1 to use memory-wiping allocator for STL")
101
102configure_file("${PROJECT_SOURCE_DIR}/version.in"
103    "${PROJECT_BINARY_DIR}/version"
104    NEWLINE_STYLE UNIX)
105
106macro(use_compilation_warning_as_error)
107    if(MSVC)
108        # Only enabled in debug because some old versions of VS STL generate
109        # warnings when compiled in release configuration.
110        add_compile_options($<$<CONFIG:Debug>:/WX>)
111    elseif(CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
112        add_compile_options(-Werror)
113        if(JSONCPP_WITH_STRICT_ISO)
114            add_compile_options(-pedantic-errors)
115        endif()
116    endif()
117endmacro()
118
119# Include our configuration header
120include_directories(${jsoncpp_SOURCE_DIR}/include)
121
122if(MSVC)
123    # Only enabled in debug because some old versions of VS STL generate
124    # unreachable code warning when compiled in release configuration.
125    add_compile_options($<$<CONFIG:Debug>:/W4>)
126endif()
127
128if(CMAKE_CXX_COMPILER_ID MATCHES "Clang")
129    # using regular Clang or AppleClang
130    add_compile_options(-Wall -Wconversion -Wshadow -Werror=conversion -Werror=sign-compare)
131elseif(CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
132    # using GCC
133    add_compile_options(-Wall -Wconversion -Wshadow -Wextra)
134    # not yet ready for -Wsign-conversion
135
136    if(JSONCPP_WITH_STRICT_ISO)
137        add_compile_options(-Wpedantic)
138    endif()
139    if(JSONCPP_WITH_WARNING_AS_ERROR)
140        add_compile_options(-Werror=conversion)
141    endif()
142elseif(CMAKE_CXX_COMPILER_ID STREQUAL "Intel")
143    # using Intel compiler
144    add_compile_options(-Wall -Wconversion -Wshadow -Wextra -Werror=conversion)
145
146    if(JSONCPP_WITH_STRICT_ISO AND NOT JSONCPP_WITH_WARNING_AS_ERROR)
147        add_compile_options(-Wpedantic)
148    endif()
149endif()
150
151if(JSONCPP_WITH_WARNING_AS_ERROR)
152    use_compilation_warning_as_error()
153endif()
154
155if(JSONCPP_WITH_PKGCONFIG_SUPPORT)
156    include(JoinPaths)
157
158    join_paths(libdir_for_pc_file "\${exec_prefix}" "${CMAKE_INSTALL_LIBDIR}")
159    join_paths(includedir_for_pc_file "\${prefix}" "${CMAKE_INSTALL_INCLUDEDIR}")
160
161    configure_file(
162        "pkg-config/jsoncpp.pc.in"
163        "pkg-config/jsoncpp.pc"
164        @ONLY)
165    install(FILES "${CMAKE_CURRENT_BINARY_DIR}/pkg-config/jsoncpp.pc"
166        DESTINATION "${CMAKE_INSTALL_LIBDIR}/pkgconfig")
167endif()
168
169if(JSONCPP_WITH_CMAKE_PACKAGE)
170    include(CMakePackageConfigHelpers)
171    install(EXPORT jsoncpp
172        DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/jsoncpp
173        FILE        jsoncppConfig.cmake)
174    write_basic_package_version_file("${CMAKE_CURRENT_BINARY_DIR}/jsoncppConfigVersion.cmake"
175        VERSION ${PROJECT_VERSION}
176        COMPATIBILITY SameMajorVersion)
177    install(FILES ${CMAKE_CURRENT_BINARY_DIR}/jsoncppConfigVersion.cmake
178        DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/jsoncpp)
179endif()
180
181if(JSONCPP_WITH_TESTS)
182    enable_testing()
183    include(CTest)
184endif()
185
186# Build the different applications
187add_subdirectory(src)
188
189#install the includes
190add_subdirectory(include)
191
192#install the example
193if(JSONCPP_WITH_EXAMPLE)
194    add_subdirectory(example)
195endif()
196