Rust on Dreamcast: Difference between revisions

From dreamcast.wiki
Jump to navigation Jump to search
 
(133 intermediate revisions by the same user not shown)
Line 1: Line 1:
Preliminary support exists for developing for the Dreamcast using the Rust programming language. This is a bit of a challenge, however, as the official Rust compiler is based on the LLVM toolchain infrastructure, which does not support the Dreamcast's CPU SuperH architecture. Dreamcast programming is instead done with GCC, the GNU Compiler Collection. There exists two solutions to this problem:
[[File:Rust-dc-logo.png|thumb|Ferris holding his Dreamcast controller]]


* rustc_codegen_gcc: A libgccjit codegen backend for rustc (preferred method)
'''Rust''' is a popular modern programming language focusing on performance, memory safety, and program correctness. As a "blazingly fast" low-level language, it is viable for running on a Dreamcast console. Doing so presents a bit of a challenge, however, as the official Rust compiler is based on the [https://llvm.org/ LLVM] toolchain infrastructure, which does not support the Dreamcast CPU's SuperH architecture. Dreamcast programming is instead typically done with [https://gcc.gnu.org/ GCC], the GNU Compiler Collection. There are currently two viable solutions to this challenge:
* gccrs: a Rust frontend for GCC


=rustc_codegen_gcc=
* '''rustc_codegen_gcc''', the current preferred method: An experimental codegen backend for the official Rust '''rustc''' compiler which interfaces with GCC's libgccjit API
GCC includes a component called '''libgccjit''' which provides an interface for an embeddable just-in-time code generator, useful for creating interpreters. However, this component can also be used to generate code ahead of time as well. '''rustc_codegen_gcc''' is a project which interfaces the official Rust compiler frontend with the '''libgccjit''' API to generate machine code from Rust using the GCC backend. Using this, we can compile Rust programs for Dreamcast using familiar compiler tools such as <code>rustc</code> and <code>cargo</code>! The familiar borrow checker still works, and one can write <code>#![no_std]</code> crates with full <code>libcore</code> support. An experimental crate binding with [[KallistiOS]] provides <code>liballoc</code> functionality such as a heap and familiar collections like <code>Vec</code>, <code>String</code>, etc. as well.
* '''gccrs''': an experimental Rust frontend for GCC


We will build '''rustc_codegen_gcc''' support for the Dreamcast in the instructions below. Before we begin, though:
Neither solution is complete at this time, but '''rustc_codegen_gcc''' is much further along and is quite usable with some patience with its current limitations. On the other hand, '''gccrs''' can compile for Dreamcast, but is in an earlier state. Below we will focus on using '''rustc_codegen_gcc'''. For more information on using gccrs, see the [[gccrs]] page.
* You must already have a KallistiOS development environment set up. This means you have created a cross-compiling toolchain for SH4 and you have built KallistiOS with it. See [[Getting Started with Dreamcast development]] for more information.
** For the purposes of this guide, we will assume you are using the standard paths for Dreamcast development tools; i.e. your environment is set up in <code>/opt/toolchains/dc</code>.
** Your KallistiOS installation will need its floating point precision setting set to <code>m4-single</code>. This setting is available in the <code>environ.sh</code>, but changing the setting may require you to rebuild your main toolchain if you have not built it with <code>m4-single</code> support. Once you modify the setting in your <code>environ.sh</code> and re-source the <code>environ.sh</code>, you'll need to rebuild KallistiOS for the changes to take effect.
* You must already have a relatively up-to-date Rust installation, either using your operating system's package manager or [https://rustup.rs/ rustup].


==Building a cross-compiling libgccjit.so for rustc_codegen_gcc==
=Current Status=
Before we can use '''rustc_codegen_gcc''', we must compile <code>libgccjit.so</code>, the '''libgccjit''' library, for your system. This entails building a unique copy of the SH4 toolchain in its own directory under <code>/opt/toolchains/dc/rust</code>, using a forked version of GCC with enhancements made to '''libgccjit'''.
* The full '''std''' library is supported.
* A [https://github.com/dreamcast-rs/KallistiOS custom version of KallistiOS] v2.1.0 (latest stable release) is used.
** This version includes minor changes for Rust and a beta libpthread addon layer to interface with the Rust standard library.
** Wrappers are provided to invoke '''cargo''' and '''rustc''' in a familiar manner to compile projects.
* A [https://github.com/dreamcast-rs/kos-sys '''kos-sys'''] raw/unsafe bindings crate is provided for easily linking KallistiOS and its unsafe C API into Rust crates. This crate is in active development, but with a large portion of KallistiOS functionality available now via C FFI.
* A [https://github.com/dreamcast-rs/kos-rs '''kos-rs'''] Rust crate with safe idiomatic interfaces is available but is in very early stages.
** Documentation for the bindings is available at [https://kos-rs.dreamcast.wiki/ kos-rs.dreamcast.wiki].
** If bindings do not yet exist for desired functionality, wrapping of KallistiOS C functions is required to get proper use of KallistiOS at this time. See the [https://doc.rust-lang.org/nomicon/ffi.html Foreign Function Interface page] in the Rustonomicon for further understanding.
** A custom [https://github.com/dreamcast-rs/libc/tree/libc-0.2-kos '''libc''' crate with KallistiOS support] is provided.
* Code generation uses '''rustc_codegen_gcc''', so its limitations apply here. See the [https://blog.antoyo.xyz/ development blog] for more information on its progress.
** rustc_codegen_gcc is pinned to a particular Rust nightly version, but is synced regularly. The current version in use is the '''2024-08-10''' nightly.
** A [https://github.com/dreamcast-rs/rust/tree/kos-2024-08-10 KallistiOS-patched version of the latest nightly] is maintained and updated regularly.
** Panic unwinding, debug info, LTO, etc. are not currently available.
** Architectures dependent on rustc_codegen_gcc are not yet able to be added to the rustc frontend, so a workaround is necessary: MIPS is falsely selected for KallistiOS, causing rustc to emit objects with a MIPS header and a provided wrapper is used when linking to rewrite these MIPS headers to SuperH before passing the compiled objects to the linker.
* A separate [https://github.com/rust-lang/gcc development version of the GCC toolchain] must be used.
** This version of GCC contains the latest libgccjit patches necessary to interface with rustc_codegen_gcc, and will be built to work with the custom KallistiOS's libpthread layer.
** New updates to rustc_codegen_gcc often require new updates to GCC, so recompiling GCC regularly will be necessary to keep up with the latest updates.
** The development version of GCC does not always compile well on every platform. Using a modern Linux platform is recommended for maximum compatibility.
** The custom KallistiOS and GCC environment can still be used to compile code or projects written in C, C++, etc.
* KallistiOS and all related libraries and tools must be compiled using the <code>-m4-single</code> ABI. This is already set up for you in the guide below, but if external libraries are being linked to Rust projects, make sure that code is compiled using the proper ABI.


Using <code>git</code>, clone the <code>rust-for-dreamcast</code> repository to <code>/opt/toolchains/dc/rust</code>:
=Setting up a Rust environment for Dreamcast development=
git clone https://github.com/darcagn/rust-for-dreamcast /opt/toolchains/dc/rust
The following guide will provide instructions for setting up a Rust development environment for Dreamcast.  
Enter your KallistiOS installation's <code>dc-chain</code> directory:
cd /opt/toolchains/dc/kos/utils/dc-chain
Clear out any existing build files:
make clean-keep-archives
Copy the necessary toolchain patches to your <code>dc-chain</code> setup:
cp /opt/toolchains/dc/rust/toolchain/*.diff patches/
Copy the '''rustc_codegen_gcc''' configuration file into place:
cp /opt/toolchains/dc/rust/toolchain/config.mk.rustc_codegen_gcc.sample config.mk
Make any desired changes to the configuration (e.g., change <code>makejobs=-j2</code> to the number of CPU threads you'd like to use during compilation), and then compile the SH4 toolchain:
make build-sh4
When this command is completed successfully, a <code>libgccjit.so</code> will be installed to <code>/opt/toolchains/dc/rust/sh-elf/lib/libgccjit.so</code>.


==Building rustc_codegen_gcc==
''If you run into any errors or other challenges while following this tutorial, or simply need clarification on any of the steps, feel free to ask for assistance on the [https://dcemulation.org/phpBB/viewforum.php?f=29 message board] or [https://discord.gg/wDEfkUh63h our Discord server] and we would be happy to aid you and update the guide for the benefit of future readers and others in the community.''
The <code>rust-for-dreamcast</code> repository contains scripts and wrappers to assist you in building '''rustc_codegen_gcc''' and using it in conjunction with <code>cargo</code> and <code>rustc</code>. We'll need to add the path to those scripts to our <code>PATH</code> environment variable:
export PATH="/opt/toolchains/dc/rust/bin:$PATH"
You may also want to add the above lines to your shell's startup file or else you'll need to run them every time you open a new shell.


Clone the '''rustc_codegen_gcc''' to your rust directory:
==Prerequisites==
git clone https://github.com/rust-lang/rustc_codegen_gcc.git /opt/toolchains/dc/rust/rustc_codegen_gcc
First, we will need to set up the prerequisites.
Set the <code>gcc_path</code> file to the location of our <code>libgccjit.so</code> library file:
echo /opt/toolchains/dc/rust/sh-elf/lib > /opt/toolchains/dc/rust/rustc_codegen_gcc/gcc_path
Various patches need to be applied to '''rustc_codegen_gcc''' for it to compile properly for our target platform. Let's apply them:
rcg-dc patch
Now let's build '''rustc_codegen_gcc'''!
rcg-dc prepare
rcg-dc build


==Compiling individual modules into object files with rustc==
* You must have the [https://rustup.rs/ rustup] tool installed for your operating system to manage Rust toolchain installations. It will automatically manage the latest Rust nightly installations for you. You cannot use a Rust toolchain that may be provided by your operating system vendor.
A wrapper script is included for generating <code>.o</code> object files with <code>rustc</code> for inclusion in a KallistiOS <code>Makefile</code>-based project. If we assume the module file is named <code>example.rs</code>, you'll need to add <code>example.o</code> as an object file in your <code>Makefile</code>'s <code>OBJS =</code> declaration. Additionally, you'll need to add the following lines so that <code>make</code> knows how to compile Rust modules into <code>.o</code> object files:
* Install the dependency packages for your operating system. These are listed in the '''Dependencies''' section on the [[Getting Started with Dreamcast development#Dependencies]] page, with lists provided for most common operating systems.
<syntaxhighlight lang="make">
* We will install our custom Rust toolchain environment within <code>/opt/toolchains/dc/rust</code>. If it does not already exist, create the <code>/opt/toolchains/dc</code> directory and grant it proper permissions using the following commands.
%.o: %.rs
sudo mkdir -p /opt/toolchains/dc
rustc-dc $< -o $@
sudo chmod -R 755 /opt/toolchains/dc
sudo chown -R $(id -u):$(id -g) /opt/toolchains/dc
* Clone the [https://github.com/dreamcast-rs/rust-for-dreamcast/ Rust for Dreamcast] repo containing necessary support files to <code>/opt/toolchains/dc/rust</code>:
git clone https://github.com/dreamcast-rs/rust-for-dreamcast.git /opt/toolchains/dc/rust
 
==Installing our custom KallistiOS and GCC toolchain==
Clone the git repository for our custom Rust-patched version of KallistiOS v2.1.0 stable:
git clone https://github.com/dreamcast-rs/KallistiOS /opt/toolchains/dc/rust/kos
 
Enter the dc-chain directory:
cd /opt/toolchains/dc/rust/kos/utils/dc-chain
 
The <code>Makefile.default.cfg</code> file in this directory has been customized for you, selecting the proper toolchain profile, paths, and build options necessary for building a Rust toolchain using this guide. It is not recommended to adjust the defaults.
 
To build the GCC toolchain using 2 CPU cores, simply type:
make makejobs=2
Adjust the <code>makejobs=...</code> value to use the number of CPU cores available to you on your system in order to speed up compilation. This may take a while to build, so be patient.
 
Once the GCC toolchain is built, source the <code>environ.sh</code> KallistiOS environment settings. A custom <code>environ.sh</code> file has been provided for you, containing settings pre-adjusted for Rust development using this guide.
 
You will need to run the source command to apply the KallistiOS environment settings to your currently running shell. Run the following now, '''and''' ''whenever'' you open a new shell to work on Dreamcast projects:
source /opt/toolchains/dc/rust/misc/environ.sh
 
Enter the KallistiOS directory:
cd /opt/toolchains/dc/rust/kos
 
Build KallistiOS:
make
 
==Building rustc_codegen_gcc and Rust sysroot==
Now that we have a working KallistiOS environment suitable for Rust development set up, we can build the Rust compiler compiler and sysroot components. A script is provided which will download the necessary components from the respective repositories and compile them for you. Run the installer script:
 
/opt/toolchains/dc/rust/misc/install-rust.sh
 
If all goes well, you'll see '''Rust for KallistiOS/Dreamcast installed!''' message!
 
==Building KOS ports libraries==
If desired, you may also built the KOS ports collection of libraries for use with your Rust environment. It is recommended to maintain a separate KOS ports installation for linking with Rust projects in <code>/opt/toolchains/dc/rust/kos-ports</code>. This is because the Rust environment is using the <code>-m4-single</code> ABI, but the KallistiOS default installation will be using the <code>-m4-single-only</code>, which cannot be mixed and matched.
 
Clone the kos-ports repository to your system:
git clone https://github.com/KallistiOS/kos-ports /opt/toolchains/dc/rust/kos-ports
An individual port can be built and installed by entering its directory and running the proper command. For example, installing GLdc:
cd /opt/toolchains/dc/rust/kos-ports/libGL
make install
Or, instead, you may run the script to build all of the included ports:
/opt/toolchains/dc/rust/kos-ports/utils/build-all.sh
 
=Using Rust for Dreamcast=
If all went well, you will now have a working Rust for Dreamcast environment!
 
* '''cargo''' can be invoked to target Dreamcast using the <code>kos-cargo</code> command.
** <code>kos-cargo run</code> can be used to build and run code on a Dreamcast console using [[dcload-ip]] or [[dcload-serial]]. You will need to set up the <code>KOS_LOADER</code> variable with the necessary command in the <code>/opt/toolchains/dc/rust/misc/environ.sh</code> file (and then re-source the file for the change to take effect).
* '''rustc''' can be invoked to target Dreamcast using the <code>kos-rustc</code> command.
 
==Examples==
Examples are included with the Rust for Dreamcast repo to help you get started.
* [https://github.com/dreamcast-rs/rust-for-dreamcast/tree/master/examples/hello <code>hello</code>] demonstrates how to create a simple "Hello, world!" application using cargo.
* [https://github.com/dreamcast-rs/rust-for-dreamcast/tree/master/examples/gldc-cube <code>gldc-cube</code>] demonstrates a Rust project using the GLdc KOS port.
* [https://github.com/dreamcast-rs/rust-for-dreamcast/tree/master/examples/romdisk <code>romdisk</code>] demonstrates how to generate and mount a read-only ROMdisk with a KallistiOS project.
* [https://github.com/dreamcast-rs/rust-for-dreamcast/tree/master/examples/addlib <code>addlib</code>] demonstrates how to create a Rust library that can be included with a KallistiOS project.
* [https://github.com/dreamcast-rs/rust-for-dreamcast/tree/master/examples/atomics <code>atomics</code>] demonstrates the use of atomic functions.
* [https://github.com/dreamcast-rs/rust-for-dreamcast/tree/master/examples/filesystem-io <code>filesystem-io</code>] demonstrates listing directory contents and opening files.
* [https://github.com/dreamcast-rs/rust-for-dreamcast/tree/master/examples/network-time <code>network-time</code>] demonstrates displaying the system time and retrieving the current UTC time from an NTP server using the [[Broadband adapter]] or [[LAN adapter]].
* [https://github.com/dreamcast-rs/rust-for-dreamcast/tree/master/examples/pvr/pvrmark_strips_direct <code>pvrmark_strips_direct</code>] demonstrates the use of PVR direct rendering
* [https://github.com/dreamcast-rs/rust-for-dreamcast/tree/master/examples/threads <code>threads</code>] demonstrates the use of threads.
* [https://github.com/dreamcast-rs/rust-for-dreamcast/tree/master/examples/tokio-async <code>tokio-async</code>] demonstrates the use of the '''tokio''' async runtime.
 
==Creating a new Rust project with Cargo==
First, we'll demonstrate creating a new "Hello, world!" project with <code>kos-cargo</code>. This will follow the [https://github.com/dreamcast-rs/rust-for-dreamcast/tree/master/examples/hello-cargo <code>cargo-hello</code>] example included in the Rust-for-Dreamcast repo.
 
In a directory of your choosing, let's invoke <code>kos-cargo</code> to create a new project and then enter the directory:
kos-cargo new hello
cd hello
 
Let's add our kos-rs crate to gain access to current KallistiOS bindings. Open <code>Cargo.toml</code> in your text editor and add:
<syntaxhighlight lang="toml">
[dependencies]
kos = { package = "kos-rs", git = "https://github.com/dreamcast-rs/kos-rs" }</syntaxhighlight>
 
Now we can open up <code>src/main.rs</code> and write our "Hello, world!" example code:
<syntaxhighlight lang="rust" line>
fn main() {
    println!("Hello, world from Rust!");
}
</syntaxhighlight>
</syntaxhighlight>
Alternatively, you can add those lines to your KallistiOS <code>Makefile.rules</code> file to avoid having to place it in every project's <code>Makefile</code>.


An example "Hello, world!"-style program built in this style which demonstrates basic C interoperation is included with the Rust-for-Dreamcast repository, located at <code>examples/rustc-hello</code>.
Now we can use <code>kos-cargo build</code> to build our project. If all goes well, there will be a <code>target/sh-elf/debug/hello.elf</code> file that can be sent to the Dreamcast with <code>dc-tool</code>.
If you have <code>KOS_LOADER</code> set in the <code>environ.sh</code> file, you can invoke it directly with <code>kos-cargo run</code>.


==Creating a new project using Cargo==
==Creating a Rust project using kos-ports libraries==
* create an empty staticlib crate with target in .cargo/config
[[File:Rust-Cube rustc codegen-gcc demo.gif|thumb|cube example in action]]
* add kos-rs crate from /opt/toolchains/dc/rust/kos-rs path
The [https://github.com/dreamcast-rs/rust-for-dreamcast/tree/master/examples/gldc-cube <code>gldc-cube</code>] example demonstrates creating a rotating 3D cube using Rust as the primary language, while calling C functions provided by the '''GLdc''' library available in kos-ports. This project's initial setup is done the same as the above <code>hello</code> example.


=gccrs=
For this example, we'll need to convert JPEG textures to VQ-encoded textures at compile time using the <code>vqenc</code> utility includes with KallistiOS. To do this, we'll add a <code>build.rs</code> file to the root of the crate with the following code (abridged to show the conversion of only one texture):
'''gccrs''' implements a new Rust compiler frontend for GCC. This essentially means creating a separate new Rust compiler from the ground up using the GCC toolchain infrastructure. This project is in early stages and is targeting the Rust 1.49 revision from December 2020. As of this writing (February 2024), it is not yet able to compile Rust's <code>libcore</code>, so many basic language features are unimplemented or not functional. Additionally, Rust standard tooling like <code>cargo</code> is not available. Borrow checking is not implemented, but the project plans to later use the next-generation Rust borrow checker [https://github.com/rust-lang/polonius Polonius] from the official Rust project.
<syntaxhighlight lang="rust">
fn main() {
    let kos_base = std::env::var("KOS_BASE").expect("Missing $KOS_BASE -- KallistiOS environment not sourced!");
    let vqenc_cmd = kos_base + "/utils/vqenc/vqenc";
    Command::new(&vqenc_cmd)
        .arg("-t")
        .arg("-v")
        .arg("rsrc/tex_claw.jpg")
        .output()
        .expect("vqenc on tex_claw.jpg failed!");
}
</syntaxhighlight>
The texture files are then included in our project using the <syntaxhighlight lang="rust" inline>include_bytes!</syntaxhighlight> macro.


It is possible to use this compiler by building the GCC 14.0.1-dev toolchain or the '''gccrs''' latest toolchain. GCC 14.0.1-dev will get you the latest code upstreamed by the '''gccrs''' team into the main development branch of GCC, while the '''gccrs''' git repo will get you the absolute latest bleeding edge updates to '''gccrs'''. The GCC 14.0.1-dev configuration file is available within the official KallistiOS repo's <code>dc-chain</code> script, while the latest '''gccrs''' configuration is available within the [https://github.com/darcagn/rust-for-dreamcast Rust for Dreamcast] repository. Brief instructions follow for setting up the latest '''gccrs''' toolchain. See [[Getting Started with Dreamcast development]] for more detailed information on how to set up and run <code>dc-chain</code>.
We will need to link the GLdc library in this example, but we don't need to do anything special to add the paths to this library, because adding the common paths to KallistiOS libraries is already done for us in the kos-rs crate. However, if in your project you need to include a separate unique library path, you can add it in your <code>build.rs</code> like so:
<syntaxhighlight lang="rust">
println!("cargo:rustc-link-search=native={}", lib_path);
</syntaxhighlight>


==Building a gccrs-enabled toolchain==
The workings of this example's source code are too great to detail here line-by-line, but the example demonstrates how to use GLdc via the [https://github.com/dreamcast-rs/gldc-sys gldc-sys] crate, which contains unsafe/raw bindings to GLdc. Check out this crate's source code for an example of declaring and binding external C functions, constants, and structures so they can be used in Rust code. Since the entirety of the gldc-cube example uses C FFI functions without safe wrappers, the <code>main()</code> source is wrapped in <code>unsafe {}</code>. In the future, this would be much less necessary as higher level safe Rust bindings to KallistiOS and other libraries become mature.
Follow the [[Getting Started with Dreamcast development]] guide for creating a Dreamcast toolchain until you arrive at the instructions for setting up the <code>dc-chain</code> configuration file. At this point, you should have a shell open to <code>/opt/toolchains/dc/kos/utils/dc-chain</code>.


Clone the [https://github.com/darcagn/rust-for-dreamcast Rust for Dreamcast] repository:
==Creating a Rust library==
git clone https://github.com/darcagn/rust-for-dreamcast.git rust
Next, we'll demonstrate creating a Rust library with <code>kos-cargo</code> that can be included in other Dreamcast code. This will follow the [https://github.com/dreamcast-rs/rust-for-dreamcast/tree/master/examples/addlib <code>addlib</code>] example. Once again, this project's initial setup is done the same as the above <code>hello</code> example, but you'll create the new project using <code>kos-cargo new --lib addlib</code> to specify that we're creating a library named <code>addlib</code>. You'll also need to add the following text to this project's <code>Cargo.toml</code> file:
Copy the GCC patch in place:
cp rust/toolchain/gcc-rs-kos.diff patches/
Copy the <code>dc-chain</code> configuration file into place:
cp rust/toolchain/config.mk.gccrs.sample config.mk
Make any desired changes to the configuration (e.g., change <code>makejobs=-j2</code> to the number of CPU threads you'd like to use during compilation). Note that to avoid conflicting with an existing stable toolchain at the default path (i.e. <code>/opt/toolchains/dc/sh-elf</code>), we will be installing to <code>/opt/toolchains/dc/gccrs/sh-elf</code> instead. To begin compilation and installation, run:
make build-sh4
After building everything, you can clean up the extraneous files in your <code>dc-chain</code> directory by entering:
make clean


==Setting up Makefiles to compile Rust modules==
<syntaxhighlight lang="toml">
As mentioned before, <code>cargo</code> is not available to use with '''gccrs''', so for our example, we will place our <code>.rs</code> modules within a typical KallistiOS <code>Makefile</code> project. If we assume the module file is named <code>example.rs</code>, you'll need to add <code>example.rox</code> as an object file in your <code>Makefile</code>'s <code>OBJS =</code> declaration. Additionally, you'll need to add the following lines so that <code>make</code> knows how to compile Rust modules into <code>rox</code> object files:
[lib]
<syntaxhighlight lang="make">
crate-type = ["staticlib"]
%.rox: %.rs
kos-cc -frust-incomplete-and-experimental-compiler-do-not-use $(CFLAGS) -c $< -o $@
</syntaxhighlight>
</syntaxhighlight>
Alternatively, you can add those lines to your KallistiOS <code>Makefile.rules</code> file to avoid having to place it in every project's <code>Makefile</code>.


In your <code>example.rs</code> file, your <code>main</code> function will need to be declared like so:.
This tells Rust to build a static <code>.a</code> library archive file from our code, which is located in <code>src/lib.rs</code>:
 
<syntaxhighlight lang="rust" line>
#[no_mangle]
pub extern "C" fn print_added(a: isize, b: isize) {
    print!("{}", a + b);
}


<syntaxhighlight lang="rust">
#[no_mangle]
#[no_mangle]
pub extern fn main() -> i32 {
pub extern "C" fn add_integers(a: isize, b: isize) -> isize {
     [...]
     a + b
}
}
</syntaxhighlight>
</syntaxhighlight>


Make sure before you compile your code that you set <code>export KOS_CC_BASE="/opt/toolchains/dc/gccrs/sh-elf"</code> in your KallistiOS <code>environ.sh</code> file or <code>make</code> will not find your '''gccrs''' compiler executable.
Two simple functions are provided: one for adding two integers and returning the result, and another for adding two integers and printing the result as text. Because these functions use <syntaxhighlight lang="rust" inline>#[no_mangle]</syntaxhighlight> and are declared <syntaxhighlight lang="rust" inline>extern "C"</syntaxhighlight>, they can be called by name in C code that links this library.
 
When built using <code>kos-cargo build</code>, a <code>target/sh-elf/debug/libaddlib.a</code> file will be generated. This can be linked into other projects to gain the use of these functions.
 
For example, this can be added to a standard <code>Makefile</code>-based KallistiOS project by editing the <code>Makefile</code>:
<syntaxhighlight lang="make">
$(TARGET): $(OBJS)
kos-cc -o $(TARGET) $(OBJS) -L/opt/toolchains/dc/rust/examples/cargo-addlib/target/sh-elf/debug -laddlib
</syntaxhighlight>
 
Then, we can use the code in our C source:
<syntaxhighlight lang="c">
/* Declare the external function from the Rust library */
int add_integers(int a, int b);
 
/* Use the function */
printf("Five plus six is %d\n", add_integers(5, 6));
</syntaxhighlight>
 
==Using the libc crate with KallistiOS==
If any of the crates in your project's dependency tree requires the '''libc''' crate, you will need to override fetching the default crate with our custom libc crate with KallistiOS/Dreamcast definitions. This custom version was copied to <code>/opt/toolchains/dc/rust/libc</code> when building the Rust sysroot earlier. Add the following text to your <code>Cargo.toml</code>:
<syntaxhighlight lang="toml">
[patch.crates-io]
libc = { path = "/opt/toolchains/dc/rust/libc" }
</syntaxhighlight>
 
==Adjusting build flags==
Build settings can be adjusted through the <code>KOS_RCG_RUSTFLAGS</code> variable in the <code>environ.sh</code> file under the <code>### Rust Flags</code> section.
* To pass arguments to the '''rustc''' frontend, e.g. to disable debug assertions:
** <syntaxhighlight lang="bash" inline>export KOS_RCG_RUSTFLAGS="${KOS_RCG_RUSTFLAGS} -Cdebug-assertions=no"</syntaxhighlight>
* To pass arguments to the GCC backend, e.g. to enable the <code>-freorder-blocks-algorithm=simple</code> optimization for Rust code:
** <syntaxhighlight lang="bash" inline>export KOS_RCG_RUSTFLAGS="${KOS_RCG_RUSTFLAGS} -Cllvm-args=-freorder-blocks-algorithm=simple"</syntaxhighlight>

Latest revision as of 23:20, 11 December 2024

Ferris holding his Dreamcast controller

Rust is a popular modern programming language focusing on performance, memory safety, and program correctness. As a "blazingly fast" low-level language, it is viable for running on a Dreamcast console. Doing so presents a bit of a challenge, however, as the official Rust compiler is based on the LLVM toolchain infrastructure, which does not support the Dreamcast CPU's SuperH architecture. Dreamcast programming is instead typically done with GCC, the GNU Compiler Collection. There are currently two viable solutions to this challenge:

  • rustc_codegen_gcc, the current preferred method: An experimental codegen backend for the official Rust rustc compiler which interfaces with GCC's libgccjit API
  • gccrs: an experimental Rust frontend for GCC

Neither solution is complete at this time, but rustc_codegen_gcc is much further along and is quite usable with some patience with its current limitations. On the other hand, gccrs can compile for Dreamcast, but is in an earlier state. Below we will focus on using rustc_codegen_gcc. For more information on using gccrs, see the gccrs page.

Current Status

  • The full std library is supported.
  • A custom version of KallistiOS v2.1.0 (latest stable release) is used.
    • This version includes minor changes for Rust and a beta libpthread addon layer to interface with the Rust standard library.
    • Wrappers are provided to invoke cargo and rustc in a familiar manner to compile projects.
  • A kos-sys raw/unsafe bindings crate is provided for easily linking KallistiOS and its unsafe C API into Rust crates. This crate is in active development, but with a large portion of KallistiOS functionality available now via C FFI.
  • A kos-rs Rust crate with safe idiomatic interfaces is available but is in very early stages.
  • Code generation uses rustc_codegen_gcc, so its limitations apply here. See the development blog for more information on its progress.
    • rustc_codegen_gcc is pinned to a particular Rust nightly version, but is synced regularly. The current version in use is the 2024-08-10 nightly.
    • A KallistiOS-patched version of the latest nightly is maintained and updated regularly.
    • Panic unwinding, debug info, LTO, etc. are not currently available.
    • Architectures dependent on rustc_codegen_gcc are not yet able to be added to the rustc frontend, so a workaround is necessary: MIPS is falsely selected for KallistiOS, causing rustc to emit objects with a MIPS header and a provided wrapper is used when linking to rewrite these MIPS headers to SuperH before passing the compiled objects to the linker.
  • A separate development version of the GCC toolchain must be used.
    • This version of GCC contains the latest libgccjit patches necessary to interface with rustc_codegen_gcc, and will be built to work with the custom KallistiOS's libpthread layer.
    • New updates to rustc_codegen_gcc often require new updates to GCC, so recompiling GCC regularly will be necessary to keep up with the latest updates.
    • The development version of GCC does not always compile well on every platform. Using a modern Linux platform is recommended for maximum compatibility.
    • The custom KallistiOS and GCC environment can still be used to compile code or projects written in C, C++, etc.
  • KallistiOS and all related libraries and tools must be compiled using the -m4-single ABI. This is already set up for you in the guide below, but if external libraries are being linked to Rust projects, make sure that code is compiled using the proper ABI.

Setting up a Rust environment for Dreamcast development

The following guide will provide instructions for setting up a Rust development environment for Dreamcast.

If you run into any errors or other challenges while following this tutorial, or simply need clarification on any of the steps, feel free to ask for assistance on the message board or our Discord server and we would be happy to aid you and update the guide for the benefit of future readers and others in the community.

Prerequisites

First, we will need to set up the prerequisites.

  • You must have the rustup tool installed for your operating system to manage Rust toolchain installations. It will automatically manage the latest Rust nightly installations for you. You cannot use a Rust toolchain that may be provided by your operating system vendor.
  • Install the dependency packages for your operating system. These are listed in the Dependencies section on the Getting Started with Dreamcast development#Dependencies page, with lists provided for most common operating systems.
  • We will install our custom Rust toolchain environment within /opt/toolchains/dc/rust. If it does not already exist, create the /opt/toolchains/dc directory and grant it proper permissions using the following commands.
sudo mkdir -p /opt/toolchains/dc
sudo chmod -R 755 /opt/toolchains/dc
sudo chown -R $(id -u):$(id -g) /opt/toolchains/dc
  • Clone the Rust for Dreamcast repo containing necessary support files to /opt/toolchains/dc/rust:
git clone https://github.com/dreamcast-rs/rust-for-dreamcast.git /opt/toolchains/dc/rust

Installing our custom KallistiOS and GCC toolchain

Clone the git repository for our custom Rust-patched version of KallistiOS v2.1.0 stable:

git clone https://github.com/dreamcast-rs/KallistiOS /opt/toolchains/dc/rust/kos

Enter the dc-chain directory:

cd /opt/toolchains/dc/rust/kos/utils/dc-chain

The Makefile.default.cfg file in this directory has been customized for you, selecting the proper toolchain profile, paths, and build options necessary for building a Rust toolchain using this guide. It is not recommended to adjust the defaults.

To build the GCC toolchain using 2 CPU cores, simply type:

make makejobs=2

Adjust the makejobs=... value to use the number of CPU cores available to you on your system in order to speed up compilation. This may take a while to build, so be patient.

Once the GCC toolchain is built, source the environ.sh KallistiOS environment settings. A custom environ.sh file has been provided for you, containing settings pre-adjusted for Rust development using this guide.

You will need to run the source command to apply the KallistiOS environment settings to your currently running shell. Run the following now, and whenever you open a new shell to work on Dreamcast projects:

source /opt/toolchains/dc/rust/misc/environ.sh

Enter the KallistiOS directory:

cd /opt/toolchains/dc/rust/kos

Build KallistiOS:

make

Building rustc_codegen_gcc and Rust sysroot

Now that we have a working KallistiOS environment suitable for Rust development set up, we can build the Rust compiler compiler and sysroot components. A script is provided which will download the necessary components from the respective repositories and compile them for you. Run the installer script:

/opt/toolchains/dc/rust/misc/install-rust.sh

If all goes well, you'll see Rust for KallistiOS/Dreamcast installed! message!

Building KOS ports libraries

If desired, you may also built the KOS ports collection of libraries for use with your Rust environment. It is recommended to maintain a separate KOS ports installation for linking with Rust projects in /opt/toolchains/dc/rust/kos-ports. This is because the Rust environment is using the -m4-single ABI, but the KallistiOS default installation will be using the -m4-single-only, which cannot be mixed and matched.

Clone the kos-ports repository to your system:

git clone https://github.com/KallistiOS/kos-ports /opt/toolchains/dc/rust/kos-ports

An individual port can be built and installed by entering its directory and running the proper command. For example, installing GLdc:

cd /opt/toolchains/dc/rust/kos-ports/libGL
make install

Or, instead, you may run the script to build all of the included ports:

/opt/toolchains/dc/rust/kos-ports/utils/build-all.sh

Using Rust for Dreamcast

If all went well, you will now have a working Rust for Dreamcast environment!

  • cargo can be invoked to target Dreamcast using the kos-cargo command.
    • kos-cargo run can be used to build and run code on a Dreamcast console using dcload-ip or dcload-serial. You will need to set up the KOS_LOADER variable with the necessary command in the /opt/toolchains/dc/rust/misc/environ.sh file (and then re-source the file for the change to take effect).
  • rustc can be invoked to target Dreamcast using the kos-rustc command.

Examples

Examples are included with the Rust for Dreamcast repo to help you get started.

  • hello demonstrates how to create a simple "Hello, world!" application using cargo.
  • gldc-cube demonstrates a Rust project using the GLdc KOS port.
  • romdisk demonstrates how to generate and mount a read-only ROMdisk with a KallistiOS project.
  • addlib demonstrates how to create a Rust library that can be included with a KallistiOS project.
  • atomics demonstrates the use of atomic functions.
  • filesystem-io demonstrates listing directory contents and opening files.
  • network-time demonstrates displaying the system time and retrieving the current UTC time from an NTP server using the Broadband adapter or LAN adapter.
  • pvrmark_strips_direct demonstrates the use of PVR direct rendering
  • threads demonstrates the use of threads.
  • tokio-async demonstrates the use of the tokio async runtime.

Creating a new Rust project with Cargo

First, we'll demonstrate creating a new "Hello, world!" project with kos-cargo. This will follow the cargo-hello example included in the Rust-for-Dreamcast repo.

In a directory of your choosing, let's invoke kos-cargo to create a new project and then enter the directory:

kos-cargo new hello
cd hello

Let's add our kos-rs crate to gain access to current KallistiOS bindings. Open Cargo.toml in your text editor and add:

[dependencies]
kos = { package = "kos-rs", git = "https://github.com/dreamcast-rs/kos-rs" }

Now we can open up src/main.rs and write our "Hello, world!" example code:

fn main() {
    println!("Hello, world from Rust!");
}

Now we can use kos-cargo build to build our project. If all goes well, there will be a target/sh-elf/debug/hello.elf file that can be sent to the Dreamcast with dc-tool. If you have KOS_LOADER set in the environ.sh file, you can invoke it directly with kos-cargo run.

Creating a Rust project using kos-ports libraries

cube example in action

The gldc-cube example demonstrates creating a rotating 3D cube using Rust as the primary language, while calling C functions provided by the GLdc library available in kos-ports. This project's initial setup is done the same as the above hello example.

For this example, we'll need to convert JPEG textures to VQ-encoded textures at compile time using the vqenc utility includes with KallistiOS. To do this, we'll add a build.rs file to the root of the crate with the following code (abridged to show the conversion of only one texture):

fn main() {
    let kos_base = std::env::var("KOS_BASE").expect("Missing $KOS_BASE -- KallistiOS environment not sourced!");
    let vqenc_cmd = kos_base + "/utils/vqenc/vqenc";
    Command::new(&vqenc_cmd)
        .arg("-t")
        .arg("-v")
        .arg("rsrc/tex_claw.jpg")
        .output()
        .expect("vqenc on tex_claw.jpg failed!");
}

The texture files are then included in our project using the include_bytes! macro.

We will need to link the GLdc library in this example, but we don't need to do anything special to add the paths to this library, because adding the common paths to KallistiOS libraries is already done for us in the kos-rs crate. However, if in your project you need to include a separate unique library path, you can add it in your build.rs like so:

println!("cargo:rustc-link-search=native={}", lib_path);

The workings of this example's source code are too great to detail here line-by-line, but the example demonstrates how to use GLdc via the gldc-sys crate, which contains unsafe/raw bindings to GLdc. Check out this crate's source code for an example of declaring and binding external C functions, constants, and structures so they can be used in Rust code. Since the entirety of the gldc-cube example uses C FFI functions without safe wrappers, the main() source is wrapped in unsafe {}. In the future, this would be much less necessary as higher level safe Rust bindings to KallistiOS and other libraries become mature.

Creating a Rust library

Next, we'll demonstrate creating a Rust library with kos-cargo that can be included in other Dreamcast code. This will follow the addlib example. Once again, this project's initial setup is done the same as the above hello example, but you'll create the new project using kos-cargo new --lib addlib to specify that we're creating a library named addlib. You'll also need to add the following text to this project's Cargo.toml file:

[lib]
crate-type = ["staticlib"]

This tells Rust to build a static .a library archive file from our code, which is located in src/lib.rs:

#[no_mangle]
pub extern "C" fn print_added(a: isize, b: isize) {
    print!("{}", a + b);
}

#[no_mangle]
pub extern "C" fn add_integers(a: isize, b: isize) -> isize {
    a + b
}

Two simple functions are provided: one for adding two integers and returning the result, and another for adding two integers and printing the result as text. Because these functions use #[no_mangle] and are declared extern "C", they can be called by name in C code that links this library.

When built using kos-cargo build, a target/sh-elf/debug/libaddlib.a file will be generated. This can be linked into other projects to gain the use of these functions.

For example, this can be added to a standard Makefile-based KallistiOS project by editing the Makefile:

$(TARGET): $(OBJS)
	kos-cc -o $(TARGET) $(OBJS) -L/opt/toolchains/dc/rust/examples/cargo-addlib/target/sh-elf/debug -laddlib

Then, we can use the code in our C source:

/* Declare the external function from the Rust library */
int add_integers(int a, int b);

/* Use the function */
printf("Five plus six is %d\n", add_integers(5, 6));

Using the libc crate with KallistiOS

If any of the crates in your project's dependency tree requires the libc crate, you will need to override fetching the default crate with our custom libc crate with KallistiOS/Dreamcast definitions. This custom version was copied to /opt/toolchains/dc/rust/libc when building the Rust sysroot earlier. Add the following text to your Cargo.toml:

[patch.crates-io]
libc = { path = "/opt/toolchains/dc/rust/libc" }

Adjusting build flags

Build settings can be adjusted through the KOS_RCG_RUSTFLAGS variable in the environ.sh file under the ### Rust Flags section.

  • To pass arguments to the rustc frontend, e.g. to disable debug assertions:
    • export KOS_RCG_RUSTFLAGS="${KOS_RCG_RUSTFLAGS} -Cdebug-assertions=no"
  • To pass arguments to the GCC backend, e.g. to enable the -freorder-blocks-algorithm=simple optimization for Rust code:
    • export KOS_RCG_RUSTFLAGS="${KOS_RCG_RUSTFLAGS} -Cllvm-args=-freorder-blocks-algorithm=simple"