| David Tolnay | f8ed073 | 2020-04-29 12:34:47 -0700 | [diff] [blame] | 1 | //! The CXX code generator for constructing and compiling C++ code. |
| 2 | //! |
| 3 | //! This is intended to be used from Cargo build scripts to execute CXX's |
| 4 | //! C++ code generator, set up any additional compiler flags depending on |
| 5 | //! the use case, and make the C++ compiler invocation. |
| 6 | //! |
| 7 | //! <br> |
| 8 | //! |
| 9 | //! # Example |
| 10 | //! |
| 11 | //! Example of a canonical Cargo build script that builds a CXX bridge: |
| 12 | //! |
| 13 | //! ```no_run |
| 14 | //! // build.rs |
| 15 | //! |
| 16 | //! fn main() { |
| 17 | //! cxx_build::bridge("src/main.rs") |
| 18 | //! .file("../demo-cxx/demo.cc") |
| Philip Craig | 7e14e2e | 2020-05-09 10:42:30 +0100 | [diff] [blame] | 19 | //! .flag_if_supported("-std=c++11") |
| David Tolnay | f8ed073 | 2020-04-29 12:34:47 -0700 | [diff] [blame] | 20 | //! .compile("cxxbridge-demo"); |
| 21 | //! |
| 22 | //! println!("cargo:rerun-if-changed=src/main.rs"); |
| 23 | //! println!("cargo:rerun-if-changed=../demo-cxx/demo.h"); |
| 24 | //! println!("cargo:rerun-if-changed=../demo-cxx/demo.cc"); |
| 25 | //! } |
| 26 | //! ``` |
| 27 | //! |
| 28 | //! A runnable working setup with this build script is shown in the |
| 29 | //! *demo-rs* and *demo-cxx* directories of [https://github.com/dtolnay/cxx]. |
| 30 | //! |
| 31 | //! [https://github.com/dtolnay/cxx]: https://github.com/dtolnay/cxx |
| 32 | //! |
| 33 | //! <br> |
| 34 | //! |
| 35 | //! # Alternatives |
| 36 | //! |
| 37 | //! For use in non-Cargo builds like Bazel or Buck, CXX provides an |
| 38 | //! alternate way of invoking the C++ code generator as a standalone command |
| 39 | //! line tool. The tool is packaged as the `cxxbridge-cmd` crate. |
| 40 | //! |
| 41 | //! ```bash |
| 42 | //! $ cargo install cxxbridge-cmd # or build it from the repo |
| 43 | //! |
| 44 | //! $ cxxbridge src/main.rs --header > path/to/mybridge.h |
| 45 | //! $ cxxbridge src/main.rs > path/to/mybridge.cc |
| 46 | //! ``` |
| 47 | |
| David Tolnay | db450b0 | 2020-05-05 10:16:57 -0700 | [diff] [blame] | 48 | #![allow( |
| 49 | clippy::inherent_to_string, |
| 50 | clippy::needless_doctest_main, |
| 51 | clippy::new_without_default, |
| 52 | clippy::toplevel_ref_arg |
| 53 | )] |
| 54 | |
| David Tolnay | f8ed073 | 2020-04-29 12:34:47 -0700 | [diff] [blame] | 55 | mod error; |
| 56 | mod gen; |
| 57 | mod paths; |
| 58 | mod syntax; |
| 59 | |
| 60 | use crate::error::Result; |
| David Tolnay | bb3ba50 | 2020-08-30 19:59:41 -0700 | [diff] [blame] | 61 | use crate::gen::error::report; |
| David Tolnay | 0d85ccd | 2020-08-30 20:18:13 -0700 | [diff] [blame] | 62 | use crate::gen::{fs, Opt}; |
| David Tolnay | f574c5e | 2020-08-31 00:22:27 -0700 | [diff] [blame^] | 63 | use cc::Build; |
| David Tolnay | f8ed073 | 2020-04-29 12:34:47 -0700 | [diff] [blame] | 64 | use std::io::{self, Write}; |
| David Tolnay | 6e80833 | 2020-05-07 19:43:56 -0700 | [diff] [blame] | 65 | use std::iter; |
| David Tolnay | f8ed073 | 2020-04-29 12:34:47 -0700 | [diff] [blame] | 66 | use std::path::Path; |
| 67 | use std::process; |
| 68 | |
| 69 | /// This returns a [`cc::Build`] on which you should continue to set up any |
| 70 | /// additional source files or compiler flags, and lastly call its [`compile`] |
| 71 | /// method to execute the C++ build. |
| 72 | /// |
| 73 | /// [`compile`]: https://docs.rs/cc/1.0.49/cc/struct.Build.html#method.compile |
| 74 | #[must_use] |
| David Tolnay | f574c5e | 2020-08-31 00:22:27 -0700 | [diff] [blame^] | 75 | pub fn bridge(rust_source_file: impl AsRef<Path>) -> Build { |
| David Tolnay | 6e80833 | 2020-05-07 19:43:56 -0700 | [diff] [blame] | 76 | bridges(iter::once(rust_source_file)) |
| 77 | } |
| 78 | |
| 79 | /// `cxx_build::bridge` but for when more than one file contains a |
| 80 | /// #\[cxx::bridge\] module. |
| 81 | /// |
| 82 | /// ```no_run |
| 83 | /// let source_files = vec!["src/main.rs", "src/path/to/other.rs"]; |
| 84 | /// cxx_build::bridges(source_files) |
| 85 | /// .file("../demo-cxx/demo.cc") |
| Philip Craig | 7e14e2e | 2020-05-09 10:42:30 +0100 | [diff] [blame] | 86 | /// .flag_if_supported("-std=c++11") |
| David Tolnay | 6e80833 | 2020-05-07 19:43:56 -0700 | [diff] [blame] | 87 | /// .compile("cxxbridge-demo"); |
| 88 | /// ``` |
| David Tolnay | f574c5e | 2020-08-31 00:22:27 -0700 | [diff] [blame^] | 89 | pub fn bridges(rust_source_files: impl IntoIterator<Item = impl AsRef<Path>>) -> Build { |
| David Tolnay | 6e80833 | 2020-05-07 19:43:56 -0700 | [diff] [blame] | 90 | let mut build = paths::cc_build(); |
| David Tolnay | 110df7d | 2020-05-08 13:06:04 -0700 | [diff] [blame] | 91 | build.cpp(true); |
| 92 | build.cpp_link_stdlib(None); // linked via link-cplusplus crate |
| 93 | |
| David Tolnay | 6e80833 | 2020-05-07 19:43:56 -0700 | [diff] [blame] | 94 | for path in rust_source_files { |
| 95 | if let Err(err) = try_generate_bridge(&mut build, path.as_ref()) { |
| David Tolnay | bb3ba50 | 2020-08-30 19:59:41 -0700 | [diff] [blame] | 96 | let _ = writeln!(io::stderr(), "\n\ncxxbridge error: {}\n\n", report(err)); |
| David Tolnay | f8ed073 | 2020-04-29 12:34:47 -0700 | [diff] [blame] | 97 | process::exit(1); |
| 98 | } |
| 99 | } |
| David Tolnay | 110df7d | 2020-05-08 13:06:04 -0700 | [diff] [blame] | 100 | |
| David Tolnay | 6e80833 | 2020-05-07 19:43:56 -0700 | [diff] [blame] | 101 | build |
| David Tolnay | f8ed073 | 2020-04-29 12:34:47 -0700 | [diff] [blame] | 102 | } |
| 103 | |
| David Tolnay | f574c5e | 2020-08-31 00:22:27 -0700 | [diff] [blame^] | 104 | fn try_generate_bridge(build: &mut Build, rust_source_file: &Path) -> Result<()> { |
| David Tolnay | a5cca31 | 2020-08-29 23:40:04 -0700 | [diff] [blame] | 105 | let opt = Opt::default(); |
| David Tolnay | 8238d4a | 2020-08-30 00:34:17 -0700 | [diff] [blame] | 106 | let generated = gen::generate_from_path(rust_source_file, &opt); |
| 107 | |
| David Tolnay | f8ed073 | 2020-04-29 12:34:47 -0700 | [diff] [blame] | 108 | let header_path = paths::out_with_extension(rust_source_file, ".h")?; |
| 109 | fs::create_dir_all(header_path.parent().unwrap())?; |
| David Tolnay | 8238d4a | 2020-08-30 00:34:17 -0700 | [diff] [blame] | 110 | fs::write(&header_path, generated.header)?; |
| David Tolnay | f8ed073 | 2020-04-29 12:34:47 -0700 | [diff] [blame] | 111 | paths::symlink_header(&header_path, rust_source_file); |
| 112 | |
| David Tolnay | 8238d4a | 2020-08-30 00:34:17 -0700 | [diff] [blame] | 113 | let implementation_path = paths::out_with_extension(rust_source_file, ".cc")?; |
| 114 | fs::write(&implementation_path, generated.implementation)?; |
| 115 | build.file(&implementation_path); |
| David Tolnay | f8ed073 | 2020-04-29 12:34:47 -0700 | [diff] [blame] | 116 | |
| 117 | let ref cxx_h = paths::include_dir()?.join("rust").join("cxx.h"); |
| 118 | let _ = fs::create_dir_all(cxx_h.parent().unwrap()); |
| 119 | let _ = fs::remove_file(cxx_h); |
| 120 | let _ = fs::write(cxx_h, gen::include::HEADER); |
| 121 | |
| David Tolnay | 6e80833 | 2020-05-07 19:43:56 -0700 | [diff] [blame] | 122 | Ok(()) |
| David Tolnay | f8ed073 | 2020-04-29 12:34:47 -0700 | [diff] [blame] | 123 | } |