Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 8903de3

Browse files
authoredMay 20, 2024
Rollup merge of #124050 - saethlin:less-sysroot-libc, r=ChrisDenton
Remove libc from MSVC targets ``@ChrisDenton`` started working on a project to remove libc from Windows MSVC targets. I'm completing that work here. The primary change is to cfg out the dependency in `library/`. And then there's a lot of test patching. Happy to separate this more if people want.
2 parents b92758a + 39ef149 commit 8903de3

30 files changed

+108
-96
lines changed
 

‎library/std/Cargo.toml

-3
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,6 @@ addr2line = { version = "0.21.0", optional = true, default-features = false }
3333
[target.'cfg(not(all(windows, target_env = "msvc")))'.dependencies]
3434
libc = { version = "0.2.153", default-features = false, features = ['rustc-dep-of-std'], public = true }
3535

36-
[target.'cfg(all(windows, target_env = "msvc"))'.dependencies]
37-
libc = { version = "0.2.153", default-features = false }
38-
3936
[target.'cfg(all(not(target_os = "aix"), not(all(windows, target_env = "msvc", not(target_vendor = "uwp")))))'.dependencies]
4037
object = { version = "0.32.0", default-features = false, optional = true, features = ['read_core', 'elf', 'macho', 'pe', 'unaligned', 'archive'] }
4138

‎library/std/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -435,6 +435,7 @@ extern crate alloc as alloc_crate;
435435
// so include it here even if it's unused.
436436
#[doc(masked)]
437437
#[allow(unused_extern_crates)]
438+
#[cfg(not(all(windows, target_env = "msvc")))]
438439
extern crate libc;
439440

440441
// We always need an unwinder currently for backtraces

‎library/std/src/os/raw/tests.rs

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#![cfg(not(all(windows, target_env = "msvc")))]
2+
13
use crate::any::TypeId;
24

