Skip to content

transpile: save platform-specific .rs files, too #1291

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jul 24, 2025
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
48 changes: 33 additions & 15 deletions c2rust-transpile/tests/snapshots.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use std::env::current_dir;
use std::ffi::OsString;
use std::fs;
use std::path::{Path, PathBuf};
use std::path::Path;
use std::process::Command;

use c2rust_transpile::{ReplaceMode, TranspilerConfig};
Expand Down Expand Up @@ -59,28 +58,47 @@ fn transpile(platform: Option<&str>, c_path: &Path) {
c2rust_transpile::transpile(config(), &temp_path, &[]);
let cwd = current_dir().unwrap();
let c_path = c_path.strip_prefix(&cwd).unwrap();
// The crate name can't have `.`s in it, so use the file stem.
// This is also why we set it explicitly with `--crate-name`,
// as once we add `.{platform}`, the crate name derived from
// the file name won't be valid anymore.
let crate_name = c_path.file_stem().unwrap().to_str().unwrap();
let rs_path = c_path.with_extension("rs");
// We need to move the `.rs` file to a platform-specific name
// so that they don't overwrite each other.
let rs_path = match platform {
None => rs_path,
Some(platform) => {
let platform_rs_path = rs_path.with_extension(format!("{platform}.rs"));
fs::rename(&rs_path, &platform_rs_path).unwrap();
platform_rs_path
}
};
let rs = fs::read_to_string(&rs_path).unwrap();
let debug_expr = format!("cat {}", rs_path.display());

let name = platform
.map(|platform| ["transpile", platform].join("-"))
.unwrap_or("transpile".into());

insta::assert_snapshot!(name, &rs, &debug_expr);
let snapshot_name = match platform {
None => "transpile".into(),
Some(platform) => format!("transpile-{platform}"),
};
insta::assert_snapshot!(snapshot_name, &rs, &debug_expr);

// Don't need to worry about platform clashes here, as this is immediately deleted.
let rlib_path = format!("lib{crate_name}.rlib");
let status = Command::new("rustc")
.args(&["--crate-type", "lib", "--edition", "2021"])
.args(&[
"--crate-type",
"lib",
"--edition",
"2021",
"--crate-name",
crate_name,
"-o",
&rlib_path,
])
.arg(&rs_path)
.status();
assert!(status.unwrap().success());
let rlib_path = {
let mut file_name = OsString::new();
file_name.push("lib");
file_name.push(rs_path.file_stem().unwrap());
file_name.push(".rlib");
PathBuf::from(file_name)
};
fs::remove_file(&rlib_path).unwrap();
}

Expand Down
2 changes: 0 additions & 2 deletions c2rust-transpile/tests/snapshots/platform-specific/dummy.c

This file was deleted.

9 changes: 0 additions & 9 deletions c2rust-transpile/tests/snapshots/platform-specific/dummy.rs

This file was deleted.

29 changes: 29 additions & 0 deletions c2rust-transpile/tests/snapshots/platform-specific/rnd.macos.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#![allow(
dead_code,
mutable_transmutes,
non_camel_case_types,
non_snake_case,
non_upper_case_globals,
unused_assignments,
unused_mut
)]
extern "C" {
fn abs(_: std::ffi::c_int) -> std::ffi::c_int;
}
pub type int32_t = std::ffi::c_int;
pub type uint32_t = std::ffi::c_uint;
#[no_mangle]
pub static mut cur_rand_seed: uint32_t = 0 as std::ffi::c_int as uint32_t;
#[no_mangle]
pub unsafe extern "C" fn set_rand_seed(mut s: uint32_t) {
cur_rand_seed = s;
}
#[no_mangle]
pub unsafe extern "C" fn get_rand_seed() -> uint32_t {
let INCREMENT: uint32_t = 1 as std::ffi::c_int as uint32_t;
let MULTIPLIER: uint32_t = 0x15a4e35 as std::ffi::c_int as uint32_t;
cur_rand_seed = MULTIPLIER.wrapping_mul(cur_rand_seed).wrapping_add(INCREMENT);
let mut ret: uint32_t = abs(cur_rand_seed as int32_t) as uint32_t;
return ret;
}

22 changes: 22 additions & 0 deletions c2rust-transpile/tests/snapshots/platform-specific/rotate.macos.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#![allow(
dead_code,
mutable_transmutes,
non_camel_case_types,
non_snake_case,
non_upper_case_globals,
unused_assignments,
unused_mut
)]
#[no_mangle]
pub unsafe extern "C" fn rotate_left_64(
mut x: std::ffi::c_ulonglong,
) -> std::ffi::c_ulonglong {
return x.rotate_left(4 as std::ffi::c_int as std::ffi::c_ulonglong as u32);
}
#[no_mangle]
pub unsafe extern "C" fn rotate_right_64(
mut x: std::ffi::c_ulonglong,
) -> std::ffi::c_ulonglong {
return x.rotate_right(4 as std::ffi::c_int as std::ffi::c_ulonglong as u32);
}

16 changes: 0 additions & 16 deletions c2rust-transpile/tests/snapshots/[email protected]

This file was deleted.

16 changes: 0 additions & 16 deletions c2rust-transpile/tests/snapshots/[email protected]

This file was deleted.