Skip to content

Commit aa5304f

Browse files
committed
Prepare for being included via crates.io into std
This commit prepares the `libc` crate to be included directly into the standard library via crates.io. More details about this can be found on rust-lang/rust#56092, but the main idea is that this crate now depends on core/compiler-builtins explicitly (but off-by-default). The main caveat here is that this activates `no_core` when building as part of libstd, which means that it needs to explicitly have an `iter` and `option` module for the expansion of `for` loops to work.
1 parent e85479b commit aa5304f

File tree

6 files changed

+29
-14
lines changed

6 files changed

+29
-14
lines changed

Cargo.toml

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
[package]
2-
32
name = "libc"
4-
version = "0.2.43"
3+
version = "0.2.44"
54
authors = ["The Rust Project Developers"]
65
license = "MIT OR Apache-2.0"
76
readme = "README.md"
@@ -19,10 +18,14 @@ exclude = ["/ci/*", "/.travis.yml", "/appveyor.yml"]
1918
travis-ci = { repository = "rust-lang/libc" }
2019
appveyor = { repository = "rust-lang/libc", project_name = "rust-lang-libs/libc" }
2120

21+
[dependencies]
22+
rustc-std-workspace-core = { version = "1.0.0", optional = true }
23+
2224
[features]
2325
default = ["use_std"]
2426
use_std = []
2527
align = []
28+
rustc-dep-of-std = ['align', 'rustc-std-workspace-core']
2629

2730
[workspace]
2831
members = ["libc-test"]

libc-test/build.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -436,7 +436,7 @@ fn main() {
436436
// which is absent in glibc, has to be defined.
437437
"__timeval" if linux => true,
438438

439-
// Fixed on stdbuild with repr(packed(4))
439+
// Fixed on feature=align with repr(packed(4))
440440
// Once repr_packed stabilizes we can fix this unconditionally
441441
// and remove this check.
442442
"kevent" | "shmid_ds" if apple && x86_64 => true,

src/lib.rs

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -108,24 +108,35 @@
108108
doc(html_root_url = "https://rust-lang.github.io/libc/sparc64-unknown-linux-gnu")
109109
)]
110110
// Attributes needed when building as part of the standard library
111-
#![cfg_attr(feature = "stdbuild", feature(staged_api, cfg_target_vendor))]
112-
#![cfg_attr(feature = "stdbuild", feature(link_cfg, repr_packed))]
113-
#![cfg_attr(feature = "stdbuild", allow(warnings))]
111+
#![cfg_attr(feature = "rustc-dep-of-std", feature(staged_api, cfg_target_vendor))]
112+
#![cfg_attr(feature = "rustc-dep-of-std", feature(link_cfg, repr_packed))]
113+
#![cfg_attr(feature = "rustc-dep-of-std", feature(no_core))]
114+
#![cfg_attr(feature = "rustc-dep-of-std", no_core)]
114115
#![cfg_attr(
115-
feature = "stdbuild",
116+
feature = "rustc-dep-of-std",
116117
unstable(
117118
feature = "libc",
118119
reason = "use `libc` from crates.io",
119120
issue = "27783"
120121
)
121122
)]
122-
#![cfg_attr(not(feature = "use_std"), no_std)]
123+
#![cfg_attr(not(any(feature = "use_std", feature = "rustc-dep-of-std")), no_std)]
124+
123125
// FIXME: this crate is empty for wasm32-unknown-unknown
124126
#![cfg(not(all(target_arch = "wasm32", not(target_os = "emscripten"))))]
125127

126128
#[cfg(all(not(cross_platform_docs), feature = "use_std"))]
127129
extern crate std as core;
128130

131+
#[cfg(feature = "rustc-dep-of-std")]
132+
extern crate rustc_std_workspace_core as core;
133+
#[cfg(feature = "rustc-dep-of-std")]
134+
#[allow(unused_imports)]
135+
use core::iter;
136+
#[cfg(feature = "rustc-dep-of-std")]
137+
#[allow(unused_imports)]
138+
use core::option;
139+
129140
#[macro_use]
130141
mod macros;
131142

src/unix/bsd/apple/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ s! {
245245
pub f_reserved: [::uint32_t; 8],
246246
}
247247

248-
#[cfg_attr(feature = "stdbuild", repr(packed(4)))]
248+
#[cfg_attr(feature = "align", repr(packed(4)))]
249249
pub struct kevent {
250250
pub ident: ::uintptr_t,
251251
pub filter: ::int16_t,
@@ -535,7 +535,7 @@ s! {
535535
pub _key: ::key_t,
536536
}
537537

538-
#[cfg_attr(feature = "stdbuild", repr(packed(4)))]
538+
#[cfg_attr(feature = "align", repr(packed(4)))]
539539
pub struct shmid_ds {
540540
pub shm_perm: ipc_perm,
541541
pub shm_segsz: ::size_t,

src/unix/mod.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -287,17 +287,18 @@ cfg_if! {
287287
// cargo build, don't pull in anything extra as the libstd dep
288288
// already pulls in all libs.
289289
} else if #[cfg(target_env = "musl")] {
290-
#[cfg_attr(feature = "stdbuild",
290+
#[cfg_attr(feature = "rustc-dep-of-std",
291291
link(name = "c", kind = "static",
292292
cfg(target_feature = "crt-static")))]
293-
#[cfg_attr(feature = "stdbuild",
293+
#[cfg_attr(feature = "rustc-dep-of-std",
294294
link(name = "c", cfg(not(target_feature = "crt-static"))))]
295295
extern {}
296296
} else if #[cfg(target_os = "emscripten")] {
297297
#[link(name = "c")]
298298
extern {}
299299
} else if #[cfg(all(target_os = "netbsd",
300-
feature = "stdbuild", target_vendor = "rumprun"))] {
300+
feature = "rustc-dep-of-std",
301+
target_vendor = "rumprun"))] {
301302
// Since we don't use -nodefaultlibs on Rumprun, libc is always pulled
302303
// in automatically by the linker. We avoid passing it explicitly, as it
303304
// causes some versions of binutils to crash with an assertion failure.

src/windows.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ pub const EILSEQ: ::c_int = 42;
159159
pub const STRUNCATE: ::c_int = 80;
160160

161161
// inline comment below appeases style checker
162-
#[cfg(all(target_env = "msvc", feature = "stdbuild"))] // " if "
162+
#[cfg(all(target_env = "msvc", feature = "rustc-dep-of-std"))] // " if "
163163
#[link(name = "msvcrt", cfg(not(target_feature = "crt-static")))]
164164
#[link(name = "libcmt", cfg(target_feature = "crt-static"))]
165165
extern {}

0 commit comments

Comments
 (0)