Skip to content

Commit 4352a85

Browse files
committed
Auto merge of #31986 - ashleysommer:emscripten_fixes, r=alexcrichton
Fix building libstd on emscripten targets. The main cause of the problem is that libstd/os/mod.rs treats emscripten targets as an alias of linux targets, whereas liblibc treats emscripten targets as musl-compliant, so it gets a slightly different struct stat64 defined. This commit adds conditional compilation checks to use the correct timestamp format on fs metadata functions in the case of compiling to emscripten targets. This commit also depends needs https://github.com/ashleysommer/rust/commit/f1575cff2d631e977038fdba3fa3422ba5f8f2fe applied in order to successfully build libstd with emscripten target.
2 parents 388ccda + 660bbf4 commit 4352a85

File tree

5 files changed

+232
-8
lines changed

5 files changed

+232
-8
lines changed

src/libstd/os/emscripten/fs.rs

+128
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
// Copyright 2016 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
#![stable(feature = "metadata_ext", since = "1.1.0")]
12+
13+
use libc;
14+
15+
use fs::Metadata;
16+
use sys_common::AsInner;
17+
18+
#[allow(deprecated)]
19+
use os::emscripten::raw;
20+
21+
/// OS-specific extension methods for `fs::Metadata`
22+
#[stable(feature = "metadata_ext", since = "1.1.0")]
23+
pub trait MetadataExt {
24+
/// Gain a reference to the underlying `stat` structure which contains
25+
/// the raw information returned by the OS.
26+
///
27+
/// The contents of the returned `stat` are **not** consistent across
28+
/// Unix platforms. The `os::unix::fs::MetadataExt` trait contains the
29+
/// cross-Unix abstractions contained within the raw stat.
30+
#[stable(feature = "metadata_ext", since = "1.1.0")]
31+
#[rustc_deprecated(since = "1.8.0",
32+
reason = "deprecated in favor of the accessor \
33+
methods of this trait")]
34+
#[allow(deprecated)]
35+
fn as_raw_stat(&self) -> &raw::stat;
36+
37+
#[stable(feature = "metadata_ext2", since = "1.8.0")]
38+
fn st_dev(&self) -> u64;
39+
#[stable(feature = "metadata_ext2", since = "1.8.0")]
40+
fn st_ino(&self) -> u64;
41+
#[stable(feature = "metadata_ext2", since = "1.8.0")]
42+
fn st_mode(&self) -> u32;
43+
#[stable(feature = "metadata_ext2", since = "1.8.0")]
44+
fn st_nlink(&self) -> u64;
45+
#[stable(feature = "metadata_ext2", since = "1.8.0")]
46+
fn st_uid(&self) -> u32;
47+
#[stable(feature = "metadata_ext2", since = "1.8.0")]
48+
fn st_gid(&self) -> u32;
49+
#[stable(feature = "metadata_ext2", since = "1.8.0")]
50+
fn st_rdev(&self) -> u64;
51+
#[stable(feature = "metadata_ext2", since = "1.8.0")]
52+
fn st_size(&self) -> u64;
53+
#[stable(feature = "metadata_ext2", since = "1.8.0")]
54+
fn st_atime(&self) -> i64;
55+
#[stable(feature = "metadata_ext2", since = "1.8.0")]
56+
fn st_atime_nsec(&self) -> i64;
57+
#[stable(feature = "metadata_ext2", since = "1.8.0")]
58+
fn st_mtime(&self) -> i64;
59+
#[stable(feature = "metadata_ext2", since = "1.8.0")]
60+
fn st_mtime_nsec(&self) -> i64;
61+
#[stable(feature = "metadata_ext2", since = "1.8.0")]
62+
fn st_ctime(&self) -> i64;
63+
#[stable(feature = "metadata_ext2", since = "1.8.0")]
64+
fn st_ctime_nsec(&self) -> i64;
65+
#[stable(feature = "metadata_ext2", since = "1.8.0")]
66+
fn st_blksize(&self) -> u64;
67+
#[stable(feature = "metadata_ext2", since = "1.8.0")]
68+
fn st_blocks(&self) -> u64;
69+
}
70+
71+
#[stable(feature = "metadata_ext", since = "1.1.0")]
72+
impl MetadataExt for Metadata {
73+
#[allow(deprecated)]
74+
fn as_raw_stat(&self) -> &raw::stat {
75+
unsafe {
76+
&*(self.as_inner().as_inner() as *const libc::stat64
77+
as *const raw::stat)
78+
}
79+
}
80+
fn st_dev(&self) -> u64 {
81+
self.as_inner().as_inner().st_dev as u64
82+
}
83+
fn st_ino(&self) -> u64 {
84+
self.as_inner().as_inner().st_ino as u64
85+
}
86+
fn st_mode(&self) -> u32 {
87+
self.as_inner().as_inner().st_mode as u32
88+
}
89+
fn st_nlink(&self) -> u64 {
90+
self.as_inner().as_inner().st_nlink as u64
91+
}
92+
fn st_uid(&self) -> u32 {
93+
self.as_inner().as_inner().st_uid as u32
94+
}
95+
fn st_gid(&self) -> u32 {
96+
self.as_inner().as_inner().st_gid as u32
97+
}
98+
fn st_rdev(&self) -> u64 {
99+
self.as_inner().as_inner().st_rdev as u64
100+
}
101+
fn st_size(&self) -> u64 {
102+
self.as_inner().as_inner().st_size as u64
103+
}
104+
fn st_atime(&self) -> i64 {
105+
self.as_inner().as_inner().st_atime as i64
106+
}
107+
fn st_atime_nsec(&self) -> i64 {
108+
self.as_inner().as_inner().st_atime_nsec as i64
109+
}
110+
fn st_mtime(&self) -> i64 {
111+
self.as_inner().as_inner().st_mtime as i64
112+
}
113+
fn st_mtime_nsec(&self) -> i64 {
114+
self.as_inner().as_inner().st_mtime_nsec as i64
115+
}
116+
fn st_ctime(&self) -> i64 {
117+
self.as_inner().as_inner().st_ctime as i64
118+
}
119+
fn st_ctime_nsec(&self) -> i64 {
120+
self.as_inner().as_inner().st_ctime_nsec as i64
121+
}
122+
fn st_blksize(&self) -> u64 {
123+
self.as_inner().as_inner().st_blksize as u64
124+
}
125+
fn st_blocks(&self) -> u64 {
126+
self.as_inner().as_inner().st_blocks as u64
127+
}
128+
}

