Skip to main content

Crate notalawyer_build

Crate notalawyer_build 

Source
Expand description

Build-time generation of dependency license notices.

This crate is the build-script half of the notalawyer family. Called from a crate’s build.rs, it gathers the license texts of all dependencies (using the cargo_about library) and writes a single NOTICE file into OUT_DIR.

The other two crates consume that file at compile time and runtime:

  • notalawyer-build (this crate) writes the NOTICE file from build.rs.
  • notalawyer embeds it via include_notice!.
  • notalawyer-clap exposes it behind a --license-notice CLI flag.

License acceptance and gathering are configured through an about.toml file, searched for from the consuming crate’s manifest directory upward; see cargo-about for its format.

§Choosing a renderer

There are three entry points, in increasing order of control:

  • build writes the built-in default format. This is all most crates need.
  • build_with gathers the data, hands you a Notice, and writes whatever String your closure returns.
  • try_build_with is the fallible variant for renderers (templating engines, etc.) that can fail.

gather is the lowest level: it returns a Notice and writes nothing, so you can inspect or serialize the data yourself. The Notice, License, UsedBy and Package types are owned and implement serde::Serialize, so you can feed them straight into your own templating engine without any of cargo-about’s lifetime-bound types leaking into your build script.

§Example

Add notalawyer-build as a build dependency and call build from the main of your build.rs:

// build.rs
println!("cargo:rerun-if-changed=Cargo.toml");

notalawyer_build::build();

To customize the output, render a Notice yourself:

// build.rs
use std::fmt::Write as _;

notalawyer_build::build_with(|notice| {
    let mut out = String::new();
    for license in &notice.licenses {
        writeln!(out, "## {} ({})", license.name, license.id).unwrap();
        out.push_str(&license.text);
    }
    out
});

Structs§

License
A single license text and the crates that use it.
Notice
A complete license notice: every distinct license text and the flat list of crates it was gathered from.
Package
An owned description of a single crate/package.
UsedBy
A single “crate uses this license” entry.

Enums§

Error
An error from try_build_with.

Functions§

build
Gather dependency licenses and write the NOTICE file into OUT_DIR using the built-in default format.
build_with
Gather licenses, render them with render, and write the result to $OUT_DIR/notalawyer.
gather
Gather dependency license data without writing anything.
try_build_with
Fallible variant of build_with for renderers that can fail (templating engines, etc.).