Skip to content

Commit 2141ae8

Browse files
committed
folder rename
1 parent e68c802 commit 2141ae8

File tree

2 files changed

+258
-0
lines changed

2 files changed

+258
-0
lines changed
Lines changed: 165 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
1+
// @ needs-clang
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(intrinsics)]
34+
#![feature(link_cfg)]
35+
#![allow(unused)]
36+
#![crate_type = "rlib"]
37+
{}
38+
#[path = "processed_mod.rs"]
39+
mod ffi;
40+
#[path = "tests.rs"]
41+
mod tests;
42+
"#,
43+
minicore_content
44+
);
45+
46+
rmake_content.push_str(&format!(
47+
"
48+
const CLANG_C_CHAR_SIZE: usize = {};
49+
const CLANG_C_CHAR_SIGNED: bool = {};
50+
const CLANG_C_SHORT_SIZE: usize = {};
51+
const CLANG_C_INT_SIZE: usize = {};
52+
const CLANG_C_LONG_SIZE: usize = {};
53+
const CLANG_C_LONGLONG_SIZE: usize = {};
54+
const CLANG_C_FLOAT_SIZE: usize = {};
55+
const CLANG_C_DOUBLE_SIZE: usize = {};
56+
",
57+
parse_size(&defines, "CHAR"),
58+
parse_signed(&defines, "CHAR"),
59+
parse_size(&defines, "SHORT"),
60+
parse_size(&defines, "INT"),
61+
parse_size(&defines, "LONG"),
62+
parse_size(&defines, "LONG_LONG"),
63+
parse_size(&defines, "FLOAT"),
64+
parse_size(&defines, "DOUBLE"),
65+
));
66+
67+
// Write to target-specific rmake file
68+
let mut file_name = format!("{}_rmake.rs", target.replace("-", "_"));
69+
70+
if target.starts_with("thumbv8m") {
71+
file_name = String::from("thumbv8m_rmake.rs");
72+
}
73+
74+
rfs::create_file(&file_name);
75+
rfs::write(&file_name, rmake_content);
76+
let rustc_output = rustc()
77+
.arg("-Zunstable-options")
78+
.arg("--emit=metadata")
79+
.arg("--target")
80+
.arg(target)
81+
.arg(&file_name)
82+
.run();
83+
rfs::remove_file(&file_name);
84+
if !rustc_output.status().success() {
85+
panic!("Failed for target {}", target);
86+
}
87+
}
88+
89+
// Cleanup
90+
rfs::remove_file("processed_mod.rs");
91+
}
92+
93+
fn get_target_list() -> String {
94+
let completed_process = rustc().arg("--print").arg("target-list").run();
95+
String::from_utf8(completed_process.stdout()).expect("error not a string")
96+
}
97+
98+
// Helper to parse size from clang defines
99+
fn parse_size(defines: &str, type_name: &str) -> usize {
100+
let search_pattern = format!("__SIZEOF_{}__ ", type_name.to_uppercase());
101+
for line in defines.lines() {
102+
if line.contains(&search_pattern) {
103+
if let Some(size_str) = line.split_whitespace().last() {
104+
return size_str.parse().unwrap_or(0);
105+
}
106+
}
107+
}
108+
109+
// Only allow CHAR to default to 1
110+
if type_name.to_uppercase() == "CHAR" {
111+
return 1;
112+
}
113+
114+
panic!("Could not find size definition for type: {}", type_name);
115+
}
116+
117+
// Helper to parse signedness from clang defines
118+
fn parse_signed(defines: &str, type_name: &str) -> bool {
119+
match type_name.to_uppercase().as_str() {
120+
"CHAR" => {
121+
// Check if char is explicitly unsigned
122+
!defines.lines().any(|line| line.contains("__CHAR_UNSIGNED__"))
123+
}
124+
_ => true,
125+
}
126+
}
127+
128+
// Parse core/ffi/mod.rs to retrieve only necessary macros and type defines
129+
fn regex_mod() {
130+
let mod_path = run_make_support::source_root().join("library/core/src/ffi/mod.rs");
131+
let mut content = rfs::read_to_string(&mod_path);
132+
133+
//remove stability features #![unstable]
134+
let mut re = regex::Regex::new(r"#!?\[(un)?stable[^]]*?\]").unwrap();
135+
content = re.replace_all(&content, "").to_string();
136+
137+
//remove doc features #[doc...]
138+
re = regex::Regex::new(r"#\[doc[^]]*?\]").unwrap();
139+
content = re.replace_all(&content, "").to_string();
140+
141+
//remove lang feature #[lang...]
142+
re = regex::Regex::new(r"#\[lang[^]]*?\]").unwrap();
143+
content = re.replace_all(&content, "").to_string();
144+
145+
//remove non inline modules
146+
re = regex::Regex::new(r".*mod.*;").unwrap();
147+
content = re.replace_all(&content, "").to_string();
148+
149+
//remove use
150+
re = regex::Regex::new(r".*use.*;").unwrap();
151+
content = re.replace_all(&content, "").to_string();
152+
153+
//remove fn fmt {...}
154+
re = regex::Regex::new(r"(?s)fn fmt.*?\{.*?\}").unwrap();
155+
content = re.replace_all(&content, "").to_string();
156+
157+
//rmv impl fmt {...}
158+
re = regex::Regex::new(r"(?s)impl fmt::Debug for.*?\{.*?\}").unwrap();
159+
content = re.replace_all(&content, "").to_string();
160+
161+
let file_name = format!("processed_mod.rs");
162+
163+
rfs::create_file(&file_name);
164+
rfs::write(&file_name, content);
165+
}
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
// tests.rs
2+
3+
use super::*; // `super` will include everything from `smallcore` once glued together
4+
5+
cfg_if! {
6+
if #[cfg(all(target_arch = "aarch64", target_abi = "ilp32"))] {
7+
// FIXME: long is not long enough on aarch64 ilp32, should be 8, defaulting to 4
8+
const XFAIL_C_LONG_SIZE: usize = 4;
9+
pub const TEST_C_LONG_SIZE: () = if size_of::<ffi::c_long>() != XFAIL_C_LONG_SIZE {
10+
panic!("wrong c_long size test ilp32");
11+
};
12+
}
13+
else {
14+
// Default test
15+
pub const TEST_C_LONG_SIZE: () = if size_of::<ffi::c_long>() != CLANG_C_LONG_SIZE {
16+
panic!("wrong c_long size");
17+
};
18+
}
19+
}
20+
21+
cfg_if! {
22+
if #[cfg(target_arch = "csky")] {
23+
// FIXME: c_char signedness misallignment on csky, should be signed on CLANG
24+
const XFAIL_C_CHAR_SIGNED: bool = false;
25+
pub const TEST_C_CHAR_UNSIGNED: () = if ffi::c_char::SIGNED ^ XFAIL_C_CHAR_SIGNED {
26+
panic!("mismatched c_char signed, target_arch: csky");
27+
};
28+
}
29+
else if #[cfg(target_arch = "msp430")] {
30+
// FIXME: c_char signedness misallignment on msp430, should be signed on CLANG
31+
const XFAIL_C_CHAR_SIGNED: bool = false; // Change to true for darwin
32+
pub const TEST_C_CHAR_UNSIGNED: () = if ffi::c_char::SIGNED ^ XFAIL_C_CHAR_SIGNED {
33+
panic!("mismatched c_char signed, target_arch: msp430");
34+
};
35+
}
36+
else {
37+
pub const TEST_C_CHAR_UNSIGNED: () = if ffi::c_char::SIGNED ^ CLANG_C_CHAR_SIGNED {
38+
panic!("mismatched c_char sign");
39+
};
40+
}
41+
}
42+
43+
cfg_if! {
44+
if #[cfg(target_arch = "avr")] {
45+
// FIXME: double is not short enough on avr-unknown-gnu-atmega328 (should be 4 bytes)
46+
const XFAIL_C_DOUBLE_SIZE: usize = 8;
47+
pub const TEST_C_DOUBLE_SIZE: () = if size_of::<ffi::c_double>() != XFAIL_C_DOUBLE_SIZE {
48+
panic!("wrong c_double size, target_arch: avr");
49+
};
50+
}
51+
else {
52+
pub const TEST_C_DOUBLE_SIZE: () = if size_of::<ffi::c_double>() != CLANG_C_DOUBLE_SIZE {
53+
panic!("wrong c_double size");
54+
};
55+
}
56+
}
57+
58+
trait Signed {
59+
const SIGNED: bool;
60+
}
61+
62+
impl Signed for i8 {
63+
const SIGNED: bool = true;
64+
}
65+
66+
impl Signed for u8 {
67+
const SIGNED: bool = false;
68+
}
69+
70+
//c_char size
71+
pub const TEST_C_CHAR_SIZE: () = if size_of::<ffi::c_char>() != CLANG_C_CHAR_SIZE {
72+
panic!("wrong c_char size");
73+
};
74+
75+
//c_int size
76+
pub const TEST_C_INT_SIZE: () = if size_of::<ffi::c_int>() != CLANG_C_INT_SIZE {
77+
panic!("mismatched c_int size");
78+
};
79+
80+
//c_short size
81+
pub const TEST_C_SHORT_SIZE: () = if size_of::<ffi::c_short>() != CLANG_C_SHORT_SIZE {
82+
panic!("wrong c_short size");
83+
};
84+
85+
//c_longlong size
86+
pub const TEST_C_LONGLONG_SIZE: () = if size_of::<ffi::c_longlong>() != CLANG_C_LONGLONG_SIZE {
87+
panic!("wrong c_longlong size");
88+
};
89+
90+
//c_float size
91+
pub const TEST_C_FLOAT_SIZE: () = if size_of::<ffi::c_float>() != CLANG_C_FLOAT_SIZE {
92+
panic!("wrong c_float size");
93+
};

0 commit comments

Comments
 (0)