| David Tolnay | 39efd73 | 2020-08-30 13:21:51 -0700 | [diff] [blame] | 1 | #[cfg(test)] |
| 2 | #[path = "test.rs"] |
| 3 | mod test; |
| 4 | |
| David Tolnay | 9ef74ce | 2020-09-13 23:24:02 -0400 | [diff] [blame] | 5 | use super::{Opt, Output}; |
| David Tolnay | 39fa366 | 2020-07-30 20:23:18 -0700 | [diff] [blame] | 6 | use clap::AppSettings; |
| 7 | use std::ffi::{OsStr, OsString}; |
| 8 | use std::path::PathBuf; |
| 9 | |
| 10 | type App = clap::App<'static, 'static>; |
| 11 | type Arg = clap::Arg<'static, 'static>; |
| 12 | |
| 13 | const 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 Tolnay | e32c502 | 2020-09-13 23:03:05 -0400 | [diff] [blame] | 16 | cxxbridge --header Emit \"rust/cxx.h\" header to stdout\ |
| David Tolnay | 39fa366 | 2020-07-30 20:23:18 -0700 | [diff] [blame] | 17 | "; |
| 18 | |
| 19 | const TEMPLATE: &str = "\ |
| 20 | {bin} {version} |
| 21 | David Tolnay <dtolnay@gmail.com> |
| 22 | https://github.com/dtolnay/cxx |
| 23 | |
| 24 | USAGE: |
| 25 | {usage} |
| 26 | |
| 27 | ARGS: |
| 28 | {positionals} |
| 29 | OPTIONS: |
| 30 | {unified}\ |
| 31 | "; |
| 32 | |
| 33 | fn 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 Tolnay | 9ef74ce | 2020-09-13 23:24:02 -0400 | [diff] [blame] | 42 | .arg(arg_output()) |
| David Tolnay | 39fa366 | 2020-07-30 20:23:18 -0700 | [diff] [blame] | 43 | .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 | |
| 51 | const INPUT: &str = "input"; |
| 52 | const CXX_IMPL_ANNOTATIONS: &str = "cxx-impl-annotations"; |
| 53 | const HEADER: &str = "header"; |
| 54 | const INCLUDE: &str = "include"; |
| David Tolnay | 9ef74ce | 2020-09-13 23:24:02 -0400 | [diff] [blame] | 55 | const OUTPUT: &str = "output"; |
| David Tolnay | 39fa366 | 2020-07-30 20:23:18 -0700 | [diff] [blame] | 56 | |
| 57 | pub(super) fn from_args() -> Opt { |
| 58 | let matches = app().get_matches(); |
| David Tolnay | 6427614 | 2020-09-24 10:43:02 -0400 | [diff] [blame] | 59 | |
| 60 | let input = matches.value_of_os(INPUT).map(PathBuf::from); |
| 61 | let cxx_impl_annotations = matches.value_of(CXX_IMPL_ANNOTATIONS).map(str::to_owned); |
| 62 | let header = matches.is_present(HEADER); |
| 63 | let include = matches |
| 64 | .values_of(INCLUDE) |
| David Tolnay | 13213b7 | 2020-09-24 10:50:07 -0400 | [diff] [blame^] | 65 | .unwrap_or_default() |
| 66 | .map(str::to_owned) |
| 67 | .collect(); |
| David Tolnay | 6427614 | 2020-09-24 10:43:02 -0400 | [diff] [blame] | 68 | let output = match matches.value_of_os(OUTPUT) { |
| 69 | None => Output::Stdout, |
| 70 | Some(path) if path == "-" => Output::Stdout, |
| 71 | Some(path) => Output::File(PathBuf::from(path)), |
| 72 | }; |
| 73 | |
| David Tolnay | 39fa366 | 2020-07-30 20:23:18 -0700 | [diff] [blame] | 74 | Opt { |
| David Tolnay | 6427614 | 2020-09-24 10:43:02 -0400 | [diff] [blame] | 75 | input, |
| 76 | cxx_impl_annotations, |
| 77 | header, |
| 78 | include, |
| 79 | output, |
| David Tolnay | 39fa366 | 2020-07-30 20:23:18 -0700 | [diff] [blame] | 80 | } |
| 81 | } |
| 82 | |
| 83 | fn validate_utf8(arg: &OsStr) -> Result<(), OsString> { |
| 84 | if arg.to_str().is_some() { |
| 85 | Ok(()) |
| 86 | } else { |
| 87 | Err(OsString::from("invalid utf-8 sequence")) |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | fn arg_input() -> Arg { |
| 92 | Arg::with_name(INPUT) |
| 93 | .help("Input Rust source file containing #[cxx::bridge].") |
| 94 | .required_unless(HEADER) |
| 95 | } |
| 96 | |
| 97 | fn arg_cxx_impl_annotations() -> Arg { |
| 98 | const HELP: &str = "\ |
| 99 | Optional annotation for implementations of C++ function wrappers |
| 100 | that may be exposed to Rust. You may for example need to provide |
| 101 | __declspec(dllexport) or __attribute__((visibility(\"default\"))) |
| 102 | if Rust code from one shared object or executable depends on |
| 103 | these C++ functions in another. |
| 104 | "; |
| 105 | Arg::with_name(CXX_IMPL_ANNOTATIONS) |
| 106 | .long(CXX_IMPL_ANNOTATIONS) |
| 107 | .takes_value(true) |
| 108 | .value_name("annotation") |
| 109 | .validator_os(validate_utf8) |
| 110 | .help(HELP) |
| 111 | } |
| 112 | |
| 113 | fn arg_header() -> Arg { |
| David Tolnay | e2e47a3 | 2020-09-24 10:23:27 -0400 | [diff] [blame] | 114 | const HELP: &str = "\ |
| 115 | Emit header with declarations only. Optional if using `-o` with |
| 116 | a path ending in `.h`. |
| 117 | "; |
| 118 | Arg::with_name(HEADER).long(HEADER).help(HELP) |
| David Tolnay | 39fa366 | 2020-07-30 20:23:18 -0700 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | fn arg_include() -> Arg { |
| 122 | const HELP: &str = "\ |
| 123 | Any additional headers to #include. The cxxbridge tool does not |
| 124 | parse or even require the given paths to exist; they simply go |
| 125 | into the generated C++ code as #include lines. |
| 126 | "; |
| 127 | Arg::with_name(INCLUDE) |
| 128 | .long(INCLUDE) |
| 129 | .short("i") |
| 130 | .takes_value(true) |
| 131 | .multiple(true) |
| 132 | .validator_os(validate_utf8) |
| 133 | .help(HELP) |
| 134 | } |
| David Tolnay | 9ef74ce | 2020-09-13 23:24:02 -0400 | [diff] [blame] | 135 | |
| 136 | fn arg_output() -> Arg { |
| 137 | const HELP: &str = "\ |
| 138 | Path of file to write as output. Output goes to stdout if -o is |
| 139 | not specified. |
| 140 | "; |
| 141 | Arg::with_name(OUTPUT) |
| 142 | .long(OUTPUT) |
| 143 | .short("o") |
| 144 | .takes_value(true) |
| 145 | .validator_os(validate_utf8) |
| 146 | .help(HELP) |
| 147 | } |