weaveffi-core 0.7.0

Generator trait, orchestrator, validation, and shared utilities for WeaveFFI
Documentation
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
//! Generator trait, dyn-erasure wrapper, and orchestration.
//!
//! Each language target implements [`Generator`] with its own associated
//! `Config` type. The orchestrator works on the object-safe [`DynGenerator`]
//! trait, which erases the concrete config and is what tests and the CLI
//! pass into [`Orchestrator::with_generator`]. The recommended way to
//! produce a `&dyn DynGenerator` is to build a [`ConfiguredGenerator`]
//! that pairs a typed generator with its concrete config value.

use anyhow::{bail, Result};
use camino::Utf8Path;
use rayon::prelude::*;
use serde::Serialize;
use weaveffi_ir::ir::Api;

use crate::cache;

pub mod common;
pub mod writer;

fn run_hook(label: &str, cmd: &str) -> Result<()> {
    let status = if cfg!(target_os = "windows") {
        std::process::Command::new("cmd")
            .args(["/C", cmd])
            .status()?
    } else {
        std::process::Command::new("sh")
            .arg("-c")
            .arg(cmd)
            .status()?
    };
    if !status.success() {
        bail!("{label} hook failed with {status}");
    }
    Ok(())
}

/// A language code generator.
///
/// Generators are dispatched in parallel, so every implementation must be
/// safe to share across threads. The associated [`Config`] type is owned
/// by the generator crate so `weaveffi-core` does not have to know about
/// target-specific options like `swift_module_name` or `cpp_namespace`.
///
/// [`Config`]: Generator::Config
pub trait Generator: Send + Sync {
    /// Per-target, fully-typed configuration consumed by [`generate`] and
    /// [`output_files`]. Must round-trip through `serde_json` so the
    /// orchestrator can hash it as part of the cache key.
    ///
    /// [`generate`]: Generator::generate
    /// [`output_files`]: Generator::output_files
    type Config: Serialize + Default + Clone + Send + Sync;

    /// Stable short name for the target (`"swift"`, `"c"`, `"node"`, …).
    /// Used as the cache file basename and the `--target` filter token.
    fn name(&self) -> &'static str;

    /// Render the bindings under `out_dir`.
    fn generate(&self, api: &Api, out_dir: &Utf8Path, config: &Self::Config) -> Result<()>;

    /// Files that [`generate`](Generator::generate) would write, relative
    /// to (or anchored under) `out_dir`. Used by `--dry-run` and `diff`.
    /// Default implementation returns the empty list; generators override
    /// to surface the list without doing any I/O.
    fn output_files(&self, _api: &Api, _out_dir: &Utf8Path, _config: &Self::Config) -> Vec<String> {
        vec![]
    }
}

/// Object-safe view of a [`Generator`] paired with a concrete config.
///
/// The orchestrator stores generators as `&dyn DynGenerator` so it can
/// hold a heterogeneous set of targets whose `Config` types differ.
/// [`ConfiguredGenerator`] is the canonical adapter.
pub trait DynGenerator: Send + Sync {
    fn name(&self) -> &'static str;
    fn generate(&self, api: &Api, out_dir: &Utf8Path) -> Result<()>;
    fn output_files(&self, api: &Api, out_dir: &Utf8Path) -> Vec<String>;
    /// Canonical-JSON encoding of the bound config, fed into the cache
    /// hash so a config-only change invalidates the entry.
    fn config_hash_input(&self) -> Vec<u8>;
}

/// Binds a [`Generator`] to a concrete [`Generator::Config`] value so it
/// can be erased to `&dyn DynGenerator`.
///
/// ```ignore
/// let swift = ConfiguredGenerator::new(SwiftGenerator, SwiftConfig::default());
/// orchestrator.with_generator(&swift);
/// ```
pub struct ConfiguredGenerator<G: Generator> {
    inner: G,
    config: G::Config,
}

impl<G: Generator> ConfiguredGenerator<G> {
    pub fn new(inner: G, config: G::Config) -> Self {
        Self { inner, config }
    }

    pub fn config(&self) -> &G::Config {
        &self.config
    }

    pub fn inner(&self) -> &G {
        &self.inner
    }
}

impl<G: Generator> DynGenerator for ConfiguredGenerator<G> {
    fn name(&self) -> &'static str {
        self.inner.name()
    }

    fn generate(&self, api: &Api, out_dir: &Utf8Path) -> Result<()> {
        self.inner.generate(api, out_dir, &self.config)
    }

    fn output_files(&self, api: &Api, out_dir: &Utf8Path) -> Vec<String> {
        self.inner.output_files(api, out_dir, &self.config)
    }

    fn config_hash_input(&self) -> Vec<u8> {
        let value =
            serde_json::to_value(&self.config).expect("generator config should serialize to JSON");
        serde_json::to_vec(&value).expect("JSON Value should serialize")
    }
}

