Skip to content

Rewrite your app in Rust, piece by piece, checked against the original at every step. Works with TypeScript, JavaScript, Python, Go, Ruby, Java, C, PHP, C#, Kotlin, PowerShell and Swift.

12.4 KB
Raw Blame
//! The Java side of record, build, diff and bench. A generated driver calls the unit's static
//! methods by reflection: compiled with the original sources to record, and with the JNI
//! wrapper classes over the Rust library to diff.

use crate::walk::Canonical;
use std::path::{Path, PathBuf};
use std::process::Command;

use anyhow::{Context, Result, bail};
use serde_json::Value;

use crate::bridge::Manifest;
use crate::harness::{BenchRow, CASES, DiffOutcome};

const WORK_DIR: &str = ".rust-anything";

fn run(cmd: &mut Command, what: &str) -> Result<()> {
    let _scratch = crate::harness::scratch_cwd(cmd)?;
    let output = cmd.output().with_context(|| format!("running {what}"))?;
    if !output.status.success() {
        let text = format!(
            "{}{}",
            String::from_utf8_lossy(&output.stdout),
            String::from_utf8_lossy(&output.stderr)
        );
        let tail: Vec<&str> = text
            .lines()
            .filter(|l| !l.starts_with("Picked up JAVA_TOOL_OPTIONS"))
            .rev()
            .take(30)
            .collect();
        bail!(
            "{what} failed ({}):\n{}",
            output.status,
            tail.into_iter().rev().collect::<Vec<_>>().join("\n")
        );
    }
    Ok(())
}

fn java_files(dir: &Path) -> Vec<PathBuf> {
    crate::walk::builder(dir)
        .require_git(false)
        .build()
        .flatten()
        .map(|e| e.into_path())
        .filter(|p| p.extension().is_some_and(|e| e == "java"))
        .collect()
}

/// Compiles the driver with the original sources (`rust: false`) or the wrapper classes.
fn compile(crate_dir: &Path, m: &Manifest, rust: bool) -> Result<PathBuf> {
    let work = crate_dir.canonical()?.join(WORK_DIR);
    let support = work.join("java-support/rust-anything");
    std::fs::create_dir_all(&support)?;
    std::fs::write(
        support.join("RustAnythingJson.java"),
        crate::codegen_java::JSON_JAVA,
    )?;
    std::fs::write(
        support.join("RustAnythingDriver.java"),
        crate::codegen_java::DRIVER_JAVA,
    )?;
    let classes = work.join(if rust { "java-rust" } else { "java-original" });
    if classes.exists() {
        std::fs::remove_dir_all(&classes)?;
    }
    std::fs::create_dir_all(&classes)?;
    let mut sources = java_files(&work.join("java-support"));
    if rust {
        sources.extend(
            java_files(&crate_dir.join("java"))
                .into_iter()
                .filter(|p| !p.ends_with("rustanything/RustAnythingJson.java")),
        );
    } else {
        // The whole source tree: the unit's classes may use others from the same module.
        let ws = crate::lang::java::discover(&m.root)?;
        sources.extend(
            ws.files
                .iter()
                .filter(|f| f.kind == crate::ts::discover::FileKind::Source)
                .map(|f| m.root.join(&f.path)),
        );
    }
    let mut javac = Command::new("javac");
    javac.args(["-J-Dfile.encoding=UTF-8", "-encoding", "UTF-8", "-nowarn"]);
    let user = user_classpath();
    if !user.is_empty() {
        javac.arg("-cp").arg(std::env::join_paths(user)?);
    }
    // The sources go in an argument file: a mid-size project's paths on the command line pass
    // Windows' 32,767-character limit (os error 206).
    let argfile = classes.with_extension("javac-sources");
    std::fs::write(&argfile, javac_argfile(&sources))?;
    let mut at = std::ffi::OsString::from("@");
    at.push(&argfile);
    run(
        javac.arg("-d").arg(&classes).arg(at),
        if rust {
            "javac (driver and wrapper classes)"
        } else {
            "javac (driver and original sources)"
        },
    )
    .map_err(|e| {
        let text = format!("{e:#}");
        if missing_classes(&text) {
            anyhow::anyhow!(
                "{text}\n\nThe sources use classes from outside their tree (a library or another \
                 module). Set CLASSPATH to the jars and class folders the project builds against \
                 and run again: javac and the driver both read it."
            )
        } else {
            e
        }
    })?;
    Ok(classes)
}

