|
| 1 | +# |
| 2 | +# Generates the glad OpenGL/EGL loader library using the glad2 generator. |
| 3 | +# Sources are generated at build time via the glad submodule's CMake integration. |
| 4 | +# |
| 5 | +include_guard(GLOBAL) |
| 6 | + |
| 7 | +# The glad2 repo does not have a root-level CMakeLists.txt; its CMake integration |
| 8 | +# lives in cmake/CMakeLists.txt which provides the glad_add_library() function. |
| 9 | +add_subdirectory("${CMAKE_SOURCE_DIR}/third-party/glad/cmake" glad2-cmake) |
| 10 | + |
| 11 | +# glad's generator requires jinja2. Find Python first (same interpreter that glad_add_library() |
| 12 | +# will bake into the ninja build command), then check if jinja2 is already importable. |
| 13 | +# Only pip-install if it isn't — this transparently handles system packages (python3-jinja2 on |
| 14 | +# Debian/Ubuntu/Arch/COPR) as well as bare Python environments. |
| 15 | +# |
| 16 | +# GLAD_SKIP_PIP_INSTALL is a hard override for sandboxed environments where pip cannot run at |
| 17 | +# all (e.g. Flatpak). In all normal cases leave it OFF and let the check below decide. |
| 18 | +option(GLAD_SKIP_PIP_INSTALL |
| 19 | + "Hard-skip pip install of jinja2 even if it is not importable. \ |
| 20 | +Only needed in sandboxed build environments (e.g. Flatpak) where pip cannot run." OFF) |
| 21 | + |
| 22 | +if(NOT GLAD_SKIP_PIP_INSTALL) |
| 23 | + # glad's generator requires Python >= 3.8 (importlib.metadata) and jinja2. |
| 24 | + # Prefer the real system Python over any venv/toolchain Python injected into PATH |
| 25 | + # (e.g. GitHub Actions setup-python). STANDARD means FindPython does not give |
| 26 | + # special priority to virtual environments. |
| 27 | + set(Python_FIND_VIRTUALENV STANDARD) # cmake-lint: disable=C0103 |
| 28 | + |
| 29 | + # On Linux, search for a sufficiently new system Python (>= 3.8) explicitly. |
| 30 | + # This is important on distros like OpenSUSE Leap where /usr/bin/python3 is 3.6, |
| 31 | + # but python3.11 or python3.8 may also be installed. Search newest-first so that |
| 32 | + # the best available interpreter is used. The NO_DEFAULT_PATH on the first pass |
| 33 | + # restricts the search to /usr/bin to prefer distro packages over venv/toolchain |
| 34 | + # Pythons (e.g. GitHub Actions setup-python injects its own python3 first on PATH). |
| 35 | + if(UNIX AND NOT APPLE) |
| 36 | + foreach(py_candidate python3.13 python3.12 python3.11 python3.10 python3.9 python3.8 python3) |
| 37 | + find_program(_system_python3 "${py_candidate}" PATHS /usr/bin /usr/local/bin NO_DEFAULT_PATH) |
| 38 | + if(_system_python3) |
| 39 | + # Verify this interpreter is >= 3.8 |
| 40 | + execute_process( |
| 41 | + COMMAND "${_system_python3}" -c |
| 42 | + "import sys; sys.exit(0 if sys.version_info >= (3,8) else 1)" |
| 43 | + RESULT_VARIABLE _py_version_ok |
| 44 | + OUTPUT_QUIET ERROR_QUIET |
| 45 | + ) |
| 46 | + if(_py_version_ok EQUAL 0) |
| 47 | + message(STATUS "glad: using Python interpreter: ${_system_python3}") |
| 48 | + set(Python_EXECUTABLE "${_system_python3}" # cmake-lint: disable=C0103 |
| 49 | + CACHE FILEPATH "Python interpreter" FORCE) |
| 50 | + break() |
| 51 | + else() |
| 52 | + message(STATUS "glad: skipping ${_system_python3} (< 3.8)") |
| 53 | + unset(_system_python3 CACHE) |
| 54 | + endif() |
| 55 | + endif() |
| 56 | + unset(_system_python3 CACHE) |
| 57 | + endforeach() |
| 58 | + endif() |
| 59 | + |
| 60 | + find_package(Python COMPONENTS Interpreter REQUIRED) |
| 61 | + |
| 62 | + # Check whether jinja2 is already importable by the found interpreter. |
| 63 | + execute_process( |
| 64 | + COMMAND "${Python_EXECUTABLE}" -c "import jinja2" |
| 65 | + RESULT_VARIABLE _jinja2_import_result |
| 66 | + OUTPUT_QUIET |
| 67 | + ERROR_QUIET |
| 68 | + ) |
| 69 | + |
| 70 | + if(NOT _jinja2_import_result EQUAL 0) |
| 71 | + message(STATUS "glad: jinja2 not found in ${Python_EXECUTABLE}, installing via pip...") |
| 72 | + execute_process( |
| 73 | + COMMAND "${Python_EXECUTABLE}" -m pip install |
| 74 | + --requirement "${CMAKE_SOURCE_DIR}/third-party/glad/requirements.txt" |
| 75 | + --quiet |
| 76 | + COMMAND_ERROR_IS_FATAL ANY |
| 77 | + ) |
| 78 | + else() |
| 79 | + message(STATUS "glad: jinja2 already available in ${Python_EXECUTABLE}, skipping pip install") |
| 80 | + endif() |
| 81 | +endif() |
| 82 | + |
| 83 | +# Generate the glad library. |
| 84 | +# EGL 1.5 --loader --mx |
| 85 | +# GL compatibility=4.6 --loader --mx |
| 86 | +# EGL_EXT_image_dma_buf_import_modifiers is included to expose eglQueryDmaBufFormatsEXT and |
| 87 | +# eglQueryDmaBufModifiersEXT |
| 88 | +# REPRODUCIBLE avoids fetching the latest spec XML from Khronos at generation time. |
| 89 | +glad_add_library(glad |
| 90 | + STATIC |
| 91 | + REPRODUCIBLE |
| 92 | + LOCATION "${CMAKE_BINARY_DIR}/gladsources/glad" |
| 93 | + LOADER |
| 94 | + MX |
| 95 | + API |
| 96 | + "egl=1.5" |
| 97 | + "gl:compatibility=4.6" |
| 98 | + EXTENSIONS |
| 99 | + EGL_EXT_image_dma_buf_import |
| 100 | + EGL_EXT_image_dma_buf_import_modifiers |
| 101 | +) |
0 commit comments