35
macro_rules! ok {

‎tests/incremental/foreign.rs

+5-22
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,21 @@
11
// Test what happens we save incremental compilation state that makes
22
// use of foreign items. This used to ICE (#34991).
3-
//@ ignore-sgx no libc
4-
53
//@ revisions: rpass1
64

7-
#![feature(rustc_private)]
8-
9-
extern crate libc;
10-
115
use std::ffi::CString;
126

137
mod mlibc {
14-
use libc::{c_char, c_long, c_longlong};
15-
168
extern "C" {
17-
pub fn atol(x: *const c_char) -> c_long;
18-
pub fn atoll(x: *const c_char) -> c_longlong;
9+
// strlen is provided either by an external library or compiler-builtins as a fallback
10+
pub fn strlen(x: *const std::ffi::c_char) -> usize;
1911
}
2012
}
2113

22-
fn atol(s: String) -> isize {
23-
let c = CString::new(s).unwrap();
24-
unsafe { mlibc::atol(c.as_ptr()) as isize }
25-
}
26-
27-
fn atoll(s: String) -> i64 {
14+
fn strlen(s: String) -> usize {
2815
let c = CString::new(s).unwrap();
29-
unsafe { mlibc::atoll(c.as_ptr()) as i64 }
16+
unsafe { mlibc::strlen(c.as_ptr()) }
3017
}
3118

3219
pub fn main() {
33-
assert_eq!(atol("1024".to_string()) * 10, atol("10240".to_string()));
34-
assert_eq!(
35-
(atoll("11111111111111111".to_string()) * 10),
36-
atoll("111111111111111110".to_string())
37-
);
20+
assert_eq!(strlen("1024".to_string()), strlen("2048".to_string()));
3821
}

‎tests/run-make/c-link-to-rust-va-list-fn/checkrust.rs

+1-4
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
#![crate_type = "staticlib"]
22
#![feature(c_variadic)]
3-
#![feature(rustc_private)]
43

5-
extern crate libc;
6-
7-
use libc::{c_char, c_double, c_int, c_long, c_longlong};
4+
use std::ffi::{c_char, c_double, c_int, c_long, c_longlong};
85
use std::ffi::VaList;
96
use std::ffi::{CString, CStr};
107

‎tests/run-make/link-path-order/main.rs

+2-4
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
1-
#![feature(rustc_private)]
2-
3-
extern crate libc;
1+
use std::ffi::c_int;
42

53
#[link(name = "foo", kind = "static")]
64
extern "C" {
7-
fn should_return_one() -> libc::c_int;
5+
fn should_return_one() -> c_int;
86
}
97

108
fn main() {

‎tests/ui/env-null-vars.rs

+4-10
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,15 @@
1+
// Ensure that env::vars() does not panic if environ is null.
2+
// Regression test for rust-lang/rust#53200
13
//@ run-pass
24

3-
#![allow(unused_imports)]
4-
5-
//@ ignore-windows
6-
7-
// issue-53200
8-
95
#![feature(rustc_private)]
10-
extern crate libc;
11-
12-
use std::env;
136

147
// FIXME: more platforms?
158
#[cfg(target_os = "linux")]
169
fn main() {
10+
extern crate libc;
1711
unsafe { libc::clearenv(); }
18-
assert_eq!(env::vars().count(), 0);
12+
assert_eq!(std::env::vars().count(), 0);
1913
}
2014

2115
#[cfg(not(target_os = "linux"))]

‎tests/ui/error-codes/E0259.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
#![feature(rustc_private)]
1+
#![feature(test)]
22

33
extern crate alloc;
44

5-
extern crate libc as alloc;
5+
extern crate test as alloc;
66
//~^ ERROR E0259
77

88
fn main() {}

‎tests/ui/error-codes/E0259.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@ error[E0259]: the name `alloc` is defined multiple times
44
LL | extern crate alloc;
55
| ------------------- previous import of the extern crate `alloc` here
66
LL |
7-
LL | extern crate libc as alloc;
7+
LL | extern crate test as alloc;
88
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ `alloc` reimported here
99
|
1010
= note: `alloc` must be defined only once in the type namespace of this module
1111
help: you can use `as` to change the binding name of the import
1212
|
13-
LL | extern crate libc as other_alloc;
13+
LL | extern crate test as other_alloc;
1414
|
1515

1616
error: aborting due to 1 previous error
+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// gate-test-rustc_private
22

3-
extern crate libc; //~ ERROR use of unstable library feature 'rustc_private'
3+
extern crate cfg_if; //~ ERROR use of unstable library feature 'rustc_private'
44

55
fn main() {}

‎tests/ui/feature-gates/rustc-private.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
error[E0658]: use of unstable library feature 'rustc_private': this crate is being loaded from the sysroot, an unstable location; did you mean to load this crate from crates.io via `Cargo.toml` instead?
22
--> $DIR/rustc-private.rs:3:1
33
|
4-
LL | extern crate libc;
5-
| ^^^^^^^^^^^^^^^^^^
4+
LL | extern crate cfg_if;
5+
| ^^^^^^^^^^^^^^^^^^^^
66
|
77
= note: see issue #27812 <https://github.com/rust-lang/rust/issues/27812> for more information
88
= help: add `#![feature(rustc_private)]` to the crate attributes to enable

‎tests/ui/foreign/foreign-fn-linkname.rs

+2-8
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,14 @@
11
//@ run-pass
22
//@ ignore-sgx no libc
33

4-
// Ensure no false positive on "unused extern crate" lint
5-
#![deny(unused_extern_crates)]
6-
7-
#![feature(rustc_private)]
8-
9-
extern crate libc;
104
use std::ffi::CString;
115

126
mod mlibc {
13-
use libc::{c_char, size_t};
7+
use std::ffi::c_char;
148

159
extern "C" {
1610
#[link_name = "strlen"]
17-
pub fn my_strlen(str: *const c_char) -> size_t;
11+
pub fn my_strlen(str: *const c_char) -> usize;
1812
}
1913
}
2014

‎tests/ui/foreign/foreign2.rs

+26-4
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
//@ run-pass
2-
#![allow(dead_code)]
32
//@ pretty-expanded FIXME #23616
4-
#![feature(rustc_private)]
53

6-
extern crate libc;
4+
#![allow(dead_code)]
5+
#![feature(rustc_private)]
76

87
mod bar {
98
extern "C" {}
@@ -13,14 +12,37 @@ mod zed {
1312
extern "C" {}
1413
}
1514

15+
#[cfg(not(windows))]
1616
mod mlibc {
17-
use libc::{c_int, c_void, size_t, ssize_t};
17+
extern crate libc;
18+
use self::libc::{c_int, c_void, size_t, ssize_t};
1819

1920
extern "C" {
2021
pub fn write(fd: c_int, buf: *const c_void, count: size_t) -> ssize_t;
2122
}
2223
}
2324

25+
#[cfg(windows)]
26+
mod mlibc {
27+
#![allow(non_snake_case)]
28+
29+
use std::ffi::c_void;
30+
31+
pub type BOOL = i32;
32+
pub type HANDLE = *mut c_void;
33+
34+
#[link(name = "ntdll")]
35+
extern "system" {
36+
pub fn WriteFile(
37+
hfile: HANDLE,
38+
lpbuffer: *const u8,
39+
nnumberofbytestowrite: u32,
40+
lpnumberofbyteswritten: *mut u32,
41+
lpoverlapped: *mut c_void,
42+
) -> BOOL;
43+
}
44+
}
45+
2446
mod baz {
2547
extern "C" {}
2648
}

‎tests/ui/imports/issue-37887.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
fn main() {
2-
extern crate libc; //~ ERROR use of unstable
3-
use libc::*; //~ ERROR unresolved import
2+
extern crate test; //~ ERROR use of unstable
3+
use test::*; //~ ERROR unresolved import
44
}

‎tests/ui/imports/issue-37887.stderr

+8-8
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
1-
error[E0432]: unresolved import `libc`
1+
error[E0432]: unresolved import `test`
22
--> $DIR/issue-37887.rs:3:9
33
|
4-
LL | use libc::*;
5-
| ^^^^ maybe a missing crate `libc`?
4+
LL | use test::*;
5+
| ^^^^ maybe a missing crate `test`?
66
|
7-
= help: consider adding `extern crate libc` to use the `libc` crate
7+
= help: consider adding `extern crate test` to use the `test` crate
88

9-
error[E0658]: use of unstable library feature 'rustc_private': this crate is being loaded from the sysroot, an unstable location; did you mean to load this crate from crates.io via `Cargo.toml` instead?
9+
error[E0658]: use of unstable library feature 'test'
1010
--> $DIR/issue-37887.rs:2:5
1111
|
12-
LL | extern crate libc;
12+
LL | extern crate test;
1313
| ^^^^^^^^^^^^^^^^^^
1414
|
15-
= note: see issue #27812 <https://github.com/rust-lang/rust/issues/27812> for more information
16-
= help: add `#![feature(rustc_private)]` to the crate attributes to enable
15+
= note: see issue #50297 <https://github.com/rust-lang/rust/issues/50297> for more information
16+
= help: add `#![feature(test)]` to the crate attributes to enable
1717
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
1818

1919
error: aborting due to 2 previous errors

‎tests/ui/lint/unnecessary-extern-crate.rs

+7-7
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
//@ edition:2018
22

33
#![deny(unused_extern_crates)]
4-
#![feature(test, rustc_private)]
4+
#![feature(test)]
55

6-
extern crate libc;
6+
extern crate core;
77
//~^ ERROR unused extern crate
88
//~| HELP remove
9-
extern crate libc as x;
9+
extern crate core as x;
1010
//~^ ERROR unused extern crate
1111
//~| HELP remove
1212

@@ -28,11 +28,11 @@ mod foo {
2828

2929
pub(super) extern crate alloc as d;
3030

31-
extern crate libc;
31+
extern crate core;
3232
//~^ ERROR unused extern crate
3333
//~| HELP remove
3434

35-
extern crate libc as x;
35+
extern crate core as x;
3636
//~^ ERROR unused extern crate
3737
//~| HELP remove
3838

@@ -41,11 +41,11 @@ mod foo {
4141
pub extern crate test as y;
4242

4343
mod bar {
44-
extern crate libc;
44+
extern crate core;
4545
//~^ ERROR unused extern crate
4646
//~| HELP remove
4747

48-
extern crate libc as x;
48+
extern crate core as x;
4949
//~^ ERROR unused extern crate
5050
//~| HELP remove
5151

‎tests/ui/lint/unnecessary-extern-crate.stderr

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
error: unused extern crate
22
--> $DIR/unnecessary-extern-crate.rs:6:1
33
|
4-
LL | extern crate libc;
4+
LL | extern crate core;
55
| ^^^^^^^^^^^^^^^^^^ help: remove it
66
|
77
note: the lint level is defined here
@@ -13,31 +13,31 @@ LL | #![deny(unused_extern_crates)]
1313
error: unused extern crate
1414
--> $DIR/unnecessary-extern-crate.rs:9:1
1515
|
16-
LL | extern crate libc as x;
16+
LL | extern crate core as x;
1717
| ^^^^^^^^^^^^^^^^^^^^^^^ help: remove it
1818

1919
error: unused extern crate
2020
--> $DIR/unnecessary-extern-crate.rs:31:5
2121
|
22-
LL | extern crate libc;
22+
LL | extern crate core;
2323
| ^^^^^^^^^^^^^^^^^^ help: remove it
2424

2525
error: unused extern crate
2626
--> $DIR/unnecessary-extern-crate.rs:35:5
2727
|
28-
LL | extern crate libc as x;
28+
LL | extern crate core as x;
2929
| ^^^^^^^^^^^^^^^^^^^^^^^ help: remove it
3030

3131
error: unused extern crate
3232
--> $DIR/unnecessary-extern-crate.rs:44:9
3333
|
34-
LL | extern crate libc;
34+
LL | extern crate core;
3535
| ^^^^^^^^^^^^^^^^^^ help: remove it
3636

3737
error: unused extern crate
3838
--> $DIR/unnecessary-extern-crate.rs:48:9
3939
|
40-
LL | extern crate libc as x;
40+
LL | extern crate core as x;
4141
| ^^^^^^^^^^^^^^^^^^^^^^^ help: remove it
4242

4343
error: aborting due to 6 previous errors

‎tests/ui/meta/no_std-extern-libc.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// Test that `download-rustc` doesn't put duplicate copies of libc in the sysroot.
22
//@ check-pass
3+
//@ ignore-windows doesn't necessarily have the libc crate
34
#![crate_type = "lib"]
45
#![no_std]
56
#![feature(rustc_private)]
There was a problem loading the remainder of the diff.

0 commit comments

Comments
 (0)
Failed to load comments.