Gccrs: Difference between revisions

From dreamcast.wiki
Jump to navigation Jump to search
No edit summary
No edit summary
 
(7 intermediate revisions by the same user not shown)
Line 1: Line 1:
'''gccrs''' is the Rust compiler frontend for GCC, currently under active development. This Rust compiler is a new implementation of Rust 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.
'''gccrs''' is the Rust compiler frontend for GCC, currently under active development. This Rust compiler is a new implementation of Rust 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.


Although it is in early stages and highly experimental for Dreamcast dev purposes, 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>.
At this time, it is recommended to use [[Rust on Dreamcast|'''rustc_codegen_gcc''']] to develop for Dreamcast using Rust instead of gccrs.
 
Although it is in early stages and highly experimental for Dreamcast dev purposes, it is possible to use this compiler by building a GCC toolchain from a development version or the '''gccrs''' latest toolchain. KallisiOS's '''dc-chain''' scripts include profiles and patches for this. The '''gccrs-dev''' profile will provide with the absolute latest bleeding edge updates to '''gccrs''', while the '''14.1.1-dev''' and '''15.0.1-dev''' profiles will provide the latest changes the '''gccrs''' team has upstreamed to mainline GCC.


==Building a gccrs-enabled toolchain==
==Building a gccrs-enabled toolchain==
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.  
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.  


Make sure your shell is currently in the correct directory:
* Make sure your shell is currently in the correct directory:
  cd /opt/toolchains/dc/kos/utils/dc-chain
  cd /opt/toolchains/dc/kos/utils/dc-chain
Clone the [https://github.com/darcagn/rust-for-dreamcast Rust for Dreamcast] repository:
 
git clone https://github.com/darcagn/rust-for-dreamcast.git rust
* (Optional) Make any desired changes to the <code style="inline">Makefile.cfg</code> configuration. Note that to avoid conflicting with an existing stable toolchain at the default path (i.e. <code style="inline">/opt/toolchains/dc/sh-elf</code>), we will be installing to <code>/opt/toolchains/dc/sh-elf-gccrs</code> instead.
Copy the GCC patch in place:
 
cp rust/toolchain/gcc-rs-kos.diff patches/
* To begin compilation and installation, run:
Copy the <code>dc-chain</code> configuration file into place:
  make toolchain_profile=gccrs-dev enable_rust=1 sh_toolchain_path=/opt/toolchains/dc/sh-elf-gccrs
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:
* After building everything, you can clean up the extraneous files in your <code>dc-chain</code> directory by entering:
  make build-sh4
After building everything, you can clean up the extraneous files in your <code>dc-chain</code> directory by entering:
  make clean
  make clean


Line 36: Line 36:
</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.
Make sure before you compile your code that you set <code>export KOS_CC_BASE="/opt/toolchains/dc/sh-elf-gccrs"</code> in your KallistiOS <code>environ.sh</code> file or <code>make</code> will not find your '''gccrs''' compiler executable.
 
==Example project==
The [https://github.com/darcagn/rust-fortran-cube/ <code>rust-fortran-cube</code>] repository contains an example project using gccrs in conjunction with GFortran, KallistiOS, and GLdc. View [https://www.youtube.com/watch?v=VUiRoEcpvtI footage of the example] running. This example was created by Eric Fradella and used as a demo in the FOSDEM 2024 talk [https://fosdem.org/2024/schedule/event/fosdem-2024-2634-sega-dreamcast-homebrew-with-gcc/ Sega Dreamcast Homebrew with GCC] by [[User:GyroVorbis|Falco Girgis]].

Latest revision as of 16:00, 8 May 2024

gccrs is the Rust compiler frontend for GCC, currently under active development. This Rust compiler is a new implementation of Rust 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 libcore, so many basic language features are unimplemented or not functional. Additionally, Rust standard tooling like cargo is not available. Borrow checking is not implemented, but the project plans to later use the next-generation Rust borrow checker Polonius from the official Rust project.

At this time, it is recommended to use rustc_codegen_gcc to develop for Dreamcast using Rust instead of gccrs.

Although it is in early stages and highly experimental for Dreamcast dev purposes, it is possible to use this compiler by building a GCC toolchain from a development version or the gccrs latest toolchain. KallisiOS's dc-chain scripts include profiles and patches for this. The gccrs-dev profile will provide with the absolute latest bleeding edge updates to gccrs, while the 14.1.1-dev and 15.0.1-dev profiles will provide the latest changes the gccrs team has upstreamed to mainline GCC.

Building a gccrs-enabled toolchain

Follow the Getting Started with Dreamcast development guide for creating a Dreamcast toolchain until you arrive at the instructions for setting up the dc-chain configuration file.

  • Make sure your shell is currently in the correct directory:
cd /opt/toolchains/dc/kos/utils/dc-chain
  • (Optional) Make any desired changes to the Makefile.cfg configuration. Note that to avoid conflicting with an existing stable toolchain at the default path (i.e. /opt/toolchains/dc/sh-elf), we will be installing to /opt/toolchains/dc/sh-elf-gccrs instead.
  • To begin compilation and installation, run:
make toolchain_profile=gccrs-dev enable_rust=1 sh_toolchain_path=/opt/toolchains/dc/sh-elf-gccrs
  • After building everything, you can clean up the extraneous files in your dc-chain directory by entering:
make clean

Setting up Makefiles to compile Rust modules

As mentioned before, cargo is not available to use with gccrs, so for our example, we will place our .rs modules within a typical KallistiOS Makefile project. If we assume the module file is named example.rs, you'll need to add example.rox as an object file in your Makefile's OBJS = declaration. Additionally, you'll need to add the following lines so that make knows how to compile Rust modules into rox object files:

%.rox: %.rs
	kos-cc -frust-incomplete-and-experimental-compiler-do-not-use $(CFLAGS) -c $< -o $@

Alternatively, you can add those lines to your KallistiOS Makefile.rules file to avoid having to place it in every project's Makefile.

In your example.rs file, your main function will need to be declared like so:.

#[no_mangle]
pub extern fn main() -> i32 {
    [...]
}

Make sure before you compile your code that you set export KOS_CC_BASE="/opt/toolchains/dc/sh-elf-gccrs" in your KallistiOS environ.sh file or make will not find your gccrs compiler executable.

Example project

The rust-fortran-cube repository contains an example project using gccrs in conjunction with GFortran, KallistiOS, and GLdc. View footage of the example running. This example was created by Eric Fradella and used as a demo in the FOSDEM 2024 talk Sega Dreamcast Homebrew with GCC by Falco Girgis.