# godotengine/godot-cpp architecture

> Architecture diagram of the GitHub repository godotengine/godot-cpp, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-05-12). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/godotengine/godot-cpp
- Repository: https://github.com/godotengine/godot-cpp

## Overview

##### Project type and purpose
- This is a C++ binding library (“godot-cpp”) for the Godot game engine’s GDExtension API.
- It generates and compiles static C++ code so that Godot projects can call C++ modules via `.gdextension` files.

##### File structure
- Top‐level build orchestrators: CMakeLists.txt, SConstruct, Makefile, cmake/ (platform toolchains), tools/*.py (platform build helpers), binding_generator.py, doc_source_generator.py, build_profile.py.
- Core API code: include/godot_cpp/ (headers and JSON‐based extension_api.json), src/ (C++ implementation organized into core, variant, classes).
- GDNative/GDExtension adapter: gdextension/ (README, interface definitions).
- Tests and examples: test/ (unit tests, example Godot project, build scripts).
- Documentation: doc/ (RST files), README.md.
- CI configuration: .github/workflows/*.yml, misc/scripts for CI helpers.

##### README for additional insights
- Explains versioning strategy (branches and tags).
- Describes build process: how a `.gdextension` file points at compiled libs, the `entry_symbol` initialization flow, and how modules register with ClassDB.
- References code‐generation steps and the template example.

##### Main components
a. Code-Generation & Configuration
- binding_generator.py & extension_api.json → generate C++ header stubs.
- doc_source_generator.py → generate documentation stubs.
b. Build System
- Build orchestration: CMake vs SCons vs Makefile wrappers.
- Platform toolchain modules (cmake/android.cmake, ios.toolchain.cmake, etc).
- tools/*.py per‐platform helper scripts.
- GitHub Actions workflows invoking these builds and caching.
c. Core Library
- Headers: include/godot_cpp/*.hpp
- Sources: src/core, src/variant, src/classes
- gdextension interface layer: gdextension/*.h
d. Tests & Examples
- test/ project: compiles example extension, runs inside a minimal Godot project.
e. Integration with Godot Engine
- `.gdextension` file → Godot Engine loads static lib → calls entry_symbol → registers classes.
- Uses GDExtensionBinding API at runtime.

##### Relationships & interactions
- Code-generation feeds into Core Library source.
- Build System compiles Core Library for each target platform.
- CI workflows invoke Build System and run Tests & Examples.
- Output artifacts (static libs, headers) are packaged and consumed by end‐user Godot projects.
- At runtime, Godot Engine loads the compiled library via the `.gdextension` file and invokes the initialization callbacks.

##### Architectural patterns & principles
- Modular separation: code-generation, build orchestrator, core API, platform‐specific toolchains, tests.
- Plugin/extension pattern: clean interface (gdextension_interface.h) separates host engine from C++ modules.
- Multi‐platform abstraction: cmake toolchains + Python helper scripts.
- CI as code: GitHub Actions + misc/scripts ensure reproducibility.

##### Elements
- binding_generator.py → Core Library headers
- Core Library + Toolchains → Build System → Artifacts
- CI/CD → triggers Build System & Tests
- Artifacts → Godot Engine at runtime
- Yellow for code-generation scripts
- Blue for build orchestration
- Green for library code
- Orange for CI/CD
- Purple for runtime integration

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Code Generation Layer
    subgraph "Code Generation"
        direction TB
        BG["Binding Generator"]:::codegen
        DG["Documentation Generator"]:::codegen
        API["Extension API Schema"]:::codegen
        Stubs["Generated Header Stubs"]:::core
        Docs["Documentation Sources"]:::test
        API -->|"used by"| BG
        BG -->|"generate headers"| Stubs
        DG -->|"generate docs"| Docs
    end

    %% Core Library Layer
    subgraph "Core Library"
        direction TB
        Headers["Public Headers"]:::core
        Interface["GDExtension Interface"]:::core
        Impl["Implementation Sources"]:::core
        Stubs
        Headers --> Impl
        Interface --> Impl
    end

    %% Build System Layer
    subgraph "Build System"
        direction TB
        CMake["CMakeLists.txt"]:::build
        SCons["SCons"]:::build
        Make["Makefile"]:::build
        Profile["Build Profile"]:::build
        Toolchains["Platform Toolchains"]:::build
        Helpers["Platform Helpers"]:::build
        Builder["Build Orchestrator"]:::build
        CMake -->|"invoke"| Builder
        SCons -->|"invoke"| Builder
        Make -->|"invoke"| Builder
        Profile -->|"invoke"| Builder
        Toolchains --> Builder
        Helpers --> Builder
        Builder -->|"compile Core"| Artifacts["Artifacts\n(static libs, headers)"]:::core
    end

    %% CI/CD Layer
    subgraph "CI/CD"
        direction TB
        WF["Workflows"]:::ci
        CIHelpers["CI Helpers"]:::ci
        WF -->|"trigger builds"| CMake
        WF -->|"trigger builds"| SCons
        WF -->|"trigger builds"| Make
        WF -->|"invoke tests"| Tests
    end

    %% Tests & Examples Layer
    subgraph "Tests & Examples"
        direction TB
        Tests["Test & Example Project"]:::test
        Tests -->|"run against"| Artifacts
    end

    %% Runtime Integration Layer
    subgraph "Runtime Integration"
        direction TB
        GD[".gdextension File"]:::runtime
        Engine["Godot Engine"]:::runtime
        Artifacts --> GD
        GD -->|"entry_symbol→GDExtensionBinding→ClassDB"| Engine
    end

    %% Inter-layer Connections
    Docs -->|"feeds docs"| WF
    WF -->|"orchestrates"| Builder

    %% Click Events
    click BG "https://github.com/godotengine/godot-cpp/blob/master/binding_generator.py"
    click DG "https://github.com/godotengine/godot-cpp/blob/master/doc_source_generator.py"
    click API "https://github.com/godotengine/godot-cpp/blob/master/gdextension/extension_api.json"
    click Headers "https://github.com/godotengine/godot-cpp/tree/master/include/godot_cpp/"
    click Interface "https://github.com/godotengine/godot-cpp/blob/master/gdextension/gdextension_interface.h"
    click Impl "https://github.com/godotengine/godot-cpp/tree/master/src/"
    click CMake "https://github.com/godotengine/godot-cpp/blob/master/CMakeLists.txt"
    click SCons "https://github.com/godotengine/godot-cpp/tree/master/SConstruct"
    click Make "https://github.com/godotengine/godot-cpp/tree/master/Makefile"
    click Profile "https://github.com/godotengine/godot-cpp/blob/master/build_profile.py"
    click Toolchains "https://github.com/godotengine/godot-cpp/tree/master/cmake/"
    click Helpers "https://github.com/godotengine/godot-cpp/tree/master/tools/"
    click Tests "https://github.com/godotengine/godot-cpp/tree/master/test/"
    click Docs "https://github.com/godotengine/godot-cpp/tree/master/doc/"
    click WF "https://github.com/godotengine/godot-cpp/blob/master/.github/workflows/ci-cmake.yml"

    %% Styles
    classDef codegen fill:#f9f,stroke:#333,stroke-width:1px
    classDef build fill:#ccf,stroke:#333,stroke-width:1px
    classDef core fill:#cfc,stroke:#333,stroke-width:1px
    classDef ci fill:#fc9,stroke:#333,stroke-width:1px
    classDef test fill:#ffd,stroke:#333,stroke-width:1px
    classDef runtime fill:#c9f,stroke:#333,stroke-width:1px
```
