| Crates.io | license-fetcher |
| lib.rs | license-fetcher |
| version | 0.8.4 |
| created_at | 2024-09-16 18:43:44.393002+00 |
| updated_at | 2025-05-17 10:09:20.540787+00 |
| description | Fetch licenses of dependencies at build time and embed them into your program. |
| homepage | |
| repository | https://github.com/WyvernIXTL/license-fetcher |
| max_upload_size | |
| id | 1376737 |
| size | 124,112 |
[!NOTE] If you are in search for a cli checkout flicense.
Crates that are compiled with your program are fetched via cargo metadata and cargo tree.
License texts are read from the .cargo/registry/src folder.
The data is then serialized and compressed.
Here is a small rundown how to use this library for fetching licenses during build time. Though fetching licenses at runtime is also supported. See the docs.
[!WARNING] Include this library as build dependency and as normal dependency!
cargo add --build --features build license-fetcher
cargo add license-fetcher
This library requires you to execute it for fetching licenses in a build script.
Create a file called build.rs in the root of your project and add following contents:
use license_fetcher::build::config::{ConfigBuilder, Config};
use license_fetcher::build::package_list_with_licenses;
use license_fetcher::PackageList;
fn main() {
// Config with environment variables set by cargo, to fetch licenses at build time.
let config: Config = ConfigBuilder::from_build_env()
.build()
.expect("Failed to build configuration.");
// Fetch metadata and licenses.
let packages: PackageList = package_list_with_licenses(config)
.expect("Failed to fetch metadata or licenses.");
// Write packages to out dir to be embedded.
packages.write_package_list_to_out_dir().expect("Failed to write package list.");
// Rerun only if one of the following files changed:
println!("cargo::rerun-if-changed=build.rs");
println!("cargo::rerun-if-changed=Cargo.lock");
println!("cargo::rerun-if-changed=Cargo.toml");
}
Add following content to your main.rs:
use license_fetcher::read_package_list_from_out_dir;
fn main() {
let package_list = read_package_list_from_out_dir!().unwrap();
}
license-fetcher fetches licenses that are at the root of a package. This results in some caveats, mainly:
To work around the former points, it is advisable to use flicense --stats . on your package,
to see what packages license-fetcher fetches.
For the later point there is no workaround, as there is no automated way to detect the use of such dependencies.
license-retrieverThis project was a big inspiration for license-fetcher. The idea of fetching licenses during build step did not even occur to me beforehand.
A big shout-out!
cargo-aboutDisplay trait included 😉
