Skip to content
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

extern-types: Generate replacement types dynamically #27

Merged
merged 1 commit into from
Mar 16, 2023
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
31 changes: 23 additions & 8 deletions build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -447,14 +447,29 @@ fn main() {

println!("cargo:rerun-if-env-changed=CARGO_FEATURE_KEEP_EXTERN_TYPES");
if env::var("CARGO_FEATURE_KEEP_EXTERN_TYPES").is_err() {
// There's only one `pub type` usually, and that breaks use on stable, and src/inline.rs has a
// workaround for that
rustcode = rustcode.replace("\n pub type __locale_t;", "");
rustcode = rustcode.replace("\n pub type _IO_wide_data;", "");
rustcode = rustcode.replace("\n pub type _IO_codecvt;", "");
rustcode = rustcode.replace("\n pub type _IO_marker;", "");
rustcode = rustcode.replace("\n pub type __lock;", "");
rustcode = rustcode.replace("\n pub type netq_t;", "");
// For documentation on why we do this, see include in src/inline.rs.

let pubtypepattern = regex::Regex::new("pub type (?P<type>[a-zA-Z0-9_]+);").unwrap();
let pubtypes = pubtypepattern
.captures_iter(&rustcode)
.map(|m| m.name("type").unwrap().as_str());

let pubtype_replacements = out_path.join("pubtype_replacements.rs");
let mut pubtype_replacements_file = std::fs::File::create(pubtype_replacements)
.expect("Failed to create pubtype_replacements.rs");

for pt in pubtypes {
writeln!(
pubtype_replacements_file,
"pub type {} = [u8; isize::MAX as _];",
pt
)
.expect("Failed to write to pubtype_replacements.rs");
}

rustcode = pubtypepattern
.replace_all(&rustcode, "/* $0 */")
.to_string();
}

// Replace the function declarations with ... usually something pub, but special considerations
Expand Down
12 changes: 1 addition & 11 deletions src/inline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,16 +98,6 @@ use c2rust_bitfields::*;
// The type is hugely sized to ensure that things crash (or preferably don't build) if at any point
// Rust code tries to touch an instance of it, eg. by allocating one on the stack or statically.
#[cfg(not(feature = "keep-extern-types"))]
pub type __locale_t = [u8; isize::MAX as _];
#[cfg(not(feature = "keep-extern-types"))]
pub type _IO_wide_data = [u8; isize::MAX as _];
#[cfg(not(feature = "keep-extern-types"))]
pub type _IO_codecvt = [u8; isize::MAX as _];
#[cfg(not(feature = "keep-extern-types"))]
pub type _IO_marker = [u8; isize::MAX as _];
#[cfg(not(feature = "keep-extern-types"))]
pub type __lock = [u8; isize::MAX as _];
#[cfg(not(feature = "keep-extern-types"))]
pub type netq_t = [u8; isize::MAX as _];
include!(concat!(env!("OUT_DIR"), "/pubtype_replacements.rs"));

include!(concat!(env!("OUT_DIR"), "/riot_c2rust_replaced.rs"));