alef 0.109.6

Opinionated polyglot binding generator for Rust libraries
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
use crate::core::backend::GeneratedFile;
use crate::core::config::{AdapterPattern, FfiTargetDepOverride, Language, ResolvedCrateConfig};
use crate::core::ir::ApiSurface;
use crate::core::template_versions as tv;
use crate::{
    scaffold::cargo_package_header, scaffold::detect_workspace_inheritance_for_crate, scaffold::render_extra_deps,
    scaffold::scaffold_meta,
};
use std::path::PathBuf;

/// Render the core-crate dependency portion of the FFI Cargo.toml.
///
/// Returns a tuple of `(core_dep_line, target_blocks)` where:
/// - `core_dep_line` is the single TOML line that goes inside the main
///   `[dependencies]` table (empty string when target overrides are in use).
/// - `target_blocks` contains any `[target.'cfg(...)'.dependencies]` sections
///   that follow the main `[dependencies]` table (empty string by default).
///
/// When `overrides` is empty the behaviour matches the historical output:
/// a single `{crate} = { path = ..., features = [...] }` line lives next to
/// `serde_json` / `tokio` inside `[dependencies]`. When overrides are
/// present the core-crate dependency moves out into per-cfg target blocks;
/// the default branch is wrapped in `cfg(not(any(<cfg1>, <cfg2>, ...)))` so
/// that exactly one variant matches on any given build (most importantly
/// `x86_64-linux-android`, which has no ONNX Runtime prebuilt).
fn render_core_dep(
    crate_name: &str,
    rel_path: &str,
    version: &str,
    default_features: &str,
    overrides: &[FfiTargetDepOverride],
) -> (String, String) {
    if overrides.is_empty() {
        let line = format!("{crate_name} = {{ path = \"{rel_path}\", version = \"{version}\"{default_features} }}");
        return (line, String::new());
    }

    let cfgs: Vec<String> = overrides.iter().map(|o| o.cfg.clone()).collect();
    let combined_cfg = if cfgs.len() == 1 {
        cfgs[0].clone()
    } else {
        format!("any({})", cfgs.join(", "))
    };

    let mut entries: Vec<(String, String)> = vec![(
        format!("not({combined_cfg})"),
        format!("{crate_name} = {{ path = \"{rel_path}\", version = \"{version}\"{default_features} }}"),
    )];
    for override_ in overrides {
        let default_block = if override_.default_features {
            String::new()
        } else {
            ", default-features = false".to_string()
        };
        let features_str = if override_.features.is_empty() {
            String::new()
        } else {
            let quoted: Vec<String> = override_.features.iter().map(|f| format!("\"{f}\"")).collect();
            format!(", features = [{}]", quoted.join(", "))
        };
        entries.push((
            override_.cfg.clone(),
            format!("{crate_name} = {{ path = \"{rel_path}\", version = \"{version}\"{default_block}{features_str} }}"),
        ));
    }
    // See `crate::scaffold::join_sorted_target_dep_blocks`: cargo-sort orders
    // `[target.'cfg(...)'.dependencies]` tables alphabetically by the raw cfg
    // predicate string, so the default `not(...)` branch is not always first. ~keep
    (String::new(), crate::scaffold::join_sorted_target_dep_blocks(entries))
}

/// How the FFI crate declares its core dependency once feature selection moves to the FFI
/// crate's own `[features]` table.
struct SlimCoreDep {
    /// Attributes appended to the core dependency: `default-features = false`, plus `serde` when
    /// configured (the FFI crate has no `serde` feature of its own to forward it through).
    attrs: String,
    /// Core `default` features the FFI `default` list must forward explicitly, because turning the
    /// core dependency's default features off would otherwise drop them.
    forwarded_defaults: Vec<String>,
}

/// Declares the core dependency with `default-features = false` and no forced `features`, so
/// `cargo build -p <ffi> --no-default-features` really builds the core crate without them: a
/// feature listed on the dependency line is on for every build of the FFI crate and nothing the
/// FFI crate's own feature flags do can remove it.
///
/// Returns `None` -- keep the historical dependency line -- when the result could differ from it:
/// the core manifest cannot be read, or its `default` feature holds a token that cannot be
/// forwarded as `<core>/<feature>` (`dep:x`, `x/y`). `extra_features` and
/// `excluded_default_features` stay declare-only and are never forwarded, as before. ~keep
fn slim_core_dep(config: &ResolvedCrateConfig, default_feature_names: &[String]) -> Option<SlimCoreDep> {
    let core_defaults = crate::scaffold::core_default_feature_tokens(config)?;
    if core_defaults.iter().any(|t| t.starts_with("dep:") || t.contains('/')) {
        return None;
    }
    let ffi = config.ffi.as_ref();
    let never_default = |name: &str| {
        ffi.is_some_and(|c| {
            c.extra_features.iter().any(|f| f == name) || c.excluded_default_features.iter().any(|f| f == name)
        })
    };
    let forwarded_defaults = core_defaults
        .into_iter()
        .filter(|name| !default_feature_names.contains(name) && !never_default(name))
        .collect();
    let serde_on_dep = config.features_for_language(Language::Ffi).iter().any(|f| f == "serde")
        && !ffi.is_some_and(|c| c.excluded_default_features.iter().any(|f| f == "serde"));
    let mut attrs = ", default-features = false".to_string();
    if serde_on_dep {
        attrs.push_str(", features = [\"serde\"]");
    }
    Some(SlimCoreDep {
        attrs,
        forwarded_defaults,
    })
}

