jffi 0.4.4

Cross-platform framework for building native apps with Rust business logic and platform-native UIs
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
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
use crate::platform::Platform;
use anyhow::{Context, Result};
use colored::*;
use std::process::Command;

pub(crate) fn installation_allowed() -> bool {
    std::env::var("JFFI_INSTALL_MISSING").as_deref() == Ok("1")
}

fn managed_tool_reconciliation_allowed() -> bool {
    if installation_allowed() {
        return true;
    }

    std::env::var("JFFI_NO_SETUP").as_deref() != Ok("1")
        && !matches!(
            std::env::var("CARGO_NET_OFFLINE").as_deref(),
            Ok("1") | Ok("true")
        )
}

fn tool_succeeds(name: &str, args: &[&str]) -> bool {
    Command::new(name)
        .args(args)
        .output()
        .map(|output| output.status.success())
        .unwrap_or(false)
}

fn resolved_uniffi_version() -> Result<String> {
    if let Ok(lock) = std::fs::read_to_string("Cargo.lock") {
        let mut in_package = false;
        for line in lock.lines() {
            let line = line.trim();
            if line == "[[package]]" {
                in_package = false;
            } else if line == "name = \"uniffi\"" {
                in_package = true;
            } else if in_package && line.starts_with("version = ") {
                return Ok(line
                    .trim_start_matches("version = ")
                    .trim_matches('"')
                    .to_string());
            }
        }
    }
    let manifest = std::fs::read_to_string("core/Cargo.toml")
        .context("Could not read core/Cargo.toml to determine the UniFFI version")?;
    let manifest: toml::Value = toml::from_str(&manifest)?;
    let dependency = manifest
        .get("dependencies")
        .and_then(|value| value.get("uniffi"))
        .context("core/Cargo.toml does not declare the uniffi dependency")?;
    let requirement = dependency
        .as_str()
        .or_else(|| dependency.get("version").and_then(|value| value.as_str()))
        .context("The uniffi dependency has no version")?;
    Ok(requirement.trim_start_matches(['=', '^', '~']).to_string())
}

fn parse_uniffi_bindgen_version(output: &str) -> Option<&str> {
    let mut fields = output.split_whitespace();
    (fields.next()? == "uniffi-bindgen")
        .then(|| fields.next())
        .flatten()
}

fn installed_uniffi_bindgen_version() -> Option<String> {
    let output = Command::new("uniffi-bindgen")
        .arg("--version")
        .output()
        .ok()?;
    output.status.success().then_some(())?;
    parse_uniffi_bindgen_version(&String::from_utf8_lossy(&output.stdout)).map(str::to_string)
}

pub fn ensure_uniffi_bindgen() -> Result<()> {
    let required = resolved_uniffi_version()?;
    let installed = installed_uniffi_bindgen_version();
    if installed.as_deref() == Some(required.as_str()) {
        println!("  {} uniffi-bindgen {}", "✓".green(), required);
        return Ok(());
    }

    if !managed_tool_reconciliation_allowed() {
        let found = installed.as_deref().unwrap_or("not installed");
        anyhow::bail!(
            "uniffi-bindgen {} is required (found {}). Automatic setup is disabled; run `jffi setup --platform {}`",
            required,
            found,
            std::env::var("JFFI_SETUP_PLATFORM").unwrap_or_else(|_| "<platform>".to_string())
        );
    }

    match installed {
        Some(version) => println!(
            "  {} Reconciling uniffi-bindgen {} → {}...",
            "→".bright_blue(),
            version,
            required
        ),
        None => println!(
            "  {} Installing required uniffi-bindgen {}...",
            "→".bright_blue(),
            required
        ),
    }

    let exact = format!("={}", required);
    let status = Command::new("cargo")
        .args([
            "install",
            "uniffi",
            "--features",
            "cli",
            "--bin",
            "uniffi-bindgen",
            "--version",
            &exact,
            "--force",
            "--locked",
        ])
        .status()
        .context("Failed to install the matching uniffi-bindgen")?;
    if !status.success() {
        anyhow::bail!("Failed to install uniffi-bindgen {}", required);
    }

    let reconciled = installed_uniffi_bindgen_version();
    if reconciled.as_deref() != Some(required.as_str()) {
        anyhow::bail!(
            "Installed uniffi-bindgen did not resolve to {} (found {})",
            required,
            reconciled.as_deref().unwrap_or("not installed")
        );
    }
    println!("  {} uniffi-bindgen {} ready", "✓".green(), required);
    Ok(())
}

