Monday 24 November 2014

Using Code::Blocks as your Rust IDE on Windows

Warnings:
  • Code::Blocks does not support Rust formatting, code completion or syntax colouring - or at least not yet.
  • I haven't yet done much development with Rust, or Code::Blocks, so your mileage may be disappointing.  I'm just happy to be able to edit, build and debug with it.
  • You will need a certain level of understanding of configuring Windows, as I have skipped such things as setting environment variables.

 

Installing Rust

Download and install Rust from http://www.rust-lang.org/install.html.  I am using the 32 bit windows version (rust-nightly-i686-pc-windows-gnu.exe), because it matches the version of Code Blocks I want to use.

During the install uncheck all the plugins - they add a lot of menus, tabs and options that just confuse things.  Do check the box that adds Code::Blocks to your PATH.

Don't try to use it yet.  For Windows you still need MinGW version of the linker that comes with Code::Blocks.

 

Installing Code::Blocks

Download and install Code::Blocks from http://www.codeblocks.org/downloads/binaries. I am using codeblocks-13.12mingw-setup.exe

During the install check the box to add the MinGW bin directory to you PATH.  This will probably be "C:\Program Files (x86)\CodeBlocks\MinGW\bin".

Open a command prompt and type:
ld --version
You should see: "GNU ld (GNU Binutils)..."

Create a file called hello.rs that contains:
fn main() {
    println!("Hello world");
}
At the command prompt type
rustc hello.rs
If this works type
hello.exe
and it should print "Hello world".

If you have got this far, you have a working Rust compiler.

 

Installing Cargo

Next we really need Cargo (the Rust build tool).  We will use it to do all the compilation.  You could configure Code::Blocks to drive the compiler directly, but this way is simpler for now.

Go to https://crates.io/install, get the cargo-nightly-i686-pc-windows-gnu.tar.gz distribution and unpack the cargo.exe in it.  Normally you would expect Windows executable to be packed in a ZIP file, and this format might present you with problems.  However, this can be unpacked with 7-zip which you can install from http://www.7-zip.org. When you unpack the distribution, you will find another file to unpack, but eventually you find cargo.exe in a bin folder.

Put cargo.exe into a directory on your PATH.  I copied it into the Rust bin directory - but you will have to do so as an administrator.

Test cargo out in the command prompt by typing
cargo new hello_world --bin
This will create a new project directory.  Change to the new directory, build and run the result by typing:
cargo run

Configuring the project in Code::Blocks

Start up Code::Blocks.

Go to Settings -> Compler..., find "No Compiler" in the drop down, click Copy and name it "Rust Compiler".

On the Toolchain tab, select Debugger "GDB/CDB debugger", set the Make program to "cargo".

Click the button next to "C compiler" and find the rustc.exe within the installation.  We won't be using it but CodeBlocks gets upset if this is not set.

Go to File -> New -> Project -> Empty Project and Go

Under "Folder to create project in" find the directory we ran cargo in, and give the project the title hello_word.

On the next dialog, select the "Rust Compiler" compiler, and set the debug output dir to "target\".
Uncheck the Release config, and finish.

Right click the "hello_world" project and "add files ...", and select "Cargo.toml" and "src/main.rs".

Right click the "hello_world" project and "properties", set Makefile to "Cargo.toml" and check "This is a custom Makefile.  Then click "Project's build options...", on the "Make" commands tab, select "Debug" and set the clean project option to "$make clean" and the others to "$make build".  Then click "OK".

On the "Build targets" tab set the Type to "Console application", and "Output filename" to "target\hello_world.exe".  Then "OK".

Find and open main.rs. Right click on line 2  and add a break point.  Click the Debug button, the program should build, run and stop at line 2.

 

Syntax Colouring Fudge

We can now build and debug, but we don't have any syntax highlighting.  The underlying editor supports Rust, but Code::Blocks doesn't yet configure it.  So for now we will piggyback on the C syntax colouring.

Go to Setting -> Editor... -> Syntax highlighting -> C/C++ and Filemasks...and add "*.rs".

Click keywords and add "let" and "fn" (and any other keywords you can think of) to the first set.

The syntax colouring and formatting needs more work - this is just a fudge.  The underlying editor (Scintilla) has a Rust lexer but there is no config for it in "share/CodeBlocks/lexers". I presume that config could be derived from one already here, and giving it an index of 111.  See SciLexer.h in the Scintilla 3.51 sources. (also see: Creating a custom lexer for Code::Blocks editor and the Scintilla sources.)