Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
46d96c9
chore(quest): claim quest/m1/ffi-export-spike.md
kixelated Oct 25, 2026
376a865
refactor(ts): move the rustc driver front end into a shared crate
kixelated Oct 25, 2026
6bbbe2e
wip(ffi): exporter skeleton, census, ABI, glue, and header generators
kixelated Oct 25, 2026
bd680fb
Merge remote-tracking branch 'origin/main' into quest/m1/ffi-export-s…
kixelated Oct 25, 2026
50474f5
wip(ffi): api fixture driven from C against its native transcript, an…
kixelated Oct 25, 2026
dab35ed
wip(ffi): export moq-net and drive it from C
kixelated Oct 25, 2026
bacb0be
wip(ffi): callback interfaces, Copy handles, and field getters
kixelated Oct 25, 2026
dc354be
wip(ffi): just ffi moq exports, builds, and runs the C test
kixelated Oct 25, 2026
4ab20ea
wip(ffi): constants, nightly job, and the shared driver in the flake …
kixelated Oct 25, 2026
90fd36b
wip(ffi): doc comments in the header, renames checked, bench sources
kixelated Oct 25, 2026
6171efc
feat(ffi): the exporter spike's report, bench, and quests
kixelated Oct 25, 2026
bc87005
fix(ffi): call eager futures at once, make recursive values objects, …
kixelated Oct 25, 2026
da759a4
fix(ffi): compare a handle with itself under one borrow, and refuse a…
kixelated Oct 25, 2026
a92e01e
Merge remote-tracking branch 'origin/main' into quest/m1/ffi-export-s…
kixelated Oct 25, 2026
a06d781
fix(ffi): follow the library's own name, and refuse a handle lent ins…
kixelated Oct 25, 2026
72dcb0a
fix(ffi): keep types with their own Drop behind handles; drivers acce…
kixelated Oct 25, 2026
46c5fa2
fix(driver): a marker per run, since runs share a target directory
kixelated Oct 25, 2026
e679853
fix(ffi): share a value that is not Sync on one thread, and take a re…
kixelated Oct 25, 2026
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 additions & 0 deletions .github/workflows/nightly.yml
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,56 @@ jobs:
- name: Summary
run: cat ts/target/bench/report.md >> "$GITHUB_STEP_SUMMARY"

ffi:
name: Export moq-net to C
runs-on: ubuntu-24.04-arm
timeout-minutes: 60

steps:
- name: Checkout
uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
with:
persist-credentials: false

- uses: ./.github/actions/nix

# moq-net's pub API at the pinned moq revision, exported, built, and driven from C.
- name: Export
run: nix develop --command just ffi moq

# What exports, and every refusal, ranked.
- name: Census
run: nix develop --command just ffi census

- name: Summarize
run: awk '/^## Refused items/ { exit } { print }' ffi/target/census/moq-net.md >> "$GITHUB_STEP_SUMMARY"

- name: Upload
uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 # v7.0.2
with:
name: ffi-census
path: ffi/target/census

ffi-bench:
name: Bench the C export against moq-c
runs-on: ubuntu-24.04-arm
timeout-minutes: 90

steps:
- name: Checkout
uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
with:
persist-credentials: false

- uses: ./.github/actions/nix

# Frame writes through each C API. The numbers go in the run's summary: no threshold
# fails the job.
- name: Bench
# Named, so a failing bench fails the pipe.
shell: bash
run: nix develop .#bench --command just ffi bench | tee -a "$GITHUB_STEP_SUMMARY"

census:
name: Census moq-net and kio
runs-on: ubuntu-24.04-arm
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
Rust to other places.

