xref: /aosp_15_r20/external/jsoncpp/CMakeLists.txt (revision 4484440890e2bc6e07362b4feaf15601abfe0071)
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})
25if(POLICY CMP0091)
26    cmake_policy(SET CMP0091 NEW)
27endif()
28#
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()
42
43# 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)
48
49# 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()
54
55set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_CURRENT_SOURCE_DIR}/cmake")
56
57# ---------------------------------------------------------------------------
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()
66
67project(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!!
75        VERSION 1.9.5 # <major>[.<minor>[.<patch>[.<tweak>]]]
76        LANGUAGES CXX)
77
78message(STATUS "JsonCpp Version: ${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}.${PROJECT_VERSION_PATCH}")
79set(PROJECT_SOVERSION 25)
80
81include(${CMAKE_CURRENT_SOURCE_DIR}/include/PreventInSourceBuilds.cmake)
82include(${CMAKE_CURRENT_SOURCE_DIR}/include/PreventInBuildInstalls.cmake)
83
84option(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)
91option(JSONCPP_STATIC_WINDOWS_RUNTIME "Use static (MT/MTd) Windows runtime" OFF)
92option(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)
95
96# Adhere to GNU filesystem layout conventions
97include(GNUInstallDirs)
98
99set(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)
112        # Only enabled in debug because some old versions of VS STL generate
113        # warnings when compiled in release configuration.
114        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()
121endmacro()
122
123# Include our configuration header
124include_directories(${jsoncpp_SOURCE_DIR}/include)
125
126if(MSVC)
127    # Only enabled in debug because some old versions of VS STL generate
128    # unreachable code warning when compiled in release configuration.
129    add_compile_options($<$<CONFIG:Debug>:/W4>)
130    if (JSONCPP_STATIC_WINDOWS_RUNTIME)
131        set(CMAKE_MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>")
132    endif()
133endif()
134
135if(CMAKE_CXX_COMPILER_ID MATCHES "Clang")
136    # using regular Clang or AppleClang
137    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()
142elseif(CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
143    # using GCC
144    add_compile_options(-Wall -Wconversion -Wshadow -Wextra)
145    # not yet ready for -Wsign-conversion
146
147    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
155    add_compile_options(-Wall -Wconversion -Wshadow -Wextra)
156
157    if(JSONCPP_WITH_WARNING_AS_ERROR)
158        add_compile_options(-Werror=conversion)
159    elseif(JSONCPP_WITH_STRICT_ISO)
160        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
186        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
190    write_basic_package_version_file("${CMAKE_CURRENT_BINARY_DIR}/jsoncppConfigVersion.cmake"
191        VERSION ${PROJECT_VERSION}
192        COMPATIBILITY SameMajorVersion)
193    install(FILES
194        ${CMAKE_CURRENT_BINARY_DIR}/jsoncppConfigVersion.cmake ${CMAKE_CURRENT_BINARY_DIR}/jsoncppConfig.cmake
195        ${CMAKE_CURRENT_SOURCE_DIR}/jsoncpp-namespaced-targets.cmake
196        DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/jsoncpp)
197endif()
198
199if(JSONCPP_WITH_TESTS)
200    enable_testing()
201    include(CTest)
202endif()
203
204# Build the different applications
205add_subdirectory(src)
206
207#install the includes
208add_subdirectory(include)
209
210#install the example
211if(JSONCPP_WITH_EXAMPLE)
212    add_subdirectory(example)
213endif()
214