Skip to content

Commit

Permalink
Prepare for being included via crates.io into std
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
alexcrichton committed Nov 21, 2018
1 parent e85479b commit 753f495
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 13 deletions.
5 changes: 4 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
[package]

name = "libc"
version = "0.2.43"
authors = ["The Rust Project Developers"]
Expand All @@ -19,10 +18,14 @@ exclude = ["/ci/*", "/.travis.yml", "/appveyor.yml"]
travis-ci = { repository = "rust-lang/libc" }
appveyor = { repository = "rust-lang/libc", project_name = "rust-lang-libs/libc" }

[dependencies]
rustc-std-workspace-core = { version = "1.0.0", optional = true }

[features]
default = ["use_std"]
use_std = []
align = []
rustc-dep-of-std = ['align', 'rustc-std-workspace-core']

[workspace]
members = ["libc-test"]
2 changes: 1 addition & 1 deletion libc-test/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -436,7 +436,7 @@ fn main() {
// which is absent in glibc, has to be defined.
"__timeval" if linux => true,

// Fixed on stdbuild with repr(packed(4))
// Fixed on feature=align with repr(packed(4))
// Once repr_packed stabilizes we can fix this unconditionally
// and remove this check.
"kevent" | "shmid_ds" if apple && x86_64 => true,
Expand Down
21 changes: 16 additions & 5 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,24 +108,35 @@
doc(html_root_url = "https://rust-lang.github.io/libc/sparc64-unknown-linux-gnu")
)]
// Attributes needed when building as part of the standard library
#![cfg_attr(feature = "stdbuild", feature(staged_api, cfg_target_vendor))]
#![cfg_attr(feature = "stdbuild", feature(link_cfg, repr_packed))]
#![cfg_attr(feature = "stdbuild", allow(warnings))]
#![cfg_attr(feature = "rustc-dep-of-std", feature(staged_api, cfg_target_vendor))]
#![cfg_attr(feature = "rustc-dep-of-std", feature(link_cfg, repr_packed))]
#![cfg_attr(feature = "rustc-dep-of-std", feature(no_core))]
#![cfg_attr(feature = "rustc-dep-of-std", no_core)]
#![cfg_attr(
feature = "stdbuild",
feature = "rustc-dep-of-std",
unstable(
feature = "libc",
reason = "use `libc` from crates.io",
issue = "27783"
)
)]
#![cfg_attr(not(feature = "use_std"), no_std)]
#![cfg_attr(not(any(feature = "use_std", feature = "rustc-dep-of-std")), no_std)]

// FIXME: this crate is empty for wasm32-unknown-unknown
#![cfg(not(all(target_arch = "wasm32", not(target_os = "emscripten"))))]

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

#[cfg(feature = "rustc-dep-of-std")]
extern crate rustc_std_workspace_core as core;
#[cfg(feature = "rustc-dep-of-std")]
#[allow(unused_imports)]
use core::iter;
#[cfg(feature = "rustc-dep-of-std")]
#[allow(unused_imports)]
use core::option;

#[macro_use]
mod macros;

Expand Down
4 changes: 2 additions & 2 deletions src/unix/bsd/apple/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ s! {
pub f_reserved: [::uint32_t; 8],
}

#[cfg_attr(feature = "stdbuild", repr(packed(4)))]
#[cfg_attr(feature = "align", repr(packed(4)))]
pub struct kevent {
pub ident: ::uintptr_t,
pub filter: ::int16_t,
Expand Down Expand Up @@ -535,7 +535,7 @@ s! {
pub _key: ::key_t,
}

#[cfg_attr(feature = "stdbuild", repr(packed(4)))]
#[cfg_attr(feature = "align", repr(packed(4)))]
pub struct shmid_ds {
pub shm_perm: ipc_perm,
pub shm_segsz: ::size_t,
Expand Down
6 changes: 3 additions & 3 deletions src/unix/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -287,17 +287,17 @@ cfg_if! {
// cargo build, don't pull in anything extra as the libstd dep
// already pulls in all libs.
} else if #[cfg(target_env = "musl")] {
#[cfg_attr(feature = "stdbuild",
#[cfg_attr(feature = "rustc-dep-of-std",
link(name = "c", kind = "static",
cfg(target_feature = "crt-static")))]
#[cfg_attr(feature = "stdbuild",
#[cfg_attr(feature = "rustc-dep-of-std",
link(name = "c", cfg(not(target_feature = "crt-static"))))]
extern {}
} else if #[cfg(target_os = "emscripten")] {
#[link(name = "c")]
extern {}
} else if #[cfg(all(target_os = "netbsd",
feature = "stdbuild", target_vendor = "rumprun"))] {
feature = "rustc-dep-of-std", target_vendor = "rumprun"))] {
// Since we don't use -nodefaultlibs on Rumprun, libc is always pulled
// in automatically by the linker. We avoid passing it explicitly, as it
// causes some versions of binutils to crash with an assertion failure.
Expand Down
2 changes: 1 addition & 1 deletion src/windows.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ pub const EILSEQ: ::c_int = 42;
pub const STRUNCATE: ::c_int = 80;

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

0 comments on commit 753f495

Please sign in to comment.