- **TypeScript**: an own rustc driver that translates a [documented Rust subset](ts/README.md) to readable TypeScript, without WASM.
- **C, then any language**: an [exporter](ffi/README.md) that turns a crate's `pub` API into a C ABI and header with no attributes, sharing the TypeScript driver's front end.
- **Any language**: [a hard fork](uniffi/README.md) of [UniFFI](https://github.com/mozilla/uniffi-rs) with every generator [moq](https://github.com/moq-dev/moq) uses.
- **Faster builds**: a reproducible [build-speed benchmark](bench/build/README.md), then a pure-Rust subset (no build scripts, no C linking) that builds faster.

Expand Down
7 changes: 7 additions & 0 deletions driver/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 20 additions & 0 deletions driver/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
[package]
name = "rust2-driver"
description = "The rustc driver front end that rust2's drivers share"
version = "0.0.0"
edition = "2024"
license = "MIT OR Apache-2.0"
repository = "https://github.com/kixelated/rust2"
# A path dependency of the drivers: it only builds on their pinned nightly.
publish = false

[package.metadata.rust-analyzer]
rustc_private = true

# Its own workspace, apart from the repo's, on the drivers' nightly: rust-toolchain.toml
# links ts's pin, which flake.nix reads.
[workspace]

[lints.clippy]
# Every suppression says why, so the next lint audit can judge it.
allow_attributes_without_reason = "warn"
1 change: 1 addition & 0 deletions driver/rust-toolchain.toml
210 changes: 210 additions & 0 deletions driver/src/cargo.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,210 @@
//! The binary as a CLI: run cargo on a package with this binary as rustc.

use std::ffi::OsStr;
use std::path::{Path, PathBuf};
use std::process::{Command, ExitCode};

/// The command line every driver takes:
/// `--config <file> (--out <dir> | --census <file>) [--manifest-path <Cargo.toml>] [-- <cargo args>]`.
pub struct Args {
/// The driver's config file, absolute.
pub config: PathBuf,
/// What to write.
pub output: Output,
/// The package's `Cargo.toml`, absolute: beside the config unless given.
pub manifest: PathBuf,
/// Passed on to `cargo check`.
pub cargo: Vec<String>,
/// The driver's own options, each with its value.
pub options: Vec<(String, String)>,
}

/// What a driver writes, absolute.
pub enum Output {
/// Its output, into this directory.
Dir(PathBuf),
/// A census of what it refuses, to this file, instead.
Census(PathBuf),
}

impl Args {
/// Parse `args`, without the program name, or `None` after printing `usage` for
/// `--help`. `options` are the driver's own, each taking a value. An error ends with
/// `usage`.
pub fn parse(args: &[String], usage: &str, options: &[&str]) -> Result<Option<Self>, String> {
let mut config = None;
let mut own = Vec::new();
let mut out = None;
let mut census = None;
let mut manifest = None;
let mut cargo: &[String] = &[];
let mut rest = args;
while let [arg, tail @ ..] = rest {
rest = tail;
match arg.as_str() {
"--config" | "--out" | "--census" | "--manifest-path" => {
let [value, tail @ ..] = rest else {
return Err(format!("{arg} needs a value\n\n{usage}"));
};
rest = tail;
let slot = match arg.as_str() {
"--config" => &mut config,
"--out" => &mut out,
"--census" => &mut census,
_ => &mut manifest,
};
*slot = Some(absolute(Path::new(value))?);
}
option if options.contains(&option) => {
let [value, tail @ ..] = rest else {
return Err(format!("{arg} needs a value\n\n{usage}"));
};
rest = tail;
own.push((option.to_string(), value.clone()));
}
"--" => {
cargo = rest;
break;
}
"-h" | "--help" => {
println!("{usage}");
return Ok(None);
}
_ => return Err(format!("unexpected argument {arg}\n\n{usage}")),
}
}
let config: PathBuf = config.ok_or_else(|| format!("--config is required\n\n{usage}"))?;
let output = match (out, census) {
(Some(out), None) => Output::Dir(out),
(None, Some(census)) => Output::Census(census),
_ => return Err(format!("one of --out and --census is required\n\n{usage}")),
};
let manifest = match manifest {
Some(manifest) => manifest,
None => config
.parent()
.expect("an absolute file path has a parent")
.join("Cargo.toml"),
};
Ok(Some(Self {
config,
output,
manifest,
cargo: cargo.to_vec(),
options: own,
}))
}
}

/// A `cargo check` of one package's library, with this executable as rustc.
pub struct Check<'a> {
/// The driver's name, which names its target directory.
pub tool: &'a str,
/// The package's `Cargo.toml`, absolute.
pub manifest: &'a Path,
/// The cargo package whose library the driver handles.
pub package: &'a str,
/// The target to check for, or `None` for the host.
pub target: Option<&'a str>,
/// Passed on to `cargo check`, such as `--features`.
pub args: &'a [String],
/// The environment the wrapper reads its job from.
pub env: &'a [(&'a str, &'a OsStr)],
}

