blob: 51b74fcc9aea708e498816e7928f1068504661f5 [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})
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()
Derek Sollenberger2eb3b4d2016-01-11 14:41:40 -050039
Haibo Huangb0bee822021-02-24 15:40:15 -080040# 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)
Derek Sollenberger2eb3b4d2016-01-11 14:41:40 -050045
Haibo Huangb0bee822021-02-24 15:40:15 -080046# 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()
Derek Sollenberger2eb3b4d2016-01-11 14:41:40 -050051
Haibo Huangb0bee822021-02-24 15:40:15 -080052set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_CURRENT_SOURCE_DIR}/cmake")
Derek Sollenberger2eb3b4d2016-01-11 14:41:40 -050053
Haibo Huangb0bee822021-02-24 15:40:15 -080054# ---------------------------------------------------------------------------
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()
Derek Sollenberger2eb3b4d2016-01-11 14:41:40 -050063
Haibo Huangb0bee822021-02-24 15:40:15 -080064project(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)
Derek Sollenberger2eb3b4d2016-01-11 14:41:40 -050074
Haibo Huangb0bee822021-02-24 15:40:15 -080075message(STATUS "JsonCpp Version: ${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}.${PROJECT_VERSION_PATCH}")
76set(PROJECT_SOVERSION 24)
Derek Sollenberger2eb3b4d2016-01-11 14:41:40 -050077
Haibo Huangb0bee822021-02-24 15:40:15 -080078option(JSONCPP_WITH_TESTS "Compile and (for jsoncpp_check) run JsonCpp test executables" ON)
79option(JSONCPP_WITH_POST_BUILD_UNITTEST "Automatically run unit-tests as a post build step" ON)
80option(JSONCPP_WITH_WARNING_AS_ERROR "Force compilation to fail if a warning occurs" OFF)
81option(JSONCPP_WITH_STRICT_ISO "Issue all the warnings demanded by strict ISO C and ISO C++" ON)
82option(JSONCPP_WITH_PKGCONFIG_SUPPORT "Generate and install .pc files" ON)
83option(JSONCPP_WITH_CMAKE_PACKAGE "Generate and install cmake package files" ON)
84option(JSONCPP_WITH_EXAMPLE "Compile JsonCpp example" OFF)
85option(BUILD_SHARED_LIBS "Build jsoncpp_lib as a shared library." ON)
86option(BUILD_STATIC_LIBS "Build jsoncpp_lib as a static library." ON)
87option(BUILD_OBJECT_LIBS "Build jsoncpp_lib as a object library." ON)
Derek Sollenberger2eb3b4d2016-01-11 14:41:40 -050088
Haibo Huangb0bee822021-02-24 15:40:15 -080089# Adhere to GNU filesystem layout conventions
90include(GNUInstallDirs)
Derek Sollenberger2eb3b4d2016-01-11 14:41:40 -050091
Haibo Huangb0bee822021-02-24 15:40:15 -080092set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/lib" CACHE PATH "Archive output dir.")
93set(CMAKE_LIBRARY_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/lib" CACHE PATH "Library output dir.")
94set(CMAKE_PDB_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin" CACHE PATH "PDB (MSVC debug symbol)output dir.")
95set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin" CACHE PATH "Executable/dll output dir.")
96
97set(JSONCPP_USE_SECURE_MEMORY "0" CACHE STRING "-D...=1 to use memory-wiping allocator for STL")
98
99configure_file("${PROJECT_SOURCE_DIR}/version.in"
100 "${PROJECT_BINARY_DIR}/version"
101 NEWLINE_STYLE UNIX)
102
103macro(use_compilation_warning_as_error)
104 if(MSVC)
Derek Sollenberger2eb3b4d2016-01-11 14:41:40 -0500105 # Only enabled in debug because some old versions of VS STL generate
106 # warnings when compiled in release configuration.
Haibo Huangb0bee822021-02-24 15:40:15 -0800107 add_compile_options($<$<CONFIG:Debug>:/WX>)
108 elseif(CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
109 add_compile_options(-Werror)
110 if(JSONCPP_WITH_STRICT_ISO)
111 add_compile_options(-pedantic-errors)
112 endif()
113 endif()
Derek Sollenberger2eb3b4d2016-01-11 14:41:40 -0500114endmacro()
115
116# Include our configuration header
Haibo Huangb0bee822021-02-24 15:40:15 -0800117include_directories(${jsoncpp_SOURCE_DIR}/include)
Derek Sollenberger2eb3b4d2016-01-11 14:41:40 -0500118
Haibo Huangb0bee822021-02-24 15:40:15 -0800119if(MSVC)
Derek Sollenberger2eb3b4d2016-01-11 14:41:40 -0500120 # Only enabled in debug because some old versions of VS STL generate
121 # unreachable code warning when compiled in release configuration.
Haibo Huangb0bee822021-02-24 15:40:15 -0800122 add_compile_options($<$<CONFIG:Debug>:/W4>)
123endif()
Derek Sollenberger2eb3b4d2016-01-11 14:41:40 -0500124
Haibo Huangb0bee822021-02-24 15:40:15 -0800125if(CMAKE_CXX_COMPILER_ID MATCHES "Clang")
126 # using regular Clang or AppleClang
127 add_compile_options(-Wall -Wconversion -Wshadow -Werror=conversion -Werror=sign-compare)
128elseif(CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
129 # using GCC
130 add_compile_options(-Wall -Wconversion -Wshadow -Wextra)
131 # not yet ready for -Wsign-conversion
Derek Sollenberger2eb3b4d2016-01-11 14:41:40 -0500132
Haibo Huangb0bee822021-02-24 15:40:15 -0800133 if(JSONCPP_WITH_STRICT_ISO)
134 add_compile_options(-Wpedantic)
135 endif()
136 if(JSONCPP_WITH_WARNING_AS_ERROR)
137 add_compile_options(-Werror=conversion)
138 endif()
139elseif(CMAKE_CXX_COMPILER_ID STREQUAL "Intel")
140 # using Intel compiler
141 add_compile_options(-Wall -Wconversion -Wshadow -Wextra -Werror=conversion)
Derek Sollenberger2eb3b4d2016-01-11 14:41:40 -0500142
Haibo Huangb0bee822021-02-24 15:40:15 -0800143 if(JSONCPP_WITH_STRICT_ISO AND NOT JSONCPP_WITH_WARNING_AS_ERROR)
144 add_compile_options(-Wpedantic)
145 endif()
146endif()
147
148if(JSONCPP_WITH_WARNING_AS_ERROR)
149 use_compilation_warning_as_error()
150endif()
151
152if(JSONCPP_WITH_PKGCONFIG_SUPPORT)
153 include(JoinPaths)
154
155 join_paths(libdir_for_pc_file "\${exec_prefix}" "${CMAKE_INSTALL_LIBDIR}")
156 join_paths(includedir_for_pc_file "\${prefix}" "${CMAKE_INSTALL_INCLUDEDIR}")
157
158 configure_file(
159 "pkg-config/jsoncpp.pc.in"
160 "pkg-config/jsoncpp.pc"
161 @ONLY)
162 install(FILES "${CMAKE_CURRENT_BINARY_DIR}/pkg-config/jsoncpp.pc"
163 DESTINATION "${CMAKE_INSTALL_LIBDIR}/pkgconfig")
164endif()
165
166if(JSONCPP_WITH_CMAKE_PACKAGE)
167 include(CMakePackageConfigHelpers)
168 install(EXPORT jsoncpp
169 DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/jsoncpp
170 FILE jsoncppConfig.cmake)
171 write_basic_package_version_file("${CMAKE_CURRENT_BINARY_DIR}/jsoncppConfigVersion.cmake"
172 VERSION ${PROJECT_VERSION}
173 COMPATIBILITY SameMajorVersion)
174 install(FILES ${CMAKE_CURRENT_BINARY_DIR}/jsoncppConfigVersion.cmake
175 DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/jsoncpp)
176endif()
177
178if(JSONCPP_WITH_TESTS)
179 enable_testing()
180 include(CTest)
181endif()
Derek Sollenberger2eb3b4d2016-01-11 14:41:40 -0500182
183# Build the different applications
Haibo Huangb0bee822021-02-24 15:40:15 -0800184add_subdirectory(src)
Derek Sollenberger2eb3b4d2016-01-11 14:41:40 -0500185
186#install the includes
Haibo Huangb0bee822021-02-24 15:40:15 -0800187add_subdirectory(include)
188
189#install the example
190if(JSONCPP_WITH_EXAMPLE)
191 add_subdirectory(example)
192endif()