/// An argument file for javac naming `sources`, one per line. Each path is quoted, so a space
/// in it stays part of it, and inside quotes javac reads `\` as an escape (`C:\src\tdir` would
/// hold a tab), so a backslash and a quote are escaped.
fn javac_argfile(sources: &[PathBuf]) -> String {
    let mut out = String::new();
    for p in sources {
        let path = p.to_string_lossy();
        out.push('"');
        out.push_str(&path.replace('\\', "\\\\").replace('"', "\\\""));
        out.push_str("\"\n");
    }
    out
}

/// Whether javac failed on types it could not find, as a Gradle or Maven module compiled
/// without its dependencies does.
fn missing_classes(javac: &str) -> bool {
    javac.lines().any(|l| {
        l.contains(": error: cannot find symbol")
            || (l.contains(": error: package ") && l.ends_with(" does not exist"))
    })
}

/// The CLASSPATH variable's entries, which `-cp` would otherwise replace. Made absolute, since
/// javac and java run in a scratch folder; a `lib/*` entry keeps its `*`.
fn user_classpath() -> Vec<PathBuf> {
    std::env::var_os("CLASSPATH")
        .map(|v| {
            std::env::split_paths(&v)
                .filter(|p| !p.as_os_str().is_empty())
                .map(|p| std::path::absolute(&p).unwrap_or(p))
                .collect()
        })
        .unwrap_or_default()
}

fn driver(
    crate_dir: &Path,
    m: &Manifest,
    mode: &str,
    cases: &Path,
    out: &Path,
    min_ms: u64,
    rust: bool,
) -> Result<()> {
    let mut cmd = driver_command(crate_dir, m, mode, cases, out, min_ms, rust)?;
    run(&mut cmd, &format!("java driver ({mode})"))
}

/// Java class files for a dexed on-device run: support driver plus either the original Java
/// sources (`rust: false`) or the generated JNI wrappers (`rust: true`).
pub(crate) fn android_classes(crate_dir: &Path, m: &Manifest, rust: bool) -> Result<Vec<PathBuf>> {
    let classes = compile(crate_dir, m, rust)?;
    Ok(crate::walk::builder(&classes)
        .require_git(false)
        .build()
        .flatten()
        .map(|e| e.into_path())
        .filter(|p| p.extension().is_some_and(|e| e == "class"))
        .collect())
}

fn driver_command(
    crate_dir: &Path,
    m: &Manifest,
    mode: &str,
    cases: &Path,
    out: &Path,
    min_ms: u64,
    rust: bool,
) -> Result<Command> {
    let classes = compile(crate_dir, m, rust)?;
    let mut classpath = vec![classes];
    if rust {
        classpath.push(compile(crate_dir, m, false)?);
    }
    classpath.extend(user_classpath());
    let classpath = std::env::join_paths(classpath)?;
    let work = crate_dir.join(WORK_DIR);
    let targets = work.join("targets.json");
    std::fs::write(&targets, crate::codegen_java::targets(m).to_string())?;
    let mut cmd = Command::new("java");
    cmd.arg("-Dfile.encoding=UTF-8");
    if m.messages {
        cmd.arg("-Drust-anything.messages=true");
    }
    if rust {
        let lib = crate::harness_c::built_library(crate_dir, m)?;
        cmd.arg(format!(
            "-Drust-anything.lib.{}={}",
            m.lib_name(),
            lib.canonical()?.display()
        ));
    }
    cmd.arg("-cp")
        .arg(&classpath)
        .arg("rustanything.RustAnythingDriver")
        .arg(mode)
        .arg(&targets)
        .arg(cases)
        .arg(out)
        .arg(min_ms.to_string());
    Ok(cmd)
}

pub fn record(
    crate_dir: &Path,
    m: &Manifest,
    cases: &[Value],
    sandbox: Option<&Path>,
) -> Result<PathBuf> {
    std::fs::create_dir_all(crate_dir.join(WORK_DIR))?;
    let out = crate_dir.join(CASES);
    let _lock = crate::harness_c::driver_lock(crate_dir)?;
    crate::harness::record_cases(
        cases,
        &crate_dir.join(WORK_DIR),
        &out,
        "java driver (record)",
        sandbox,
        |inputs, part| driver_command(crate_dir, m, "record", inputs, part, 0, false),
    )?;
    Ok(out)
}

