Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Build using Zig toolchain from within Godot editor #236

Open
fwsGonzo opened this issue Dec 13, 2024 · 0 comments
Open

Build using Zig toolchain from within Godot editor #236

fwsGonzo opened this issue Dec 13, 2024 · 0 comments
Labels
documentation Improvements or additions to documentation enhancement New feature or request

Comments

@fwsGonzo
Copy link
Collaborator

fwsGonzo commented Dec 13, 2024

It is currently possible to build C++ from within the editor in two major ways:

  1. Docker automatically picks up all .cpp files in a folder and compiles an ELF file on all platforms
  2. When CMake is present, the Godot editor calls cmake --build instead, turning it into a CMake build

This means that it is possible to build any kind of project within the Godot editor at present.

Most of the friction currently lies in the CMake configuration step. Many are not familiar with how to follow basic instructions download CMake, git, Ninja and Zig and initialize a CMake project with Zig as a cross compiler. I'm joking. Nobody should need to know all of that. It would be very beneficial for everyone (me included) if we could automate this process on the 3 desktop platforms that we now support with Zig. There is one extra step on Windows where we are forced to use Ninja as the build executor. We could in theory just use Ninja on all platforms.

So, perhaps what we can do is to make it possible to download these dependencies and initialize a CMake project in the root of the current Godot project. Ideas and implementation efforts are welcome.

Complete example for macOS:

  1. Install dependencies. Different on all platforms.
brew install git cmake
  1. Download zig nightly somewhere and add to PATH. It will use 350MB after extraction. Same on all platforms, just different URL.
wget https://ziglang.org/builds/zig-macos-aarch64-0.14.0-dev.2441+3670910f2.tar.xz
tar -xf zig-macos-aarch64-0.14.0-dev.2441+3670910f2.tar.xz
export PATH=$PWD/zig-macos-aarch64-0.14.0-dev.2441+3670910f2:$PATH
  1. Create cmake folder in project root. Same on all platforms.
mkdir -p cmake
cd cmake
  1. Create template CMakeLists.txt in cmake folder. Same on all platforms.
cmake_minimum_required(VERSION 3.14)
project(example)

# Fetch godot-sandbox repository (add_subdirectory is implicitly called)
include(FetchContent)
FetchContent_Declare(
	godot-sandbox
	GIT_REPOSITORY https://github.com/libriscv/godot-sandbox.git
	GIT_TAG        main
	SOURCE_SUBDIR  "program/cpp/cmake"
)
FetchContent_MakeAvailable(godot-sandbox)

add_sandbox_program(example.elf
    example.cpp
)
  1. Create toolchain.cmake in the cmake folder. Same on all platforms.

Copy toolchain.cmake from Godot Sandbox Programs repo.

  1. Initialize CMake with the toolchain file as argument. The toolchain file is only used to initialize CMake. Same on all platforms.
mkdir -p .build
cd .build
cmake .. -DCMAKE_BUILD_TYPE=Release -DCMAKE_TOOLCHAIN_FILE=../toolchain.cmake

That's it. Now that CMake is initialized, it doesn't have to be initialized again. We can now build using CMake from the editor. As you can see, almost all steps are the same on all platforms. On Windows the CMake invocation needs to add -G Ninja.

The example CMake script looks for example.cpp and builds example.elf. It has full C++ API and Godot run-time API support.

@fwsGonzo fwsGonzo added enhancement New feature or request documentation Improvements or additions to documentation labels Dec 13, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant