liveBook · Manning

Compilation target

As explained in Rust in Action: Systems programming concepts and techniques

A compilation target is the specified hardware and software environment for which a compiler generates code.

A compilation target tells the compiler what kind of machine and software environment its output must run on. For an OS kernel, that environment may not exist yet: the kernel is being built to create it. A custom target is a machine-readable definition of conventions such as CPU architecture. section 11.2.1

Listing 11.2 Kernel definition for FledgeOS
{
  "llvm-target": "x86_64-unknown-none",
  "data-layout": "e-m:e-i64:64-f80:128-n8:16:32:64-S128",
  "arch": "x86_64",
  "target-endian": "little",
  "target-pointer-width": "64",
  "target-c-int-width": "32",
  "os": "none",
  "linker": "rust-lld",
  "linker-flavor": "ld.lld",
  "executables": true,
  "features": "-mmx,-sse,+soft-float",
  "disable-redzone": true,
  "panic-strategy": "abort"
}

The kernel’s fledge.json is a target definition understood by the Rust compiler. It specifies an x86-64 architecture, 64-bit pointers, no operating system, a linker, and an abort panic strategy. section 11.2.3

Defining the target is only one step. Rust’s core library must be built for it, without the usual standard library. The kernel is then compiled for that target, and a bootloader loads it in a virtual environment. section 11.2.1

Listing 11.3 Extra build-time configuration for cargo
[build]
target = "fledge.json"
 
[unstable]
build-std = ["core", "compiler_builtins"]
build-std-features = ["compiler-builtins-mem"]
 
[target.'cfg(target_os = "none")']
runner = "bootimage runner"

Cargo’s .cargo/config.toml selects fledge.json as the build target, requests core and compiler_builtins for that target, and sets bootimage runner for a target with no OS. section 11.2.3

To try the configured project, run cargo +nightly run. The build uses bootimage to prepare the kernel for booting, then runs it in QEMU. The first visible result is a small colored block on the screen—not a full desktop, but evidence that the target, kernel, and bootloader work together. section 11.2.1 section 11.2.3

How Rust in Action: Systems programming concepts and techniques covers it

How deep it goes
Defines a target, shows its JSON specification and Cargo configuration, and leaves further custom-target details to rustc documentation.
Examples
Uses an x86-64 kernel target in fledge.json, its Cargo configuration, and a QEMU boot that displays a colored block.
What it assumes
Introduces custom targets from their purpose while using a command-line prompt and Cargo configuration for the hands-on build.
What you can do after
Operate/configure a custom Rust target to build and boot a minimal kernel with Cargo and bootimage.
How it connects
Connects compilation targets to cross-compilation, Cargo, core, bootloaders, and OS kernels.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Rust in Action: Systems programming concepts and techniques. The section links point to the parts of the book it draws on.