[cosmetic] separate c-lang format commit
[xbmc.git] / cmake / modules / FindTinyXML2.cmake
blobd65d4b597713f5734bc9952a9092261be0b84759
1 #.rst:
2 # FindTinyXML2
3 # -----------
4 # Finds the TinyXML2 library
6 # This will define the following target:
8 #   ${APP_NAME_LC}::TinyXML2   - The TinyXML2 library
10 macro(buildTinyXML2)
11   set(TINYXML2_VERSION ${${MODULE}_VER})
12   set(TINYXML2_DEBUG_POSTFIX d)
14   find_package(Patch MODULE REQUIRED)
16   if(UNIX)
17     # ancient patch (Apple/freebsd) fails to patch tinyxml2 CMakeLists.txt file due to it being crlf encoded
18     # Strip crlf before applying patches.
19     # Freebsd fails even harder and requires both .patch and CMakeLists.txt to be crlf stripped
20     # possibly add requirement for freebsd on gpatch? Wouldnt need to copy/strip the patch file then
21     set(PATCH_COMMAND sed -ie s|\\r\$|| ${CMAKE_BINARY_DIR}/${CORE_BUILD_DIR}/${MODULE_LC}/src/${MODULE_LC}/CMakeLists.txt
22               COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_SOURCE_DIR}/tools/depends/target/tinyxml2/001-debug-pdb.patch ${CMAKE_BINARY_DIR}/${CORE_BUILD_DIR}/${MODULE_LC}/src/${MODULE_LC}/001-debug-pdb.patch
23               COMMAND sed -ie s|\\r\$|| ${CMAKE_BINARY_DIR}/${CORE_BUILD_DIR}/${MODULE_LC}/src/${MODULE_LC}/001-debug-pdb.patch
24               COMMAND ${PATCH_EXECUTABLE} -p1 -i ${CMAKE_BINARY_DIR}/${CORE_BUILD_DIR}/${MODULE_LC}/src/${MODULE_LC}/001-debug-pdb.patch)
25   else()
26     set(PATCH_COMMAND ${PATCH_EXECUTABLE} -p1 -i ${CMAKE_SOURCE_DIR}/tools/depends/target/tinyxml2/001-debug-pdb.patch)
27   endif()
29   if(CMAKE_GENERATOR MATCHES "Visual Studio" OR CMAKE_GENERATOR STREQUAL Xcode)
30     # Multiconfig generators fail due to file(GENERATE tinyxml.pc) command.
31     # This patch makes it generate a distinct named pc file for each build type and rename
32     # pc file on install
33     list(APPEND PATCH_COMMAND COMMAND ${PATCH_EXECUTABLE} -p1 -i ${CMAKE_SOURCE_DIR}/tools/depends/target/tinyxml2/002-multiconfig-gen-pkgconfig.patch)
34   endif()
36   set(CMAKE_ARGS -DCMAKE_INSTALL_PREFIX=${CMAKE_BINARY_DIR}/${CORE_BUILD_DIR}
37                  -DCMAKE_CXX_EXTENSIONS=${CMAKE_CXX_EXTENSIONS}
38                  -DCMAKE_CXX_STANDARD=${CMAKE_CXX_STANDARD}
39                  -Dtinyxml2_BUILD_TESTING=OFF)
41   BUILD_DEP_TARGET()
42 endmacro()
44 if(NOT TARGET ${APP_NAME_LC}::${CMAKE_FIND_PACKAGE_NAME})
45   include(cmake/scripts/common/ModuleHelpers.cmake)
47   set(MODULE_LC tinyxml2)
49   SETUP_BUILD_VARS()
51   find_package(tinyxml2 CONFIG QUIET
52                                HINTS ${DEPENDS_PATH}/lib/cmake
53                                ${${CORE_PLATFORM_NAME_LC}_SEARCH_CONFIG})
55   # Check for existing TINYXML2. If version >= TINYXML2-VERSION file version, dont build
56   # A corner case, but if a linux/freebsd user WANTS to build internal tinyxml2, build anyway
57   if((tinyxml2_VERSION VERSION_LESS ${${MODULE}_VER} AND ENABLE_INTERNAL_TINYXML2) OR
58      ((CORE_SYSTEM_NAME STREQUAL linux OR CORE_SYSTEM_NAME STREQUAL freebsd) AND ENABLE_INTERNAL_TINYXML2))
60     buildTinyXML2()
61   else()
62     if(TARGET tinyxml2::tinyxml2)
63       # This is for the case where a distro provides a non standard (Debug/Release) config type
64       # eg Debian's config file is tinyxml2ConfigTargets-none.cmake
65       # convert this back to either DEBUG/RELEASE or just RELEASE
66       # we only do this because we use find_package_handle_standard_args for config time output
67       # and it isnt capable of handling TARGETS, so we have to extract the info
68       get_target_property(_TINYXML2_CONFIGURATIONS tinyxml2::tinyxml2 IMPORTED_CONFIGURATIONS)
69       foreach(_tinyxml2_config IN LISTS _TINYXML2_CONFIGURATIONS)
70         # Some non standard config (eg None on Debian)
71         # Just set to RELEASE var so select_library_configurations can continue to work its magic
72         string(TOUPPER ${_tinyxml2_config} _tinyxml2_config_UPPER)
73         if((NOT ${_tinyxml2_config_UPPER} STREQUAL "RELEASE") AND
74            (NOT ${_tinyxml2_config_UPPER} STREQUAL "DEBUG"))
75           get_target_property(TINYXML2_LIBRARY_RELEASE tinyxml2::tinyxml2 IMPORTED_LOCATION_${_tinyxml2_config_UPPER})
76         else()
77           get_target_property(TINYXML2_LIBRARY_${_tinyxml2_config_UPPER} tinyxml2::tinyxml2 IMPORTED_LOCATION_${_tinyxml2_config_UPPER})
78         endif()
79       endforeach()
81       # Need this, as we may only get the existing TARGET from system and not build or use pkg-config
82       get_target_property(TINYXML2_INCLUDE_DIR tinyxml2::tinyxml2 INTERFACE_INCLUDE_DIRECTORIES)
83     else()
84       if(PKG_CONFIG_FOUND)
85         pkg_check_modules(PC_TINYXML2 tinyxml2 QUIET)
86       endif()
88       find_path(TINYXML2_INCLUDE_DIR NAMES tinyxml2.h
89                                      HINTS ${DEPENDS_PATH}/include ${PC_TINYXML2_INCLUDEDIR}
90                                      ${${CORE_PLATFORM_LC}_SEARCH_CONFIG})
91       find_library(TINYXML2_LIBRARY_RELEASE NAMES tinyxml2
92                                             HINTS ${DEPENDS_PATH}/lib ${PC_TINYXML2_LIBDIR}
93                                             ${${CORE_PLATFORM_LC}_SEARCH_CONFIG})
94       find_library(TINYXML2_LIBRARY_DEBUG NAMES tinyxml2d
95                                           HINTS ${DEPENDS_PATH}/lib ${PC_TINYXML2_LIBDIR}
96                                           ${${CORE_PLATFORM_LC}_SEARCH_CONFIG})
98       set(TINYXML2_VERSION ${PC_TINYXML2_VERSION})
99     endif()
100   endif()
102   include(SelectLibraryConfigurations)
103   select_library_configurations(TINYXML2)
104   unset(TINYXML2_LIBRARIES)
106   include(FindPackageHandleStandardArgs)
107   find_package_handle_standard_args(TinyXML2
108                                     REQUIRED_VARS TINYXML2_LIBRARY TINYXML2_INCLUDE_DIR
109                                     VERSION_VAR TINYXML2_VERSION)
111   if(TinyXML2_FOUND)
112     # cmake target and not building internal
113     if(TARGET tinyxml2::tinyxml2 AND NOT TARGET tinyxml2)
114       add_library(${APP_NAME_LC}::${CMAKE_FIND_PACKAGE_NAME} ALIAS tinyxml2::tinyxml2)
115     else()
116       add_library(${APP_NAME_LC}::${CMAKE_FIND_PACKAGE_NAME} UNKNOWN IMPORTED)
117       if(TINYXML2_LIBRARY_RELEASE)
118         set_target_properties(${APP_NAME_LC}::${CMAKE_FIND_PACKAGE_NAME} PROPERTIES
119                                                                          IMPORTED_CONFIGURATIONS RELEASE
120                                                                          IMPORTED_LOCATION_RELEASE "${TINYXML2_LIBRARY_RELEASE}")
121       endif()
122       if(TINYXML2_LIBRARY_DEBUG)
123         set_target_properties(${APP_NAME_LC}::${CMAKE_FIND_PACKAGE_NAME} PROPERTIES
124                                                                          IMPORTED_LOCATION_DEBUG "${TINYXML2_LIBRARY_DEBUG}")
125         set_property(TARGET ${APP_NAME_LC}::${CMAKE_FIND_PACKAGE_NAME} APPEND PROPERTY
126                                                                               IMPORTED_CONFIGURATIONS DEBUG)
127       endif()
128       set_target_properties(${APP_NAME_LC}::${CMAKE_FIND_PACKAGE_NAME} PROPERTIES
129                                                                        INTERFACE_INCLUDE_DIRECTORIES "${TINYXML2_INCLUDE_DIR}")
130     endif()
132     if(TARGET tinyxml2)
133       add_dependencies(${APP_NAME_LC}::${CMAKE_FIND_PACKAGE_NAME} tinyxml2)
134     endif()
136     # Add internal build target when a Multi Config Generator is used
137     # We cant add a dependency based off a generator expression for targeted build types,
138     # https://gitlab.kitware.com/cmake/cmake/-/issues/19467
139     # therefore if the find heuristics only find the library, we add the internal build
140     # target to the project to allow user to manually trigger for any build type they need
141     # in case only a specific build type is actually available (eg Release found, Debug Required)
142     # This is mainly targeted for windows who required different runtime libs for different
143     # types, and they arent compatible
144     if(_multiconfig_generator)
145       if(NOT TARGET tinyxml2)
146         buildTinyXML2()
147         set_target_properties(tinyxml2 PROPERTIES EXCLUDE_FROM_ALL TRUE)
148       endif()
149       add_dependencies(build_internal_depends tinyxml2)
150     endif()
151   else()
152     if(TinyXML2_FIND_REQUIRED)
153       message(FATAL_ERROR "TinyXML2 libraries were not found. You may want to try -DENABLE_INTERNAL_TINYXML2=ON")
154     endif()
155   endif()
156 endif()