[cosmetic] separate c-lang format commit
[xbmc.git] / cmake / modules / FindRapidJSON.cmake
blob10a21390d1953d7f49c4f8e87b4184d65bc678f5
1 #.rst:
2 # FindRapidJSON
3 # -----------
4 # Finds the RapidJSON library
6 # This will define the following target:
8 #   ${APP_NAME_LC}::RapidJSON - The RapidJSON library
11 if(NOT TARGET ${APP_NAME_LC}::${CMAKE_FIND_PACKAGE_NAME})
12   include(cmake/scripts/common/ModuleHelpers.cmake)
14   macro(buildrapidjson)
15     set(RapidJSON_VERSION ${${MODULE}_VER})
17     set(patches "${CORE_SOURCE_DIR}/tools/depends/target/rapidjson/001-remove_custom_cxx_flags.patch"
18                 "${CORE_SOURCE_DIR}/tools/depends/target/rapidjson/002-cmake-standardise_config_installpath.patch"
19                 "${CORE_SOURCE_DIR}/tools/depends/target/rapidjson/003-cmake-removedocs-examples.patch"
20                 "${CORE_SOURCE_DIR}/tools/depends/target/rapidjson/004-win-arm64.patch")
22     generate_patchcommand("${patches}")
24     set(CMAKE_ARGS -DRAPIDJSON_BUILD_DOC=OFF
25                    -DRAPIDJSON_BUILD_EXAMPLES=OFF
26                    -DRAPIDJSON_BUILD_TESTS=OFF
27                    -DRAPIDJSON_BUILD_THIRDPARTY_GTEST=OFF)
29     set(BUILD_BYPRODUCTS ${DEPENDS_PATH}/include/rapidjson/rapidjson.h)
31     BUILD_DEP_TARGET()
33     set(RAPIDJSON_INCLUDE_DIRS ${${MODULE}_INCLUDE_DIR})
34   endmacro()
36   set(MODULE_LC rapidjson)
38   SETUP_BUILD_VARS()
40   if(RapidJSON_FIND_VERSION)
41     if(RapidJSON_FIND_VERSION_EXACT)
42       set(RapidJSON_FIND_SPEC "=${RapidJSON_FIND_VERSION_COMPLETE}")
43       set(RapidJSON_CONFIG_SPEC "${RapidJSON_FIND_VERSION_COMPLETE}" EXACT)
44     else()
45       set(RapidJSON_FIND_SPEC ">=${RapidJSON_FIND_VERSION_COMPLETE}")
46       set(RapidJSON_CONFIG_SPEC "${RapidJSON_FIND_VERSION_COMPLETE}")
47     endif()
48   endif()
50   find_package(RapidJSON CONFIG ${RapidJSON_CONFIG_SPEC}
51                                 HINTS ${DEPENDS_PATH}/lib/cmake
52                                 ${${CORE_PLATFORM_NAME_LC}_SEARCH_CONFIG})
54   # Check for existing RAPIDJSON. If version >= RAPIDJSON-VERSION file version, dont build
55   # A corner case, but if a linux/freebsd user WANTS to build internal tinyxml2, build anyway
56   if((RapidJSON_VERSION VERSION_LESS ${${MODULE}_VER} AND ENABLE_INTERNAL_RapidJSON) OR
57      ((CORE_SYSTEM_NAME STREQUAL linux OR CORE_SYSTEM_NAME STREQUAL freebsd) AND ENABLE_INTERNAL_RapidJSON))
58     # Build internal rapidjson
59     buildrapidjson()
60   else()
61     # If RAPIDJSON_INCLUDE_DIRS exists, then the find_package command found a config
62     # and suitable version. If its not, we fall back to a pkgconfig/manual search
63     if(NOT DEFINED RAPIDJSON_INCLUDE_DIRS)
64       find_package(PkgConfig)
65       # Fallback to pkg-config and individual lib/include file search
66       # Do not use pkgconfig on windows
67       if(PKG_CONFIG_FOUND AND NOT WIN32)
68         pkg_check_modules(PC_RapidJSON RapidJSON${RapidJSON_FIND_SPEC} QUIET)
69         set(RapidJSON_VERSION ${PC_RapidJSON_VERSION})
70       endif()
72       find_path(RAPIDJSON_INCLUDE_DIRS NAMES rapidjson/rapidjson.h
73                                        HINTS ${DEPENDS_PATH}/include ${PC_RapidJSON_INCLUDEDIR}
74                                        ${${CORE_PLATFORM_NAME_LC}_SEARCH_CONFIG})
75     endif()
76   endif()
78   include(FindPackageHandleStandardArgs)
79   find_package_handle_standard_args(RapidJSON
80                                     REQUIRED_VARS RAPIDJSON_INCLUDE_DIRS RapidJSON_VERSION
81                                     VERSION_VAR RapidJSON_VERSION)
83   if(RAPIDJSON_FOUND)
84     add_library(${APP_NAME_LC}::${CMAKE_FIND_PACKAGE_NAME} INTERFACE IMPORTED)
85     set_target_properties(${APP_NAME_LC}::${CMAKE_FIND_PACKAGE_NAME} PROPERTIES
86                                                                      INTERFACE_INCLUDE_DIRECTORIES "${RAPIDJSON_INCLUDE_DIRS}")
87     if(TARGET rapidjson)
88       add_dependencies(${APP_NAME_LC}::${CMAKE_FIND_PACKAGE_NAME} rapidjson)
89     endif()
91     # Add internal build target when a Multi Config Generator is used
92     # We cant add a dependency based off a generator expression for targeted build types,
93     # https://gitlab.kitware.com/cmake/cmake/-/issues/19467
94     # therefore if the find heuristics only find the library, we add the internal build
95     # target to the project to allow user to manually trigger for any build type they need
96     # in case only a specific build type is actually available (eg Release found, Debug Required)
97     # This is mainly targeted for windows who required different runtime libs for different
98     # types, and they arent compatible
99     if(_multiconfig_generator)
100       if(NOT TARGET rapidjson)
101         buildrapidjson()
102         set_target_properties(rapidjson PROPERTIES EXCLUDE_FROM_ALL TRUE)
103       endif()
104       add_dependencies(build_internal_depends rapidjson)
105     endif()
106   else()
107     if(RapidJSON_FIND_REQUIRED)
108       message(FATAL_ERROR "RapidJSON library not found. You may want to try -DENABLE_INTERNAL_RapidJSON=ON")
109     endif()
110   endif()
111 endif()