Skip to content

Commit

Permalink
Fix ABI compatibility with Emscripten >= 3.1.42
Browse files Browse the repository at this point in the history
  • Loading branch information
kleisauke committed Jun 25, 2023
1 parent 1e8943d commit 63b0d67
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 0 deletions.
40 changes: 40 additions & 0 deletions build.rs
Expand Up @@ -7,6 +7,7 @@ use std::string::String;
// need to know all the possible cfgs that this script will set. If you need to set another cfg
// make sure to add it to this list as well.
const ALLOWED_CFGS: &'static [&'static str] = &[
"emscripten_new_stat_abi",
"freebsd10",
"freebsd11",
"freebsd12",
Expand Down Expand Up @@ -69,6 +70,18 @@ fn main() {
Some(_) | None => set_cfg("freebsd11"),
}

match emcc_version() {
Some((major, minor, patch))
if (major > 3)
|| (major == 3 && minor > 1)
|| (major == 3 && minor == 1 && patch >= 42) =>
{
set_cfg("emscripten_new_stat_abi")
}
// Non-Emscripten or version < 3.1.42.
Some(_) | None => (),
}

// On CI: deny all warnings
if libc_ci {
set_cfg("libc_deny_warnings");
Expand Down Expand Up @@ -238,6 +251,33 @@ fn which_freebsd() -> Option<i32> {
}
}

fn emcc_version() -> Option<(u32, u32, u32)> {
let output = std::process::Command::new("emcc")
.arg("-dumpversion")
.output()
.ok();
if output.is_none() {
return None;
}
let output = output.unwrap();
if !output.status.success() {
return None;
}

let stdout = String::from_utf8(output.stdout).ok();
if stdout.is_none() {
return None;
}
let version = stdout.unwrap();
let mut pieces = version.trim().split('.');

let major = pieces.next()?.parse().unwrap();
let minor = pieces.next()?.parse().unwrap();
let patch = pieces.next()?.parse().unwrap();

Some((major, minor, patch))
}

fn set_cfg(cfg: &str) {
if !ALLOWED_CFGS.contains(&cfg) {
panic!("trying to set cfg {}, but it is not in ALLOWED_CFGS", cfg);
Expand Down
6 changes: 6 additions & 0 deletions src/unix/linux_like/emscripten/mod.rs
Expand Up @@ -260,13 +260,16 @@ s! {
}
pub struct stat {
pub st_dev: ::dev_t,
#[cfg(not(emscripten_new_stat_abi))]
__st_dev_padding: ::c_int,
#[cfg(not(emscripten_new_stat_abi))]
__st_ino_truncated: ::c_long,
pub st_mode: ::mode_t,
pub st_nlink: ::nlink_t,
pub st_uid: ::uid_t,
pub st_gid: ::gid_t,
pub st_rdev: ::dev_t,
#[cfg(not(emscripten_new_stat_abi))]
__st_rdev_padding: ::c_int,
pub st_size: ::off_t,
pub st_blksize: ::blksize_t,
Expand All @@ -282,13 +285,16 @@ s! {

pub struct stat64 {
pub st_dev: ::dev_t,
#[cfg(not(emscripten_new_stat_abi))]
__st_dev_padding: ::c_int,
#[cfg(not(emscripten_new_stat_abi))]
__st_ino_truncated: ::c_long,
pub st_mode: ::mode_t,
pub st_nlink: ::nlink_t,
pub st_uid: ::uid_t,
pub st_gid: ::gid_t,
pub st_rdev: ::dev_t,
#[cfg(not(emscripten_new_stat_abi))]
__st_rdev_padding: ::c_int,
pub st_size: ::off_t,
pub st_blksize: ::blksize_t,
Expand Down

0 comments on commit 63b0d67

Please sign in to comment.