Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
4 changes: 4 additions & 0 deletions ffi/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ extension the crate builds with `--features python`, `swift/<prefix>.swift` hold
Swift, over the C header as a clang module, `go/` holds it in Go, a package per crate
over the C ABI through cgo, and `dart/<prefix>.dart` holds it in Dart, over the C ABI through
`dart:ffi` and what the crate builds with `--features dart`.
An export again rewrites only the files whose content changed, and removes from
`include/<prefix>/` and `go/` only what it no longer writes, so a build of an unchanged
export stays fresh. The crate's version, `0.0.0+<hash>`, hashes its Rust sources and
manifest, so exports that differ build apart in a target directory they share.

```toml
# ffi.toml, beside the package's Cargo.toml unless --manifest-path says otherwise.
Expand Down
31 changes: 27 additions & 4 deletions ffi/justfile
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,19 @@ moq-c checkout="": (moq-write checkout)
cargo rustc --locked --quiet --manifest-path "$out/Cargo.toml" --lib --crate-type staticlib --features kotlin,dart 2>&1 | tee target/moq-c-build.log
sed -n 's/^note: native-static-libs: //p' target/moq-c-build.log > target/moq-c-native
read -ra native < target/moq-c-native
cc -std=c11 -pedantic -Wall -Wextra -Werror -I "$out/include" moq/moq.c "$out/target/debug/libmoq.a" "${native[@]}" -o target/moq-c-test
link=(cc -std=c11 -pedantic -Wall -Wextra -Werror -I "$out/include" moq/moq.c "$out/target/debug/libmoq.a" "${native[@]}" -o target/moq-c-test)
# The link reruns only when what it reads, the compiler, or the command changed, or its
# program did: the stamp holds a hash of each, by content, since an export keeps mtimes.
key() {
{ command -v cc; cc --version; printf '%s\n' "${link[@]}"; find "$out/include" -type f | LC_ALL=C sort | xargs sha256sum; sha256sum moq/moq.c "$out/target/debug/libmoq.a"; } | sha256sum
sha256sum < target/moq-c-test
}
stamp=target/moq-c-test.stamp
if ! [ -f target/moq-c-test ] || ! [ -f "$stamp" ] || [ "$(key)" != "$(cat "$stamp")" ]; then
rm -f "$stamp"
"${link[@]}"
key > "$stamp"
fi
target/moq-c-test

# Export moq's crates' C ABI into target/export/moq, unbuilt.
Expand Down Expand Up @@ -335,6 +347,15 @@ standin checkout="":
# moq-ffi's dependencies, moq-tokio with its default features as moq-ffi has them.
deps=(hang kio moq-mux moq-net moq-pattern moq-tokio)
mkdir -p "$dir/src"
# Replace a file only when its content changes, so an unchanged stand-in, and the
# export over it, stay fresh.
update() {
if cmp -s "$1" "$2"; then
rm "$1"
else
mv "$1" "$2"
fi
}
{
printf '# Generated by just ffi standin, from %s.\n' "$src"
printf '[package]\nname = "moq-ffi"\nversion = "0.0.0"\nedition = "2024"\npublish = false\n\n'
Expand All @@ -347,14 +368,15 @@ standin checkout="":
# moq's profiles, which the export carries as it would moq-ffi's own; the dev ones
# tune test-only crates the stand-in does not resolve.
awk '/^\[/ { keep = /^\[profile\./ && !/^\[profile\.dev[].]/ } keep' "$src/Cargo.toml"
} > "$dir/Cargo.toml"
} > "$dir/Cargo.toml.new"
update "$dir/Cargo.toml.new" "$dir/Cargo.toml"
# What moq-ffi ships, re-exported: moq-net and moq-pattern at the root, beside the
# waiter a driver's poll registers and the clock it polls at, and each other crate in a
# module of its own, since several name an item alike, such as `Error`. moq-tokio's
# session side only: the client, server, and origin a binding drives, with the configs
# and the bind they take. Its QUIC driver, CLI, worker groups, and listener internals
# stay out.
cat > "$dir/src/lib.rs" <<'EOF'
cat > "$dir/src/lib.rs.new" <<'EOF'
pub use kio::{Park, Waiter, WaiterList, clock, coop, log};
pub use moq_net::*;
pub use moq_pattern::*;
Expand Down Expand Up @@ -400,8 +422,9 @@ standin checkout="":
}
}
EOF
update "$dir/src/lib.rs.new" "$dir/src/lib.rs"
# moq's lockfile pins what moq's own builds use.
cp "$src/Cargo.lock" "$dir/Cargo.lock"
cmp -s "$src/Cargo.lock" "$dir/Cargo.lock" || cp "$src/Cargo.lock" "$dir/Cargo.lock"

# Check out the pinned moq revision in target/moq, apart from `just ts`'s so neither recipe touches the other's checkout, copying the objects its clone already has.
[private]
Expand Down
17 changes: 16 additions & 1 deletion ffi/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,13 +148,28 @@ fn try_run(args: &[String]) -> Result<ExitCode, String> {
/// check did not resolve for the same dependent, which the glue was not generated against.
fn lock(checked: &Checked, out: &Path, picks: &Picks<'_>) -> Result<(), String> {
let lockfile = checked.metadata.workspace_root.join("Cargo.lock");
std::fs::copy(&lockfile, out.join("Cargo.lock"))
let written = out.join("Cargo.lock");
let earlier = std::fs::read(&written)
.ok()
.zip(std::fs::metadata(&written).and_then(|meta| meta.modified()).ok());
std::fs::copy(&lockfile, &written)
.map_err(|err| format!("copying {} into {}: {err}", lockfile.display(), out.display()))?;
let output = run_cargo(
&out.join("Cargo.toml"),
&["metadata", "--format-version", "1"],
&picks.resolving(),
)?;
// Resolved as before, the lockfile keeps its mtime, as every unchanged file of the
// export does: cargo reruns a build script when any file of its package is newer.
if let Some((bytes, mtime)) = earlier
&& std::fs::read(&written).is_ok_and(|now| now == bytes)
{
std::fs::File::options()
.write(true)
.open(&written)
.and_then(|file| file.set_modified(mtime))
.map_err(|err| format!("writing {}: {err}", written.display()))?;
}
let generated: Metadata =
serde_json::from_slice(&output).map_err(|err| format!("reading cargo metadata: {err}"))?;
// The check's resolution holds more than its build: cargo metadata enables what the
Expand Down
34 changes: 31 additions & 3 deletions ffi/src/driver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -544,11 +544,39 @@ fn at(tcx: TyCtxt<'_>, span: Span) -> String {
format!("{name}:{line}:{col}")
}

/// Write `text` to `path` unless it already holds it, so its mtime moves only on a change.
/// A changed file is written beside `path` and renamed over it, so a reader sees the old
/// content or the new, never neither or part, and a link at `path` goes and leaves what it
/// points to.
pub fn write(path: &Path, text: &str) -> Result<(), String> {
if let Some(dir) = path.parent() {
std::fs::create_dir_all(dir).map_err(|err| format!("creating {}: {err}", dir.display()))?;
if std::fs::read(path).is_ok_and(|old| old == text.as_bytes()) {
return Ok(());
}
std::fs::write(path, text).map_err(|err| format!("writing {}: {err}", path.display()))
let (Some(dir), Some(name)) = (path.parent(), path.file_name()) else {
return Err(format!("writing {}: not a file path", path.display()));
};
std::fs::create_dir_all(dir).map_err(|err| format!("creating {}: {err}", dir.display()))?;
let mut temp = std::ffi::OsString::from(".");
temp.push(name);
temp.push(".rust2-ffi");
let temp = dir.join(temp);
// A link left at the temporary path goes first, so the write cannot follow it.
match std::fs::remove_file(&temp) {
Err(err) if err.kind() != std::io::ErrorKind::NotFound => {
return Err(format!("removing {}: {err}", temp.display()));
}
_ => {}
}
std::fs::File::options()
.write(true)
.create_new(true)
.open(&temp)
.and_then(|mut file| std::io::Write::write_all(&mut file, text.as_bytes()))
.map_err(|err| format!("writing {}: {err}", temp.display()))?;
std::fs::rename(&temp, path).map_err(|err| {
let _ = std::fs::remove_file(&temp);
format!("writing {}: {err}", path.display())
})
}

/// Whether the struct, enum, or union `adt` declares a field or variant `name` under a
Expand Down
Loading