Packages
A package is R65's unit used for sharing code and assets. A simplified version of Rust crates, an R65 package is a library one project can use in another. A package can ship R65 source, such as functions, types, traits and macros, as well as asset data such as tiles, palettes, maps and samples. They can be distributed from a git URL, with versioned releases, or from a file path on disk. A manifest file gives its name, its version, and the packages it depends on in turn.
A project lists the packages it uses in its own r65.toml. r65pkg fetches
them and locks their exact commits, and r65c makes their files available to
include! and include_bytes!. A game is a package too, an application
rather than a library. The standard library is a package as well: every
project made with r65x init depends on stdlib, which comes with r65 and is
never fetched.
r65pkg add https://github.com/someone/r65-tween
include!("tween/tween.r65")
How packages work
Five pieces make up the system:
| Piece | Role |
|---|---|
r65.toml | The manifest: a package's name, version and dependencies |
r65.lock | The exact commit chosen for every git dependency. Commit it |
.r65/deps/ | Where fetched dependencies are cloned. Gitignored |
r65pkg | Edits manifests, writes the lockfile, runs git. The only tool that touches the network |
r65c | Reads the manifest and lockfile and puts each package on the include path. It never fetches |
The manifest: r65.toml
A package is either an application (it builds a ROM) or a library (other packages include it).
An application has a [bin] table:
[package]
name = "my_game"
version = "0.1.0"
[bin]
main = "src/main.r65"
cfg = ["snes"] # conditions every build is compiled with
[dependencies]
stdlib = { version = "0.1" }
A library has a [lib] table naming the directory its files are included
from:
[package]
name = "tween"
version = "0.2.0"
r65-version = "0.3" # the oldest r65c that builds it
cfg = ["snes"] # conditions it must be built with
[lib]
include_dir = "."
[dependencies]
stdlib = { version = "0.1" }
r65c finds the manifest by walking up from the source file it compiles, the
same way it finds r65-lint.toml.
Dependencies
A dependency is known by its package name, the key on the left. There is no renaming.
| Form | Source |
|---|---|
{ git = URL, version = "0.2" } | The newest tagged release matching the requirement. The usual form |
{ git = URL, tag = "tween-v0.2.1" } | An exact tag |
{ git = URL, rev = "0bf2f0f" } | An exact commit |
{ git = URL, branch = "main" } | The head of a branch, locked at the commit fetched |
{ path = "../tween" } | A directory on disk, optionally with a version requirement it must satisfy |
{ version = "0.1" } | The copy installed with r65. Only for stdlib; see The standard library |
A git repository can hold one package at its root, or several in a
workspace. In a workspace, the dependency's name selects the member whose
[package] name matches, so one URL serves every package in the repository.
Versions and releases
A release is a git tag named NAME-vX.Y.Z: tween-v0.2.0, easing-v0.1.0.
Each package is tagged by itself, so packages that share a repository release
on their own schedules.
A version is a requirement with Cargo's caret meaning:
| Requirement | Accepts |
|---|---|
"0.2" | 0.2.0 up to, but not including, 0.3.0 |
"0.2.3" | 0.2.3 up to, but not including, 0.3.0 |
"1.4" | 1.4.0 up to, but not including, 2.0.0 |
"=0.2.3" | exactly 0.2.3 |
A tag is trusted only when it agrees with the manifest it points at. If
tween-v0.2.0 points at a commit whose r65.toml says 0.1.9, the tag is
refused.
One version of each package
Since every package shares one namespace, two versions of the same package
cannot be in one build: their names would collide. When several packages
require the same dependency, the resolver picks the newest release that
satisfies all of them. If game needs easing "0.1" and tween needs
easing "0.1.2", both get the newest 0.1.x at or above 0.1.2. If no release
satisfies every requirement, the build fails with an error naming each
requirer and its requirement.
This also makes diamond dependencies free. Every route to easing reaches the
same directory, and a file included twice compiles once.
The lockfile: r65.lock
r65.lock sits next to the application's r65.toml and records what was
chosen:
# Generated by r65pkg: do not edit by hand.
version = 1
[[package]]
name = "tween"
version = "0.2.0"
source = "git+https://github.com/someone/r65-tween?tag=tween-v0.2.0"
rev = "9317fe8953999025cb58aacdb2389b390394f1b1"
Commit it. A fresh checkout then builds against exactly the commits you
tested. The lock changes only when you run r65pkg add, remove or update,
or when fetch locks a dependency that is not locked yet. A newer release
never arrives on its own.
Only the application's lockfile counts. A dependency's own r65.lock is
ignored.
Including a package's files
Once a package is resolved, its include_dir joins the include path. A file
can be named two ways:
include!("stdlib/math.r65") // qualified: package name, then the path in it
include!("math.r65") // bare: searched across every package
The qualified form names the package, so it can never be ambiguous. Use it. A bare name that two packages both provide is an error that lists both candidates.
include! searches in this order:
- the including file's directory;
- each
-Idirectory given tor65c; - a package-qualified path;
- a bare name across all packages.
Your own files come first, so a project can replace a single file of a package with its own copy, under either spelling, without forking the package.
include_bytes! searches package directories too, after -I.
Build requirements
A package can state what it needs from the build in [package], and the
build checks it before compiling anything:
r65-versionis the oldest compiler that builds the package. An older compiler stops witherror: tween 0.2.0 requires r65c >= 0.3; this is r65c 0.2.0rather than a confusing parse error halfway through the package.cfglists conditions the package must be compiled with, such as["snes"]. The application's[bin] cfgmust provide them. A condition a package merely supports (code under#[cfg(snes)]) is not listed.
The application's [bin] cfg applies to every build. --cfg on the command
line adds to it, and giving a key=value condition a different value from
[bin] is an error.
Overriding a package with [patch]
[patch] in the root manifest replaces a package with a local directory for
the whole build, including where other packages depend on it:
[patch]
tween = { path = "../tween" }
Use it to test a fix to a package before you release it. A patched package
must still satisfy every version requirement on it. [patch] in a
dependency's manifest is ignored: only the root decides.
Workspaces
A repository holding several packages lists them in a root r65.toml:
[workspace]
members = ["tween", "easing", "demo"]
[patch]
easing = { path = "easing" }
Each member has its own r65.toml and its own version. The [patch] makes
members see each other's working copies while you develop, while a consumer
outside the repository gets the tagged releases.
r65c and the build
r65c reads the manifest itself, so a Makefile does not list package
directories with -I:
| Flag | Description |
|---|---|
--manifest PATH | Use this r65.toml instead of the nearest one above the source file |
--no-manifest | Ignore r65.toml; resolve includes from the source directory and -I only |
--depfile PATH | Write a make rule naming every file the build read, including manifests and the lockfile |
r65c never fetches. If a dependency is missing or the lockfile no longer
matches the manifest, it stops and tells you to run r65pkg fetch.
The Makefile from r65x init fetches before it compiles, and uses the depfile
so that editing a package file, or running r65pkg update, triggers a
rebuild:
$(BUILD_DIR)/main.asm: $(MAIN_SRC) | deps
$(R65C) $(MAIN_SRC) -o $@ $(R65C_FLAGS) --depfile $(BUILD_DIR)/main.d
-include $(BUILD_DIR)/main.d
deps:
$(R65PKG) fetch
The standard library
stdlib comes with r65. A build uses the stdlib installed with the r65c
running it, so the compiler and its standard library are always the same
release. A project names only the versions it accepts:
[dependencies]
stdlib = { version = "0.1" }
Nothing is cloned, copied or locked for it, so a project with no other git
dependencies has no r65.lock and needs no network to build. When the
installed stdlib doesn't satisfy a requirement, the build stops and names the
version this r65 ships; install an r65 whose stdlib does. A git or path
dependency on stdlib is an error. To build against another copy, such as
a fork, [patch] it in the root manifest.
The r65pkg tool
r65pkg runs in the directory of the package it manages, or on the manifest
named by --manifest PATH placed before the command.
r65pkg --help
r65pkg <command> --help
| Command | Does |
|---|---|
r65pkg init [--lib | --bin] [NAME] | Write an r65.toml in the current directory. An application by default |
r65pkg add URL | Add a git dependency on the newest release, as a caret requirement, and lock it. The package's name comes from the repository |
r65pkg add URL --version REQ | The same with your own requirement. --tag, --rev or --branch pins an exact commit instead |
r65pkg add NAME --git URL | Pick one package from a repository that holds several |
r65pkg add NAME --path DIR | Add a path dependency |
r65pkg add stdlib | Depend on the stdlib installed with r65, as a caret requirement on its version. --version REQ gives your own |
r65pkg remove NAME | Drop a dependency and its lock entry |
r65pkg fetch | Lock any dependency not locked yet, then clone everything in r65.lock into .r65/deps/ |
r65pkg update [NAME] | Move every dependency, or just NAME, to the newest release its requirements allow |
r65pkg tree | Print the dependency graph with versions and sources |
r65pkg list | Print each resolved package with its version, source and include directory |
r65pkg check | Validate manifests, the lockfile, version conflicts and build requirements without compiling |
r65pkg path [--include-flags] | Print the include directories, or -I flags for tools other than r65c |
r65pkg tag [VERSION] | In a package's directory: set its version to VERSION and commit r65.toml if it differs, then create the release tag NAME-v<version> |
fetch, update and add accept --offline, which fails rather than
contacting a remote. A dependency already cloned at its locked commit is never
fetched again, so a fetch with nothing new runs no network command.
r65pkg check also catches mistakes the compiler would only report later, or
not at all:
- a tag that disagrees with its manifest version;
- two packages providing the same bare file name;
- a package reaching another package's file by its bare name instead of the qualified one.
Example: a library and a game
This walk-through builds a small library, tile2bpp, that plots pixels into
8x8 2bpp tiles in memory, and a game that uses it.
The library
tile2bpp/
├── r65.toml
└── tile2bpp.r65
mkdir tile2bpp && cd tile2bpp
r65pkg init --lib tile2bpp
r65pkg add stdlib
Then add the minimum compiler version, so tile2bpp/r65.toml reads:
[package]
name = "tile2bpp"
version = "0.1.0"
r65-version = "0.3"
[lib]
include_dir = "."
[dependencies]
stdlib = { version = "0.1" }
The library works through a pointer to a tile and declares no statics, so the
game decides where its tiles live. It uses shr8 from stdlib's math.r65 for
the pixel mask, since >> takes only constant amounts:
//! Plot pixels into 8x8 2bpp tiles held in memory.
//!
//! A tile is 16 bytes: two per row, top row first. The first byte of a row
//! is bitplane 0, the second bitplane 1, and bit 7 is the leftmost pixel.
include!("stdlib/math.r65")
/// Clear the 16-byte tile at `tile` to colour 0.
fn tile2bpp_clear(tile: *u8) {
let mut i: u16 = 0;
while i < 16 {
tile[i] = 0;
i++;
}
}
/// Set pixel (`x`, `y`) of the tile at `tile` to `color` (0-3).
fn tile2bpp_plot(tile: *u8, x: u8, y: u8, color: u8) {
let row: u16 = (y as u16) * 2;
let mask: u8 = shr8(0x80, x as u16);
if color & 1 != 0 {
tile[row] = tile[row] | mask;
} else {
tile[row] = tile[row] & ~mask;
}
if color & 2 != 0 {
tile[row + 1] = tile[row + 1] | mask;
} else {
tile[row + 1] = tile[row + 1] & ~mask;
}
}
The library includes stdlib by its qualified name, stdlib/math.r65. If the
game includes the same file, both names reach one file, so it compiles once.
Using packages in your project
Start from r65x init, which already depends on stdlib, and add the library
by path while you work on both:
r65x init --platform snes my_game
cd my_game
r65pkg add tile2bpp --path ../tile2bpp
include!("stdlib/sneslib.r65")
include!("tile2bpp/tile2bpp.r65")
#[lowram]
static mut TILE: [u8; 16];
#[entry]
fn main() -> ! {
snes_init();
// A diagonal in colour 3, then the top-right pixel in colour 1.
tile2bpp_clear(&TILE as *u8);
let mut i: u8 = 0;
while i < 8 {
tile2bpp_plot(&TILE as *u8, i, i, 3);
i++;
}
tile2bpp_plot(&TILE as *u8, 7, 0, 1);
loop {
wait_nmi!();
}
}
#[interrupt(nmi)]
fn vblank() { }
After it runs, TILE holds 81 80 40 40 20 20 10 10 08 08 04 04 02 02 01 01:
each row's two planes have the diagonal's bit set, and the top row's plane 0
also has bit 0 for the colour-1 pixel.
r65pkg tree shows the graph. stdlib appears twice but resolves to one
version:
$ r65pkg tree
my_game 0.1.0
├── stdlib 0.1.0 (toolchain)
└── tile2bpp 0.1.0 (path+../tile2bpp)
└── stdlib 0.1.0 (toolchain)
make fetches, compiles and assembles as usual.
Once tile2bpp is published (see below), switch the game from the path to the
release:
r65pkg remove tile2bpp
r65pkg add https://github.com/you/r65-tile2bpp
r65.toml then holds the git dependency with a "0.1" requirement, and
r65.lock pins the commit behind tile2bpp-v0.1.0:
tile2bpp = { git = "https://github.com/you/r65-tile2bpp", version = "0.1" }
Publishing your own package
There is no package registry. A package is published by pushing it to a git repository anyone can clone and tagging a release.
1. Choose a layout
One package per repository. Put r65.toml at the repository root. The
example above is already in this shape:
r65-tile2bpp/
├── .gitignore # .r65/
├── README.md
├── r65.toml
└── tile2bpp.r65
Several packages in one repository. Give each package its own directory
and list them in a root [workspace]. A consumer uses the same URL for all of
them, and the dependency name picks the package:
my-r65-libs/
├── r65.toml # [workspace] members = ["tile2bpp", "tween"]
├── tile2bpp/
│ ├── r65.toml
│ └── tile2bpp.r65
└── tween/
├── r65.toml
└── tween.r65
Keep each directory named after the package it holds.
Either way, add .r65/ to .gitignore before the first commit. Fetching your
package's own dependencies clones them there, and those clones must not be
committed inside your repository.
2. Check it
Run these in the package's directory:
r65pkg fetch # lock and fetch the package's own dependencies
r65pkg check # manifests, requirements and includes, without compiling
A library has no main to compile, so build it through a small application
that includes every file and calls what it exports. A path dependency on the
library, tile2bpp = { path = ".." }, from an example/ directory inside it
works well, and doubles as documentation.
3. Tag a release
git add -A && git commit -m "tile2bpp 0.1.0"
r65pkg tag # creates tile2bpp-v0.1.0 from r65.toml
git push origin main tile2bpp-v0.1.0
Run r65pkg tag in the package's directory, next to its r65.toml. With no
argument it tags the version the manifest names. It refuses while the
manifest has uncommitted changes, or when that version is already tagged, so a
tag can never disagree with the version it claims. Always tag through it.
Pushing is left to you.
For each later release, name the new version: r65pkg tag 0.1.1 writes it
into r65.toml, commits that file, and creates tile2bpp-v0.1.1. Follow semantic versioning: while a package is 0.x, a
breaking change bumps the minor version (0.1.4 → 0.2.0), since "0.1"
requirements accept any 0.1.x.
4. Tell people how to use it
The README should show the dependency line and the include:
[dependencies]
tile2bpp = { git = "https://github.com/you/r65-tile2bpp", version = "0.1" }
include!("tile2bpp/tile2bpp.r65")
General-purpose libraries are collected at r65-packages, a workspace of independently versioned packages that follows the same steps.
Guidelines
Every package shares one namespace with the game and with every other package, so a few habits keep packages working together:
- Prefix public names. Use names like
tween_startandTWEEN_SLOTS, notstartandSLOTS. Methods are already qualified by their type, so give types distinctive names. - Treat every addition as a possible break. A new public function can collide with a name a game already uses, even in a minor release.
- Allocate nothing. Declare types, functions and macros, and let the game
declare the statics: it chooses their storage class (
#[zeropage],#[lowram],#[ram]) and how many instances it needs, and two packages can never claim the same memory. - Never fix addresses. Two packages that both use
#[zeropage(0x40)]overlap without any error. Let the compiler place anything you must declare. - Include other packages by qualified name, as in
include!("stdlib/math.r65"). A bare name fails as ambiguous once a second package provides a file with the same name. - State what you need. Set
r65-versionto the oldest compiler you have tested, and list required conditions incfg. - Keep files independent. Users include only the files they need, so a file should not pull in more than it uses.
Limits
Some language features are program-wide, which limits what a package can offer:
- A
#[bank(n)]directive inside a package applies only to the package's own declarations. Your code after theinclude!stays in the bank it was in. - A function or macro defined in two different files is an error.