blob: 064056ed412454a474ddbea24247ea89a1a18736 [file] [log] [blame]
Brian Silverman4e662aa2022-05-11 23:10:19 -07001// Copyright 2020 Google LLC
2//
3// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
4// https://www.apache.org/licenses/LICENSE-2.0> or the MIT license
5// <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your
6// option. This file may not be copied, modified, or distributed
7// except according to those terms.
8
9use autocxx_parser::file_locations::FileLocationStrategy;
10use miette::Diagnostic;
11use thiserror::Error;
12
Austin Schuh6ea9bfa2023-08-06 19:05:10 -070013use crate::{generate_rs_single, CodegenOptions};
14use crate::{get_cxx_header_bytes, CppCodegenOptions, ParseError, RebuildDependencyRecorder};
Brian Silverman4e662aa2022-05-11 23:10:19 -070015use std::ffi::OsStr;
16use std::ffi::OsString;
17use std::fs::File;
18use std::io::Write;
19use std::marker::PhantomData;
20use std::path::{Path, PathBuf};
21
22/// Errors returned during creation of a [`cc::Build`] from an include_cxx
23/// macro.
24#[derive(Error, Diagnostic, Debug)]
25#[cfg_attr(feature = "nightly", doc(cfg(feature = "build")))]
26pub enum BuilderError {
27 #[error("cxx couldn't handle our generated bindings - could be a bug in autocxx: {0}")]
28 InvalidCxx(cxx_gen::Error),
29 #[error(transparent)]
30 #[diagnostic(transparent)]
31 ParseError(ParseError),
32 #[error("we couldn't write the generated code to disk at {1}: {0}")]
33 FileWriteFail(std::io::Error, PathBuf),
34 #[error("no include_cpp! macro was found")]
35 NoIncludeCxxMacrosFound,
36 #[error("could not create a directory {1}: {0}")]
37 UnableToCreateDirectory(std::io::Error, PathBuf),
38}
39
40#[cfg_attr(feature = "nightly", doc(cfg(feature = "build")))]
41pub type BuilderBuild = cc::Build;
42
43/// For test purposes only, a [`cc::Build`] and lists of Rust and C++
44/// files generated.
45#[cfg_attr(feature = "nightly", doc(cfg(feature = "build")))]
46pub struct BuilderSuccess(pub BuilderBuild, pub Vec<PathBuf>, pub Vec<PathBuf>);
47
48/// Results of a build.
49#[cfg_attr(feature = "nightly", doc(cfg(feature = "build")))]
50pub type BuilderResult = Result<BuilderSuccess, BuilderError>;
51
52/// The context in which a builder object lives. Callbacks for various
53/// purposes.
54#[cfg_attr(feature = "nightly", doc(cfg(feature = "build")))]
55pub trait BuilderContext {
56 /// Perform any initialization specific to the context in which this
57 /// builder lives.
58 fn setup() {}
59
60 /// Create a dependency recorder, if any.
61 fn get_dependency_recorder() -> Option<Box<dyn RebuildDependencyRecorder>>;
62}
63
64/// An object to allow building of bindings from a `build.rs` file.
65///
Austin Schuh6ea9bfa2023-08-06 19:05:10 -070066/// It would be unusual to create this directly - see the `autocxx_build` or
Brian Silverman4e662aa2022-05-11 23:10:19 -070067/// `autocxx_gen` crates.
Austin Schuh6ea9bfa2023-08-06 19:05:10 -070068///
69/// Once you've got one of these objects, you may set some configuration
70/// options but then you're likely to want to call the [`build`] method.
71///
72/// # Setting C++ version
73///
74/// Ensure you use [`extra_clang_args`] as well as giving an appropriate
75/// option to the [`cc::Build`] which you receive from the [`build`] function.
Brian Silverman4e662aa2022-05-11 23:10:19 -070076#[cfg_attr(feature = "nightly", doc(cfg(feature = "build")))]
77pub struct Builder<'a, BuilderContext> {
78 rs_file: PathBuf,
79 autocxx_incs: Vec<OsString>,
80 extra_clang_args: Vec<String>,
81 dependency_recorder: Option<Box<dyn RebuildDependencyRecorder>>,
82 custom_gendir: Option<PathBuf>,
83 auto_allowlist: bool,
Austin Schuh6ea9bfa2023-08-06 19:05:10 -070084 codegen_options: CodegenOptions<'a>,
Brian Silverman4e662aa2022-05-11 23:10:19 -070085 // This member is to ensure that this type is parameterized
86 // by a BuilderContext. The goal is to balance three needs:
87 // (1) have most of the functionality over in autocxx_engine,
88 // (2) expose this type to users of autocxx_build and to
89 // make it easy for callers simply to call Builder::new,
90 // (3) ensure that such a Builder does a few tasks specific to its use
91 // in a cargo environment.
92 ctx: PhantomData<BuilderContext>,
93}
94
95impl<CTX: BuilderContext> Builder<'_, CTX> {
96 /// Create a new Builder object. You'll need to pass in the Rust file
97 /// which contains the bindings (typically an `include_cpp!` macro
98 /// though `autocxx` can also handle manually-crafted `cxx::bridge`
99 /// bindings), and a list of include directories which should be searched
100 /// by autocxx as it tries to hunt for the include files specified
101 /// within the `include_cpp!` macro.
102 ///
103 /// Usually after this you'd call [`build`].
104 pub fn new(
105 rs_file: impl AsRef<Path>,
106 autocxx_incs: impl IntoIterator<Item = impl AsRef<OsStr>>,
107 ) -> Self {
108 CTX::setup();
109 Self {
110 rs_file: rs_file.as_ref().to_path_buf(),
111 autocxx_incs: autocxx_incs
112 .into_iter()
113 .map(|s| s.as_ref().to_os_string())
114 .collect(),
115 extra_clang_args: Vec::new(),
116 dependency_recorder: CTX::get_dependency_recorder(),
117 custom_gendir: None,
118 auto_allowlist: false,
Austin Schuh6ea9bfa2023-08-06 19:05:10 -0700119 codegen_options: CodegenOptions::default(),
Brian Silverman4e662aa2022-05-11 23:10:19 -0700120 ctx: PhantomData,
121 }
122 }
123
Austin Schuh6ea9bfa2023-08-06 19:05:10 -0700124 /// Specify extra arguments for clang. These are used when parsing
125 /// C++ headers. For example, you might want to provide
126 /// `-std=c++17` to specify C++17.
Brian Silverman4e662aa2022-05-11 23:10:19 -0700127 pub fn extra_clang_args(mut self, extra_clang_args: &[&str]) -> Self {
128 self.extra_clang_args = extra_clang_args.iter().map(|s| s.to_string()).collect();
129 self
130 }
131
132 /// Where to generate the code.
133 pub fn custom_gendir(mut self, custom_gendir: PathBuf) -> Self {
134 self.custom_gendir = Some(custom_gendir);
135 self
136 }
137
138 /// Update C++ code generation options. See [`CppCodegenOptions`] for details.
139 pub fn cpp_codegen_options<F>(mut self, modifier: F) -> Self
140 where
141 F: FnOnce(&mut CppCodegenOptions),
142 {
Austin Schuh6ea9bfa2023-08-06 19:05:10 -0700143 modifier(&mut self.codegen_options.cpp_codegen_options);
Brian Silverman4e662aa2022-05-11 23:10:19 -0700144 self
145 }
146
147 /// Automatically discover uses of the C++ `ffi` mod and generate the allowlist
148 /// from that.
149 /// This is a highly experimental option, not currently recommended.
150 /// It doesn't work in the following cases:
151 /// * Static function calls on types within the FFI mod.
152 /// * Anything inside a macro invocation.
153 /// * You're using a different name for your `ffi` mod
154 /// * You're using multiple FFI mods
155 /// * You've got usages scattered across files beyond that with the
156 /// `include_cpp` invocation
157 /// * You're using `use` statements to rename mods or items. If this
158 /// proves to be a promising or helpful direction, autocxx would be happy
159 /// to accept pull requests to remove some of these limitations.
160 pub fn auto_allowlist(mut self, do_it: bool) -> Self {
161 self.auto_allowlist = do_it;
162 self
163 }
164
Austin Schuh6ea9bfa2023-08-06 19:05:10 -0700165 #[doc(hidden)]
166 /// Whether to force autocxx always to generate extra Rust and C++
167 /// side shims. This is only used by the integration test suite to
168 /// exercise more code paths - don't use it!
169 pub fn force_wrapper_generation(mut self, do_it: bool) -> Self {
170 self.codegen_options.force_wrapper_gen = do_it;
171 self
172 }
173
Brian Silverman4e662aa2022-05-11 23:10:19 -0700174 /// Whether to suppress inclusion of system headers (`memory`, `string` etc.)
175 /// from generated C++ bindings code. This should not normally be used,
176 /// but can occasionally be useful if you're reducing a test case and you
177 /// have a preprocessed header file which already contains absolutely everything
178 /// that the bindings could ever need.
179 pub fn suppress_system_headers(mut self, do_it: bool) -> Self {
Austin Schuh6ea9bfa2023-08-06 19:05:10 -0700180 self.codegen_options
181 .cpp_codegen_options
182 .suppress_system_headers = do_it;
Brian Silverman4e662aa2022-05-11 23:10:19 -0700183 self
184 }
185
186 /// An annotation optionally to include on each C++ function.
187 /// For example to export the symbol from a library.
188 pub fn cxx_impl_annotations(mut self, cxx_impl_annotations: Option<String>) -> Self {
Austin Schuh6ea9bfa2023-08-06 19:05:10 -0700189 self.codegen_options
190 .cpp_codegen_options
191 .cxx_impl_annotations = cxx_impl_annotations;
Brian Silverman4e662aa2022-05-11 23:10:19 -0700192 self
193 }
194
195 /// Build autocxx C++ files and return a [`cc::Build`] you can use to build
196 /// more from a build.rs file.
197 ///
198 /// The error type returned by this function supports [`miette::Diagnostic`],
199 /// so if you use the `miette` crate and its `fancy` feature, then simply
200 /// return a `miette::Result` from your main function, you should get nicely
201 /// printed diagnostics.
Austin Schuh6ea9bfa2023-08-06 19:05:10 -0700202 ///
203 /// As this is a [`cc::Build`] there are lots of options you can apply to
204 /// the resulting options, but please bear in mind that these only apply
205 /// to the build process for the generated code - such options will not
206 /// influence autocxx's process for parsing header files.
207 ///
208 /// For example, if you wish to set the C++ version to C++17, you might
209 /// be tempted to use [`cc::Build::flag_if_supported`] to add the
210 /// `-std=c++17` flag. However, this won't affect the header parsing which
211 /// autocxx does internally (by means of bindgen) so you _additionally_
212 /// should call [`extra_clang_args`] with that same option.
Brian Silverman4e662aa2022-05-11 23:10:19 -0700213 pub fn build(self) -> Result<BuilderBuild, BuilderError> {
214 self.build_listing_files().map(|r| r.0)
215 }
216
217 /// For use in tests only, this does the build and returns additional information
218 /// about the files generated which can subsequently be examined for correctness.
219 /// In production, please use simply [`build`].
220 pub fn build_listing_files(self) -> Result<BuilderSuccess, BuilderError> {
221 let clang_args = &self
222 .extra_clang_args
223 .iter()
224 .map(|s| &s[..])
225 .collect::<Vec<_>>();
226 rust_version_check();
227 let gen_location_strategy = match self.custom_gendir {
228 None => FileLocationStrategy::new(),
229 Some(custom_dir) => FileLocationStrategy::Custom(custom_dir),
230 };
231 let incdir = gen_location_strategy.get_include_dir();
232 ensure_created(&incdir)?;
233 let cxxdir = gen_location_strategy.get_cxx_dir();
234 ensure_created(&cxxdir)?;
235 let rsdir = gen_location_strategy.get_rs_dir();
236 ensure_created(&rsdir)?;
237 // We are incredibly unsophisticated in our directory arrangement here
238 // compared to cxx. I have no doubt that we will need to replicate just
239 // about everything cxx does, in due course...
240 // Write cxx.h to that location, as it may be needed by
241 // some of our generated code.
242 write_to_file(
243 &incdir,
244 "cxx.h",
Austin Schuh6ea9bfa2023-08-06 19:05:10 -0700245 &get_cxx_header_bytes(
246 self.codegen_options
247 .cpp_codegen_options
248 .suppress_system_headers,
249 ),
Brian Silverman4e662aa2022-05-11 23:10:19 -0700250 )?;
251
252 let autocxx_inc = build_autocxx_inc(self.autocxx_incs, &incdir);
253 gen_location_strategy.set_cargo_env_vars_for_build();
254
255 let mut parsed_file = crate::parse_file(self.rs_file, self.auto_allowlist)
256 .map_err(BuilderError::ParseError)?;
257 parsed_file
258 .resolve_all(
259 autocxx_inc,
260 clang_args,
261 self.dependency_recorder,
Austin Schuh6ea9bfa2023-08-06 19:05:10 -0700262 &self.codegen_options,
Brian Silverman4e662aa2022-05-11 23:10:19 -0700263 )
264 .map_err(BuilderError::ParseError)?;
265 let mut counter = 0;
266 let mut builder = cc::Build::new();
267 builder.cpp(true);
268 if std::env::var_os("AUTOCXX_ASAN").is_some() {
269 builder.flag_if_supported("-fsanitize=address");
270 }
271 let mut generated_rs = Vec::new();
272 let mut generated_cpp = Vec::new();
273 builder.includes(parsed_file.include_dirs());
274 for include_cpp in parsed_file.get_cpp_buildables() {
275 let generated_code = include_cpp
Austin Schuh6ea9bfa2023-08-06 19:05:10 -0700276 .generate_h_and_cxx(&self.codegen_options.cpp_codegen_options)
Brian Silverman4e662aa2022-05-11 23:10:19 -0700277 .map_err(BuilderError::InvalidCxx)?;
278 for filepair in generated_code.0 {
Austin Schuh6ea9bfa2023-08-06 19:05:10 -0700279 let fname = format!("gen{counter}.cxx");
Brian Silverman4e662aa2022-05-11 23:10:19 -0700280 counter += 1;
281 if let Some(implementation) = &filepair.implementation {
282 let gen_cxx_path = write_to_file(&cxxdir, &fname, implementation)?;
283 builder.file(&gen_cxx_path);
284 generated_cpp.push(gen_cxx_path);
285 }
286 write_to_file(&incdir, &filepair.header_name, &filepair.header)?;
287 generated_cpp.push(incdir.join(filepair.header_name));
288 }
289 }
290
291 for rs_output in parsed_file.get_rs_outputs() {
292 let rs = generate_rs_single(rs_output);
293 generated_rs.push(write_to_file(&rsdir, &rs.filename, rs.code.as_bytes())?);
294 }
295 if counter == 0 {
296 Err(BuilderError::NoIncludeCxxMacrosFound)
297 } else {
298 Ok(BuilderSuccess(builder, generated_rs, generated_cpp))
299 }
300 }
Brian Silverman4e662aa2022-05-11 23:10:19 -0700301}
302
303fn ensure_created(dir: &Path) -> Result<(), BuilderError> {
304 std::fs::create_dir_all(dir)
305 .map_err(|e| BuilderError::UnableToCreateDirectory(e, dir.to_path_buf()))
306}
307
308fn build_autocxx_inc<I, T>(paths: I, extra_path: &Path) -> Vec<PathBuf>
309where
310 I: IntoIterator<Item = T>,
311 T: AsRef<OsStr>,
312{
313 paths
314 .into_iter()
315 .map(|p| PathBuf::from(p.as_ref()))
316 .chain(std::iter::once(extra_path.to_path_buf()))
317 .collect()
318}
319
320fn write_to_file(dir: &Path, filename: &str, content: &[u8]) -> Result<PathBuf, BuilderError> {
321 let path = dir.join(filename);
Austin Schuh6ea9bfa2023-08-06 19:05:10 -0700322 if let Ok(existing_contents) = std::fs::read(&path) {
323 // Avoid altering timestamps on disk if the file already exists,
324 // to stop downstream build steps recurring.
325 if existing_contents == content {
326 return Ok(path);
327 }
328 }
Brian Silverman4e662aa2022-05-11 23:10:19 -0700329 try_write_to_file(&path, content).map_err(|e| BuilderError::FileWriteFail(e, path.clone()))?;
330 Ok(path)
331}
332
333fn try_write_to_file(path: &Path, content: &[u8]) -> std::io::Result<()> {
334 let mut f = File::create(path)?;
335 f.write_all(content)
336}
337
338fn rust_version_check() {
339 if !version_check::is_min_version("1.54.0").unwrap_or(false) {
340 panic!("Rust 1.54 or later is required.")
341 }
342}