pub fn diff(crate_dir: &Path, m: &Manifest) -> Result<DiffOutcome> {
    let cases = crate_dir.join(CASES);
    if !cases.exists() {
        bail!(
            "no {CASES} in {} (run `rust-anything record` first)",
            crate_dir.display()
        );
    }
    let out = crate::harness::fresh_result(crate_dir, "diff")?;
    let _lock = crate::harness_c::driver_lock(crate_dir)?;
    driver(crate_dir, m, "diff", &cases, &out, 0, true)?;
    crate::harness::read_result(crate_dir, &out, "Java driver (diff)")
}

pub fn bench(crate_dir: &Path, m: &Manifest, min_ms: u64) -> Result<Vec<BenchRow>> {
    let cases = crate_dir.join(CASES);
    if !cases.exists() {
        bail!(
            "no {CASES} in {} (run `rust-anything record` first)",
            crate_dir.display()
        );
    }
    let _lock = crate::harness_c::driver_lock(crate_dir)?;
    let mut timings = Vec::new();
    for rust in [false, true] {
        let out = crate_dir.join(WORK_DIR).join(if rust {
            "bench-rust.json"
        } else {
            "bench-original.json"
        });
        timings.push(crate::harness_c::timings(&out, || {
            driver(crate_dir, m, "bench", &cases, &out, min_ms, rust)
        })?);
    }
    Ok(crate::harness::bench_rows(m, &timings[0], &timings[1]))
}

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

    #[test]
    fn the_javac_argfile_quotes_and_escapes_each_path() {
        let sources = [
            PathBuf::from("src/a b/A.java"),
            PathBuf::from(r"C:\src\tdir\B.java"),
        ];
        assert_eq!(
            javac_argfile(&sources),
            "\"src/a b/A.java\"\n\"C:\\\\src\\\\tdir\\\\B.java\"\n"
        );
    }

    /// javac reads the file back as the paths written, a space and (on Windows) backslashes
    /// included: unescaped, `\t` in `tdir` would be a tab and the file not found.
    #[test]
    fn javac_compiles_the_sources_an_argfile_names() {
        if Command::new("javac").arg("-version").output().is_err() {
            return;
        }
        let root = std::env::temp_dir().join(format!(
            "rust-anything-javac-argfile-{}",
            std::process::id()
        ));
        let _ = std::fs::remove_dir_all(&root);
        let src = root.join("a b").join("tdir");
        std::fs::create_dir_all(&src).unwrap();
        let file = src.join("A.java");
        std::fs::write(&file, "public class A {}\n").unwrap();
        let argfile = root.join("sources");
        std::fs::write(&argfile, javac_argfile(&[file])).unwrap();
        let mut at = std::ffi::OsString::from("@");
        at.push(&argfile);
        let out = Command::new("javac")
            .arg("-d")
            .arg(root.join("out"))
            .arg(at)
            .output()
            .unwrap();
        assert!(
            out.status.success(),
            "{}",
            String::from_utf8_lossy(&out.stderr)
        );
        assert!(root.join("out").join("A.class").exists());
        let _ = std::fs::remove_dir_all(&root);
    }

    #[test]
    fn missing_classes_spots_the_two_javac_errors_a_classpath_fixes() {
        assert!(missing_classes(
            "src/Text.java:3: error: cannot find symbol\n  symbol: class StringUtils\n1 error"
        ));
        assert!(missing_classes(
            "src/Text.java:1: error: package org.apache.commons.lang3 does not exist\nimport org.apache.commons.lang3.StringUtils;"
        ));
        assert!(!missing_classes(
            "src/Text.java:5: error: incompatible types: int cannot be converted to String\n1 error"
        ));
        assert!(!missing_classes(""));
    }

    #[test]
    fn java_files_finds_nested_java_files_without_git() {
        let dir = std::env::temp_dir().join(format!("ra-java-files-{}", std::process::id()));
        let _ = std::fs::remove_dir_all(&dir);
        std::fs::create_dir_all(dir.join("com").join("example")).unwrap();
        std::fs::write(dir.join("Text.java"), "class Text {}").unwrap();
        std::fs::write(dir.join("com/example/Util.java"), "class Util {}").unwrap();
        std::fs::write(dir.join("com/example/Other.kt"), "object Other").unwrap();
        let mut found: Vec<PathBuf> = java_files(&dir)
            .into_iter()
            .map(|p| p.strip_prefix(&dir).unwrap().to_path_buf())
            .collect();
        found.sort();
        std::fs::remove_dir_all(&dir).unwrap();
        assert_eq!(
            found,
            [
                Path::new("Text.java").to_path_buf(),
                Path::new("com").join("example").join("Util.java"),
            ]
        );
    }
}