Fix jsoncpp.pc file
[platform/upstream/jsoncpp.git] / CMakeLists.txt
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 #
15 set(JSONCPP_OLDEST_VALIDATED_POLICIES_VERSION "3.8.0")
16 set(JSONCPP_NEWEST_VALIDATED_POLICIES_VERSION "3.13.2")
17 cmake_minimum_required(VERSION ${JSONCPP_OLDEST_VALIDATED_POLICIES_VERSION})
18 if("${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}")
21 else()
22     set(JSONCPP_CMAKE_POLICY_VERSION "${JSONCPP_NEWEST_VALIDATED_POLICIES_VERSION}")
23 endif()
24 cmake_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 #
29 foreach(pnew "") # Currently Empty
30     if(POLICY ${pnew})
31         cmake_policy(SET ${pnew} NEW)
32     endif()
33 endforeach()
34 foreach(pold "") # Currently Empty
35     if(POLICY ${pold})
36         cmake_policy(SET ${pold} OLD)
37     endif()
38 endforeach()
39
40 # ==== Define language standard configurations requiring at least c++11 standard
41 if(CMAKE_CXX_STANDARD EQUAL "98" )
42     message(FATAL_ERROR "CMAKE_CXX_STANDARD:STRING=98 is not supported.")
43 endif()
44
45 #####
46 ##  Set the default target properties
47 if(NOT CMAKE_CXX_STANDARD)
48     set(CMAKE_CXX_STANDARD 11) # Supported values are ``11``, ``14``, and ``17``.
49 endif()
50 if(NOT CMAKE_CXX_STANDARD_REQUIRED)
51     set(CMAKE_CXX_STANDARD_REQUIRED ON)
52 endif()
53 if(NOT CMAKE_CXX_EXTENSIONS)
54     set(CMAKE_CXX_EXTENSIONS OFF)
55 endif()
56
57 # ====
58
59 # Ensures that CMAKE_BUILD_TYPE has a default value
60 if(NOT DEFINED CMAKE_BUILD_TYPE)
61     set(CMAKE_BUILD_TYPE Release CACHE STRING
62         "Choose the type of build, options are: None Debug Release RelWithDebInfo MinSizeRel Coverage.")
63 endif()
64
65 project(JSONCPP
66         # Note: version must be updated in three places when doing a release. This
67         # annoying process ensures that amalgamate, CMake, and meson all report the
68         # correct version.
69         # 1. /meson.build
70         # 2. /include/json/version.h
71         # 3. /CMakeLists.txt
72         # IMPORTANT: also update the SOVERSION!!
73         VERSION 1.9.2 # <major>[.<minor>[.<patch>[.<tweak>]]]
74         LANGUAGES CXX)
75
76 message(STATUS "JsonCpp Version: ${JSONCPP_VERSION_MAJOR}.${JSONCPP_VERSION_MINOR}.${JSONCPP_VERSION_PATCH}")
77 set( JSONCPP_SOVERSION 22 )
78
79 option(JSONCPP_WITH_TESTS "Compile and (for jsoncpp_check) run JsonCpp test executables" ON)
80 option(JSONCPP_WITH_POST_BUILD_UNITTEST "Automatically run unit-tests as a post build step" ON)
81 option(JSONCPP_WITH_WARNING_AS_ERROR "Force compilation to fail if a warning occurs" OFF)
82 option(JSONCPP_WITH_STRICT_ISO "Issue all the warnings demanded by strict ISO C and ISO C++" ON)
83 option(JSONCPP_WITH_PKGCONFIG_SUPPORT "Generate and install .pc files" ON)
84 option(JSONCPP_WITH_CMAKE_PACKAGE "Generate and install cmake package files" ON)
85 option(BUILD_SHARED_LIBS "Build jsoncpp_lib as a shared library." OFF)
86
87 # Enable runtime search path support for dynamic libraries on OSX
88 if(APPLE)
89     set(CMAKE_MACOSX_RPATH 1)
90 endif()
91
92 # Adhere to GNU filesystem layout conventions
93 include(GNUInstallDirs)
94
95 set(DEBUG_LIBNAME_SUFFIX "" CACHE STRING "Optional suffix to append to the library name for a debug build")
96
97 set(JSONCPP_USE_SECURE_MEMORY "0" CACHE STRING "-D...=1 to use memory-wiping allocator for STL" )
98
99 configure_file( "${PROJECT_SOURCE_DIR}/version.in"
100                 "${PROJECT_BINARY_DIR}/version"
101                 NEWLINE_STYLE UNIX )
102
103 macro(UseCompilationWarningAsError)
104     if(MSVC)
105         # Only enabled in debug because some old versions of VS STL generate
106         # warnings when compiled in release configuration.
107         if(CMAKE_VERSION VERSION_GREATER_EQUAL 3.12.0)
108           add_compile_options($<$<CONFIG:Debug>:/WX>)
109         else()
110           set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} /WX ")
111         endif()
112     elseif(CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
113         if(CMAKE_VERSION VERSION_GREATER_EQUAL 3.12.0)
114           add_compile_options(-Werror)
115         else()
116           set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Werror")
117         endif()
118         if(JSONCPP_WITH_STRICT_ISO)
119           if(CMAKE_VERSION VERSION_GREATER_EQUAL 3.12.0)
120             add_compile_options(-pedantic-errors)
121           else()
122             set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -pedantic-errors")
123           endif()
124         endif()
125     endif()
126 endmacro()
127
128 # Include our configuration header
129 include_directories( ${jsoncpp_SOURCE_DIR}/include )
130
131 if(MSVC)
132     # Only enabled in debug because some old versions of VS STL generate
133     # unreachable code warning when compiled in release configuration.
134     if(CMAKE_VERSION VERSION_GREATER_EQUAL 3.12.0)
135       add_compile_options($<$<CONFIG:Debug>:/W4>)
136     else()
137       set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} /W4 ")
138     endif()
139 endif()
140
141 if(CMAKE_CXX_COMPILER_ID MATCHES "Clang")
142     # using regular Clang or AppleClang
143     if(CMAKE_VERSION VERSION_GREATER_EQUAL 3.12.0)
144       add_compile_options(-Wall -Wconversion -Wshadow -Werror=conversion -Werror=sign-compare)
145     else()
146       set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wconversion -Wshadow -Werror=conversion -Werror=sign-compare")
147     endif()
148 elseif(CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
149     # using GCC
150     if(CMAKE_VERSION VERSION_GREATER_EQUAL 3.12.0)
151       add_compile_options(-Wall -Wconversion -Wshadow -Wextra)
152     else()
153       set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wconversion -Wshadow -Wextra")
154     endif()
155     # not yet ready for -Wsign-conversion
156
157     if(JSONCPP_WITH_STRICT_ISO)
158       if(CMAKE_VERSION VERSION_GREATER_EQUAL 3.12.0)
159         add_compile_options(-pedantic)
160       else()
161         set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -pedantic")
162       endif()
163     endif()
164     if(JSONCPP_WITH_WARNING_AS_ERROR)
165       if(CMAKE_VERSION VERSION_GREATER_EQUAL 3.12.0)
166         add_compile_options(-Werror=conversion)
167       else()
168         set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Werror=conversion")
169       endif()
170     endif()
171 elseif(CMAKE_CXX_COMPILER_ID STREQUAL "Intel")
172     # using Intel compiler
173     if(CMAKE_VERSION VERSION_GREATER_EQUAL 3.12.0)
174       add_compile_options(-Wall -Wconversion -Wshadow -Wextra -Werror=conversion)
175     else()
176       set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wconversion -Wshadow -Wextra -Werror=conversion")
177     endif()
178
179     if(JSONCPP_WITH_STRICT_ISO AND NOT JSONCPP_WITH_WARNING_AS_ERROR)
180       if(CMAKE_VERSION VERSION_GREATER_EQUAL 3.12.0)
181         add_compile_options(-pedantic)
182       else()
183         set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -pedantic")
184       endif()
185     endif()
186 endif()
187
188 find_program(CCACHE_FOUND ccache)
189 if(CCACHE_FOUND)
190     set_property(GLOBAL PROPERTY RULE_LAUNCH_COMPILE ccache)
191     set_property(GLOBAL PROPERTY RULE_LAUNCH_LINK ccache)
192 endif(CCACHE_FOUND)
193
194 if(JSONCPP_WITH_WARNING_AS_ERROR)
195     UseCompilationWarningAsError()
196 endif()
197
198 if(JSONCPP_WITH_PKGCONFIG_SUPPORT)
199     configure_file(
200         "pkg-config/jsoncpp.pc.in"
201         "pkg-config/jsoncpp.pc"
202         @ONLY)
203     install(FILES "${CMAKE_CURRENT_BINARY_DIR}/pkg-config/jsoncpp.pc"
204         DESTINATION "${CMAKE_INSTALL_LIBDIR}/pkgconfig")
205 endif()
206
207 if(JSONCPP_WITH_CMAKE_PACKAGE)
208         include (CMakePackageConfigHelpers)
209         install(EXPORT jsoncpp
210                 DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/jsoncpp
211                 FILE        jsoncppConfig.cmake)
212         write_basic_package_version_file ("${CMAKE_CURRENT_BINARY_DIR}/jsoncppConfigVersion.cmake"
213                 VERSION ${PROJECT_VERSION}
214                 COMPATIBILITY SameMajorVersion)
215         install(FILES ${CMAKE_CURRENT_BINARY_DIR}/jsoncppConfigVersion.cmake
216                 DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/jsoncpp)
217 endif()
218
219 if(JSONCPP_WITH_TESTS)
220   enable_testing()
221   include(CTest)
222 endif()
223
224 # Build the different applications
225 add_subdirectory( src )
226
227 #install the includes
228 add_subdirectory( include )
229
230 #install the example
231 add_subdirectory( example )