src/libstd/os/emscripten/mod.rs

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
//! Linux-specific definitions
12+
13+
#![stable(feature = "raw_ext", since = "1.1.0")]
14+
15+
pub mod raw;
16+
pub mod fs;

src/libstd/os/emscripten/raw.rs

+80
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
//! Emscripten-specific raw type definitions
12+
//! This is basically exactly the same as the linux definitions,
13+
//! except using the musl-specific stat64 structure in liblibc.
14+
15+
#![stable(feature = "raw_ext", since = "1.1.0")]
16+
#![rustc_deprecated(since = "1.8.0",
17+
reason = "these type aliases are no longer supported by \
18+
the standard library, the `libc` crate on \
19+
crates.io should be used instead for the correct \
20+
definitions")]
21+
#![allow(deprecated)]
22+
23+
use os::raw::{c_long, c_short, c_uint, c_ulong};
24+
25+
#[stable(feature = "raw_ext", since = "1.1.0")] pub type dev_t = u64;
26+
#[stable(feature = "raw_ext", since = "1.1.0")] pub type mode_t = u32;
27+
28+
#[unstable(feature = "pthread_t", issue = "29791")] pub type pthread_t = c_ulong;
29+
30+
#[doc(inline)]
31+
#[stable(feature = "raw_ext", since = "1.1.0")] pub type blkcnt_t = u64;
32+
#[stable(feature = "raw_ext", since = "1.1.0")] pub type blksize_t = u64;
33+
#[stable(feature = "raw_ext", since = "1.1.0")] pub type ino_t = u64;
34+
#[stable(feature = "raw_ext", since = "1.1.0")] pub type nlink_t = u64;
35+
#[stable(feature = "raw_ext", since = "1.1.0")] pub type off_t = u64;
36+
#[stable(feature = "raw_ext", since = "1.1.0")] pub type time_t = c_long;
37+
38+
#[repr(C)]
39+
#[derive(Clone)]
40+
#[stable(feature = "raw_ext", since = "1.1.0")]
41+
pub struct stat {
42+
#[stable(feature = "raw_ext", since = "1.1.0")]
43+
pub st_dev: u64,
44+
#[stable(feature = "raw_ext", since = "1.1.0")]
45+
pub __pad1: c_short,
46+
#[stable(feature = "raw_ext", since = "1.1.0")]
47+
pub __st_ino: u32,
48+
#[stable(feature = "raw_ext", since = "1.1.0")]
49+
pub st_mode: u32,
50+
#[stable(feature = "raw_ext", since = "1.1.0")]
51+
pub st_nlink: u32,
52+
#[stable(feature = "raw_ext", since = "1.1.0")]
53+
pub st_uid: u32,
54+
#[stable(feature = "raw_ext", since = "1.1.0")]
55+
pub st_gid: u32,
56+
#[stable(feature = "raw_ext", since = "1.1.0")]
57+
pub st_rdev: u64,
58+
#[stable(feature = "raw_ext", since = "1.1.0")]
59+
pub __pad2: c_uint,
60+
#[stable(feature = "raw_ext", since = "1.1.0")]
61+
pub st_size: i64,
62+
#[stable(feature = "raw_ext", since = "1.1.0")]
63+
pub st_blksize: i32,
64+
#[stable(feature = "raw_ext", since = "1.1.0")]
65+
pub st_blocks: i64,
66+
#[stable(feature = "raw_ext", since = "1.1.0")]
67+
pub st_atime: time_t,
68+
#[stable(feature = "raw_ext", since = "1.1.0")]
69+
pub st_atime_nsec: c_long,
70+
#[stable(feature = "raw_ext", since = "1.1.0")]
71+
pub st_mtime: time_t,
72+
#[stable(feature = "raw_ext", since = "1.1.0")]
73+
pub st_mtime_nsec: c_long,
74+
#[stable(feature = "raw_ext", since = "1.1.0")]
75+
pub st_ctime: time_t,
76+
#[stable(feature = "raw_ext", since = "1.1.0")]
77+
pub st_ctime_nsec: c_long,
78+
#[stable(feature = "raw_ext", since = "1.1.0")]
79+
pub st_ino: u64,
80+
}