impl Check<'_> {
/// Run it, after cleaning the package: cargo can't tell the driver's output is
/// stale, so the package always recompiles.
pub fn run(&self) -> Result<ExitCode, String> {
let exe = std::env::current_exe().map_err(|err| format!("finding this executable: {err}"))?;
let target_dir = match std::env::var_os("CARGO_TARGET_DIR") {
// Absolute, as the caller meant it: cargo runs in the package's directory.
Some(dir) => absolute(Path::new(&dir))?,
// Apart from the workspace's own builds, which a different rustc would invalidate.
None => workspace_root(self.manifest)?.join("target").join(self.tool),
};
let package_dir = self.manifest.parent().expect("a manifest path has a parent");
let cargo = |subcommand: &str| {
let mut cmd = cargo();
// Cargo reads its config from where it runs, so run where the package is.
cmd.current_dir(package_dir);
cmd.arg(subcommand)
.arg("--manifest-path")
.arg(self.manifest)
.arg("--package")
.arg(self.package)
.arg("--target-dir")
.arg(&target_dir);
if let Some(target) = self.target {
cmd.arg("--target").arg(target);
}
cmd
};
run_cargo(cargo("clean").arg("--quiet"))?;
// Its own, since runs may share a target directory.
let ran = target_dir.join(format!("{}-{}.ran", self.tool, std::process::id()));
match std::fs::remove_file(&ran) {
Err(err) if err.kind() != std::io::ErrorKind::NotFound => {
return Err(format!("removing {}: {err}", ran.display()));
}
_ => {}
}
let status = cargo("check")
.arg("--lib")
.args(self.args)
.env("RUSTC_WRAPPER", &exe)
.env(crate::rustc::RAN, &ran)
.envs(self.env.iter().copied())
.status()
.map_err(|err| format!("running cargo: {err}"))?;
if !status.success() {
return Ok(ExitCode::FAILURE);
}
// A library the wrapper never recognized would otherwise succeed with nothing written.
let compiled = ran.exists();
let _ = std::fs::remove_file(&ran);
if !compiled {
return Err(format!("cargo compiled no library of package {}", self.package));
}
Ok(ExitCode::SUCCESS)
}
}

fn absolute(path: &Path) -> Result<PathBuf, String> {
std::path::absolute(path).map_err(|err| format!("resolving {}: {err}", path.display()))
}

fn cargo() -> Command {
Command::new(std::env::var_os("CARGO").unwrap_or_else(|| "cargo".into()))
}

fn run_cargo(cmd: &mut Command) -> Result<(), String> {
let status = cmd.status().map_err(|err| format!("running cargo: {err}"))?;
match status.success() {
true => Ok(()),
false => Err(format!("{cmd:?} failed")),
}
}

