1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
use std::env::var_os;
use std::fmt::Debug;
use std::path::{Path, PathBuf};
use subplot::{get_basedir_from, Result};
use tracing::{event, instrument, span, Level};
#[instrument(level = "trace")]
pub fn codegen<P>(filename: P) -> Result<()>
where
P: AsRef<Path> + Debug,
{
let span = span!(Level::TRACE, "codegen_buildrs");
let _enter = span.enter();
event!(Level::TRACE, "Generating code in build.rs");
let out_dir = var_os("OUT_DIR").expect("OUT_DIR is not defined in the environment");
let out_dir = Path::new(&out_dir);
let filename = filename.as_ref();
let test_rs =
buildrs_output(out_dir, filename, "rs").expect("could not create output filename");
let output = subplot::codegen(filename, &test_rs)?;
let base_path = get_basedir_from(filename);
let meta = output.doc.meta();
buildrs_deps(&base_path, &meta.bindings_filenames());
buildrs_deps(&base_path, &meta.functions_filenames());
buildrs_deps(&base_path, &[filename]);
event!(Level::TRACE, "Finished generating code");
Ok(())
}
fn buildrs_deps(base_path: &Path, filenames: &[&Path]) {
for filename in filenames {
let filename = base_path.join(filename);
if filename.exists() {
println!("cargo:rerun-if-changed={}", filename.display());
}
}
}
fn buildrs_output(dir: &Path, filename: &Path, new_extension: &str) -> Option<PathBuf> {
if let Some(basename) = filename.file_name() {
let basename = Path::new(basename);
if let Some(stem) = basename.file_stem() {
let stem = Path::new(stem);
return Some(dir.join(stem.with_extension(new_extension)));
}
}
None
}