Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm

## [Unreleased]

### Fixed

- The build script now writes the generated `table_gen.rs` into `$OUT_DIR` instead of writing to `src/table_gen.rs`.
This is intended as a fix to docs.rs/multi-coded since it mounts the directory as read-only

### CI

- Added a macOS build job covering aarch64-apple-darwin and x86_64-apple-darwin on the self-hosted macOS runner pool.
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -287,12 +287,12 @@ cargo clippy --all-targets --all-features -- -D warnings

## Updating the Codec Table

A build script generates the codec enum from `table.csv` at the crate root. The build script `build.rs` parses the CSV. It checks that all codes and names are unique. It emits `src/table_gen.rs`, which the build includes into `src/codec.rs`.
A build script generates the codec enum from `table.csv` at the crate root. The build script `build.rs` parses the CSV. It checks that all codes and names are unique. It emits `table_gen.rs` into Cargo's build output directory (`$OUT_DIR`), which `src/codec.rs` includes. Generating into `$OUT_DIR` keeps the crate source tree writable-agnostic so it also builds on read-only checkouts such as docs.rs.

To update the table:

1. Replace `table.csv` with the latest version from the [official multicodec repository](https://github.com/multiformats/multicodec/blob/master/table.csv). You can also apply your local additions.
2. Run `cargo build`. The build script regenerates `src/table_gen.rs`.
2. Run `cargo build`. The build script regenerates the included file in the build output directory.
3. Run `cargo test` to make sure the new codecs work.

If the CSV has duplicate codes or names, the build fails. The error names the offending row.
Expand Down
18 changes: 8 additions & 10 deletions build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,23 +22,21 @@ struct Record {
fn main() -> Result<(), Box<dyn std::error::Error>> {
println!("cargo:rerun-if-changed=table.csv");

let mut pb = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
let mut tpb = pb.clone();
// Input path (table.csv lives in the crate root).
let input_path = PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("table.csv");

// Input path
tpb.push("table.csv");

// Output path
pb.push("src");
pb.push("table_gen.rs");
// The generated file must go into Cargo's OUT_DIR rather than
// the source tree since docs.rs mounts the src directory as read-only
let output_path = PathBuf::from(std::env::var("OUT_DIR")?).join("table_gen.rs");

// Open input file
let inf = File::open(&tpb).map_err(|e| {
let inf = File::open(&input_path).map_err(|e| {
format!("Failed to open table.csv: {e}. Make sure table.csv exists in the crate root.")
})?;

// Open output file
let mut f = File::create(&pb).map_err(|e| format!("Failed to create table_gen.rs: {e}"))?;
let mut f =
File::create(&output_path).map_err(|e| format!("Failed to create table_gen.rs: {e}"))?;

let mut rdr = csv::Reader::from_reader(inf);
let conv = Converter::new().to_case(Case::Pascal);
Expand Down
4 changes: 2 additions & 2 deletions src/codec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
)]
#![allow(missing_docs)]
// Allow because variants are generated
// The `build_codec_enum!` invocation in the included `table_gen.rs` contains
// The `build_codec_enum!` invocation in the generated `table_gen.rs` contains
// raw hex literals (e.g. `0xd03009`) for multicodec codes that clippy
// `unreadable_literal` would flag. These are generated from the official
// multicodec table and should not be hand-edited.
Expand Down Expand Up @@ -388,4 +388,4 @@ mod tests {
}
}

include!("table_gen.rs");
include!(concat!(env!("OUT_DIR"), "/table_gen.rs"));
Loading