[cosmetic] separate c-lang format commit
[xbmc.git] / cmake / modules / FindCEC.cmake
blobaa2ab9ead7ca64bf8a91a75917c9e5245f599079
1 #.rst:
2 # FindCEC
3 # -------
4 # Finds the libCEC library
6 # This will define the following target:
8 #   ${APP_NAME_LC}::CEC - The libCEC library
10 if(NOT TARGET ${APP_NAME_LC}::${CMAKE_FIND_PACKAGE_NAME})
11   include(cmake/scripts/common/ModuleHelpers.cmake)
13   macro(buildCEC)
14     set(CEC_VERSION ${${MODULE}_VER})
16     set(patches "${CORE_SOURCE_DIR}/tools/depends/target/${MODULE_LC}/001-all-cmakelists.patch"
17                 "${CORE_SOURCE_DIR}/tools/depends/target/${MODULE_LC}/002-all-libceccmakelists.patch"
18                 "${CORE_SOURCE_DIR}/tools/depends/target/${MODULE_LC}/003-all-remove_git_info.patch"
19                 "${CORE_SOURCE_DIR}/tools/depends/target/${MODULE_LC}/004-win-remove_32bit_timet.patch"
20                 "${CORE_SOURCE_DIR}/tools/depends/target/${MODULE_LC}/005-win-pdbstatic.patch")
22     generate_patchcommand("${patches}")
24     set(CMAKE_ARGS -DBUILD_SHARED_LIBS=ON
25                    -DSKIP_PYTHON_WRAPPER=ON
26                    -DCMAKE_PLATFORM_NO_VERSIONED_SONAME=ON)
28     if(WIN32 AND ARCH STREQUAL "x64")
29       # Disable _USE_32BIT_TIME_T for x64 win target
30       list(APPEND CMAKE_ARGS -DWIN64=ON)
31     endif()
33     if(CORE_SYSTEM_NAME STREQUAL "osx")
34       set(CEC_BYPRODUCT_EXTENSION "dylib")
35     endif()
37     BUILD_DEP_TARGET()
39     if(CORE_SYSTEM_NAME STREQUAL "osx")
40       find_program(INSTALL_NAME_TOOL NAMES install_name_tool)
41       add_custom_command(TARGET cec POST_BUILD
42                                     COMMAND ${INSTALL_NAME_TOOL} -id ${CEC_LIBRARY} ${CEC_LIBRARY})
43     endif()
45     add_dependencies(cec ${APP_NAME_LC}::P8Platform)
46   endmacro()
48   # We only need to check p8-platform if we have any intention to build internal
49   if(ENABLE_INTERNAL_CEC)
50     # Check for dependencies - Must be done before SETUP_BUILD_VARS
51     get_libversion_data("p8-platform" "target")
52     find_package(P8Platform ${LIB_P8-PLATFORM_VER} MODULE QUIET REQUIRED)
53     # Check if we want to force a build due to a dependency rebuild
54     get_property(LIB_FORCE_REBUILD TARGET ${APP_NAME_LC}::P8Platform PROPERTY LIB_BUILD)
55   endif()
57   set(MODULE_LC cec)
59   SETUP_BUILD_VARS()
61   # Check for existing libcec. If version >= LIBCEC-VERSION file version, dont build
62   find_package(libcec CONFIG
63                       HINTS ${DEPENDS_PATH}/lib/cmake
64                       ${${CORE_PLATFORM_NAME_LC}_SEARCH_CONFIG})
66   if((libcec_VERSION VERSION_LESS ${${MODULE}_VER} AND ENABLE_INTERNAL_CEC) OR LIB_FORCE_REBUILD)
67     # Build lib
68     buildCEC()
69   else()
70     # if libcec::cec target exists, it meets version requirements
71     # we only do a pkgconfig search when a suitable cmake config returns nothing
72     if(TARGET libcec::cec)
73       get_target_property(_CEC_CONFIGURATIONS libcec::cec IMPORTED_CONFIGURATIONS)
74       foreach(_cec_config IN LISTS _CEC_CONFIGURATIONS)
75         # Some non standard config (eg None on Debian)
76         # Just set to RELEASE var so select_library_configurations can continue to work its magic
77         string(TOUPPER ${_cec_config} _cec_config_UPPER)
78         if((NOT ${_cec_config_UPPER} STREQUAL "RELEASE") AND
79            (NOT ${_cec_config_UPPER} STREQUAL "DEBUG"))
80           get_target_property(CEC_LIBRARY_RELEASE libcec::cec IMPORTED_LOCATION_${_cec_config_UPPER})
81         else()
82           get_target_property(CEC_LIBRARY_${_cec_config_UPPER} libcec::cec IMPORTED_LOCATION_${_cec_config_UPPER})
83         endif()
84       endforeach()
86       # CEC cmake config doesnt include INTERFACE_INCLUDE_DIRECTORIES
87       find_path(CEC_INCLUDE_DIR NAMES libcec/cec.h libCEC/CEC.h
88                                 HINTS ${DEPENDS_PATH}/include
89                                 ${${CORE_PLATFORM_LC}_SEARCH_CONFIG})
90       set(CEC_VERSION ${libcec_VERSION})
91     else()
92       find_package(PkgConfig)
93       # Fallback to pkg-config and individual lib/include file search
94       if(PKG_CONFIG_FOUND)
95         pkg_check_modules(PC_CEC libcec QUIET)
96       endif()
97       find_library(CEC_LIBRARY_RELEASE NAMES cec
98                                        HINTS ${DEPENDS_PATH}/lib ${PC_CEC_LIBDIR}
99                                        ${${CORE_PLATFORM_LC}_SEARCH_CONFIG})
101       find_path(CEC_INCLUDE_DIR NAMES libcec/cec.h libCEC/CEC.h
102                                 HINTS ${DEPENDS_PATH}/include ${PC_CEC_INCLUDEDIR}
103                                 ${${CORE_PLATFORM_LC}_SEARCH_CONFIG})
105       if(PC_CEC_VERSION)
106         set(CEC_VERSION ${PC_CEC_VERSION})
107       elseif(CEC_INCLUDE_DIR AND EXISTS "${CEC_INCLUDE_DIR}/libcec/version.h")
108         file(STRINGS "${CEC_INCLUDE_DIR}/libcec/version.h" cec_version_str REGEX "^[\t ]+LIBCEC_VERSION_TO_UINT\\(.*\\)")
109         string(REGEX REPLACE "^[\t ]+LIBCEC_VERSION_TO_UINT\\(([0-9]+), ([0-9]+), ([0-9]+)\\)" "\\1.\\2.\\3" CEC_VERSION "${cec_version_str}")
110         unset(cec_version_str)
111       endif()
112     endif()
113   endif()
115   include(SelectLibraryConfigurations)
116   select_library_configurations(CEC)
117   unset(CEC_LIBRARIES)
119   include(FindPackageHandleStandardArgs)
120   find_package_handle_standard_args(CEC
121                                     REQUIRED_VARS CEC_LIBRARY CEC_INCLUDE_DIR
122                                     VERSION_VAR CEC_VERSION)
124   if(CEC_FOUND)
125     # cmake target and not building internal
126     if(TARGET libcec::cec AND NOT TARGET cec)
127       add_library(${APP_NAME_LC}::${CMAKE_FIND_PACKAGE_NAME} ALIAS libcec::cec)
128       # We need to append in case the cmake config already has definitions
129       set_property(TARGET libcec::cec APPEND PROPERTY
130                                              INTERFACE_COMPILE_DEFINITIONS HAVE_LIBCEC)
131     # pkgconfig target found
132     elseif(TARGET PkgConfig::PC_CEC)
133       add_library(${APP_NAME_LC}::${CMAKE_FIND_PACKAGE_NAME} ALIAS PkgConfig::PC_CEC)
134       set_property(TARGET PkgConfig::PC_CEC APPEND PROPERTY
135                                             INTERFACE_COMPILE_DEFINITIONS HAVE_LIBCEC)
136     # building internal or no cmake config or pkgconfig
137     else()
138       add_library(${APP_NAME_LC}::${CMAKE_FIND_PACKAGE_NAME} UNKNOWN IMPORTED)
139       set_target_properties(${APP_NAME_LC}::${CMAKE_FIND_PACKAGE_NAME} PROPERTIES
140                                                                        IMPORTED_LOCATION "${CEC_LIBRARY}"
141                                                                        INTERFACE_INCLUDE_DIRECTORIES "${CEC_INCLUDE_DIR}"
142                                                                        INTERFACE_COMPILE_DEFINITIONS HAVE_LIBCEC)
143     endif()
145     if(TARGET cec)
146       add_dependencies(${APP_NAME_LC}::${CMAKE_FIND_PACKAGE_NAME} cec)
147     endif()
149     # Add internal build target when a Multi Config Generator is used
150     # We cant add a dependency based off a generator expression for targeted build types,
151     # https://gitlab.kitware.com/cmake/cmake/-/issues/19467
152     # therefore if the find heuristics only find the library, we add the internal build
153     # target to the project to allow user to manually trigger for any build type they need
154     # in case only a specific build type is actually available (eg Release found, Debug Required)
155     # This is mainly targeted for windows who required different runtime libs for different
156     # types, and they arent compatible
157     if(_multiconfig_generator)
158       if(NOT TARGET cec)
159         buildCEC()
160         set_target_properties(cec PROPERTIES EXCLUDE_FROM_ALL TRUE)
161       endif()
162       add_dependencies(build_internal_depends cec)
163     endif()
164   endif()
165 endif()