forked from redox-os/cbindgen
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.rs
214 lines (193 loc) · 6.64 KB
/
main.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
use std::env;
use std::io;
use std::path::{Path, PathBuf};
extern crate clap;
#[macro_use]
extern crate log;
#[macro_use]
extern crate serde;
extern crate serde_json;
extern crate standalone_syn as syn;
extern crate toml;
use clap::{App, Arg, ArgMatches};
mod logging;
mod bindgen;
use bindgen::{Bindings, Builder, Cargo, Config, Error, Language, Style};
fn apply_config_overrides<'a>(config: &mut Config, matches: &ArgMatches<'a>) {
// We allow specifying a language to override the config default. This is
// used by compile-tests.
if let Some(lang) = matches.value_of("lang") {
config.language = match lang {
"C++" => Language::Cxx,
"c++" => Language::Cxx,
"C" => Language::C,
"c" => Language::C,
_ => {
error!("Unknown language specified.");
return;
}
};
}
if let Some(style) = matches.value_of("style") {
config.style = match style {
"Both" => Style::Both,
"both" => Style::Both,
"Tag" => Style::Tag,
"tag" => Style::Tag,
"Type" => Style::Type,
"type" => Style::Type,
_ => {
error!("Unknown style specified.");
return;
}
}
}
if matches.is_present("d") {
config.parse.parse_deps = true;
}
}
fn load_bindings<'a>(input: &Path, matches: &ArgMatches<'a>) -> Result<Bindings, Error> {
// If a file is specified then we load it as a single source
if !input.is_dir() {
// Load any config specified or search in the input directory
let mut config = match matches.value_of("config") {
Some(c) => Config::from_file(c).unwrap(),
None => Config::from_root_or_default(input),
};
apply_config_overrides(&mut config, &matches);
return Builder::new()
.with_config(config)
.with_src(input)
.generate();
}
// We have to load a whole crate, so we use cargo to gather metadata
let lib = Cargo::load(input, matches.value_of("lockfile"), matches.value_of("crate"), true)?;
// Load any config specified or search in the binding crate directory
let mut config = match matches.value_of("config") {
Some(c) => Config::from_file(c).unwrap(),
None => {
let binding_crate_dir = lib.find_crate_dir(&lib.binding_crate_ref());
if let Some(binding_crate_dir) = binding_crate_dir {
Config::from_root_or_default(&binding_crate_dir)
} else {
// This shouldn't happen
Config::from_root_or_default(input)
}
}
};
apply_config_overrides(&mut config, &matches);
Builder::new()
.with_config(config)
.with_cargo(lib)
.generate()
}
fn main() {
let matches = App::new("cbindgen")
.version(bindgen::VERSION)
.about("Generate C bindings for a Rust library")
.arg(
Arg::with_name("v")
.short("v")
.multiple(true)
.help("Enable verbose logging"),
)
.arg(
Arg::with_name("config")
.short("c")
.long("config")
.value_name("PATH")
.help("Specify path to a `cbindgen.toml` config to use"),
)
.arg(
Arg::with_name("lang")
.short("l")
.long("lang")
.value_name("LANGUAGE")
.help("Specify the language to output bindings in")
.possible_values(&["c++", "C++", "c", "C"]),
)
.arg(
Arg::with_name("style")
.short("s")
.long("style")
.value_name("STYLE")
.help("Specify the language to output bindings in")
.possible_values(&["Both", "both", "Tag", "tag", "Type", "type"]),
)
.arg(
Arg::with_name("d")
.short("d")
.long("parse-dependencies")
.help("Whether to parse dependencies when generating bindings"),
)
.arg(
Arg::with_name("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)
.index(1),
)
.arg(
Arg::with_name("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::with_name("out")
.short("o")
.long("output")
.value_name("PATH")
.help("The file to output the bindings to")
.required(false),
)
.arg(
Arg::with_name("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.")
.required(false),
)
.get_matches();
// Initialize logging
match matches.occurrences_of("v") {
0 => logging::WarnLogger::init().unwrap(),
1 => logging::InfoLogger::init().unwrap(),
_ => logging::TraceLogger::init().unwrap(),
}
// Find the input directory
let input = match matches.value_of("INPUT") {
Some(input) => PathBuf::from(input),
None => 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.value_of("out") {
Some(file) => {
bindings.write_to_file(file);
}
_ => {
bindings.write(io::stdout());
}
}
}