/// The directory of the workspace `manifest` belongs to.
fn workspace_root(manifest: &Path) -> Result<PathBuf, String> {
let output = cargo()
.args([
"locate-project",
"--workspace",
"--message-format",
"plain",
"--manifest-path",
])
.arg(manifest)
.output()
.map_err(|err| format!("running cargo: {err}"))?;
if !output.status.success() {
return Err(String::from_utf8_lossy(&output.stderr).into_owned());
}
let root = String::from_utf8(output.stdout).map_err(|_| "cargo printed a non-UTF-8 path".to_string())?;
Ok(Path::new(root.trim())
.parent()
.expect("a manifest path has a parent")
.to_path_buf())
}
14 changes: 14 additions & 0 deletions driver/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
//! The front end rust2's rustc drivers share.
//!
//! A driver binary is two programs. Run directly, it is a CLI that runs cargo on a
//! package with itself as `RUSTC_WRAPPER` ([`cargo`]). Run by cargo as that wrapper, it
//! is rustc ([`rustc`]), which compiles every crate as rustc would and hands the
//! configured library to the driver's callbacks.

#![feature(rustc_private)]

extern crate rustc_driver;
extern crate rustc_session;

pub mod cargo;
pub mod rustc;
67 changes: 67 additions & 0 deletions driver/src/rustc.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
//! The binary as rustc, run by cargo as its `RUSTC_WRAPPER`.

use std::path::Path;
use std::process::ExitCode;

use rustc_driver::Callbacks;
use rustc_session::EarlyDiagCtxt;
use rustc_session::config::ErrorOutputType;

const BUG_REPORT_URL: &str = "https://github.com/kixelated/rust2/issues/new";

/// Whether cargo ran this binary as its wrapper, which it does with the rustc it would
/// have run as the first argument.
pub fn wrapped(args: &[String]) -> bool {
args.get(1).map(Path::new).and_then(Path::file_stem) == Some("rustc".as_ref())
}

/// The callbacks for one rustc invocation.
pub type Job = Box<dyn Callbacks + Send>;

/// Run as rustc, with `args` as cargo passed them. `job` picks the callbacks from
/// rustc's own arguments, which it may add to, or `None` to compile the crate as rustc
/// would; its error ends the run before rustc starts, prefixed with `tool`.
pub fn run(
tool: &str,
mut args: Vec<String>,
job: impl FnOnce(&mut Vec<String>) -> Result<Option<Job>, String>,
) -> ExitCode {
args.remove(1);
let early_dcx = EarlyDiagCtxt::new(ErrorOutputType::default());
rustc_driver::init_rustc_env_logger(&early_dcx);
rustc_driver::install_ice_hook(BUG_REPORT_URL, |_| ());
let job = match job(&mut args) {
Ok(job) => job,
Err(err) => early_dcx.early_fatal(format!("{tool}: {err}")),
};
rustc_driver::catch_with_exit_code(|| match job {
Some(mut job) => {
rustc_driver::compiler_entrypoint(&args, job.as_mut());
// Tell the CLI its library was compiled with the job.
if let Some(ran) = std::env::var_os(RAN) {
let _ = std::fs::write(ran, "");
}
}
None => rustc_driver::compiler_entrypoint(&args, &mut Plain),
})
}

/// The file the wrapper creates once it has run a job, so the CLI knows cargo compiled
/// the configured library at all.
pub(crate) const RAN: &str = "RUST2_DRIVER_RAN";

/// Compiles a crate as rustc would.
struct Plain;

impl Callbacks for Plain {}

/// Whether rustc's `args` compile the library of the cargo package `package`, not its
/// tests. Cargo names the package being compiled in `CARGO_PKG_NAME`, whatever its
/// library target is called.
pub fn compiles_lib(args: &[String], package: &str) -> bool {
let ours = std::env::var("CARGO_PKG_NAME").is_ok_and(|name| name == package);
let lib = args
.windows(2)
.any(|w| w[0] == "--crate-type" && matches!(w[1].as_str(), "lib" | "rlib"));
ours && lib && !args.iter().any(|a| a == "--test")
}
Loading