Skip to content

Commit c790e73

Browse files
committed
Add compiler version check for VaListImpl support
1 parent d0a448b commit c790e73

File tree

3 files changed

+64
-22
lines changed

3 files changed

+64
-22
lines changed

esp-wifi/build.rs

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,41 @@ fn main() -> Result<(), String> {
4343
#[cfg(feature = "coex")]
4444
println!("cargo:rustc-cfg=coex");
4545

46+
let version_output = std::process::Command::new(
47+
std::env::var_os("RUSTC").unwrap_or_else(|| std::ffi::OsString::from("rustc")),
48+
)
49+
.arg("-V")
50+
.output()
51+
.unwrap()
52+
.stdout;
53+
let version_string = String::from_utf8_lossy(&version_output);
54+
55+
// HACK: we detect the xtensa-enabled compiler by existence of the second version string in parens
56+
// - upstream output format: rustc 1.75.0-nightly (cae0791da 2023-10-05)
57+
// - xtensa output format: rustc 1.73.0-nightly (9163a2087 2023-10-03) (1.73.0.0)
58+
if version_string.chars().filter(|&c| c == '(').count() == 2 {
59+
let version = version_string
60+
.split('(')
61+
.last()
62+
.unwrap()
63+
.split(')')
64+
.next()
65+
.unwrap();
66+
67+
let mut version = version.split('.');
68+
69+
let major = version.next().unwrap().parse::<u32>().unwrap();
70+
let minor = version.next().unwrap().parse::<u32>().unwrap();
71+
let patch = version.next().unwrap().parse::<u32>().unwrap();
72+
let release = version.next().unwrap().parse::<u32>().unwrap();
73+
74+
let version = Version4(major, minor, patch, release);
75+
76+
if version >= Version4(1, 73, 0, 1) {
77+
println!("cargo:rustc-cfg=xtensa_has_vaarg");
78+
}
79+
}
80+
4681
Ok(())
4782
}
4883

@@ -57,3 +92,26 @@ fn main() -> Result<(), String> {
5792
fn main() {
5893
panic!("Select a chip via it's cargo feature");
5994
}
95+
96+
use std::cmp::Ordering;
97+
98+
#[derive(Debug, Clone, Copy, PartialEq)]
99+
struct Version4(u32, u32, u32, u32);
100+
101+
impl PartialOrd for Version4 {
102+
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
103+
match self.0.partial_cmp(&other.0) {
104+
Some(Ordering::Equal) => {}
105+
ord => return ord,
106+
}
107+
match self.1.partial_cmp(&other.1) {
108+
Some(Ordering::Equal) => {}
109+
ord => return ord,
110+
}
111+
match self.2.partial_cmp(&other.2) {
112+
Some(Ordering::Equal) => {}
113+
ord => return ord,
114+
}
115+
self.3.partial_cmp(&other.3)
116+
}
117+
}

esp-wifi/src/compat/common.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -106,15 +106,15 @@ pub unsafe fn str_from_c<'a>(s: *const u8) -> &'a str {
106106

107107
pub unsafe extern "C" fn syslog(_priority: u32, format: *const u8, mut args: VaListImpl) {
108108
#[cfg(feature = "wifi-logs")]
109-
{
110-
#[cfg(target_arch = "riscv32")]
109+
cfg_if::cfg_if! {
110+
if #[cfg(any(target_arch = "riscv32", all(target_arch = "xtensa", xtensa_has_vaarg)))]
111111
{
112112
let mut buf = [0u8; 512];
113113
vsnprintf(&mut buf as *mut u8, 512, format, args);
114114
let res_str = str_from_c(&buf as *const u8);
115115
info!("{}", res_str);
116116
}
117-
#[cfg(not(target_arch = "riscv32"))]
117+
else
118118
{
119119
let res_str = str_from_c(format);
120120
info!("{}", res_str);

esp-wifi/src/wifi/os_adapter.rs

Lines changed: 3 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ use crate::{
2828
timer::yield_task,
2929
};
3030

31-
#[cfg(target_arch = "riscv32")]
3231
use crate::compat::common::syslog;
3332

3433
use super::WifiEvent;
@@ -1508,10 +1507,7 @@ pub unsafe extern "C" fn log_write(
15081507
_format: *const crate::binary::c_types::c_char,
15091508
_args: ...
15101509
) {
1511-
#[cfg(not(feature = "wifi-logs"))]
1512-
return;
1513-
1514-
#[cfg(target_arch = "riscv32")]
1510+
let _args = core::mem::transmute(_args);
15151511
syslog(_level, _format as *const u8, _args);
15161512
}
15171513

@@ -1538,20 +1534,8 @@ pub unsafe extern "C" fn log_writev(
15381534
_format: *const crate::binary::c_types::c_char,
15391535
_args: va_list,
15401536
) {
1541-
#[cfg(feature = "wifi-logs")]
1542-
{
1543-
#[cfg(target_arch = "xtensa")]
1544-
{
1545-
let s = str_from_c(_format as *const u8);
1546-
info!("{}", s);
1547-
}
1548-
1549-
#[cfg(target_arch = "riscv32")]
1550-
{
1551-
let _args = core::mem::transmute(_args);
1552-
syslog(_level, _format as *const u8, _args);
1553-
}
1554-
}
1537+
let _args = core::mem::transmute(_args);
1538+
syslog(_level, _format as *const u8, _args);
15551539
}
15561540

15571541
/****************************************************************************

0 commit comments

Comments
 (0)