src/libstd/os/mod.rs

+1-5
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,6 @@ pub use sys::ext as windows;
3131
#[cfg(target_os = "netbsd")] pub mod netbsd;
3232
#[cfg(target_os = "openbsd")] pub mod openbsd;
3333
#[cfg(target_os = "solaris")] pub mod solaris;
34-
35-
// Emscripten is just like linux
36-
#[cfg(target_os = "emscripten")]
37-
#[path = "linux/mod.rs"]
38-
pub mod emscripten;
34+
#[cfg(target_os = "emscripten")] pub mod emscripten;
3935

4036
pub mod raw;

src/libstd/sys/unix/fs.rs

+7-3
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,19 @@ use sys::time::SystemTime;
2525
use sys::{cvt, cvt_r};
2626
use sys_common::{AsInner, FromInner};
2727

28-
#[cfg(target_os = "linux")]
28+
#[cfg(any(target_os = "linux", target_os = "emscripten"))]
2929
use libc::{stat64, fstat64, lstat64, off64_t, ftruncate64, lseek64, dirent64, readdir64_r, open64};
3030
#[cfg(target_os = "android")]
3131
use libc::{stat as stat64, fstat as fstat64, lstat as lstat64, off64_t, ftruncate64, lseek64,
3232
dirent as dirent64, open as open64};
33-
#[cfg(not(any(target_os = "linux", target_os = "android")))]
33+
#[cfg(not(any(target_os = "linux",
34+
target_os = "emscripten",
35+
target_os = "android")))]
3436
use libc::{stat as stat64, fstat as fstat64, lstat as lstat64, off_t as off64_t,
3537
ftruncate as ftruncate64, lseek as lseek64, dirent as dirent64, open as open64};
36-
#[cfg(not(any(target_os = "linux", target_os = "solaris")))]
38+
#[cfg(not(any(target_os = "linux",
39+
target_os = "emscripten",
40+
target_os = "solaris")))]
3741
use libc::{readdir_r as readdir64_r};
3842

3943
pub struct File(FileDesc);

0 commit comments

Comments
 (0)