blob: b2cde402fd18395f2c85c2b9f227654c485ca3c7 [file] [log] [blame]
David Tolnay30d214c2020-03-15 23:54:34 -07001#![allow(
2 clippy::inherent_to_string,
3 clippy::large_enum_variant,
4 clippy::new_without_default,
5 clippy::toplevel_ref_arg
6)]
7
David Tolnay7db73692019-10-20 14:51:12 -04008mod gen;
9mod syntax;
10
David Tolnay7eb9c6b2020-01-27 21:52:09 -080011use gen::include;
12use std::io::{self, Write};
David Tolnay7db73692019-10-20 14:51:12 -040013use std::path::PathBuf;
14use structopt::StructOpt;
15
16#[derive(StructOpt, Debug)]
David Tolnayfa66e2a2020-01-27 23:05:22 -080017#[structopt(
18 name = "cxxbridge",
19 author,
20 about = "https://github.com/dtolnay/cxx",
21 usage = "\
22 cxxbridge <input>.rs Emit .cc file for bridge to stdout
23 cxxbridge <input>.rs --header Emit .h file for bridge to stdout
David Tolnay736cbca2020-03-11 16:49:18 -070024 cxxbridge --header Emit rust/cxx.h header to stdout",
David Tolnayfa66e2a2020-01-27 23:05:22 -080025 help_message = "Print help information",
26 version_message = "Print version information"
27)]
David Tolnay7db73692019-10-20 14:51:12 -040028struct Opt {
29 /// Input Rust source file containing #[cxx::bridge]
David Tolnay4aea2752020-01-27 22:55:55 -080030 #[structopt(parse(from_os_str), required_unless = "header")]
RS0a2d1172020-01-27 21:47:37 -080031 input: Option<PathBuf>,
David Tolnay7db73692019-10-20 14:51:12 -040032
David Tolnay7eb9c6b2020-01-27 21:52:09 -080033 /// Emit header with declarations only
David Tolnay7db73692019-10-20 14:51:12 -040034 #[structopt(long)]
35 header: bool,
36}
37
David Tolnay4aea2752020-01-27 22:55:55 -080038fn write(content: impl AsRef<[u8]>) {
39 let _ = io::stdout().lock().write_all(content.as_ref());
40}
41
David Tolnay7eb9c6b2020-01-27 21:52:09 -080042fn main() {
David Tolnay7db73692019-10-20 14:51:12 -040043 let opt = Opt::from_args();
RS0a2d1172020-01-27 21:47:37 -080044
David Tolnay4aea2752020-01-27 22:55:55 -080045 match (opt.input, opt.header) {
46 (Some(input), true) => write(gen::do_generate_header(&input)),
47 (Some(input), false) => write(gen::do_generate_bridge(&input)),
48 (None, true) => write(include::HEADER),
49 (None, false) => unreachable!(), // enforced by required_unless
RS0a2d1172020-01-27 21:47:37 -080050 }
David Tolnay7db73692019-10-20 14:51:12 -040051}