pub(crate) fn scaffold_ffi(api: &ApiSurface, config: &ResolvedCrateConfig) -> anyhow::Result<Vec<GeneratedFile>> {
    let meta = scaffold_meta(config);
    let version = &api.version;
    let core_crate_dir = config.core_crate_dir();
    let crate_dir = format!("crates/{core_crate_dir}-ffi");
    let ws = detect_workspace_inheritance_for_crate(config.workspace_root.as_deref(), &crate_dir);
    let cargo_filters = crate::scaffold::cargo_package_file_filters(config);
    let pkg_header = cargo_package_header(
        &format!("{core_crate_dir}-ffi"),
        version,
        "2024",
        &meta,
        &ws,
        cargo_filters,
    );

    let rendered_extra_deps = render_extra_deps(config, Language::Ffi);
    let mut extra_dep_lines: Vec<String> = if rendered_extra_deps.is_empty() {
        Vec::new()
    } else {
        rendered_extra_deps.lines().map(str::to_string).collect()
    };
    let has_trait_bridges = config.trait_bridges_for(Language::Ffi).next().is_some();
    if has_trait_bridges && !extra_dep_lines.iter().any(|l| l.starts_with("async-trait")) {
        extra_dep_lines.push(format!("async-trait = \"{}\"", tv::cargo::ASYNC_TRAIT));
    }
    if has_trait_bridges && !extra_dep_lines.iter().any(|l| l.starts_with("tracing")) {
        extra_dep_lines.push(format!("tracing = \"{}\"", tv::cargo::TRACING));
    }
    let has_streaming = config
        .adapters
        .iter()
        .any(|a| matches!(a.pattern, AdapterPattern::Streaming));
    if has_streaming && !extra_dep_lines.iter().any(|l| l.starts_with("futures-util")) {
        extra_dep_lines.push(format!("futures-util = \"{}\"", tv::cargo::FUTURES_UTIL));
    }
    if let Some(ffi) = config.ffi.as_ref() {
        for capsule in ffi.capsule_types.values() {
            let (Some(package), Some(version)) = (capsule.package.as_ref(), capsule.package_version.as_ref()) else {
                continue;
            };
            let dep_prefix = format!("{package} ");
            if !extra_dep_lines.iter().any(|l| l.starts_with(&dep_prefix)) {
                extra_dep_lines.push(format!("{package} = \"{version}\""));
            }
        }
    }
    if !config.components.is_empty() {
        let alef_version = env!("CARGO_PKG_VERSION");
        for (name, dependency) in [
            ("alef-component-abi", format!("alef-component-abi = \"{alef_version}\"")),
            (
                "alef-component-runtime",
                format!("alef-component-runtime = \"{alef_version}\""),
            ),
            ("directories", "directories = \"6\"".to_string()),
        ] {
            let key = format!("{name} =");
            if !extra_dep_lines.iter().any(|line| line.starts_with(&key)) {
                extra_dep_lines.push(dependency);
            }
        }
    }
    crate::scaffold::sort_dependency_lines(&mut extra_dep_lines);

    let mut machete_ignored: Vec<&str> = vec!["ahash", "serde", "serde_json", "tokio"];
    if has_trait_bridges {
        machete_ignored.push("async-trait");
        machete_ignored.push("tracing");
    }
    if has_streaming {
        machete_ignored.push("futures-util");
    }
    let machete_ignored_str = machete_ignored
        .iter()
        .map(|d| format!("\"{d}\""))
        .collect::<Vec<_>>()
        .join(", ");

    let target_overrides: &[FfiTargetDepOverride] = config
        .ffi
        .as_ref()
        .map(|c| c.target_dep_overrides.as_slice())
        .unwrap_or(&[]);
    let excluded_default_features: std::collections::HashSet<&str> = config
        .ffi
        .as_ref()
        .map(|c| c.excluded_default_features.iter().map(String::as_str).collect())
        .unwrap_or_default();
    // Source-ordered view of the same names, for the declare-only feature rows below: the
    // config's `Vec<String>` is already in authored order, so iterating it directly (instead of
    // the `HashSet` above, whose iteration order is randomly seeded per process) keeps the
    // emitted `[features]` table deterministic across regenerations. ~keep
    let excluded_default_features_ordered: &[String] = config
        .ffi
        .as_ref()
        .map(|c| c.excluded_default_features.as_slice())
        .unwrap_or(&[]);
    let core_dep_path = config.core_crate_dep_path(std::path::Path::new(&crate_dir));
    let default_feature_names_owned = crate::codegen::cfg::effective_ffi_default_features(api, config);
    // Target overrides pick the core dependency's features per cfg, with `default-features = false`
    // on the branches that turn core defaults off; forwarding those defaults from the FFI
    // `default` list would switch them back on everywhere, so overrides keep the explicit line.
    let slim_core_dep = target_overrides
        .is_empty()
        .then(|| slim_core_dep(config, &default_feature_names_owned))
        .flatten();
    let core_dep_attrs = match &slim_core_dep {
        Some(slim) => slim.attrs.clone(),
        None => crate::scaffold::core_dep_features_excluding(config, Language::Ffi, &excluded_default_features),
    };
    let (core_dep_line, target_blocks) =
        render_core_dep(&config.name, &core_dep_path, version, &core_dep_attrs, target_overrides);

    // Cargo features are per-crate: `full = ["<core>/full"]` enabling `X` on the dependency
    // does NOT create feature `X` here, so a `#[cfg(feature = "X")]` the codegen emits into
    // this crate is unsatisfiable unless this crate declares `X` itself. An undeclared gate is
    // never true, which silently drops the export from the cdylib while cbindgen still declares
    // it in the header -- a link failure for every C-ABI consumer. Declare every feature the
    // emitted surface actually gates on. ~keep
    //
    // `default_feature_names` is `effective_ffi_default_features` -- the ONE derivation of what
    // the compiled FFI cdylib builds with by default. `warn_on_ffi_feature_drift` compares
    // against this exact same derivation instead of re-deriving it from the raw configured features
    // alone, so the two can no longer disagree about what "the FFI crate's feature set" means
    // (see github.com/xberg-io/alef/issues/257). ~keep
    let default_feature_names: Vec<&str> = default_feature_names_owned.iter().map(String::as_str).collect();
    let forwarded_core_defaults = slim_core_dep
        .as_ref()
        .map(|slim| slim.forwarded_defaults.iter().map(|f| format!("{}/{f}", config.name)));
    let core_features_default_list = default_feature_names
        .iter()
        .map(|f| f.to_string())
        .chain(forwarded_core_defaults.into_iter().flatten())
        .map(|f| format!("\"{f}\""))
        .collect::<Vec<_>>()
        .join(", ");
    let mut core_features_passthrough_block = if default_feature_names.is_empty() {
        String::new()
    } else {
        default_feature_names
            .iter()
            .map(|f| format!("{f} = [\"{}/{f}\"]", config.name))
            .collect::<Vec<_>>()
            .join("\n")
    };
    if let Some(line) = crate::scaffold::android_target_feature_line(config, &default_feature_names) {
        if core_features_passthrough_block.is_empty() {
            core_features_passthrough_block = line;
        } else {
            core_features_passthrough_block.push('\n');
            core_features_passthrough_block.push_str(&line);
        }
    }
    // feature in a `#[cfg(feature = "X")]` gate (e.g. a `wasm-http` backend that is
    if let Some(extra) = config.ffi.as_ref().map(|c| c.extra_features.as_slice()) {
        for feat in extra {
            if feat.is_empty() || default_feature_names.contains(&feat.as_str()) {
                continue;
            }
            let line = format!("{feat} = [\"{}/{feat}\"]", config.name);
            if core_features_passthrough_block.is_empty() {
                core_features_passthrough_block = line;
            } else {
                core_features_passthrough_block.push('\n');
                core_features_passthrough_block.push_str(&line);
            }
        }
    }
    // `effective_ffi_default_features` already drops `excluded_default_features` names from
    // `default_feature_names`, so `cargo build --features <name>` would fail to declare the
    // feature at all without this: a name listed there stays a *declared* opt-in flag, just
    // never defaulted, the same tradeoff `extra_features` makes above. ~keep
    for feat in excluded_default_features_ordered {
        // Deliberately checked against `default_feature_names` alone, not the raw configured
        // feature list: an excluded name is stripped out of the former even when explicitly
        // configured, so the latter cannot prove a declare-only row already exists. ~keep
        if default_feature_names.contains(&feat.as_str()) {
            continue;
        }
        let line = format!("{feat} = [\"{}/{feat}\"]", config.name);
        if core_features_passthrough_block.is_empty() {
            core_features_passthrough_block = line;
        } else {
            core_features_passthrough_block.push('\n');
            core_features_passthrough_block.push_str(&line);
        }
    }
    let target_blocks_section = if target_blocks.is_empty() {
        String::new()
    } else {
        format!("\n{target_blocks}\n")
    };

    let mut dep_entries: Vec<String> = vec![
        format!("ahash = \"{}\"", tv::cargo::AHASH),
        format!("serde = \"{}\"", tv::cargo::SERDE),
        format!("serde_json = \"{}\"", tv::cargo::SERDE_JSON),
        format!(
            "tokio = {{ version = \"{}\", features = [\"full\"] }}",
            tv::cargo::TOKIO
        ),
    ];
    if !core_dep_line.is_empty() {
        dep_entries.push(core_dep_line.clone());
    }
    for line in &extra_dep_lines {
        dep_entries.push(line.clone());
    }
    crate::scaffold::sort_dependency_lines(&mut dep_entries);
    let dep_block = dep_entries.join("\n");
    let repository_line = meta
        .configured_repository
        .as_deref()
        .map(|repository| format!("\nrepository = \"{repository}\""))
        .unwrap_or_default();
    let lints_section = crate::scaffold::cargo_lints_section(config);

    let content = format!(
        r#"{pkg_header}{repository_line}

# `serde`, `serde_json`, `ahash`, and `tokio` are emitted unconditionally above so the
# manifest is stable across regens (and so the C FFI codegen can pull them in
# when an async / Result-typed function appears in the API surface), but for
# umbrella crates with no async fns and no JSON-marshalled return types they
# are genuinely unused. The conditional `async-trait` / `futures-util` deps
# are similarly flagged when the umbrella has trait-bridge / streaming adapters
# configured but no actual async-trait / async-stream callsite in the generated
# FFI shim.
[package.metadata.cargo-machete]
ignored = [{machete_ignored_str}]

[lib]
crate-type = ["cdylib", "staticlib", "rlib"]

[features]
default = [{core_features_default_list}]
{core_features_passthrough_block}

[dependencies]
{dep_block}
{target_blocks_section}
[build-dependencies]
cbindgen = "{cbindgen}"

[dev-dependencies]
tempfile = "{tempfile}"
{lints_section}"#,
        pkg_header = pkg_header,
        repository_line = repository_line,
        lints_section = lints_section,
        dep_block = dep_block,
        target_blocks_section = target_blocks_section,
        cbindgen = tv::cargo::CBINDGEN,
        tempfile = tv::cargo::TEMPFILE,
        machete_ignored_str = machete_ignored_str,
        core_features_default_list = core_features_default_list,
        core_features_passthrough_block = core_features_passthrough_block,
    );

    let ffi_name = format!("{core_crate_dir}-ffi");
    let header_name = config.ffi_header_name();
    let lib_name = config.ffi_lib_name();
    let ffi_name_under = ffi_name.replace('-', "_");

    // The odd, non-uniform indentation below (top-level `if`/`endif` bodies flush left,
    // nested blocks stepping by 4 spaces instead of consistently by 2) is not a style
    // choice — it is poly's actual fixed point for `.cmake` files under its tree-sitter-based
    // formatter (verified empirically: `poly fmt --fix --fix-generated` on a conventionally
    // 2-space-indented equivalent converges to exactly this shape and is then stable under a
    // repeated `--check`). Matching it here avoids the generate/format oscillation that a
    // "cleaner" hand-indented version would trigger on every regen. ~keep
    let cmake_content = crate::scaffold::template_env::render(
        "ffi_config.cmake.jinja",
        crate::alef_context! {
            ffi_name => ffi_name,
            ffi_name_under => ffi_name_under,
            lib_name => lib_name,
            header_name => header_name,
        },
    );

    let files = vec![
        GeneratedFile {
            path: PathBuf::from(format!("crates/{}-ffi/Cargo.toml", core_crate_dir)),
            content,
            generated_header: true,
        },
        GeneratedFile {
            path: PathBuf::from(format!(
                "crates/{}-ffi/cmake/{}-ffi-config.cmake",
                core_crate_dir, core_crate_dir
            )),
            content: cmake_content,
            generated_header: true,
        },
    ];
    Ok(files)
}

#[cfg(test)]
#[path = "ffi/tests.rs"]
mod tests;