| 1 |
/* This Source Code Form is subject to the terms of the Mozilla Public |
| 2 |
* License, v. 2.0. If a copy of the MPL was not distributed with this |
| 3 |
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
| 4 |
|
| 5 |
use std::env; |
| 6 |
use std::io; |
| 7 |
use std::path::{Path, PathBuf}; |
| 8 |
use std::str::FromStr; |
| 9 |
|
| 10 |
extern crate clap; |
| 11 |
#[macro_use] |
| 12 |
extern crate log; |
| 13 |
extern crate proc_macro2; |
| 14 |
#[macro_use] |
| 15 |
extern crate serde; |
| 16 |
extern crate serde_json; |
| 17 |
#[macro_use] |
| 18 |
extern crate quote; |
| 19 |
#[macro_use] |
| 20 |
extern crate syn; |
| 21 |
extern crate toml; |
| 22 |
|
| 23 |
use clap::{Arg, ArgMatches, Command}; |
| 24 |
|
| 25 |
mod bindgen; |
| 26 |
mod logging; |
| 27 |
|
| 28 |
use crate::bindgen::{Bindings, Builder, Cargo, Config, Error, Profile, Style}; |
| 29 |
|
| 30 |
fn apply_config_overrides(config: &mut Config, matches: &ArgMatches) { |
| 31 |
// We allow specifying a language to override the config default. This is |
| 32 |
// used by compile-tests. |
| 33 |
if let Some(lang) = matches.value_of("lang") { |
| 34 |
config.language = match lang.parse() { |
| 35 |
Ok(lang) => lang, |
| 36 |
Err(reason) => { |
| 37 |
error!("{}", reason); |
| 38 |
return; |
| 39 |
} |
| 40 |
} |
| 41 |
} |
| 42 |
|
| 43 |
if matches.is_present("cpp-compat") { |
| 44 |
config.cpp_compat = true; |
| 45 |
} |
| 46 |
|
| 47 |
if matches.is_present("only-target-dependencies") { |
| 48 |
config.only_target_dependencies = true; |
| 49 |
} |
| 50 |
|
| 51 |
if let Some(style) = matches.value_of("style") { |
| 52 |
config.style = match style { |
| 53 |
"Both" => Style::Both, |
| 54 |
"both" => Style::Both, |
| 55 |
"Tag" => Style::Tag, |
| 56 |
"tag" => Style::Tag, |
| 57 |
"Type" => Style::Type, |
| 58 |
"type" => Style::Type, |
| 59 |
_ => { |
| 60 |
error!("Unknown style specified."); |
| 61 |
return; |
| 62 |
} |
| 63 |
} |
| 64 |
} |
| 65 |
|
| 66 |
if let Some(profile) = matches.value_of("profile") { |
| 67 |
config.parse.expand.profile = match Profile::from_str(profile) { |
| 68 |
Ok(p) => p, |
| 69 |
Err(e) => { |
| 70 |
error!("{}", e); |
| 71 |
return; |
| 72 |
} |
| 73 |
} |
| 74 |
} |
| 75 |
|
| 76 |
if matches.is_present("d") { |
| 77 |
config.parse.parse_deps = true; |
| 78 |
} |
| 79 |
} |
| 80 |
|
| 81 |
fn load_bindings(input: &Path, matches: &ArgMatches) -> Result<Bindings, Error> { |
| 82 |
// If a file is specified then we load it as a single source |
| 83 |
if !input.is_dir() { |
| 84 |
// Load any config specified or search in the input directory |
| 85 |
let mut config = match matches.value_of("config") { |
| 86 |
Some(c) => Config::from_file(c).unwrap(), |
| 87 |
None => Config::from_root_or_default( |
| 88 |
input |
| 89 |
.parent() |
| 90 |
.expect("All files should have a parent directory"), |
| 91 |
), |
| 92 |
}; |
| 93 |
|
| 94 |
apply_config_overrides(&mut config, matches); |
| 95 |
|
| 96 |
return Builder::new() |
| 97 |
.with_config(config) |
| 98 |
.with_src(input) |
| 99 |
.generate(); |
| 100 |
} |
| 101 |
|
| 102 |
// We have to load a whole crate, so we use cargo to gather metadata |
| 103 |
let lib = Cargo::load( |
| 104 |
input, |
| 105 |
matches.value_of("lockfile"), |
| 106 |
matches.value_of("crate"), |
| 107 |
true, |
| 108 |
matches.is_present("clean"), |
| 109 |
matches.is_present("only-target-dependencies"), |
| 110 |
matches.value_of("metadata").map(Path::new), |
| 111 |
)?; |
| 112 |
|
| 113 |
// Load any config specified or search in the binding crate directory |
| 114 |
let mut config = match matches.value_of("config") { |
| 115 |
Some(c) => Config::from_file(c).unwrap(), |
| 116 |
None => { |
| 117 |
let binding_crate_dir = lib.find_crate_dir(&lib.binding_crate_ref()); |
| 118 |
|
| 119 |
if let Some(binding_crate_dir) = binding_crate_dir { |
| 120 |
Config::from_root_or_default(binding_crate_dir) |
| 121 |
} else { |
| 122 |
// This shouldn't happen |
| 123 |
Config::from_root_or_default(input) |
| 124 |
} |
| 125 |
} |
| 126 |
}; |
| 127 |
|
| 128 |
apply_config_overrides(&mut config, matches); |
| 129 |
|
| 130 |
Builder::new() |
| 131 |
.with_config(config) |
| 132 |
.with_cargo(lib) |
| 133 |
.generate() |
| 134 |
} |
| 135 |
|
| 136 |
fn main() { |
| 137 |
let matches = Command::new("cbindgen") |
| 138 |
.version(bindgen::VERSION) |
| 139 |
.about("Generate C bindings for a Rust library") |
| 140 |
.arg( |
| 141 |
Arg::new("v") |
| 142 |
.short('v') |
| 143 |
.multiple_occurrences(true) |
| 144 |
.help("Enable verbose logging"), |
| 145 |
) |
| 146 |
.arg( |
| 147 |
Arg::new("verify") |
| 148 |
.long("verify") |
| 149 |
.help("Generate bindings and compare it to the existing bindings file and error if they are different"), |
| 150 |
) |
| 151 |
.arg( |
| 152 |
Arg::new("config") |
| 153 |
.short('c') |
| 154 |
.long("config") |
| 155 |
.value_name("PATH") |
| 156 |
.help("Specify path to a `cbindgen.toml` config to use"), |
| 157 |
) |
| 158 |
.arg( |
| 159 |
Arg::new("lang") |
| 160 |
.short('l') |
| 161 |
.long("lang") |
| 162 |
.value_name("LANGUAGE") |
| 163 |
.help("Specify the language to output bindings in") |
| 164 |
.possible_values(["c++", "C++", "c", "C", "cython", "Cython"]), |
| 165 |
) |
| 166 |
.arg( |
| 167 |
Arg::new("cpp-compat") |
| 168 |
.long("cpp-compat") |
| 169 |
.help("Whether to add C++ compatibility to generated C bindings") |
| 170 |
) |
| 171 |
.arg( |
| 172 |
Arg::new("only-target-dependencies") |
| 173 |
.long("only-target-dependencies") |
| 174 |
.help("Only fetch dependencies needed by the target platform. \ |
| 175 |
The target platform defaults to the host platform; set TARGET to override.") |
| 176 |
) |
| 177 |
.arg( |
| 178 |
Arg::new("style") |
| 179 |
.short('s') |
| 180 |
.long("style") |
| 181 |
.value_name("STYLE") |
| 182 |
.help("Specify the declaration style to use for bindings") |
| 183 |
.possible_values(["Both", "both", "Tag", "tag", "Type", "type"]), |
| 184 |
) |
| 185 |
.arg( |
| 186 |
Arg::new("d") |
| 187 |
.short('d') |
| 188 |
.long("parse-dependencies") |
| 189 |
.help("Whether to parse dependencies when generating bindings"), |
| 190 |
) |
| 191 |
.arg( |
| 192 |
Arg::new("clean") |
| 193 |
.long("clean") |
| 194 |
.help( |
| 195 |
"Whether to use a new temporary directory for expanding macros. \ |
| 196 |
Affects performance, but might be required in certain build processes.") |
| 197 |
.required(false) |
| 198 |
) |
| 199 |
.arg( |
| 200 |
Arg::new("INPUT") |
| 201 |
.help( |
| 202 |
"A crate directory or source file to generate bindings for. \ |
| 203 |
In general this is the folder where the Cargo.toml file of \ |
| 204 |
source Rust library resides.") |
| 205 |
.required(false) |
| 206 |
.index(1), |
| 207 |
) |
| 208 |
.arg( |
| 209 |
Arg::new("crate") |
| 210 |
.long("crate") |
| 211 |
.value_name("CRATE_NAME") |
| 212 |
.help( |
| 213 |
"If generating bindings for a crate, \ |
| 214 |
the specific crate to generate bindings for", |
| 215 |
) |
| 216 |
.required(false), |
| 217 |
) |
| 218 |
.arg( |
| 219 |
Arg::new("out") |
| 220 |
.short('o') |
| 221 |
.long("output") |
| 222 |
.value_name("PATH") |
| 223 |
.help("The file to output the bindings to") |
| 224 |
.required(false), |
| 225 |
) |
| 226 |
.arg( |
| 227 |
Arg::new("lockfile") |
| 228 |
.long("lockfile") |
| 229 |
.value_name("PATH") |
| 230 |
.help( |
| 231 |
"Specify the path to the Cargo.lock file explicitly. If this \ |
| 232 |
is not specified, the Cargo.lock file is searched for in the \ |
| 233 |
same folder as the Cargo.toml file. This option is useful for \ |
| 234 |
projects that use workspaces.") |
| 235 |
.required(false), |
| 236 |
) |
| 237 |
.arg( |
| 238 |
Arg::new("metadata") |
| 239 |
.long("metadata") |
| 240 |
.value_name("PATH") |
| 241 |
.help( |
| 242 |
"Specify the path to the output of a `cargo metadata` \ |
| 243 |
command that allows to get dependency information. \ |
| 244 |
This is useful because cargo metadata may be the longest \ |
| 245 |
part of cbindgen runtime, and you may want to share it \ |
| 246 |
across cbindgen invocations. By default cbindgen will run \ |
| 247 |
`cargo metadata --all-features --format-version 1 \ |
| 248 |
--manifest-path <path/to/crate/Cargo.toml>" |
| 249 |
) |
| 250 |
.required(false), |
| 251 |
) |
| 252 |
.arg( |
| 253 |
Arg::new("profile") |
| 254 |
.long("profile") |
| 255 |
.value_name("PROFILE") |
| 256 |
.help( |
| 257 |
"Specify the profile to use when expanding macros. \ |
| 258 |
Has no effect otherwise." |
| 259 |
) |
| 260 |
.possible_values(["Debug", "debug", "Release", "release"]), |
| 261 |
) |
| 262 |
.arg( |
| 263 |
Arg::new("quiet") |
| 264 |
.short('q') |
| 265 |
.long("quiet") |
| 266 |
.help("Report errors only (overrides verbosity options).") |
| 267 |
.required(false), |
| 268 |
) |
| 269 |
.arg( |
| 270 |
Arg::new("depfile") |
| 271 |
.value_name("PATH") |
| 272 |
.long("depfile") |
| 273 |
.takes_value(true) |
| 274 |
.min_values(1) |
| 275 |
.max_values(1) |
| 276 |
.required(false) |
| 277 |
.help("Generate a depfile at the given Path listing the source files \ |
| 278 |
cbindgen traversed when generating the bindings. Useful when \ |
| 279 |
integrating cbindgen into 3rd party build-systems. \ |
| 280 |
This option is ignored if `--out` is missing." |
| 281 |
) |
| 282 |
) |
| 283 |
.get_matches(); |
| 284 |
|
| 285 |
if !matches.is_present("out") && matches.is_present("verify") { |
| 286 |
error!( |
| 287 |
"Cannot verify bindings against `stdout`, please specify a file to compare against." |
| 288 |
); |
| 289 |
std::process::exit(2); |
| 290 |
} |
| 291 |
|
| 292 |
// Initialize logging |
| 293 |
if matches.is_present("quiet") { |
| 294 |
logging::ErrorLogger::init().unwrap(); |
| 295 |
} else { |
| 296 |
match matches.occurrences_of("v") { |
| 297 |
0 => logging::WarnLogger::init().unwrap(), |
| 298 |
1 => logging::InfoLogger::init().unwrap(), |
| 299 |
_ => logging::TraceLogger::init().unwrap(), |
| 300 |
} |
| 301 |
} |
| 302 |
|
| 303 |
// Find the input directory |
| 304 |
let input = match matches.value_of("INPUT") { |
| 305 |
Some(input) => PathBuf::from(input), |
| 306 |
None => env::current_dir().unwrap(), |
| 307 |
}; |
| 308 |
|
| 309 |
let bindings = match load_bindings(&input, &matches) { |
| 310 |
Ok(bindings) => bindings, |
| 311 |
Err(msg) => { |
| 312 |
error!("{}", msg); |
| 313 |
error!("Couldn't generate bindings for {}.", input.display()); |
| 314 |
std::process::exit(1); |
| 315 |
} |
| 316 |
}; |
| 317 |
|
| 318 |
// Write the bindings file |
| 319 |
match matches.value_of("out") { |
| 320 |
Some(file) => { |
| 321 |
let changed = bindings.write_to_file(file); |
| 322 |
|
| 323 |
if matches.is_present("verify") && changed { |
| 324 |
error!("Bindings changed: {}", file); |
| 325 |
std::process::exit(2); |
| 326 |
} |
| 327 |
if let Some(depfile) = matches.value_of("depfile") { |
| 328 |
bindings.generate_depfile(file, depfile) |
| 329 |
} |
| 330 |
} |
| 331 |
_ => { |
| 332 |
bindings.write(io::stdout()); |
| 333 |
} |
| 334 |
} |
| 335 |
} |