---
title: Local Development Environment for JOS and CS 444
date: 2019-04-07 06:01:00.282
tags: ["C and C++", "OS Dev"]
---
Working with the instructor's dotfiles on the server is great and all,
but there's a few things you can do loccally that you can't do on the school server:

1. __Use QEMU's X environment to test VGA.__ If you're like me, and want to get
color working in JOS, you want to use QEMU's X display. But to my knowlege, there
is no easy way to do that from the server.
2. __Work offline.__ What if you don't have internet available? You won't
have a way to connect to the shcool server, and therefore work on the lab.
3. __Work with no latency.__ In my experience, the school server is fairly laggy
and inconvenient to use. It's much nicer to develop locally, where your keys don't
take a noticeable amount of time to appear on the screen.
4. __Use your own editor.__ vim is nice, and it's my editor of choice, but some others
prefer VScode, Atom, and the like.

### Getting Started
By default, most distributions of `gcc` do not provide
the `i386` architecture. We need `i386` since that's the archtecture of our virtual target CPU.
The JOS makefile looks for a compiler that does support the architecture - but usually
doesn't find it. So, we need a version of that compiler that does. Before we get started, __make sure you need to do this__. If `make` inside the JOS directory works out of the box, you don't!

Let's first make a directory where we will put all the programs that will be needed
to compile for `i386`.

```
mkdir ~/.i386-toolchain
```

Additionally, let's make a directory where we can download and extract various tarballs,
if only for a while. I usually consider my "downloads" folder a workspace where that can be done.

```
cd ~/Downloads
```

Let's start by downloading and building GNU `binutils`, followed by `gcc`.

### Building binutils
Let's download `binutils` v2.32 (latest at the time of writing):

```
curl -O http://ftp.gnu.org/gnu/binutils/binutils-2.32.tar.gz
tar -xzf binutils-2.32.tar.gz
cd binutils-2.32
```

Now, we will run the `configure` script to generate a working `Makefile`. We can't just run configure with no parameters - we need to specify a custom install location (our `i386-toolchain` folder), and the target architecture that we want to build for (`i386`, of course). It's easiest to keep these in variables:

```
export TARGET=i386-elf
export PREFIX=~/.i386-toolchain
```

We're ready to run the configure script:

```
./configure --prefix=$PREFIX --target=$TARGET --disable-nls --enable=languages=c
```

This generates a `Makefile`, which we will promptly use:

```
make -j8 && make install
```

This should install `binutils` to the toolchain directory.

### Building gcc
The latest version of `gcc` at the time of writing is v8.3.0. Let's similarly download and untar this:

```
curl -O ftp://ftp.gnu.org/gnu/gcc/gcc-8.3.0/gcc-8.3.0.tar.gz
tar -xzf gcc-8.3.0.tar.gz
cd gcc-8.3.0
```

I've run into issues compiling `gcc` without using a separate build directory, so I recommend you do that:

```
mkdir build && cd build
```

We will now run a similar `configure` script, with one addition: we specify the languages we want to use.

```
../configure --prefix=$PREFIX --target=$TARGET --disable-nls --enable-languages=c
```

Ah, but we don't want to just run `make` - we just want the compiler and the standard library (`libgcc`, which `gcc` tries to link to JOS and everything else). Thus, our make command is as follows:

```
make all-gcc all-target-libgcc -j8
make install-gcc install-target-libgcc
```

Finally, we can move into our local copy of JOS. I assume you have it cloned - I'm sure you don't need my help to do that. There's a configuration setting that we have to set to make
sure the `Makefile` uses the correct compiler. This is inside `conf/env.mk`:

```
GCCPREFIX='~/.i386-toolchain/bin/i386-elf-'
```

If you run `make` inside of the JOS directory, the kernel should compile!

### dotfiles
A lot of the dotfiles the professor provides are very nice. The one I find most useful is the `.gdbinit` file and the dashboard it comes with. Fortunately, even without having access to the script Dr. Jang provides in the server's network filesystem, we can set up most of his dotfiles. It's easy enough - they're hosted on his GitHub! For simplicity, let's clone these files in the same location as the canonical script does:

```
git clone https://github.com/blue9057/peda ~/.cs444
```

Personally, I only use the `gdbinit` script. If you don't have an existing one, we can link it in:

```
cd ~
ln -sf ~/.cs444/gdbinit .gdbinit
```

That should do it!

### Conclusion
With this, we've set up a compiler for `i386` and pulled some dotfiles recommended by the professor. I hope this makes for easier development - with whatever tools you prefer, locally!
