1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
//! Statically link the VCRuntime while dynamically linking the UCRT.
//! This only applies when using the MSVC toolchain.
//!
//! By default, Rust requires programs to deploy `vcruntime140.dll`
//! (or equivalent) when redistributing binaries.
//! You can use the `-C target-feature=+crt-static` rustc flag to statically link it
//! but that also statically links the Universal CRT.
//! The Universal CRT is a component of Windows so can always be dynamically linked.
//!
//! See [Details] for more information.
//!
//! # Usage
//!
//! Add this to your `Cargo.toml`:
//!
//! ```toml
//! [build-dependencies]
//! static_vcruntime = "3.0"
//! ```
//!
//! And then in your [build script] you can call [`static_vcruntime::metabuild`] like so:
//!
//! ```rust,ignore
//! fn main() {
//! static_vcruntime::metabuild();
//! }
//! ```
//!
//! For the best compatibility it is recommended that you also set the `target-feature` flag to `+crt-static`.
//! In the same directory as your Cargo.toml, create a folder called `.cargo`. In that folder create the file `config.toml` and add the following:
//!
//! ```toml
//! # In .cargo/config.toml
//! [target.'cfg(all(windows, target_env = "msvc"))']
//! rustflags = ["-C", "target-feature=+crt-static"]
//! ```
//!
//! # Details
//!
//! What we call the "CRT" is actually three components:
//! - The C startup files. This provide startup/shutdown code.
//! - The VC runtime. In rust this is mainly used for panic handling but also provides some fundamental functions such as `memcpy`.
//! - The Universal CRT (aka UCRT). This is where most of the C standard library lives.
//!
//! Each of these can be linked either dynamically or statically, however it is usually required that if one is linked statically then they are all linked statically (and ditto for dynamica linking).
//! There is one exceptions.
//! If the VC runtime and C startup files are linked statically then the UCRT can be linked dynamically.
//!
//! The following table summarises these options:
//!
//! | | C startup | VC runtime | Universal CRT |
//! | ------------------ | ---------- | ---------- | ------------- |
//! | Default | dynamic | dynamic | dynamic |
//! | `+crt-static` | static | static | static |
//! | `static_vcruntime` | static | static | dynamic |
//!
//! # Issues
//!
//! If you have problems then you may need to clean the build directory before rebuilding:
//!
//! ```text
//! cargo clean
//! ```
//!
//! It is possible that some C/C++ dependencies may not work in this configuration.
//!
//! Note that Microsoft recommends all the runtime libraries be dynamically linked (which is the default).
//!
//! [Details]: #details
//! [build script]: https://doc.rust-lang.org/cargo/reference/build-scripts.html
//! [`static_vcruntime::metabuild`]: metabuild
use env;
/// Use dynamically linked ucrt with a statically linked vcruntime.
///
/// This must be called from a [build script], like so:
///
/// ```rust,ignore
/// // build.rs
/// fn main() {
/// static_vcruntime::metabuild();
/// }
/// ```
///
/// You can restrict it to only be enabled on specific profiles, such as `release`.
///
/// ```rust,ignore
/// fn main() {
/// if std::env::var("PROFILE").as_deref() == Ok("release") {
/// static_vcruntime::metabuild();
/// }
/// }
/// ```
///
/// [build script]: https://doc.rust-lang.org/cargo/reference/build-scripts.html