$ show Dav1dde/glad
Glad is a multi-language code generator that produces loaders for Khronos graphics APIs (OpenGL, Vulkan, EGL, GLX, WGL). It parses official XML specification registries and generates C/C++ and Rust source files that load all required function pointers at runtime, supporting multiple contexts (MX), header-only builds, debug callbacks, on-demand loading, and alias resolution. The architecture is modular: a Python CLI dispatches to language-specific generators via a plugin system, using Jinja2 templates for output. It includes CMake integration, extensive compile/run tests, and examples for GLFW, SDL2/3, Vulkan, and embedded contexts. The codebase demonstrates strong engineering practicesβclean separation of parsing, generation, and template rendering, with reusable patterns for configuration, plugin discovery, and XML processing.
The LLM identified 5 files worth highlighting for reuse:
Implements a typed configuration system with ConfigOption, constraints (RequirementConstraint, UnsupportedConstraint), and converters for user-provided values. Each option can have a default, validator, and multiple constraints. The class is self-contained and reusable for any CLI or config interface that needs declarative, validated options.
Discovers and loads generators and specifications via Python entry points (glad.generator, glad.specification), with fallback to pkg_resources. Provides a clean, extensible way to add new output languages or API specifications without modifying core code. The pattern is easily extracted for any Python application needing plugin support.
Parses Khronos XML registries using a factory pattern (Type.register subclass mapping), memoization for expensive traversals, topological sorting for type dependency ordering, and a merge function that combines multiple API feature sets. This module is highly reusable for any project that needs to parse complex, hierarchical XML specifications with feature selection and dependency resolution.
The C/C++ generator implements advanced features: resolution of aliased commands/extensions, on-demand function pointer loading (lazy), multi-context (MX) support with per-context function tables, debug callback wrapping, and platform-specific dlopen/GetProcAddress logic. The architecture cleanly separates loader generation from platform details, making it reusable for any project needing runtime symbol resolution for C APIs.
Provides glad_add_library() CMake function that invokes the Python generator during build to produce a library target with configurable options (API, profile, loader, MX, etc.). Includes EXCLUDE_FROM_ALL, REPRODUCIBLE mode, and proper dependency tracking. This pattern is reusable for any code generator that wants seamless CMake integration with build-time generation.