in src/main.rs [138:367]
fn main() {
let matches = Command::new("cbindgen")
.version(bindgen::VERSION)
.about("Generate C bindings for a Rust library")
.arg(
Arg::new("v")
.short('v')
.action(ArgAction::Count)
.help("Enable verbose logging"),
)
.arg(
Arg::new("verify")
.long("verify")
.action(ArgAction::SetTrue)
.help("Generate bindings and compare it to the existing bindings file and error if they are different"),
)
.arg(
Arg::new("config")
.short('c')
.long("config")
.value_name("PATH")
.value_parser(value_parser!(PathBuf))
.help("Specify path to a `cbindgen.toml` config to use"),
)
.arg(
Arg::new("lang")
.short('l')
.long("lang")
.value_name("LANGUAGE")
.help("Specify the language to output bindings in")
.value_parser(["c++", "C++", "c", "C", "cython", "Cython"]),
)
.arg(
Arg::new("package-version")
.long("package-version")
.action(ArgAction::SetTrue)
.help("Include the package version in the header comment")
)
.arg(
Arg::new("cpp-compat")
.long("cpp-compat")
.action(ArgAction::SetTrue)
.help("Whether to add C++ compatibility to generated C bindings")
)
.arg(
Arg::new("only-target-dependencies")
.long("only-target-dependencies")
.action(ArgAction::SetTrue)
.help("Only fetch dependencies needed by the target platform. \
The target platform defaults to the host platform; set TARGET to override.")
)
.arg(
Arg::new("style")
.short('s')
.long("style")
.value_name("STYLE")
.help("Specify the declaration style to use for bindings")
.value_parser(["Both", "both", "Tag", "tag", "Type", "type"]),
)
.arg(
Arg::new("d")
.short('d')
.long("parse-dependencies")
.action(ArgAction::SetTrue)
.help("Whether to parse dependencies when generating bindings"),
)
.arg(
Arg::new("clean")
.long("clean")
.action(ArgAction::SetTrue)
.help(
"Whether to use a new temporary directory for expanding macros. \
Affects performance, but might be required in certain build processes.")
.required(false)
)
.arg(
Arg::new("INPUT")
.help(
"A crate directory or source file to generate bindings for. \
In general this is the folder where the Cargo.toml file of \
source Rust library resides.")
.required(false)
.value_parser(value_parser!(PathBuf))
.index(1),
)
.arg(
Arg::new("crate")
.long("crate")
.value_name("CRATE_NAME")
.help(
"If generating bindings for a crate, \
the specific crate to generate bindings for",
)
.required(false),
)
.arg(
Arg::new("out")
.short('o')
.long("output")
.value_name("PATH")
.help("The file to output the bindings to")
.value_parser(value_parser!(PathBuf))
.required(false),
)
.arg(
Arg::new("lockfile")
.long("lockfile")
.value_name("PATH")
.help(
"Specify the path to the Cargo.lock file explicitly. If this \
is not specified, the Cargo.lock file is searched for in the \
same folder as the Cargo.toml file. This option is useful for \
projects that use workspaces.")
.value_parser(value_parser!(PathBuf))
.required(false),
)
.arg(
Arg::new("metadata")
.long("metadata")
.value_name("PATH")
.help(
"Specify the path to the output of a `cargo metadata` \
command that allows to get dependency information. \
This is useful because cargo metadata may be the longest \
part of cbindgen runtime, and you may want to share it \
across cbindgen invocations. By default cbindgen will run \
`cargo metadata --all-features --format-version 1 \
--manifest-path <path/to/crate/Cargo.toml>"
)
.value_parser(value_parser!(PathBuf))
.required(false),
)
.arg(
Arg::new("profile")
.long("profile")
.value_name("PROFILE")
.help(
"Specify the profile to use when expanding macros. \
Has no effect otherwise."
)
.value_parser(["Debug", "debug", "Release", "release"]),
)
.arg(
Arg::new("quiet")
.short('q')
.long("quiet")
.action(ArgAction::SetTrue)
.help("Report errors only (overrides verbosity options).")
.required(false),
)
.arg(
Arg::new("depfile")
.value_name("PATH")
.long("depfile")
.num_args(1)
.required(false)
.value_parser(value_parser!(PathBuf))
.help("Generate a depfile at the given Path listing the source files \
cbindgen traversed when generating the bindings. Useful when \
integrating cbindgen into 3rd party build-systems. \
This option is ignored if `--out` is missing."
)
)
.arg(
Arg::new("symfile")
.value_name("PATH")
.long("symfile")
.num_args(1)
.required(false)
.help("Generate a list of symbols at the given Path. This list can be \
given to a linker in order to compile an application that exposes \
dynamic symbols. Useful when creating a plugin system with a C interface."
)
)
.get_matches();
if matches.get_flag("verify") && !matches.contains_id("out") {
error!(
"Cannot verify bindings against `stdout`, please specify a file to compare against."
);
std::process::exit(2);
}
// Initialize logging
if matches.get_flag("quiet") {
logging::ErrorLogger::init().unwrap();
} else {
match matches.get_count("v") {
0 => logging::WarnLogger::init().unwrap(),
1 => logging::InfoLogger::init().unwrap(),
_ => logging::TraceLogger::init().unwrap(),
}
}
// Find the input directory
let input: PathBuf = matches
.get_one("INPUT")
.cloned()
.unwrap_or_else(|| env::current_dir().unwrap());
let bindings = match load_bindings(&input, &matches) {
Ok(bindings) => bindings,
Err(msg) => {
error!("{}", msg);
error!("Couldn't generate bindings for {}.", input.display());
std::process::exit(1);
}
};
// Write the bindings file
match matches.get_one::<PathBuf>("out") {
Some(file) => {
let changed = bindings.write_to_file(file);
if matches.get_flag("verify") && changed {
error!("Bindings changed: {}", file.display());
std::process::exit(2);
}
if let Some(depfile) = matches.get_one("depfile") {
bindings.generate_depfile(file, depfile);
}
if let Some(symfile) = matches.get_one::<String>("symfile") {
bindings.generate_symfile(symfile);
}
}
_ => {
bindings.write(io::stdout());
}
}
}