blob: caab254eb1f08ea869d565b003805daeed4c9bd4 [file] [log] [blame]
David Tolnay39efd732020-08-30 13:21:51 -07001#[cfg(test)]
2#[path = "test.rs"]
3mod test;
4
David Tolnay9ef74ce2020-09-13 23:24:02 -04005use super::{Opt, Output};
David Tolnay39fa3662020-07-30 20:23:18 -07006use clap::AppSettings;
7use std::ffi::{OsStr, OsString};
8use std::path::PathBuf;
9
10type App = clap::App<'static, 'static>;
11type Arg = clap::Arg<'static, 'static>;
12
13const USAGE: &str = "\
14 cxxbridge <input>.rs Emit .cc file for bridge to stdout
15 cxxbridge <input>.rs --header Emit .h file for bridge to stdout
David Tolnaye32c5022020-09-13 23:03:05 -040016 cxxbridge --header Emit \"rust/cxx.h\" header to stdout\
David Tolnay39fa3662020-07-30 20:23:18 -070017";
18
19const TEMPLATE: &str = "\
20{bin} {version}
21David Tolnay <dtolnay@gmail.com>
22https://github.com/dtolnay/cxx
23
24USAGE:
25 {usage}
26
27ARGS:
28{positionals}
29OPTIONS:
30{unified}\
31";
32
33fn app() -> App {
34 let mut app = App::new("cxxbridge")
35 .usage(USAGE)
36 .template(TEMPLATE)
37 .setting(AppSettings::NextLineHelp)
38 .arg(arg_input())
39 .arg(arg_cxx_impl_annotations())
40 .arg(arg_header())
41 .arg(arg_include())
David Tolnay9ef74ce2020-09-13 23:24:02 -040042 .arg(arg_output())
David Tolnay39fa3662020-07-30 20:23:18 -070043 .help_message("Print help information.")
44 .version_message("Print version information.");
45 if let Some(version) = option_env!("CARGO_PKG_VERSION") {
46 app = app.version(version);
47 }
48 app
49}
50
51const INPUT: &str = "input";
52const CXX_IMPL_ANNOTATIONS: &str = "cxx-impl-annotations";
53const HEADER: &str = "header";
54const INCLUDE: &str = "include";
David Tolnay9ef74ce2020-09-13 23:24:02 -040055const OUTPUT: &str = "output";
David Tolnay39fa3662020-07-30 20:23:18 -070056
57pub(super) fn from_args() -> Opt {
58 let matches = app().get_matches();
59 Opt {
60 input: matches.value_of_os(INPUT).map(PathBuf::from),
61 cxx_impl_annotations: matches.value_of(CXX_IMPL_ANNOTATIONS).map(str::to_owned),
62 header: matches.is_present(HEADER),
63 include: matches
64 .values_of(INCLUDE)
65 .map_or_else(Vec::new, |v| v.map(str::to_owned).collect()),
David Tolnay9ef74ce2020-09-13 23:24:02 -040066 output: match matches.value_of_os(OUTPUT) {
67 None => Output::Stdout,
68 Some(path) if path == "-" => Output::Stdout,
69 Some(path) => Output::File(PathBuf::from(path)),
70 },
David Tolnay39fa3662020-07-30 20:23:18 -070071 }
72}
73
74fn validate_utf8(arg: &OsStr) -> Result<(), OsString> {
75 if arg.to_str().is_some() {
76 Ok(())
77 } else {
78 Err(OsString::from("invalid utf-8 sequence"))
79 }
80}
81
82fn arg_input() -> Arg {
83 Arg::with_name(INPUT)
84 .help("Input Rust source file containing #[cxx::bridge].")
85 .required_unless(HEADER)
86}
87
88fn arg_cxx_impl_annotations() -> Arg {
89 const HELP: &str = "\
90Optional annotation for implementations of C++ function wrappers
91that may be exposed to Rust. You may for example need to provide
92__declspec(dllexport) or __attribute__((visibility(\"default\")))
93if Rust code from one shared object or executable depends on
94these C++ functions in another.
95 ";
96 Arg::with_name(CXX_IMPL_ANNOTATIONS)
97 .long(CXX_IMPL_ANNOTATIONS)
98 .takes_value(true)
99 .value_name("annotation")
100 .validator_os(validate_utf8)
101 .help(HELP)
102}
103
104fn arg_header() -> Arg {
105 Arg::with_name(HEADER)
106 .long(HEADER)
107 .help("Emit header with declarations only.")
108}
109
110fn arg_include() -> Arg {
111 const HELP: &str = "\
112Any additional headers to #include. The cxxbridge tool does not
113parse or even require the given paths to exist; they simply go
114into the generated C++ code as #include lines.
115 ";
116 Arg::with_name(INCLUDE)
117 .long(INCLUDE)
118 .short("i")
119 .takes_value(true)
120 .multiple(true)
121 .validator_os(validate_utf8)
122 .help(HELP)
123}
David Tolnay9ef74ce2020-09-13 23:24:02 -0400124
125fn arg_output() -> Arg {
126 const HELP: &str = "\
127Path of file to write as output. Output goes to stdout if -o is
128not specified.
129 ";
130 Arg::with_name(OUTPUT)
131 .long(OUTPUT)
132 .short("o")
133 .takes_value(true)
134 .validator_os(validate_utf8)
135 .help(HELP)
136}