blob: 2841277c0fa2a4db5ffb6f9f5b8da06bc4eae9b6 [file] [log] [blame]
Haibo Huangb0bee822021-02-24 15:40:15 -08001# vim: et ts=4 sts=4 sw=4 tw=0
Derek Sollenberger2eb3b4d2016-01-11 14:41:40 -05002
Haibo Huangb0bee822021-02-24 15:40:15 -08003# ==== 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})
Elliott Hughes1601ea02021-12-07 09:43:38 -080025if(POLICY CMP0091)
26 cmake_policy(SET CMP0091 NEW)
27endif()
Haibo Huangb0bee822021-02-24 15:40:15 -080028#
29# Now enumerate specific policies newer than JSONCPP_NEWEST_VALIDATED_POLICIES_VERSION
30# that may need to be individually set to NEW/OLD
31#
32foreach(pnew "") # Currently Empty
33 if(POLICY ${pnew})
34 cmake_policy(SET ${pnew} NEW)
35 endif()
36endforeach()
37foreach(pold "") # Currently Empty
38 if(POLICY ${pold})
39 cmake_policy(SET ${pold} OLD)
40 endif()
41endforeach()
Derek Sollenberger2eb3b4d2016-01-11 14:41:40 -050042
Haibo Huangb0bee822021-02-24 15:40:15 -080043# Build the library with C++11 standard support, independent from other including
44# software which may use a different CXX_STANDARD or CMAKE_CXX_STANDARD.
45set(CMAKE_CXX_STANDARD 11)
46set(CMAKE_CXX_EXTENSIONS OFF)
47set(CMAKE_CXX_STANDARD_REQUIRED ON)
Derek Sollenberger2eb3b4d2016-01-11 14:41:40 -050048
Haibo Huangb0bee822021-02-24 15:40:15 -080049# Ensure that CMAKE_BUILD_TYPE has a value specified for single configuration generators.
50if(NOT DEFINED CMAKE_BUILD_TYPE AND NOT DEFINED CMAKE_CONFIGURATION_TYPES)
51 set(CMAKE_BUILD_TYPE Release CACHE STRING
52 "Choose the type of build, options are: None Debug Release RelWithDebInfo MinSizeRel Coverage.")
53endif()
Derek Sollenberger2eb3b4d2016-01-11 14:41:40 -050054
Haibo Huangb0bee822021-02-24 15:40:15 -080055set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_CURRENT_SOURCE_DIR}/cmake")
Derek Sollenberger2eb3b4d2016-01-11 14:41:40 -050056
Haibo Huangb0bee822021-02-24 15:40:15 -080057# ---------------------------------------------------------------------------
58# use ccache if found, has to be done before project()
59# ---------------------------------------------------------------------------
60find_program(CCACHE_EXECUTABLE "ccache" HINTS /usr/local/bin /opt/local/bin)
61if(CCACHE_EXECUTABLE)
62 message(STATUS "use ccache")
63 set(CMAKE_CXX_COMPILER_LAUNCHER "${CCACHE_EXECUTABLE}" CACHE PATH "ccache" FORCE)
64 set(CMAKE_C_COMPILER_LAUNCHER "${CCACHE_EXECUTABLE}" CACHE PATH "ccache" FORCE)
65endif()
Derek Sollenberger2eb3b4d2016-01-11 14:41:40 -050066
Haibo Huangb0bee822021-02-24 15:40:15 -080067project(jsoncpp
68 # Note: version must be updated in three places when doing a release. This
69 # annoying process ensures that amalgamate, CMake, and meson all report the
70 # correct version.
71 # 1. ./meson.build
72 # 2. ./include/json/version.h
73 # 3. ./CMakeLists.txt
74 # IMPORTANT: also update the PROJECT_SOVERSION!!
Elliott Hughes1601ea02021-12-07 09:43:38 -080075 VERSION 1.9.5 # <major>[.<minor>[.<patch>[.<tweak>]]]
Haibo Huangb0bee822021-02-24 15:40:15 -080076 LANGUAGES CXX)
Derek Sollenberger2eb3b4d2016-01-11 14:41:40 -050077
Haibo Huangb0bee822021-02-24 15:40:15 -080078message(STATUS "JsonCpp Version: ${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}.${PROJECT_VERSION_PATCH}")
Elliott Hughes1601ea02021-12-07 09:43:38 -080079set(PROJECT_SOVERSION 25)
80
81include(${CMAKE_CURRENT_SOURCE_DIR}/include/PreventInSourceBuilds.cmake)
82include(${CMAKE_CURRENT_SOURCE_DIR}/include/PreventInBuildInstalls.cmake)
Derek Sollenberger2eb3b4d2016-01-11 14:41:40 -050083
Haibo Huangb0bee822021-02-24 15:40:15 -080084option(JSONCPP_WITH_TESTS "Compile and (for jsoncpp_check) run JsonCpp test executables" ON)
85option(JSONCPP_WITH_POST_BUILD_UNITTEST "Automatically run unit-tests as a post build step" ON)
86option(JSONCPP_WITH_WARNING_AS_ERROR "Force compilation to fail if a warning occurs" OFF)
87option(JSONCPP_WITH_STRICT_ISO "Issue all the warnings demanded by strict ISO C and ISO C++" ON)
88option(JSONCPP_WITH_PKGCONFIG_SUPPORT "Generate and install .pc files" ON)
89option(JSONCPP_WITH_CMAKE_PACKAGE "Generate and install cmake package files" ON)
90option(JSONCPP_WITH_EXAMPLE "Compile JsonCpp example" OFF)
Elliott Hughes1601ea02021-12-07 09:43:38 -080091option(JSONCPP_STATIC_WINDOWS_RUNTIME "Use static (MT/MTd) Windows runtime" OFF)
Haibo Huangb0bee822021-02-24 15:40:15 -080092option(BUILD_SHARED_LIBS "Build jsoncpp_lib as a shared library." ON)
93option(BUILD_STATIC_LIBS "Build jsoncpp_lib as a static library." ON)
94option(BUILD_OBJECT_LIBS "Build jsoncpp_lib as a object library." ON)
Derek Sollenberger2eb3b4d2016-01-11 14:41:40 -050095
Haibo Huangb0bee822021-02-24 15:40:15 -080096# Adhere to GNU filesystem layout conventions
97include(GNUInstallDirs)
Derek Sollenberger2eb3b4d2016-01-11 14:41:40 -050098
Haibo Huangb0bee822021-02-24 15:40:15 -080099set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/lib" CACHE PATH "Archive output dir.")
100set(CMAKE_LIBRARY_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/lib" CACHE PATH "Library output dir.")
101set(CMAKE_PDB_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin" CACHE PATH "PDB (MSVC debug symbol)output dir.")
102set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin" CACHE PATH "Executable/dll output dir.")
103
104set(JSONCPP_USE_SECURE_MEMORY "0" CACHE STRING "-D...=1 to use memory-wiping allocator for STL")
105
106configure_file("${PROJECT_SOURCE_DIR}/version.in"
107 "${PROJECT_BINARY_DIR}/version"
108 NEWLINE_STYLE UNIX)
109
110macro(use_compilation_warning_as_error)
111 if(MSVC)
Derek Sollenberger2eb3b4d2016-01-11 14:41:40 -0500112 # Only enabled in debug because some old versions of VS STL generate
113 # warnings when compiled in release configuration.
Haibo Huangb0bee822021-02-24 15:40:15 -0800114 add_compile_options($<$<CONFIG:Debug>:/WX>)
115 elseif(CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
116 add_compile_options(-Werror)
117 if(JSONCPP_WITH_STRICT_ISO)
118 add_compile_options(-pedantic-errors)
119 endif()
120 endif()
Derek Sollenberger2eb3b4d2016-01-11 14:41:40 -0500121endmacro()
122
123# Include our configuration header
Haibo Huangb0bee822021-02-24 15:40:15 -0800124include_directories(${jsoncpp_SOURCE_DIR}/include)
Derek Sollenberger2eb3b4d2016-01-11 14:41:40 -0500125
Haibo Huangb0bee822021-02-24 15:40:15 -0800126if(MSVC)
Derek Sollenberger2eb3b4d2016-01-11 14:41:40 -0500127 # Only enabled in debug because some old versions of VS STL generate
128 # unreachable code warning when compiled in release configuration.
Haibo Huangb0bee822021-02-24 15:40:15 -0800129 add_compile_options($<$<CONFIG:Debug>:/W4>)
Elliott Hughes1601ea02021-12-07 09:43:38 -0800130 if (JSONCPP_STATIC_WINDOWS_RUNTIME)
131 set(CMAKE_MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>")
132 endif()
Haibo Huangb0bee822021-02-24 15:40:15 -0800133endif()
Derek Sollenberger2eb3b4d2016-01-11 14:41:40 -0500134
Haibo Huangb0bee822021-02-24 15:40:15 -0800135if(CMAKE_CXX_COMPILER_ID MATCHES "Clang")
136 # using regular Clang or AppleClang
Elliott Hughes1601ea02021-12-07 09:43:38 -0800137 add_compile_options(-Wall -Wconversion -Wshadow)
138
139 if(JSONCPP_WITH_WARNING_AS_ERROR)
140 add_compile_options(-Werror=conversion -Werror=sign-compare)
141 endif()
Haibo Huangb0bee822021-02-24 15:40:15 -0800142elseif(CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
143 # using GCC
144 add_compile_options(-Wall -Wconversion -Wshadow -Wextra)
145 # not yet ready for -Wsign-conversion
Derek Sollenberger2eb3b4d2016-01-11 14:41:40 -0500146
Haibo Huangb0bee822021-02-24 15:40:15 -0800147 if(JSONCPP_WITH_STRICT_ISO)
148 add_compile_options(-Wpedantic)
149 endif()
150 if(JSONCPP_WITH_WARNING_AS_ERROR)
151 add_compile_options(-Werror=conversion)
152 endif()
153elseif(CMAKE_CXX_COMPILER_ID STREQUAL "Intel")
154 # using Intel compiler
Elliott Hughes1601ea02021-12-07 09:43:38 -0800155 add_compile_options(-Wall -Wconversion -Wshadow -Wextra)
Derek Sollenberger2eb3b4d2016-01-11 14:41:40 -0500156
Elliott Hughes1601ea02021-12-07 09:43:38 -0800157 if(JSONCPP_WITH_WARNING_AS_ERROR)
158 add_compile_options(-Werror=conversion)
159 elseif(JSONCPP_WITH_STRICT_ISO)
Haibo Huangb0bee822021-02-24 15:40:15 -0800160 add_compile_options(-Wpedantic)
161 endif()
162endif()
163
164if(JSONCPP_WITH_WARNING_AS_ERROR)
165 use_compilation_warning_as_error()
166endif()
167
168if(JSONCPP_WITH_PKGCONFIG_SUPPORT)
169 include(JoinPaths)
170
171 join_paths(libdir_for_pc_file "\${exec_prefix}" "${CMAKE_INSTALL_LIBDIR}")
172 join_paths(includedir_for_pc_file "\${prefix}" "${CMAKE_INSTALL_INCLUDEDIR}")
173
174 configure_file(
175 "pkg-config/jsoncpp.pc.in"
176 "pkg-config/jsoncpp.pc"
177 @ONLY)
178 install(FILES "${CMAKE_CURRENT_BINARY_DIR}/pkg-config/jsoncpp.pc"
179 DESTINATION "${CMAKE_INSTALL_LIBDIR}/pkgconfig")
180endif()
181
182if(JSONCPP_WITH_CMAKE_PACKAGE)
183 include(CMakePackageConfigHelpers)
184 install(EXPORT jsoncpp
185 DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/jsoncpp
Elliott Hughes1601ea02021-12-07 09:43:38 -0800186 FILE jsoncpp-targets.cmake)
187 configure_package_config_file(jsoncppConfig.cmake.in ${CMAKE_CURRENT_BINARY_DIR}/jsoncppConfig.cmake
188 INSTALL_DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/jsoncpp)
189
Haibo Huangb0bee822021-02-24 15:40:15 -0800190 write_basic_package_version_file("${CMAKE_CURRENT_BINARY_DIR}/jsoncppConfigVersion.cmake"
191 VERSION ${PROJECT_VERSION}
192 COMPATIBILITY SameMajorVersion)
Elliott Hughes1601ea02021-12-07 09:43:38 -0800193 install(FILES
194 ${CMAKE_CURRENT_BINARY_DIR}/jsoncppConfigVersion.cmake ${CMAKE_CURRENT_BINARY_DIR}/jsoncppConfig.cmake
195 ${CMAKE_CURRENT_SOURCE_DIR}/jsoncpp-namespaced-targets.cmake
Haibo Huangb0bee822021-02-24 15:40:15 -0800196 DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/jsoncpp)
197endif()
198
199if(JSONCPP_WITH_TESTS)
200 enable_testing()
201 include(CTest)
202endif()
Derek Sollenberger2eb3b4d2016-01-11 14:41:40 -0500203
204# Build the different applications
Haibo Huangb0bee822021-02-24 15:40:15 -0800205add_subdirectory(src)
Derek Sollenberger2eb3b4d2016-01-11 14:41:40 -0500206
207#install the includes
Haibo Huangb0bee822021-02-24 15:40:15 -0800208add_subdirectory(include)
209
210#install the example
211if(JSONCPP_WITH_EXAMPLE)
212 add_subdirectory(example)
213endif()