|
| 1 | +//@ needs-force-clang-based-tests |
| 2 | + |
| 3 | +use run_make_support::{clang, regex, rfs, rustc}; |
| 4 | + |
| 5 | +const SKIPPED_TARGETS: &[&str] = &[ |
| 6 | + "riscv", //error: unknown target triple 'riscv32e-unknown-none-elf' |
| 7 | + "wasm", //error: unknown target triple 'wasm32v1-none' |
| 8 | + "xtensa", //error: unknown target triple 'xtensa-esp32-espidf' |
| 9 | +]; |
| 10 | + |
| 11 | +fn main() { |
| 12 | + let targets = get_target_list(); |
| 13 | + |
| 14 | + let minicore_path = run_make_support::source_root().join("tests/auxiliary/minicore.rs"); |
| 15 | + |
| 16 | + regex_mod(); |
| 17 | + |
| 18 | + for target in targets.lines() { |
| 19 | + if SKIPPED_TARGETS.iter().any(|prefix| target.starts_with(prefix)) { |
| 20 | + continue; |
| 21 | + } |
| 22 | + |
| 23 | + let clang_output = |
| 24 | + clang().args(&["-E", "-dM", "-x", "c", "/dev/null", "-target", target]).run(); |
| 25 | + |
| 26 | + let defines = String::from_utf8(clang_output.stdout()).expect("Invalid UTF-8"); |
| 27 | + |
| 28 | + let minicore_content = rfs::read_to_string(&minicore_path); |
| 29 | + let mut rmake_content = format!( |
| 30 | + r#" |
| 31 | + #![no_std] |
| 32 | + #![no_core] |
| 33 | + #![feature(link_cfg)] |
| 34 | + #![allow(unused)] |
| 35 | + #![crate_type = "rlib"] |
| 36 | + {} |
| 37 | + #[path = "processed_mod.rs"] |
| 38 | + mod ffi; |
| 39 | + #[path = "tests.rs"] |
| 40 | + mod tests; |
| 41 | + "#, |
| 42 | + minicore_content |
| 43 | + ); |
| 44 | + |
| 45 | + rmake_content.push_str(&format!( |
| 46 | + " |
| 47 | + const CLANG_C_CHAR_SIZE: usize = {}; |
| 48 | + const CLANG_C_CHAR_SIGNED: bool = {}; |
| 49 | + const CLANG_C_SHORT_SIZE: usize = {}; |
| 50 | + const CLANG_C_INT_SIZE: usize = {}; |
| 51 | + const CLANG_C_LONG_SIZE: usize = {}; |
| 52 | + const CLANG_C_LONGLONG_SIZE: usize = {}; |
| 53 | + const CLANG_C_FLOAT_SIZE: usize = {}; |
| 54 | + const CLANG_C_DOUBLE_SIZE: usize = {}; |
| 55 | + ", |
| 56 | + parse_size(&defines, "CHAR"), |
| 57 | + parse_signed(&defines, "CHAR"), |
| 58 | + parse_size(&defines, "SHORT"), |
| 59 | + parse_size(&defines, "INT"), |
| 60 | + parse_size(&defines, "LONG"), |
| 61 | + parse_size(&defines, "LONG_LONG"), |
| 62 | + parse_size(&defines, "FLOAT"), |
| 63 | + parse_size(&defines, "DOUBLE"), |
| 64 | + )); |
| 65 | + |
| 66 | + // Write to target-specific rmake file |
| 67 | + let mut file_name = format!("{}_rmake.rs", target.replace("-", "_")); |
| 68 | + |
| 69 | + if target.starts_with("thumbv8m") { |
| 70 | + file_name = String::from("thumbv8m_rmake.rs"); |
| 71 | + } |
| 72 | + |
| 73 | + rfs::create_file(&file_name); |
| 74 | + rfs::write(&file_name, rmake_content); |
| 75 | + let rustc_output = rustc() |
| 76 | + .arg("-Zunstable-options") |
| 77 | + .arg("--emit=metadata") |
| 78 | + .arg("--target") |
| 79 | + .arg(target) |
| 80 | + .arg(&file_name) |
| 81 | + .run(); |
| 82 | + rfs::remove_file(&file_name); |
| 83 | + if !rustc_output.status().success() { |
| 84 | + panic!("Failed for target {}", target); |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + // Cleanup |
| 89 | + rfs::remove_file("processed_mod.rs"); |
| 90 | +} |
| 91 | + |
| 92 | +fn get_target_list() -> String { |
| 93 | + let completed_process = rustc().arg("--print").arg("target-list").run(); |
| 94 | + String::from_utf8(completed_process.stdout()).expect("error not a string") |
| 95 | +} |
| 96 | + |
| 97 | +// Helper to parse size from clang defines |
| 98 | +fn parse_size(defines: &str, type_name: &str) -> usize { |
| 99 | + let search_pattern = format!("__SIZEOF_{}__ ", type_name.to_uppercase()); |
| 100 | + for line in defines.lines() { |
| 101 | + if line.contains(&search_pattern) { |
| 102 | + if let Some(size_str) = line.split_whitespace().last() { |
| 103 | + return size_str.parse().unwrap_or(0); |
| 104 | + } |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + // Only allow CHAR to default to 1 |
| 109 | + if type_name.to_uppercase() == "CHAR" { |
| 110 | + return 1; |
| 111 | + } |
| 112 | + |
| 113 | + panic!("Could not find size definition for type: {}", type_name); |
| 114 | +} |
| 115 | + |
| 116 | +// Helper to parse signedness from clang defines |
| 117 | +fn parse_signed(defines: &str, type_name: &str) -> bool { |
| 118 | + match type_name.to_uppercase().as_str() { |
| 119 | + "CHAR" => { |
| 120 | + // Check if char is explicitly unsigned |
| 121 | + !defines.lines().any(|line| line.contains("__CHAR_UNSIGNED__")) |
| 122 | + } |
| 123 | + _ => true, |
| 124 | + } |
| 125 | +} |
| 126 | + |
| 127 | +// Parse core/ffi/mod.rs to retrieve only necessary macros and type defines |
| 128 | +fn regex_mod() { |
| 129 | + let mod_path = run_make_support::source_root().join("library/core/src/ffi/mod.rs"); |
| 130 | + let mut content = rfs::read_to_string(&mod_path); |
| 131 | + |
| 132 | + //remove stability features #![unstable] |
| 133 | + let mut re = regex::Regex::new(r"#!?\[(un)?stable[^]]*?\]").unwrap(); |
| 134 | + content = re.replace_all(&content, "").to_string(); |
| 135 | + |
| 136 | + //remove doc features #[doc...] |
| 137 | + re = regex::Regex::new(r"#\[doc[^]]*?\]").unwrap(); |
| 138 | + content = re.replace_all(&content, "").to_string(); |
| 139 | + |
| 140 | + //remove lang feature #[lang...] |
| 141 | + re = regex::Regex::new(r"#\[lang[^]]*?\]").unwrap(); |
| 142 | + content = re.replace_all(&content, "").to_string(); |
| 143 | + |
| 144 | + //remove non inline modules |
| 145 | + re = regex::Regex::new(r".*mod.*;").unwrap(); |
| 146 | + content = re.replace_all(&content, "").to_string(); |
| 147 | + |
| 148 | + //remove use |
| 149 | + re = regex::Regex::new(r".*use.*;").unwrap(); |
| 150 | + content = re.replace_all(&content, "").to_string(); |
| 151 | + |
| 152 | + //remove fn fmt {...} |
| 153 | + re = regex::Regex::new(r"(?s)fn fmt.*?\{.*?\}").unwrap(); |
| 154 | + content = re.replace_all(&content, "").to_string(); |
| 155 | + |
| 156 | + //rmv impl fmt {...} |
| 157 | + re = regex::Regex::new(r"(?s)impl fmt::Debug for.*?\{.*?\}").unwrap(); |
| 158 | + content = re.replace_all(&content, "").to_string(); |
| 159 | + |
| 160 | + let file_name = format!("processed_mod.rs"); |
| 161 | + |
| 162 | + rfs::create_file(&file_name); |
| 163 | + rfs::write(&file_name, content); |
| 164 | +} |
0 commit comments