[WebRTC][GStreamer] Build and use the openh264 based encoder if present on the system
[WebKit-https.git] / Source / ThirdParty / libwebrtc / cmake / FindOpenh264.cmake
1 # Copyright (C) 2020 Sony Interactive Entertainment Inc.
2 # Copyright (C) 2021 Igalia S.L.
3 #
4 # Redistribution and use in source and binary forms, with or without
5 # modification, are permitted provided that the following conditions
6 # are met:
7 # 1. Redistributions of source code must retain the above copyright
8 #    notice, this list of conditions and the following disclaimer.
9 # 2. Redistributions in binary form must reproduce the above copyright
10 #    notice, this list of conditions and the following disclaimer in the
11 #    documentation and/or other materials provided with the distribution.
12 #
13 # THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
14 # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15 # THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 # PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17 # BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18 # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19 # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20 # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21 # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22 # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23 # THE POSSIBILITY OF SUCH DAMAGE.
24
25 #[=======================================================================[.rst:
26 FindOpenh264
27 --------------
28
29 Find Openh264 headers and libraries.
30
31 Imported Targets
32 ^^^^^^^^^^^^^^^^
33
34 ``Openh264::libopenh264``
35   The libopenh264 library, if found.
36
37 Result Variables
38 ^^^^^^^^^^^^^^^^
39
40 This will define the following variables in your project:
41
42 ``Openh264_FOUND``
43   true if (the requested version of) Openh264 is available.
44 ``Openh264_VERSION``
45   the version of Openh264.
46 ``Openh264_LIBRARIES``
47   the libraries to link against to use Openh264.
48 ``Openh264_INCLUDE_DIRS``
49   where to find the Openh264 headers.
50
51 #]=======================================================================]
52
53 find_package(PkgConfig QUIET)
54 pkg_check_modules(PC_Openh264 QUIET openh264)
55 set(Openh264_VERSION ${PC_Openh264_VERSION})
56
57 find_path(Openh264_INCLUDE_DIR
58     NAMES wels/codec_api.h
59     HINTS ${PC_Openh264_INCLUDEDIR} ${PC_Openh264_INCLUDE_DIRS}
60 )
61
62 find_library(Openh264_LIBRARY
63     NAMES ${Openh264_NAMES} openh264
64     HINTS ${PC_Openh264_LIBDIR} ${PC_Openh264_LIBRARY_DIRS}
65 )
66
67 if (Openh264_INCLUDE_DIR AND NOT Openh264_VERSION)
68     if (EXISTS "${Openh264_INCLUDE_DIR}/wels/codec_ver.h")
69         file(READ "${Openh264_INCLUDE_DIR}/wels/codec_ver.h" Openh264_VERSION_CONTENT)
70
71         string(REGEX MATCH "#[ \t]*define[ \t]+OPENH264_MAJOR[ \t]+\\(([0-9]+)\\)" _dummy "${Openh264_VERSION_CONTENT}")
72         set(Openh264_VERSION_MAJOR "${CMAKE_MATCH_1}")
73
74         string(REGEX MATCH "#[ \t]*define[ \t]+OPENH264_MINOR[ \t]+\\(([0-9]+)\\)" _dummy "${Openh264_VERSION_CONTENT}")
75         set(Openh264_VERSION_MINOR "${CMAKE_MATCH_1}")
76
77         string(REGEX MATCH "#[ \t]*define[ \t]+OPENH264_REVISION[ \t]+\\(([0-9]+)\\)" _dummy "${Openh264_VERSION_CONTENT}")
78         set(Openh264_VERSION_REVISION "${CMAKE_MATCH_1}")
79
80         set(Openh264_VERSION "${Openh264_VERSION_MAJOR}.${Openh264_VERSION_MINOR}.${Openh264_VERSION_REVISION}")
81     endif ()
82 endif ()
83
84 include(FindPackageHandleStandardArgs)
85 find_package_handle_standard_args(Openh264
86     FOUND_VAR Openh264_FOUND
87     REQUIRED_VARS Openh264_LIBRARY Openh264_INCLUDE_DIR
88     VERSION_VAR Openh264_VERSION
89 )
90
91 if (Openh264_LIBRARY AND NOT TARGET Openh264::libopenh264)
92     add_library(Openh264::libopenh264 UNKNOWN IMPORTED GLOBAL)
93     set_target_properties(Openh264::libopenh264 PROPERTIES
94         IMPORTED_LOCATION "${Openh264_LIBRARY}"
95         INTERFACE_COMPILE_OPTIONS "${Openh264_COMPILE_OPTIONS}"
96         INTERFACE_INCLUDE_DIRECTORIES "${Openh264_INCLUDE_DIR}"
97     )
98 endif ()
99
100 mark_as_advanced(Openh264_INCLUDE_DIR Openh264_LIBRARY)
101
102 if (Openh264_FOUND)
103     set(Openh264_LIBRARIES ${Openh264_LIBRARY})
104     set(Openh264_INCLUDE_DIRS ${Openh264_INCLUDE_DIR})
105 endif ()