granite-rust/library/core/src
bors a8a88fe524 Auto merge of #122268 - ChrisDenton:no-libc, r=Mark-Simulacrum
Link MSVC default lib in core

## The Problem

On Windows MSVC, Rust invokes the linker directly. This means only the objects and libraries Rust explicitly passes to the linker are used. In short, this is equivalent to passing `-nodefaultlibs`, `-nostartfiles`, etc for gnu compilers.

To compensate for this [the libc crate links to the necessary libraries](a0f5b4b213/src/windows/mod.rs (L258-L261)). The libc crate is then linked from std, thus when you use std you get the defaults back.or integrate with C/C++.

However, this has a few problems:

- For `no_std`, users are left to manually pass the default lib to the linker
- Whereas `std` has the opposite problem, using [`/nodefaultlib`](https://learn.microsoft.com/en-us/cpp/build/reference/nodefaultlib-ignore-libraries?view=msvc-170) doesn't work as expected because Rust treats them as normal libs. This is a particular problem when you want to use e.g. the debug CRT libraries in their place or integrate with C/C++..

## The solution

This PR fixes this in two ways:

- moves linking the default lib into `core`
- passes the lib to the linker using [`/defaultlib`](https://learn.microsoft.com/en-us/cpp/build/reference/defaultlib-specify-default-library?view=msvc-170). This allows users to override it in the normal way (i.e. with [`/nodefaultlib`](https://learn.microsoft.com/en-us/cpp/build/reference/nodefaultlib-ignore-libraries?view=msvc-170)).

This is more or less equivalent to what the MSVC C compiler does. You can see what this looks like in my second commit, which I'll reproduce here for convenience:

```rust
// In library/core
#[cfg(all(windows, target_env = "msvc"))]
#[link(
    name = "/defaultlib:msvcrt",
    modifiers = "+verbatim",
    cfg(not(target_feature = "crt-static"))
)]
#[link(name = "/defaultlib:libcmt", modifiers = "+verbatim", cfg(target_feature = "crt-static"))]
extern "C" {}
```

## Alternatives

- Add the above to `unwind` and `std` but not `core`
- The status quo
- Some other kind of compiler magic maybe

This bares some discussion so I've t-libs nominated it.
2024-04-14 13:28:21 +00:00
..
alloc SeqCst->Relaxed in doc examples. 2024-03-19 15:27:11 +01:00
array Import the 2021 prelude in the core crate 2024-03-25 13:12:06 -07:00
ascii implement Default for AsciiChar 2024-02-13 12:04:44 +01:00
async_iter Hide async_gen_internals from standard library documentation 2024-04-05 18:54:38 -07:00
cell impl get_mut_or_init and get_mut_or_try_init for OnceCell and OnceLock 2024-03-27 16:16:08 +08:00
char Auto merge of #122616 - Jules-Bertholet:casemappingiter-layout, r=Nilstrieb 2024-03-29 07:02:56 +00:00
cmp Use generic NonZero everywhere in core. 2024-02-22 15:17:33 +01:00
convert Remove From impls for unstable types that break inference 2024-04-11 18:04:31 -04:00
ffi Move msvc libs to core 2024-04-14 07:11:53 +00:00
fmt Rollup merge of #123875 - Ghamza-Jd:master, r=joboet 2024-04-14 09:01:58 +02:00
future Fix typo in Future::poll() docs 2024-04-09 01:40:03 -04:00
hash Add fn const BuildHasherDefault::new 2024-03-29 17:10:17 +01:00
intrinsics rename expose_addr to expose_provenance 2024-04-03 16:00:38 +02:00
io core: panic on overflow in BorrowedCursor 2024-04-11 18:33:46 +02:00
iter Auto merge of #107462 - WaffleLapkin:from_iterator_for_tuple, r=dtolnay 2024-04-14 03:15:53 +00:00
macros Fix error message for env! when env var is not valid Unicode 2024-04-01 05:44:45 +01:00
mem Auto merge of #122582 - scottmcm:swap-intrinsic-v2, r=oli-obk 2024-03-23 13:57:55 +00:00
net Avoid invalid socket address in length calculation 2024-04-11 17:27:23 +02:00
num Add a Debug impl and some basic functions to f16 and f128 2024-04-11 15:31:10 -04:00
ops Add basic library support for f16 and f128 2024-04-10 13:50:27 -04:00
panic Rollup merge of #122291 - lilasta:stabilize_const_location_fields, r=dtolnay 2024-04-06 13:00:05 +02:00
prelude Remove RustcEncodable/Decodable from 2024 prelude 2024-03-22 13:30:48 -07:00
ptr Stabilize (const_)slice_ptr_len and (const_)slice_ptr_is_empty_nonnull 2024-04-12 21:23:20 +02:00
slice Auto merge of #122945 - andy-k:sorted-vec-example, r=jhpratt 2024-04-02 03:14:05 +00:00
str Use unchecked_sub in str indexing 2024-04-06 14:09:03 -04:00
sync Stabilize const Atomic*::into_inner 2024-04-05 16:04:07 -07:00
task set tracking issue 2024-04-02 15:45:53 -07:00
unicode Bump Unicode printables to version 15.1, align to unicode_data 2024-03-28 11:21:52 +01:00
any.rs Improve wording in std::any explanation 2024-03-29 10:10:52 -07:00
arch.rs Rust is a proper name: rust → Rust 2024-03-07 07:49:22 +01:00
ascii.rs Use generic NonZero internally. 2024-02-15 08:09:42 +01:00
asserting.rs [RFC 2011] Library code 2022-05-22 07:18:32 -03:00
bool.rs core is now compilable 2023-04-16 07:20:26 +00:00
borrow.rs doc: replace wrong punctuation mark 2023-07-28 14:46:17 +02:00
cell.rs Stabilize const Atomic*::into_inner 2024-04-05 16:04:07 -07:00
clone.rs Revert "Put basic impls for f16 and f128 behind cfg(not(bootstrap))" 2024-04-10 13:50:27 -04:00
cmp.rs Revert "Put basic impls for f16 and f128 behind cfg(not(bootstrap))" 2024-04-10 13:50:27 -04:00
default.rs Add basic trait impls for f16 and f128 2024-03-28 15:02:51 -04:00
error.md Fix minor grammar typo 2023-09-06 09:47:22 -07:00
error.rs style library/core/src/error.rs 2024-03-02 16:03:23 +08:00
escape.rs Use generic NonZero internally. 2024-02-15 08:09:42 +01:00
hint.rs move assert_unsafe_preconditions to its own file 2024-03-23 18:44:17 +01:00
internal_macros.rs add track_caller for arith ops 2023-11-24 00:54:06 +08:00
intrinsics.rs Put checks that detect UB under their own flag below debug_assertions 2024-04-06 11:21:47 -04:00
lib.miri.rs add 'x.py miri', and make it work for 'library/{core,alloc,std}' 2024-04-03 20:27:20 +02:00
lib.rs Move msvc libs to core 2024-04-14 07:11:53 +00:00
marker.rs Revert "Put basic impls for f16 and f128 behind cfg(not(bootstrap))" 2024-04-10 13:50:27 -04:00
option.rs Import the 2021 prelude in the core crate 2024-03-25 13:12:06 -07:00
panic.rs Distinguish between library and lang UB in assert_unsafe_precondition 2024-03-08 18:53:58 -05:00
panicking.rs core/panicking: fix outdated comment 2024-03-23 21:36:22 +01:00
pat.rs Add pattern types to parser 2024-04-08 11:57:17 +00:00
pin.rs fix pin.rs typo 2024-04-12 01:28:58 -05:00
primitive.rs
primitive_docs.rs Add primitive documentation for f16 and f128 2024-04-10 13:50:27 -04:00
result.rs Import the 2021 prelude in the core crate 2024-03-25 13:12:06 -07:00
time.rs Implement Duration::as_millis_{f64,f32} 2024-03-14 01:37:12 +03:00
tuple.rs Rollup merge of #118307 - scottmcm:tuple-eq-simpler, r=joshtriplett 2024-02-11 08:25:41 +01:00
ub_checks.rs refactor check_{lang,library}_ub: use a single intrinsic, put policy into library 2024-03-23 18:45:05 +01:00
unit.rs Import the 2021 prelude in the core crate 2024-03-25 13:12:06 -07:00