Site icon #kitlang

Cross-compiling with Kit

Kit is a systems programming language that compiles to C; as of today, the latest pre-release version of Kit can also be used as a cross-compiler – i.e. you can write code on one system that runs on another. This is a necessity if you want to use Kit to develop for game consoles, microcontrollers like Arduino, or even desktop operating systems other than the one you’re developing on. Here’s how it works.

Toolchains

Previously, Kit had specific built-in support for GCC and Clang, and would choose C compiler flags based on the one you were using (which it would infer from the executable’s name.) If you wanted to use Kit with a nonstandard toolchain, you’d need to use it only for code generation and handle compilation yourself. Now, this functionality has been moved out of the compiler into toolchain files, which describe the configuration of your C compiler. As an example, here’s the built-in toolchain for running GCC on Linux, which is very simple:

CC=gcc
CFLAGS="-std=c99 -pedantic -O3 -Os -Wno-missing-braces -Wno-shift-op-parentheses"

And here’s a more involved example, using the devkitPPC homebrew compiler toolchain to build an executable that runs on GameCube (or in an emulator):

CC=/opt/devkitpro/devkitPPC/bin/powerpc-eabi-gcc
CPPFLAGS="-D_GNU_SOURCE -I/opt/devkitpro/libogc/include"
CFLAGS="-DGEKKO -mogc -mcpu=750 -meabi -mhard-float"
LDFLAGS="-L/opt/devkitpro/libogc/lib/cube -logc -lbba -lm"

I don’t know that anyone actually wants to do this, but I’m on a GameCube kick recently.

Compiling

During compilation you can specify two toolchains using the --build and/or --host flags. Both of these will use system defaults if you don’t provide them. If you provide only host, we’ll assume you’re cross-compiling and use the inferred default build toolchain for your system. If you provide only build, we’ll assume you’re not cross-compiling and use the same toolchain for the host.

Why two toolchains? As part of compilation, Kit may expand procedural macros; it actually compiles and executes C programs in order to do this. So if there are any macro invocations in your program, Kit needs a C compiler that can produce working executables on the system you’re compiling on.

Example: Emscripten

Kit comes packaged with an Emscripten toolchain, allowing you to compile Kit to JavaScript. Save this file as hellojs.kit:

function main() {
    puts("Hello from Kit!");
}

and compile: ENVIRONMENT=web kitc hellojs.kit --host emscripten -o helloworld.js

You can now execute helloworld.js with node helloworld.js, or load it from HTML in your browser.

Example: GameCube

Here’s an example source file of a GameCube program written in Kit. You can run this example yourself:

Exit mobile version