Skip to content

Commit 478db48

Browse files
committed
Move -l option parsing into its own submodule
No functional change (yet).
1 parent 2902bca commit 478db48

File tree

2 files changed

+144
-140
lines changed

2 files changed

+144
-140
lines changed

compiler/rustc_session/src/config.rs

+4-140
Original file line numberDiff line numberDiff line change
@@ -30,17 +30,18 @@ use rustc_target::spec::{
3030
};
3131
use tracing::debug;
3232

33+
pub use crate::config::cfg::{Cfg, CheckCfg, ExpectedValues};
34+
use crate::config::native_libs::parse_libs;
3335
use crate::errors::FileWriteFail;
3436
pub use crate::options::*;
3537
use crate::search_paths::SearchPath;
36-
use crate::utils::{CanonicalizedPath, NativeLib, NativeLibKind};
38+
use crate::utils::CanonicalizedPath;
3739
use crate::{EarlyDiagCtxt, HashStableContext, Session, filesearch, lint};
3840

3941
mod cfg;
42+
mod native_libs;
4043
pub mod sigpipe;
4144

42-
pub use cfg::{Cfg, CheckCfg, ExpectedValues};
43-
4445
/// The different settings that the `-C strip` flag can have.
4546
#[derive(Clone, Copy, PartialEq, Hash, Debug)]
4647
pub enum Strip {
@@ -2134,143 +2135,6 @@ fn parse_assert_incr_state(
21342135
}
21352136
}
21362137

