The open source OpenXR runtime
1#.rst:
2# FindEGL
3# -------
4#
5# Try to find EGL.
6#
7# This will define the following variables:
8#
9# ``EGL_FOUND``
10# True if (the requested version of) EGL is available
11# ``EGL_VERSION``
12# The version of EGL; note that this is the API version defined in the
13# headers, rather than the version of the implementation (eg: Mesa)
14# ``EGL_LIBRARIES``
15# This can be passed to target_link_libraries() instead of the ``EGL::EGL``
16# target
17# ``EGL_INCLUDE_DIRS``
18# This should be passed to target_include_directories() if the target is not
19# used for linking
20# ``EGL_DEFINITIONS``
21# This should be passed to target_compile_options() if the target is not
22# used for linking
23#
24# If ``EGL_FOUND`` is TRUE, it will also define the following imported target:
25#
26# ``EGL::EGL``
27# The EGL library
28#
29# In general we recommend using the imported target, as it is easier to use.
30# Bear in mind, however, that if the target is in the link interface of an
31# exported library, it must be made available by the package config file.
32#
33# Since pre-1.0.0.
34
35# SPDX-License-Identifier: BSD-3-Clause
36#
37# Note: This module is originally from the KDE "Extra CMake Modules" repo,
38# adapted to work standalone. Original source:
39# https://github.com/KDE/extra-cmake-modules/blob/3b0bf71a72789eb2b79310b4f67602115e347f56/find-modules/FindEGL.cmake
40#=============================================================================
41# Copyright 2014 Alex Merry <alex.merry@kde.org>
42# Copyright 2014 Martin Gräßlin <mgraesslin@kde.org>
43# Copyright 2019, 2021 Rylie Pavlik <rylie.pavlik@collabora.com>
44#
45# Redistribution and use in source and binary forms, with or without
46# modification, are permitted provided that the following conditions
47# are met:
48#
49# 1. Redistributions of source code must retain the copyright
50# notice, this list of conditions and the following disclaimer.
51# 2. Redistributions in binary form must reproduce the copyright
52# notice, this list of conditions and the following disclaimer in the
53# documentation and/or other materials provided with the distribution.
54# 3. The name of the author may not be used to endorse or promote products
55# derived from this software without specific prior written permission.
56#
57# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
58# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
59# OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
60# IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
61# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
62# NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
63# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
64# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
65# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
66# THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
67#=============================================================================
68
69include(CheckCXXSourceCompiles)
70include(CMakePushCheckState)
71
72# Use pkg-config to get the directories and then use these values
73# in the FIND_PATH() and FIND_LIBRARY() calls
74if(NOT ANDROID)
75 find_package(PkgConfig QUIET)
76 if(PKGCONFIG_FOUND)
77 pkg_check_modules(PKG_EGL QUIET egl)
78 endif()
79endif()
80
81set(EGL_DEFINITIONS ${PKG_EGL_CFLAGS_OTHER})
82
83find_path(EGL_INCLUDE_DIR
84 NAMES
85 EGL/egl.h
86 HINTS
87 ${PKG_EGL_INCLUDE_DIRS}
88)
89find_library(EGL_LIBRARY
90 NAMES
91 EGL
92 HINTS
93 ${PKG_EGL_LIBRARY_DIRS}
94)
95
96# NB: We do *not* use the version information from pkg-config, as that
97# is the implementation version (eg: the Mesa version)
98if(EGL_INCLUDE_DIR)
99 # egl.h has defines of the form EGL_VERSION_x_y for each supported
100 # version; so the header for EGL 1.1 will define EGL_VERSION_1_0 and
101 # EGL_VERSION_1_1. Finding the highest supported version involves
102 # finding all these defines and selecting the highest numbered.
103 file(READ "${EGL_INCLUDE_DIR}/EGL/egl.h" _EGL_header_contents)
104 string(REGEX MATCHALL
105 "[ \t]EGL_VERSION_[0-9_]+"
106 _EGL_version_lines
107 "${_EGL_header_contents}"
108 )
109 unset(_EGL_header_contents)
110 foreach(_EGL_version_line ${_EGL_version_lines})
111 string(REGEX REPLACE
112 "[ \t]EGL_VERSION_([0-9_]+)"
113 "\\1"
114 _version_candidate
115 "${_EGL_version_line}"
116 )
117 string(REPLACE "_" "." _version_candidate "${_version_candidate}")
118 if(NOT DEFINED EGL_VERSION OR EGL_VERSION VERSION_LESS _version_candidate)
119 set(EGL_VERSION "${_version_candidate}")
120 endif()
121 endforeach()
122 unset(_EGL_version_lines)
123endif()
124
125cmake_push_check_state(RESET)
126list(APPEND CMAKE_REQUIRED_LIBRARIES "${EGL_LIBRARY}")
127list(APPEND CMAKE_REQUIRED_INCLUDES "${EGL_INCLUDE_DIR}")
128
129check_cxx_source_compiles("
130#include <EGL/egl.h>
131
132int main(int argc, char *argv[]) {
133 EGLint x = 0; EGLDisplay dpy = 0; EGLContext ctx = 0;
134 eglDestroyContext(dpy, ctx);
135}" HAVE_EGL)
136
137cmake_pop_check_state()
138
139set(required_vars EGL_INCLUDE_DIR HAVE_EGL)
140if(NOT EMSCRIPTEN)
141 list(APPEND required_vars EGL_LIBRARY)
142endif()
143
144include(FindPackageHandleStandardArgs)
145find_package_handle_standard_args(EGL
146 FOUND_VAR
147 EGL_FOUND
148 REQUIRED_VARS
149 ${required_vars}
150 VERSION_VAR
151 EGL_VERSION
152)
153
154if(EGL_FOUND AND NOT TARGET EGL::EGL)
155 if (EMSCRIPTEN)
156 add_library(EGL::EGL INTERFACE IMPORTED)
157 # Nothing further to be done, system include paths have headers and linkage is implicit.
158 else()
159 add_library(EGL::EGL UNKNOWN IMPORTED)
160 set_target_properties(EGL::EGL PROPERTIES
161 IMPORTED_LOCATION "${EGL_LIBRARY}"
162 INTERFACE_COMPILE_OPTIONS "${EGL_DEFINITIONS}"
163 INTERFACE_INCLUDE_DIRECTORIES "${EGL_INCLUDE_DIR}"
164 )
165 endif()
166endif()
167
168mark_as_advanced(EGL_LIBRARY EGL_INCLUDE_DIR HAVE_EGL)
169
170# compatibility variables
171set(EGL_LIBRARIES ${EGL_LIBRARY})
172set(EGL_INCLUDE_DIRS ${EGL_INCLUDE_DIR})
173set(EGL_VERSION_STRING ${EGL_VERSION})
174
175include(FeatureSummary)
176set_package_properties(EGL PROPERTIES
177 URL "https://www.khronos.org/egl/"
178 DESCRIPTION "A platform-independent mechanism for creating rendering surfaces for use with other graphics libraries, such as OpenGL|ES and OpenVG."
179)