From c95802afeb2ebc702bed61a9437f6e85e4f833e0 Mon Sep 17 00:00:00 2001 From: Stenzek Date: Sun, 14 Oct 2018 23:17:31 +1000 Subject: [PATCH] CMake: Make X11 and EGL optional --- CMake/FindEGL.cmake | 54 ++++++++++++++++++++++++ CMakeLists.txt | 35 +++++++++------- Source/Core/Common/CMakeLists.txt | 13 +++--- Source/Core/Common/GL/GLContext.cpp | 65 +++++++++++++++++++---------- Source/Core/UICommon/CMakeLists.txt | 2 +- 5 files changed, 126 insertions(+), 43 deletions(-) create mode 100644 CMake/FindEGL.cmake diff --git a/CMake/FindEGL.cmake b/CMake/FindEGL.cmake new file mode 100644 index 0000000000..dd7a8bcff0 --- /dev/null +++ b/CMake/FindEGL.cmake @@ -0,0 +1,54 @@ +# from https://raw.githubusercontent.com/WebKit/webkit/master/Source/cmake/FindEGL.cmake +# - Try to Find EGL +# Once done, this will define +# +# EGL_FOUND - system has EGL installed. +# EGL_INCLUDE_DIRS - directories which contain the EGL headers. +# EGL_LIBRARIES - libraries required to link against EGL. +# EGL_DEFINITIONS - Compiler switches required for using EGL. +# +# Copyright (C) 2012 Intel Corporation. All rights reserved. +# +# Redistribution and use in source and binary forms, with or without +# modification, are permitted provided that the following conditions +# are met: +# 1. Redistributions of source code must retain the above copyright +# notice, this list of conditions and the following disclaimer. +# 2. Redistributions in binary form must reproduce the above copyright +# notice, this list of conditions and the following disclaimer in the +# documentation and/or other materials provided with the distribution. +# +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER AND ITS CONTRIBUTORS ``AS +# IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, +# THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR +# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR ITS +# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, +# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, +# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; +# OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, +# WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR +# OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF +# ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + + +find_package(PkgConfig) + +pkg_check_modules(PC_EGL egl) + +if (PC_EGL_FOUND) + set(EGL_DEFINITIONS ${PC_EGL_CFLAGS_OTHER}) +endif () + +find_path(EGL_INCLUDE_DIRS NAMES EGL/egl.h + HINTS ${PC_EGL_INCLUDEDIR} ${PC_EGL_INCLUDE_DIRS} +) + +set(EGL_NAMES ${EGL_NAMES} egl EGL) +find_library(EGL_LIBRARIES NAMES ${EGL_NAMES} + HINTS ${PC_EGL_LIBDIR} ${PC_EGL_LIBRARY_DIRS} +) + +include(FindPackageHandleStandardArgs) +FIND_PACKAGE_HANDLE_STANDARD_ARGS(EGL DEFAULT_MSG EGL_INCLUDE_DIRS EGL_LIBRARIES) + +mark_as_advanced(EGL_INCLUDE_DIRS EGL_LIBRARIES) diff --git a/CMakeLists.txt b/CMakeLists.txt index 69440d0c52..e69edabddc 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -414,18 +414,30 @@ if (OPENGL_GL) endif() if(ENABLE_X11) - find_package(X11 REQUIRED) - add_definitions(-DHAVE_X11=1) - message(STATUS "X11 support enabled") - - check_lib(XRANDR xrandr Xrandr) - if(XRANDR_FOUND) - add_definitions(-DHAVE_XRANDR=1) + find_package(X11) + if(X11_FOUND) + add_definitions(-DHAVE_X11=1) + check_lib(XRANDR xrandr Xrandr) + if(XRANDR_FOUND) + add_definitions(-DHAVE_XRANDR=1) + else() + add_definitions(-DHAVE_XRANDR=0) + endif() + pkg_check_modules(X11_INPUT REQUIRED xi>=1.5.0) + message(STATUS "X11 support enabled") else() - add_definitions(-DHAVE_XRANDR=0) + message(WARNING "X11 support enabled but not found. This build will not support X11.") endif() +endif() - pkg_check_modules(X11_INPUT REQUIRED xi>=1.5.0) +if(ENABLE_EGL) + find_package(EGL) + if(EGL_FOUND) + add_definitions(-DHAVE_EGL=1) + message(STATUS "EGL OpenGL interface enabled") + else() + message(WARNING "EGL support enabled but not found. This build will not support EGL.") + endif() endif() if(ENCODE_FRAMEDUMPS) @@ -451,11 +463,6 @@ if(OPROFILING) endif() endif() -if(ENABLE_EGL) - message(STATUS "EGL OpenGL interface enabled") - add_definitions(-DUSE_EGL=1) -endif() - if(ENABLE_EVDEV) find_package(Libudev REQUIRED) find_package(Libevdev REQUIRED) diff --git a/Source/Core/Common/CMakeLists.txt b/Source/Core/Common/CMakeLists.txt index 62fbb9d0d3..b75c88d013 100644 --- a/Source/Core/Common/CMakeLists.txt +++ b/Source/Core/Common/CMakeLists.txt @@ -113,12 +113,15 @@ target_sources(common PRIVATE GL/GLContext.cpp ) -if(ENABLE_EGL) +if(ENABLE_EGL AND EGL_FOUND) target_sources(common PRIVATE GL/GLInterface/EGL.cpp) if(ANDROID) target_sources(common PRIVATE GL/GLInterface/EGLAndroid.cpp) + elseif(ENABLE_X11 AND X11_FOUND) + target_sources(common PRIVATE GL/GLInterface/EGLX11.cpp) endif() - target_link_libraries(common PUBLIC EGL) + target_include_directories(common PRIVATE ${EGL_INCLUDE_DIRS}) + target_link_libraries(common PUBLIC ${EGL_LIBRARIES}) endif() if(WIN32) @@ -128,7 +131,7 @@ if(WIN32) ) elseif(APPLE) target_sources(common PRIVATE GL/GLInterface/AGL.mm) -elseif(ENABLE_X11) +elseif(ENABLE_X11 AND X11_FOUND) target_sources(common PRIVATE GL/GLX11Window.cpp GL/GLInterface/GLX.cpp) @@ -136,10 +139,6 @@ elseif(ENABLE_X11) # GLX has a hard dependency on libGL. # Make sure to link to it if using GLX. target_link_libraries(common PUBLIC ${OPENGL_LIBRARIES}) - - if (ENABLE_EGL) - target_sources(common PRIVATE GL/GLInterface/EGLX11.cpp) - endif() endif() if(CMAKE_SYSTEM_NAME STREQUAL "Linux") diff --git a/Source/Core/Common/GL/GLContext.cpp b/Source/Core/Common/GL/GLContext.cpp index 81bf4e12b6..d4a9ed2e7a 100644 --- a/Source/Core/Common/GL/GLContext.cpp +++ b/Source/Core/Common/GL/GLContext.cpp @@ -8,15 +8,21 @@ #if defined(__APPLE__) #include "Common/GL/GLInterface/AGL.h" -#elif defined(_WIN32) +#endif +#if defined(WIN32) #include "Common/GL/GLInterface/WGL.h" -#elif defined(ANDROID) -#include "Common/GL/GLInterface/EGLAndroid.h" -#elif HAVE_X11 -#include "Common/GL/GLInterface/EGLX11.h" +#endif +#if HAVE_X11 #include "Common/GL/GLInterface/GLX.h" -#elif HAVE_EGL +#endif +#if HAVE_EGL #include "Common/GL/GLInterface/EGL.h" +#if HAVE_X11 +#include "Common/GL/GLInterface/EGLX11.h" +#endif +#if defined(ANDROID) +#include "Common/GL/GLInterface/EGLAndroid.h" +#endif #endif GLContext::~GLContext() = default; @@ -72,22 +78,39 @@ std::unique_ptr GLContext::Create(const WindowSystemInfo& wsi, bool s { std::unique_ptr context; #if defined(__APPLE__) - context = std::make_unique(); -#elif defined(_WIN32) - context = std::make_unique(); -#elif defined(ANDROID) - context = std::make_unique(); -#elif HAVE_X11 - // GLES is not supported via GLX? - if (prefer_egl || prefer_gles) - context = std::make_unique(); - else - context = std::make_unique(); -#elif HAVE_EGL - context = std::make_unique(); -#else - return nullptr; + if (wsi.type == WindowSystemType::MacOS || wsi.type == WindowSystemType::Headless) + context = std::make_unique(); #endif +#if defined(_WIN32) + if (wsi.type == WindowSystemType::Windows) + context = std::make_unique(); +#endif +#if defined(ANDROID) + if (wsi.type == WindowSystemType::Android) + context = std::make_unique(); +#endif +#if HAVE_X11 + if (wsi.type == WindowSystemType::X11) + { + // GLES 3 is not supported via GLX. + const bool use_egl = prefer_egl || prefer_gles; +#if defined(HAVE_EGL) + if (use_egl) + context = std::make_unique(); + else + context = std::make_unique(); +#else + context = std::make_unique(); +#endif + } +#endif +#if HAVE_EGL + if (wsi.type == WindowSystemType::Headless) + context = std::make_unique(); +#endif + + if (!context) + return nullptr; // Option to prefer GLES on desktop platforms, useful for testing. if (prefer_gles) diff --git a/Source/Core/UICommon/CMakeLists.txt b/Source/Core/UICommon/CMakeLists.txt index 5956031531..f5ef5d6f11 100644 --- a/Source/Core/UICommon/CMakeLists.txt +++ b/Source/Core/UICommon/CMakeLists.txt @@ -24,7 +24,7 @@ if ((DEFINED CMAKE_ANDROID_ARCH_ABI AND CMAKE_ANDROID_ARCH_ABI MATCHES "x86|x86_ target_link_libraries(uicommon PRIVATE bdisasm) endif() -if(ENABLE_X11) +if(ENABLE_X11 AND X11_FOUND) target_include_directories(uicommon PRIVATE ${X11_INCLUDE_DIR}) target_sources(uicommon PRIVATE X11Utils.cpp) target_link_libraries(uicommon PUBLIC ${XRANDR_LIBRARIES})