2137-
fn parse_native_lib_kind(
2138-
early_dcx: &EarlyDiagCtxt,
2139-
matches: &getopts::Matches,
2140-
kind: &str,
2141-
) -> (NativeLibKind, Option<bool>) {
2142-
let (kind, modifiers) = match kind.split_once(':') {
2143-
None => (kind, None),
2144-
Some((kind, modifiers)) => (kind, Some(modifiers)),
2145-
};
2146-
2147-
let kind = match kind {
2148-
"static" => NativeLibKind::Static { bundle: None, whole_archive: None },
2149-
"dylib" => NativeLibKind::Dylib { as_needed: None },
2150-
"framework" => NativeLibKind::Framework { as_needed: None },
2151-
"link-arg" => {
2152-
if !nightly_options::is_unstable_enabled(matches) {
2153-
let why = if nightly_options::match_is_nightly_build(matches) {
2154-
" and only accepted on the nightly compiler"
2155-
} else {
2156-
", the `-Z unstable-options` flag must also be passed to use it"
2157-
};
2158-
early_dcx.early_fatal(format!("library kind `link-arg` is unstable{why}"))
2159-
}
2160-
NativeLibKind::LinkArg
2161-
}
2162-
_ => early_dcx.early_fatal(format!(
2163-
"unknown library kind `{kind}`, expected one of: static, dylib, framework, link-arg"
2164-
)),
2165-
};
2166-
match modifiers {
2167-
None => (kind, None),
2168-
Some(modifiers) => parse_native_lib_modifiers(early_dcx, kind, modifiers, matches),
2169-
}
2170-
}
2171-
2172-
fn parse_native_lib_modifiers(
2173-
early_dcx: &EarlyDiagCtxt,
2174-
mut kind: NativeLibKind,
2175-
modifiers: &str,
2176-
matches: &getopts::Matches,
2177-
) -> (NativeLibKind, Option<bool>) {
2178-
let mut verbatim = None;
2179-
for modifier in modifiers.split(',') {
2180-
let (modifier, value) = match modifier.strip_prefix(['+', '-']) {
2181-
Some(m) => (m, modifier.starts_with('+')),
2182-
None => early_dcx.early_fatal(
2183-
"invalid linking modifier syntax, expected '+' or '-' prefix \
2184-
before one of: bundle, verbatim, whole-archive, as-needed",
2185-
),
2186-
};
2187-
2188-
let report_unstable_modifier = || {
2189-
if !nightly_options::is_unstable_enabled(matches) {
2190-
let why = if nightly_options::match_is_nightly_build(matches) {
2191-
" and only accepted on the nightly compiler"
2192-
} else {
2193-
", the `-Z unstable-options` flag must also be passed to use it"
2194-
};
2195-
early_dcx.early_fatal(format!("linking modifier `{modifier}` is unstable{why}"))
2196-
}
2197-
};
2198-
let assign_modifier = |dst: &mut Option<bool>| {
2199-
if dst.is_some() {
2200-
let msg = format!("multiple `{modifier}` modifiers in a single `-l` option");
2201-
early_dcx.early_fatal(msg)
2202-
} else {
2203-
*dst = Some(value);
2204-
}
2205-
};
2206-
match (modifier, &mut kind) {
2207-
("bundle", NativeLibKind::Static { bundle, .. }) => assign_modifier(bundle),
2208-
("bundle", _) => early_dcx.early_fatal(
2209-
"linking modifier `bundle` is only compatible with `static` linking kind",
2210-
),
2211-
2212-
("verbatim", _) => assign_modifier(&mut verbatim),
2213-
2214-
("whole-archive", NativeLibKind::Static { whole_archive, .. }) => {
2215-
assign_modifier(whole_archive)
2216-
}
2217-
("whole-archive", _) => early_dcx.early_fatal(
2218-
"linking modifier `whole-archive` is only compatible with `static` linking kind",
2219-
),
2220-
2221-
("as-needed", NativeLibKind::Dylib { as_needed })
2222-
| ("as-needed", NativeLibKind::Framework { as_needed }) => {
2223-
report_unstable_modifier();
2224-
assign_modifier(as_needed)
2225-
}
2226-
("as-needed", _) => early_dcx.early_fatal(
2227-
"linking modifier `as-needed` is only compatible with \
2228-
`dylib` and `framework` linking kinds",
2229-
),
2230-
2231-
// Note: this error also excludes the case with empty modifier
2232-
// string, like `modifiers = ""`.
2233-
_ => early_dcx.early_fatal(format!(
2234-
"unknown linking modifier `{modifier}`, expected one \
2235-
of: bundle, verbatim, whole-archive, as-needed"
2236-
)),
2237-
}
2238-
}
2239-
2240-
(kind, verbatim)
2241-
}
2242-
2243-
fn parse_libs(early_dcx: &EarlyDiagCtxt, matches: &getopts::Matches) -> Vec<NativeLib> {
2244-
matches
2245-
.opt_strs("l")
2246-
.into_iter()
2247-
.map(|s| {
2248-
// Parse string of the form "[KIND[:MODIFIERS]=]lib[:new_name]",
2249-
// where KIND is one of "dylib", "framework", "static", "link-arg" and
2250-
// where MODIFIERS are a comma separated list of supported modifiers
2251-
// (bundle, verbatim, whole-archive, as-needed). Each modifier is prefixed
2252-
// with either + or - to indicate whether it is enabled or disabled.
2253-
// The last value specified for a given modifier wins.
2254-
let (name, kind, verbatim) = match s.split_once('=') {
2255-
None => (s, NativeLibKind::Unspecified, None),
2256-
Some((kind, name)) => {
2257-
let (kind, verbatim) = parse_native_lib_kind(early_dcx, matches, kind);
2258-
(name.to_string(), kind, verbatim)
2259-
}
2260-
};
2261-
2262-
let (name, new_name) = match name.split_once(':') {
2263-
None => (name, None),
2264-
Some((name, new_name)) => (name.to_string(), Some(new_name.to_owned())),
2265-
};
2266-
if name.is_empty() {
2267-
early_dcx.early_fatal("library name must not be empty");
2268-
}
2269-
NativeLib { name, new_name, kind, verbatim }
2270-
})
2271-
.collect()
2272-
}
2273-
22742138
pub fn parse_externs(
22752139
early_dcx: &EarlyDiagCtxt,
22762140
matches: &getopts::Matches,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
use crate::EarlyDiagCtxt;
2+
use crate::config::nightly_options;
3+
use crate::utils::{NativeLib, NativeLibKind};
4+
5+
pub(crate) fn parse_libs(early_dcx: &EarlyDiagCtxt, matches: &getopts::Matches) -> Vec<NativeLib> {
6+
matches
7+
.opt_strs("l")
8+
.into_iter()
9+
.map(|s| {
10+
// Parse string of the form "[KIND[:MODIFIERS]=]lib[:new_name]",
11+
// where KIND is one of "dylib", "framework", "static", "link-arg" and
12+
// where MODIFIERS are a comma separated list of supported modifiers
13+
// (bundle, verbatim, whole-archive, as-needed). Each modifier is prefixed
14+
// with either + or - to indicate whether it is enabled or disabled.
15+
// The last value specified for a given modifier wins.
16+
let (name, kind, verbatim) = match s.split_once('=') {
17+
None => (s, NativeLibKind::Unspecified, None),
18+
Some((kind, name)) => {
19+
let (kind, verbatim) = parse_native_lib_kind(early_dcx, matches, kind);
20+
(name.to_string(), kind, verbatim)
21+
}
22+
};
23+
24+
let (name, new_name) = match name.split_once(':') {
25+
None => (name, None),
26+
Some((name, new_name)) => (name.to_string(), Some(new_name.to_owned())),
27+
};
28+
if name.is_empty() {
29+
early_dcx.early_fatal("library name must not be empty");
30+
}
31+
NativeLib { name, new_name, kind, verbatim }
32+
})
33+
.collect()
34+
}
35+
36+
fn parse_native_lib_kind(
37+
early_dcx: &EarlyDiagCtxt,
38+
matches: &getopts::Matches,
39+
kind: &str,
40+
) -> (NativeLibKind, Option<bool>) {
41+
let (kind, modifiers) = match kind.split_once(':') {
42+
None => (kind, None),
43+
Some((kind, modifiers)) => (kind, Some(modifiers)),
44+
};
45+
46+
let kind = match kind {
47+
"static" => NativeLibKind::Static { bundle: None, whole_archive: None },
48+
"dylib" => NativeLibKind::Dylib { as_needed: None },
49+
"framework" => NativeLibKind::Framework { as_needed: None },
50+
"link-arg" => {
51+
if !nightly_options::is_unstable_enabled(matches) {
52+
let why = if nightly_options::match_is_nightly_build(matches) {
53+
" and only accepted on the nightly compiler"
54+
} else {
55+
", the `-Z unstable-options` flag must also be passed to use it"
56+
};
57+
early_dcx.early_fatal(format!("library kind `link-arg` is unstable{why}"))
58+
}
59+
NativeLibKind::LinkArg
60+
}
61+
_ => early_dcx.early_fatal(format!(
62+
"unknown library kind `{kind}`, expected one of: static, dylib, framework, link-arg"
63+
)),
64+
};
65+
match modifiers {
66+
None => (kind, None),
67+
Some(modifiers) => parse_native_lib_modifiers(early_dcx, kind, modifiers, matches),
68+
}
69+
}
70+
71+
fn parse_native_lib_modifiers(
72+
early_dcx: &EarlyDiagCtxt,
73+
mut kind: NativeLibKind,
74+
modifiers: &str,
75+
matches: &getopts::Matches,
76+
) -> (NativeLibKind, Option<bool>) {
77+
let mut verbatim = None;
78+
for modifier in modifiers.split(',') {
79+
let (modifier, value) = match modifier.strip_prefix(['+', '-']) {
80+
Some(m) => (m, modifier.starts_with('+')),
81+
None => early_dcx.early_fatal(
82+
"invalid linking modifier syntax, expected '+' or '-' prefix \
83+
before one of: bundle, verbatim, whole-archive, as-needed",
84+
),
85+
};
86+
87+
let report_unstable_modifier = || {
88+
if !nightly_options::is_unstable_enabled(matches) {
89+
let why = if nightly_options::match_is_nightly_build(matches) {
90+
" and only accepted on the nightly compiler"
91+
} else {
92+
", the `-Z unstable-options` flag must also be passed to use it"
93+
};
94+
early_dcx.early_fatal(format!("linking modifier `{modifier}` is unstable{why}"))
95+
}
96+
};
97+
let assign_modifier = |dst: &mut Option<bool>| {
98+
if dst.is_some() {
99+
let msg = format!("multiple `{modifier}` modifiers in a single `-l` option");
100+
early_dcx.early_fatal(msg)
101+
} else {
102+
*dst = Some(value);
103+
}
104+
};
105+
match (modifier, &mut kind) {
106+
("bundle", NativeLibKind::Static { bundle, .. }) => assign_modifier(bundle),
107+
("bundle", _) => early_dcx.early_fatal(
108+
"linking modifier `bundle` is only compatible with `static` linking kind",
109+
),
110+
111+
("verbatim", _) => assign_modifier(&mut verbatim),
112+
113+
("whole-archive", NativeLibKind::Static { whole_archive, .. }) => {
114+
assign_modifier(whole_archive)
115+
}
116+
("whole-archive", _) => early_dcx.early_fatal(
117+
"linking modifier `whole-archive` is only compatible with `static` linking kind",
118+
),
119+
120+
("as-needed", NativeLibKind::Dylib { as_needed })
121+
| ("as-needed", NativeLibKind::Framework { as_needed }) => {
122+
report_unstable_modifier();
123+
assign_modifier(as_needed)
124+
}
125+
("as-needed", _) => early_dcx.early_fatal(
126+
"linking modifier `as-needed` is only compatible with \
127+
`dylib` and `framework` linking kinds",
128+
),
129+
130+
// Note: this error also excludes the case with empty modifier
131+
// string, like `modifiers = ""`.
132+
_ => early_dcx.early_fatal(format!(
133+
"unknown linking modifier `{modifier}`, expected one \
134+
of: bundle, verbatim, whole-archive, as-needed"
135+
)),
136+
}
137+
}
138+
139+
(kind, verbatim)
140+
}

0 commit comments

Comments
 (0)