/// Global hooks the orchestrator runs around the parallel codegen pass.
#[derive(Default, Debug, Clone)]
pub struct OrchestratorHooks {
    pub pre_generate: Option<String>,
    pub post_generate: Option<String>,
}

#[derive(Default)]
pub struct Orchestrator<'a> {
    generators: Vec<&'a dyn DynGenerator>,
}

impl<'a> Orchestrator<'a> {
    pub fn new() -> Self {
        Self::default()
    }

    pub fn with_generator(mut self, gen: &'a dyn DynGenerator) -> Self {
        self.generators.push(gen);
        self
    }

    pub fn run(
        &self,
        api: &Api,
        out_dir: &Utf8Path,
        hooks: &OrchestratorHooks,
        force: bool,
    ) -> Result<()> {
        if force {
            cache::invalidate_all(out_dir)?;
        }

        // Pair each generator with its expected hash and decide individually
        // whether it needs to run, so a single generator can be re-run while
        // the others stay cached.
        let mut pending: Vec<(&'a dyn DynGenerator, String)> = Vec::new();
        for &g in &self.generators {
            let cfg_bytes = g.config_hash_input();
            let hash = cache::hash_generator_inputs(api, g.name(), &cfg_bytes);
            let cached = cache::read_generator_cache(out_dir, g.name());
            if cached.as_deref() != Some(hash.as_str()) {
                pending.push((g, hash));
            }
        }

        if pending.is_empty() {
            println!("No changes detected, skipping code generation.");
            return Ok(());
        }

        if let Some(cmd) = &hooks.pre_generate {
            run_hook("pre_generate", cmd)?;
        }

        pending
            .par_iter()
            .map(|(g, _)| g.generate(api, out_dir))
            .collect::<Result<Vec<_>>>()?;

        if let Some(cmd) = &hooks.post_generate {
            run_hook("post_generate", cmd)?;
        }

        for (g, hash) in &pending {
            cache::write_generator_cache(out_dir, g.name(), hash)?;
        }
        Ok(())
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::sync::atomic::{AtomicUsize, Ordering};
    use std::sync::Arc;
    use weaveffi_ir::ir::{Function, Module, Param, TypeRef};

    /// Test generator with a minimal config so tests don't have to depend
    /// on any real per-language generator crate.
    #[derive(Default, Clone, serde::Serialize, serde::Deserialize)]
    struct TestConfig {
        knob: Option<String>,
    }

    struct CountingGenerator {
        name: &'static str,
        calls: Arc<AtomicUsize>,
    }

    impl Generator for CountingGenerator {
        type Config = TestConfig;

        fn name(&self) -> &'static str {
            self.name
        }

        fn generate(&self, _api: &Api, out_dir: &Utf8Path, _config: &Self::Config) -> Result<()> {
            self.calls.fetch_add(1, Ordering::SeqCst);
            let dir = out_dir.join(self.name);
            std::fs::create_dir_all(dir.as_std_path())?;
            std::fs::write(dir.join("output.txt").as_std_path(), "generated")?;
            Ok(())
        }
    }

    fn test_api() -> Api {
        Api {
            version: "0.1.0".to_string(),
            modules: vec![Module {
                name: "math".to_string(),
                functions: vec![Function {
                    name: "add".to_string(),
                    params: vec![
                        Param {
                            name: "a".to_string(),
                            ty: TypeRef::I32,
                            mutable: false,
                            doc: None,
                        },
                        Param {
                            name: "b".to_string(),
                            ty: TypeRef::I32,
                            mutable: false,
                            doc: None,
                        },
                    ],
                    returns: Some(TypeRef::I32),
                    doc: None,
                    r#async: false,
                    cancellable: false,
                    deprecated: None,
                    since: None,
                }],
                structs: vec![],
                enums: vec![],
                callbacks: vec![],
                listeners: vec![],
                errors: None,
                modules: vec![],
            }],
            generators: None,
        }
    }

    fn configured(
        name: &'static str,
        calls: Arc<AtomicUsize>,
    ) -> ConfiguredGenerator<CountingGenerator> {
        ConfiguredGenerator::new(CountingGenerator { name, calls }, TestConfig::default())
    }

    #[test]
    fn incremental_skips_when_unchanged() {
        let dir = tempfile::tempdir().unwrap();
        let out_dir = Utf8Path::from_path(dir.path()).unwrap();
        let api = test_api();
        let hooks = OrchestratorHooks::default();
        let calls = Arc::new(AtomicUsize::new(0));
        let gen = configured("counting", Arc::clone(&calls));

        let orch = Orchestrator::new().with_generator(&gen);

        orch.run(&api, out_dir, &hooks, false).unwrap();
        assert_eq!(calls.load(Ordering::SeqCst), 1);
        let content_after_first =
            std::fs::read_to_string(out_dir.join("counting/output.txt")).unwrap();

        orch.run(&api, out_dir, &hooks, false).unwrap();
        assert_eq!(
            calls.load(Ordering::SeqCst),
            1,
            "generator should not run again"
        );
        let content_after_second =
            std::fs::read_to_string(out_dir.join("counting/output.txt")).unwrap();

        assert_eq!(content_after_first, content_after_second);
    }

    #[test]
    fn force_bypasses_cache() {
        let dir = tempfile::tempdir().unwrap();
        let out_dir = Utf8Path::from_path(dir.path()).unwrap();
        let api = test_api();
        let hooks = OrchestratorHooks::default();
        let calls = Arc::new(AtomicUsize::new(0));
        let gen = configured("counting", Arc::clone(&calls));

        let orch = Orchestrator::new().with_generator(&gen);

        orch.run(&api, out_dir, &hooks, false).unwrap();
        assert_eq!(calls.load(Ordering::SeqCst), 1);

        orch.run(&api, out_dir, &hooks, true).unwrap();
        assert_eq!(calls.load(Ordering::SeqCst), 2, "force should bypass cache");
    }

    #[test]
    fn parallel_orchestrator_runs_all_generators() {
        let dir = tempfile::tempdir().unwrap();
        let out_dir = Utf8Path::from_path(dir.path()).unwrap();
        let api = test_api();
        let hooks = OrchestratorHooks::default();

        let names = ["g0", "g1", "g2", "g3", "g4", "g5"];
        let counters: Vec<Arc<AtomicUsize>> = names
            .iter()
            .map(|_| Arc::new(AtomicUsize::new(0)))
            .collect();
        let gens: Vec<ConfiguredGenerator<CountingGenerator>> = names
            .iter()
            .zip(counters.iter())
            .map(|(name, calls)| configured(name, Arc::clone(calls)))
            .collect();

        let mut orch = Orchestrator::new();
        for g in &gens {
            orch = orch.with_generator(g);
        }

        orch.run(&api, out_dir, &hooks, false).unwrap();

        for (name, calls) in names.iter().zip(counters.iter()) {
            assert_eq!(
                calls.load(Ordering::SeqCst),
                1,
                "generator '{name}' should have run exactly once",
            );
            assert!(
                out_dir.join(name).join("output.txt").exists(),
                "generator '{name}' should have written its output",
            );
        }
    }

    #[test]
    fn single_generator_cache_invalidates_independently() {
        let dir = tempfile::tempdir().unwrap();
        let out_dir = Utf8Path::from_path(dir.path()).unwrap();
        let hooks = OrchestratorHooks::default();

        let c_calls = Arc::new(AtomicUsize::new(0));
        let s_calls = Arc::new(AtomicUsize::new(0));
        let c_gen = configured("c", Arc::clone(&c_calls));
        let s_gen = configured("swift", Arc::clone(&s_calls));

        let orch = Orchestrator::new()
            .with_generator(&c_gen)
            .with_generator(&s_gen);

        let api = test_api();
        orch.run(&api, out_dir, &hooks, false).unwrap();
        assert_eq!(c_calls.load(Ordering::SeqCst), 1);
        assert_eq!(s_calls.load(Ordering::SeqCst), 1);

        // Mutate the API in a way that affects both generators' hashes by
        // renaming a module. Then pre-seed the Swift cache with the *new*
        // expected hash so only the C entry stays stale and re-runs.
        let mut modified = api.clone();
        modified.modules[0].name = "math2".to_string();

        let new_swift_hash =
            cache::hash_generator_inputs(&modified, "swift", &s_gen.config_hash_input());
        cache::write_generator_cache(out_dir, "swift", &new_swift_hash).unwrap();

        orch.run(&modified, out_dir, &hooks, false).unwrap();
        assert_eq!(
            c_calls.load(Ordering::SeqCst),
            2,
            "C generator should re-run because its cache entry no longer matches",
        );
        assert_eq!(
            s_calls.load(Ordering::SeqCst),
            1,
            "Swift generator's cache matched the new API and must be skipped",
        );
    }

    #[test]
    fn config_change_invalidates_cache() {
        let dir = tempfile::tempdir().unwrap();
        let out_dir = Utf8Path::from_path(dir.path()).unwrap();
        let hooks = OrchestratorHooks::default();
        let api = test_api();

        let calls = Arc::new(AtomicUsize::new(0));
        let g1 = ConfiguredGenerator::new(
            CountingGenerator {
                name: "counting",
                calls: Arc::clone(&calls),
            },
            TestConfig::default(),
        );
        Orchestrator::new()
            .with_generator(&g1)
            .run(&api, out_dir, &hooks, false)
            .unwrap();
        assert_eq!(calls.load(Ordering::SeqCst), 1);

        // Same generator, different config value: must re-run.
        let g2 = ConfiguredGenerator::new(
            CountingGenerator {
                name: "counting",
                calls: Arc::clone(&calls),
            },
            TestConfig {
                knob: Some("changed".into()),
            },
        );
        Orchestrator::new()
            .with_generator(&g2)
            .run(&api, out_dir, &hooks, false)
            .unwrap();
        assert_eq!(
            calls.load(Ordering::SeqCst),
            2,
            "config-only change must invalidate the cache",
        );
    }
}