Merge pull request #23092 from webosbrew/feature/webOS
[xbmc.git] / cmake / modules / FindFmt.cmake
blob7f0023be0512aa41f8da048cf7422807b62a3f06
1 # FindFmt
2 # -------
3 # Finds the Fmt library
5 # This will define the following variables::
7 # FMT_FOUND - system has Fmt
8 # FMT_INCLUDE_DIRS - the Fmt include directory
9 # FMT_LIBRARIES - the Fmt libraries
11 # and the following imported targets::
13 #   fmt::fmt   - The Fmt library
15 define_property(TARGET PROPERTY LIB_BUILD
16                        BRIEF_DOCS "This target will be compiling the library"
17                        FULL_DOCS "This target will be compiling the library")
19 set(FORCE_BUILD OFF)
21 # If target exists, no need to rerun find
22 # Allows a module that may be a dependency for multiple libraries to just be executed
23 # once to populate all required variables/targets
24 if((NOT TARGET fmt::fmt OR Fmt_FIND_REQUIRED) AND NOT TARGET fmt)
26   # Build if ENABLE_INTERNAL_FMT, or if required version in find_package call is greater 
27   # than already found FMT_VERSION from a previous find_package call
28   if(ENABLE_INTERNAL_FMT OR (Fmt_FIND_REQUIRED AND FMT_VERSION VERSION_LESS Fmt_FIND_VERSION))
30     include(cmake/scripts/common/ModuleHelpers.cmake)
32     set(MODULE_LC fmt)
34     SETUP_BUILD_VARS()
36     # Check for existing FMT. If version >= FMT-VERSION file version, dont build
37     find_package(FMT CONFIG QUIET)
39     if(Fmt_FIND_VERSION)
40       if(FMT_VERSION VERSION_LESS ${Fmt_FIND_VERSION})
41         set(FORCE_BUILD ON)
42       endif()
43     else()
44       set(FORCE_BUILD ON)
45     endif()
47     if(${FORCE_BUILD} OR FMT_VERSION VERSION_LESS ${${MODULE}_VER})
49       # Set FORCE_BUILD to enable fmt::fmt property that build will occur
50       set(FORCE_BUILD ON)
52       if(APPLE)
53         set(EXTRA_ARGS "-DCMAKE_OSX_ARCHITECTURES=${CMAKE_OSX_ARCHITECTURES}")
54       endif()
56       set(FMT_VERSION ${${MODULE}_VER})
57       # fmt debug uses postfix d for all platforms
58       set(FMT_DEBUG_POSTFIX d)
60       if(WIN32 OR WINDOWS_STORE)
61         set(patches "${CMAKE_SOURCE_DIR}/tools/depends/target/${MODULE_LC}/001-windows-pdb-symbol-gen.patch")
62         generate_patchcommand("${patches}")
63       endif()
65       set(CMAKE_ARGS -DCMAKE_CXX_EXTENSIONS=${CMAKE_CXX_EXTENSIONS}
66                      -DCMAKE_CXX_STANDARD=${CMAKE_CXX_STANDARD}
67                      -DFMT_DOC=OFF
68                      -DFMT_TEST=OFF
69                      -DFMT_INSTALL=ON
70                      "${EXTRA_ARGS}")
72       BUILD_DEP_TARGET()
73     else()
74       # Populate paths for find_package_handle_standard_args
75       find_path(FMT_INCLUDE_DIR NAMES fmt/format.h)
76       find_library(FMT_LIBRARY_RELEASE NAMES fmt)
77       find_library(FMT_LIBRARY_DEBUG NAMES fmtd)
78     endif()
79   else()
80     find_package(FMT 6.1.2 CONFIG REQUIRED QUIET)
82     if(PKG_CONFIG_FOUND)
83       pkg_check_modules(PC_FMT libfmt QUIET)
84       if(PC_FMT_VERSION AND NOT FMT_VERSION)
85         set(FMT_VERSION ${PC_FMT_VERSION})
86       endif()
87     endif()
89     find_path(FMT_INCLUDE_DIR NAMES fmt/format.h
90                               PATHS ${PC_FMT_INCLUDEDIR})
92     find_library(FMT_LIBRARY_RELEASE NAMES fmt
93                                     PATHS ${PC_FMT_LIBDIR})
94     find_library(FMT_LIBRARY_DEBUG NAMES fmtd
95                                    PATHS ${PC_FMT_LIBDIR})
97   endif()
99   include(SelectLibraryConfigurations)
100   select_library_configurations(FMT)
102   include(FindPackageHandleStandardArgs)
103   find_package_handle_standard_args(Fmt
104                                     REQUIRED_VARS FMT_LIBRARY FMT_INCLUDE_DIR
105                                     VERSION_VAR FMT_VERSION)
107   if(FMT_FOUND)
108     set(FMT_LIBRARIES ${FMT_LIBRARY})
109     set(FMT_INCLUDE_DIRS ${FMT_INCLUDE_DIR})
111     # Reorder this to allow handling of FMT_FORCE_BUILD and not duplicate in property
112     if(NOT TARGET fmt::fmt)
113       set_property(GLOBAL APPEND PROPERTY INTERNAL_DEPS_PROP fmt::fmt)
114     endif()
116     if(NOT TARGET fmt::fmt OR FORCE_BUILD)
117       if(NOT TARGET fmt::fmt)
118         add_library(fmt::fmt UNKNOWN IMPORTED)
119       endif()
121       if(FMT_LIBRARY_RELEASE)
122         set_target_properties(fmt::fmt PROPERTIES
123                                        IMPORTED_CONFIGURATIONS RELEASE
124                                        IMPORTED_LOCATION "${FMT_LIBRARY_RELEASE}")
125       endif()
126       if(FMT_LIBRARY_DEBUG)
127         set_target_properties(fmt::fmt PROPERTIES
128                                        IMPORTED_CONFIGURATIONS DEBUG
129                                        IMPORTED_LOCATION "${FMT_LIBRARY_DEBUG}")
130       endif()
131       set_target_properties(fmt::fmt PROPERTIES
132                                      INTERFACE_INCLUDE_DIRECTORIES "${FMT_INCLUDE_DIR}")
134       # If a force build is done, let any calling packages know they may want to rebuild
135       if(FORCE_BUILD)
136         set_target_properties(fmt::fmt PROPERTIES LIB_BUILD ON)
137       endif()
138     endif()
139     if(TARGET fmt)
140       add_dependencies(fmt::fmt fmt)
141     endif()
142   else()
143     if(FMT_FIND_REQUIRED)
144       message(FATAL_ERROR "Fmt lib not found. Maybe use -DENABLE_INTERNAL_FMT=ON")
145     endif()
146   endif()
148   mark_as_advanced(FMT_INCLUDE_DIR FMT_LIBRARY)
149 endif()