/// Ensure a CLI tool is installed, or try to install it.
pub fn ensure_tool(name: &str, install_cmd: &[&str]) -> Result<()> {
    print!("  {} Checking {}... ", "→".bright_blue(), name);

    let exists = Command::new(name)
        .arg("--version")
        .output()
        .map(|output| output.status.success())
        .unwrap_or(false);

    if exists {
        println!("{}", "✓".green());
        return Ok(());
    }

    println!("{}", "not found".yellow());
    if !installation_allowed() {
        anyhow::bail!(
            "{} is required but not installed. Run `jffi setup --platform {}`",
            name,
            std::env::var("JFFI_SETUP_PLATFORM").unwrap_or_else(|_| "<platform>".to_string())
        );
    }
    println!("  {} Installing {}...", "→".bright_blue(), name);

    let status = Command::new(install_cmd[0])
        .args(&install_cmd[1..])
        .status()
        .context(format!("Failed to install {}", name))?;

    if !status.success() {
        anyhow::bail!("Failed to install {}. Please install it manually.", name);
    }

    println!("  {} {} installed successfully!", "✓".green(), name);
    Ok(())
}

/// Ensure cargo-ndk is available through its supported Cargo subcommand entrypoint.
/// Recent cargo-ndk releases intentionally reject direct `cargo-ndk --version`
/// invocation even though the tool is correctly installed.
fn ensure_cargo_ndk() -> Result<()> {
    print!("  {} Checking cargo ndk... ", "→".bright_blue());
    if tool_succeeds("cargo", &["ndk", "--version"]) {
        println!("{}", "✓".green());
        return Ok(());
    }

    println!("{}", "not found".yellow());
    if !installation_allowed() {
        anyhow::bail!(
            "cargo-ndk is required but not installed. Run `jffi setup --platform {}`",
            std::env::var("JFFI_SETUP_PLATFORM").unwrap_or_else(|_| "android".to_string())
        );
    }

    println!("  {} Installing cargo-ndk...", "→".bright_blue());
    let status = Command::new("cargo")
        .args(["install", "cargo-ndk"])
        .status()
        .context("Failed to install cargo-ndk")?;
    if !status.success() || !tool_succeeds("cargo", &["ndk", "--version"]) {
        anyhow::bail!("Failed to install cargo-ndk. Please install it manually.");
    }
    println!("  {} cargo-ndk installed successfully!", "✓".green());
    Ok(())
}

/// Ensure Rust targets are installed.
pub fn ensure_rust_targets(targets: &[&str]) -> Result<()> {
    let output = Command::new("rustup")
        .args(["target", "list", "--installed"])
        .output()
        .context("Failed to run rustup. Please install Rust via rustup.")?;

    let installed = String::from_utf8_lossy(&output.stdout);
    let mut missing = Vec::new();

    for target in targets {
        if !installed.lines().any(|l| l.trim() == *target) {
            missing.push(*target);
        }
    }

    if missing.is_empty() {
        return Ok(());
    }

    if !installation_allowed() {
        anyhow::bail!(
            "Missing Rust targets: {}. Run `jffi setup --platform {}`",
            missing.join(", "),
            std::env::var("JFFI_SETUP_PLATFORM").unwrap_or_else(|_| "<platform>".to_string())
        );
    }

    println!(
        "  {} Installing missing Rust targets: {}...",
        "→".bright_blue(),
        missing.join(", ")
    );

    let status = Command::new("rustup")
        .arg("target")
        .arg("add")
        .args(&missing)
        .status()
        .context("Failed to install Rust targets via rustup")?;

    if !status.success() {
        anyhow::bail!("Failed to install Rust targets: {}", missing.join(", "));
    }

    Ok(())
}

