puffin_build/harness/
bundle.rs1use itertools::Itertools;
2
3use super::Put;
4use crate::utils::cmake;
5
6#[derive(Debug, Clone, Default)]
7pub struct BundleBuilder {
8 puts: Vec<Put>,
9}
10
11impl BundleBuilder {
12 pub fn new(puts: Vec<Put>) -> Self {
13 Self { puts }
14 }
15
16 pub fn build(self, out_dir: impl AsRef<std::path::Path>) -> Bundle {
17 let out_dir = out_dir.as_ref();
18
19 let bundle_bindings = format!(
20 r#"
21 #[allow(unused_macros)]
22 #[macro_export]
23 macro_rules! for_puts {{
24 ( $($tail:tt)* ) => {{
25 {puts_macros}
26 }};
27 }}
28
29 pub use for_puts;
30
31 fn register() -> Vec<crate::put::CPut> {{
32 vec![None, {registration}].into_iter().flatten().collect()
33 }}
34
35 {puts_modules}
36 "#,
37 registration = self
38 .puts
39 .iter()
40 .map(|put| format!("{}::register()", put.id()))
41 .join(", "),
42 puts_modules = self
43 .puts
44 .iter()
45 .map(|put| put.registration())
46 .join("\n\n"),
47 puts_macros = self.puts.iter().map(|put| {
48 format!(r#"puffin_macros::replace!(__PUTSTR__ => "{name}" in {{ puffin_macros::replace!(__PUT__ => {id} in {{ $($tail)* }}); }});"#, name = put.name(), id = put.id())
49 }).join("\n")
50 );
51
52 std::fs::create_dir_all(out_dir).unwrap();
53 std::fs::write(out_dir.join(Bundle::bindings_file()), bundle_bindings)
54 .expect("failed to write Rust PUT bindings");
55
56 let mut cmake_conf = cmake::command("bundle", out_dir);
57
58 cmake_conf.cfg_args.push(format!(
59 "-D=PUTS={}",
60 self.puts
61 .iter()
62 .flat_map(|put| put.objects())
63 .map(|obj| obj.display().to_string())
64 .collect::<Vec<_>>()
65 .join(",")
66 ));
67
68 cmake_conf.build().unwrap();
69
70 Bundle::new(self.puts, out_dir.to_path_buf())
71 }
72}
73
74pub struct Bundle {
75 puts: Vec<Put>,
76 dir: std::path::PathBuf,
77}
78
79impl Bundle {
80 pub fn print_cargo_metadata(&self) {
81 for put in self.puts.iter() {
82 put.print_cargo_metadata();
83 }
84
85 println!(
86 "cargo:rustc-env=RUST_PUTS_BUNDLE_FILE={}",
87 self.dir.join(Self::bindings_file()).display()
88 );
89
90 println!("cargo:rustc-link-search=native={}", self.dir.display());
91 println!("cargo:rustc-link-lib=static=puts-bundle");
92 }
93
94 pub(in crate::harness) fn new(puts: Vec<Put>, dir: impl Into<std::path::PathBuf>) -> Self {
95 Self {
96 puts,
97 dir: dir.into(),
98 }
99 }
100
101 pub(self) fn bindings_file() -> std::path::PathBuf {
102 std::path::PathBuf::from("put_bindings.rs")
103 }
104}