Skip to content

Commit

Permalink
extern-types: Generate replacement types dynamically
Browse files Browse the repository at this point in the history
Merges: #27
  • Loading branch information
chrysn authored Mar 16, 2023
2 parents 404128d + db3ca0a commit 27f6865
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 19 deletions.
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"));

0 comments on commit 27f6865

Please sign in to comment.