/// Ensure Python requirements are installed.
pub fn ensure_python_requirements(platform: &str) -> Result<()> {
    let requirements_path = std::path::Path::new("platforms")
        .join(platform)
        .join("requirements.txt");
    if !requirements_path.exists() {
        return Ok(());
    }

    if !installation_allowed() {
        let status = Command::new("python3")
            .args(["-c", "import gi"])
            .status()
            .context("Failed to check Python GTK bindings")?;
        if status.success() {
            return Ok(());
        }
        anyhow::bail!(
            "Python requirements for {} are missing. Run `jffi setup --platform {}`",
            platform,
            platform
        );
    }

    let status = Command::new("python3")
        .args(["-c", "import gi"])
        .status()
        .context("Failed to verify Python GTK bindings")?;
    if status.success() {
        return Ok(());
    }
    anyhow::bail!(
        "Python GTK bindings are still unavailable after platform package setup; install the dependencies listed in {} using your OS package manager",
        requirements_path.display()
    )
}

/// Setup all dependencies for a platform.
pub fn setup_platform(platform: &Platform) -> Result<()> {
    std::env::set_var("JFFI_SETUP_PLATFORM", platform.as_str());
    println!(
        "{}",
        format!("🔧 Checking environment for {}...", platform.as_str())
            .bright_cyan()
            .bold()
    );

    // Bindgen must exactly match the project's resolved UniFFI library.
    ensure_uniffi_bindgen()?;

    match platform {
        Platform::Ios => {
            if !tool_succeeds("xcodebuild", &["-version"]) {
                anyhow::bail!("Xcode (xcodebuild) is required for iOS builds. Please install it from the App Store.");
            }
            ensure_rust_targets(&[
                "aarch64-apple-ios",
                "x86_64-apple-ios",
                "aarch64-apple-ios-sim",
            ])?;
        }
        Platform::Macos => {
            if !tool_succeeds("xcodebuild", &["-version"]) {
                anyhow::bail!("Xcode (xcodebuild) is required for macOS builds. Please install it from the App Store.");
            }
            ensure_rust_targets(&["aarch64-apple-darwin", "x86_64-apple-darwin"])?;
        }
        Platform::Android => {
            ensure_cargo_ndk()?;
            ensure_rust_targets(&[
                "aarch64-linux-android",
                "armv7-linux-androideabi",
                "x86_64-linux-android",
            ])?;
        }
        Platform::Linux => {
            if !tool_succeeds("cc", &["--version"]) {
                anyhow::bail!("C compiler (cc) is missing. Install build-essential or equivalent.");
            }

            // Helper to install system packages
            let install_system_deps = |packages: Vec<&str>| -> Result<()> {
                if !installation_allowed() {
                    anyhow::bail!(
                        "Missing system dependencies: {}. Run `jffi setup --platform linux`",
                        packages.join(", ")
                    );
                }
                if std::env::consts::OS == "linux"
                    && Command::new("apt-get").arg("--version").output().is_ok()
                {
                    let has_sudo = Command::new("sudo")
                        .arg("-n")
                        .arg("true")
                        .status()
                        .map(|s| s.success())
                        .unwrap_or(false);
                    let mut cmd = if has_sudo {
                        let mut c = Command::new("sudo");
                        c.arg("apt-get");
                        c
                    } else {
                        Command::new("apt-get")
                    };

                    println!(
                        "  {} Installing Linux system dependencies: {}...",
                        "→".bright_blue(),
                        packages.join(", ")
                    );
                    let status = cmd.args(["install", "-y"]).args(packages).status()?;
                    if !status.success() {
                        anyhow::bail!("System package installation failed");
                    }
                    Ok(())
                } else if std::env::consts::OS == "macos"
                    && Command::new("brew").arg("--version").output().is_ok()
                {
                    println!(
                        "  {} Installing macOS system dependencies for Linux support: {}...",
                        "→".bright_blue(),
                        packages.join(", ")
                    );

                    // Map linux package names to brew package names if needed
                    let brew_packages: Vec<&str> = packages
                        .iter()
                        .map(|&p| match p {
                            "libgtk-4-dev" => "gtk4",
                            "libadwaita-1-dev" => "libadwaita",
                            "python3-gi" => "pygobject3",
                            _ => p,
                        })
                        .collect();

                    let status = Command::new("brew")
                        .arg("install")
                        .args(brew_packages)
                        .status()?;

                    if !status.success() {
                        anyhow::bail!("Homebrew failed to install dependencies.");
                    }
                    Ok(())
                } else {
                    if std::env::consts::OS != "linux" && std::env::consts::OS != "macos" {
                        anyhow::bail!("Building for Linux requires a Linux or macOS host. {} is not supported.", std::env::consts::OS);
                    } else {
                        anyhow::bail!("Missing system dependencies: {}. Please install them manually using your package manager.", packages.join(", "));
                    }
                }
            };

            if Command::new("pkg-config")
                .arg("--version")
                .output()
                .is_err()
            {
                install_system_deps(vec!["pkg-config"])?;
            }

            // Check for GTK 4 and Libadwaita
            let has_gtk4 = Command::new("pkg-config")
                .args(["--exists", "gtk4"])
                .status()
                .map(|s| s.success())
                .unwrap_or(false);
            let has_adwaita = Command::new("pkg-config")
                .args(["--exists", "libadwaita-1"])
                .status()
                .map(|s| s.success())
                .unwrap_or(false);

            if !has_gtk4 || !has_adwaita {
                install_system_deps(vec![
                    "libgtk-4-dev",
                    "libadwaita-1-dev",
                    "python3-gi",
                    "python3-gi-cairo",
                    "gir1.2-gtk-4.0",
                    "gir1.2-adw-1",
                ])?;
            }

            // Install Python requirements
            ensure_python_requirements("linux")?;
        }
        Platform::Windows => {
            if std::env::consts::OS != "windows" {
                anyhow::bail!("Building for Windows requires a Windows host. Cross-compilation from {} is not yet fully supported.", std::env::consts::OS);
            }
            if !tool_succeeds("dotnet", &["--version"]) {
                anyhow::bail!(".NET SDK is required for Windows builds. Please install it from https://dotnet.microsoft.com/");
            }
            ensure_rust_targets(&["x86_64-pc-windows-msvc", "aarch64-pc-windows-msvc"])?;
            crate::commands::build::ensure_uniffi_bindgen_cs()?;
        }
        Platform::Web => {
            ensure_tool("wasm-pack", &["cargo", "install", "wasm-pack"])?;
            ensure_rust_targets(&["wasm32-unknown-unknown"])?;
            crate::commands::build::ensure_wasm_bindgen_cli()?;
        }
    }

    Ok(())
}

pub fn install_platform(platform: &Platform) -> Result<()> {
    std::env::set_var("JFFI_INSTALL_MISSING", "1");
    setup_platform(platform)
}

#[cfg(test)]
mod tests {
    use super::parse_uniffi_bindgen_version;

    #[test]
    fn parses_uniffi_bindgen_version() {
        assert_eq!(
            parse_uniffi_bindgen_version("uniffi-bindgen 0.31.1\n"),
            Some("0.31.1")
        );
    }

    #[test]
    fn rejects_unrelated_or_incomplete_version_output() {
        assert_eq!(parse_uniffi_bindgen_version("uniffi 0.31.1"), None);
        assert_eq!(parse_uniffi_bindgen_version("uniffi-bindgen"), None);
    }
}