Skip to content

Commit

Permalink
Update CMake docs
Browse files Browse the repository at this point in the history
  • Loading branch information
fwsGonzo committed Dec 13, 2024
1 parent c41b852 commit d986c73
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 11 deletions.
52 changes: 42 additions & 10 deletions docs/godot_docs/cmake.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,32 +10,53 @@ This method requires a local RISC-V compiler installed on your system. If you do

CMake is completely optional. Normally, you can use Docker which compiles for you in the Godot editor.

## Setup & Installation
## Build using Zig

There is a [CMake project in the Godot Sandbox](https://github.com/libriscv/godot-sandbox/tree/main/program/cpp/cmake) repository that can be used to create ELFs with the API pre-included.
In order to build programs you will need to install: CMake, git, Zig

The easiest way to access it is to create a symlink to the cmake folder above in your project so that you can directly reference it in CMake:
[Download Zig](https://ziglang.org/download/) and add the extracted folder to PATH so that it becomes globally accessible. This means that typing `zig` from anywhere should work.

The easiest way to use CMake is to have a look at what [Godot Sandbox Programs](https://github.com/libriscv/godot-sandbox-programs) is doing. It has build scripts for Linux, macOS and Windows, and builds several projects for you.

Either fork the godot-sandbox-programs repository or copy it into a new one that you just created. Then go into the programs folder and remove everything except hello-world. Also edit `programs/CMakeLists.txt` to remove the other projects that you just deleted. You can rename hello-world, if you want.

Now you have two options:
1. If you commit changes and push them, Github Actions will build programs for you and upload them to a draft release. These are ready-to-use and will be very small.
2. You can build programs yourself using any of the root-level scripts. Linux/macOS: `./build.sh` Windows: `./build.cmd`


## Manual CMake setup

There is a [CMake project in the Godot Sandbox](https://github.com/libriscv/godot-sandbox/tree/main/program/cpp/cmake) repository that can be used to create ELFs with the API pre-included. This CMake script supports both RISC-V cross-compilers and Zig cross-compilation.

```cmake
cmake_minimum_required(VERSION 3.10)
project(example LANGUAGES CXX)
# Add the Godot Sandbox build functions
add_subdirectory(cmake)
# 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
example.cpp
)
```

Here `example` is a regular CMake target that you can use like normal. You can have as many programs as you want.
Here `example` becomes a program that you can load in the sandbox. You can add as many programs as you want.

In order to build this project, we will use a simple build script:

```sh
#!/bin/bash

# Change this to reflect your RISC-V toolchain
# You can also use a Zig toolchain here if you want
export CC="riscv64-linux-gnu-gcc-14"
export CXX="riscv64-linux-gnu-g++-14"

Expand All @@ -53,6 +74,19 @@ Remember to make the script executable:
chmod +x build.sh
```

### macOS

You can use Zig as a cross-compiler. Have a look at the first chapter.

The [macOS github action](https://github.com/libriscv/godot-sandbox-programs/blob/main/.github/workflows/zig-macos.yml) shows you how to build on macOS.

### Windows

You can use Zig as a cross-compiler. Have a look at the first chapter.

The [Windows github action](https://github.com/libriscv/godot-sandbox-programs/blob/main/.github/workflows/zig-windows.yml) shows you how to build on Windows.


### Ubuntu and Windows WSL2

On Linux and Windows WSL2 we can install a RISC-V compiler like so:
Expand All @@ -65,6 +99,8 @@ On some systems you may only have access to g++ version 12 or 13. Modify the `bu

### Windows MSYS2

MSYS2 has a RISC-V compiler that is not capable of compiling all kinds of programs. But it's enough to compile most C++.

```sh
pacman -Sy mingw-w64-x86_64-riscv64-unknown-elf-gcc ninja cmake git
mkdir -p build
Expand All @@ -76,10 +112,6 @@ cmake --build .
You can find a working [MSYS2 build example here](https://github.com/libriscv/godot-sandbox-demo/tree/master/json_diff_sample/json_diff). For `unknown-elf`-type toolchains a toolchain file is needed.


### macOS

On macOS there are RISC-V toolchains in brew. Let us know which ones worked for you.

### Arch Linux

```sh
Expand Down
2 changes: 1 addition & 1 deletion docs/godot_intro/cppprogram.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ The default C++ template should look something like this:
```cpp
#include "api.hpp"

extern "C" Variant public_function(String arg) {
PUBLIC Variant public_function(String arg) {
print("Arguments: ", arg);
return "Hello from the other side";
}
Expand Down

0 comments on commit d986c73

Please sign in to comment.