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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
use super::{Config, ConfigKey, ConfigRelativePath, OptValue, PathAndArgs, StringList, CV};
use crate::core::compiler::{BuildOutput, LinkType};
use crate::util::CargoResult;
use serde::Deserialize;
use std::collections::{BTreeMap, HashMap};
use std::path::PathBuf;
#[derive(Debug, Deserialize)]
pub struct TargetCfgConfig {
pub runner: OptValue<PathAndArgs>,
pub rustflags: OptValue<StringList>,
#[serde(flatten)]
pub other: BTreeMap<String, toml::Value>,
}
#[derive(Debug, Clone)]
pub struct TargetConfig {
pub runner: OptValue<PathAndArgs>,
pub rustflags: OptValue<StringList>,
pub linker: OptValue<ConfigRelativePath>,
pub links_overrides: BTreeMap<String, BuildOutput>,
}
pub(super) fn load_target_cfgs(config: &Config) -> CargoResult<Vec<(String, TargetCfgConfig)>> {
let mut result = Vec::new();
let target: BTreeMap<String, TargetCfgConfig> = config.get("target")?;
log::debug!("Got all targets {:#?}", target);
for (key, cfg) in target {
if key.starts_with("cfg(") {
for other_key in cfg.other.keys() {
config.shell().warn(format!(
"unused key `{}` in [target] config table `{}`",
other_key, key
))?;
}
result.push((key, cfg));
}
}
Ok(result)
}
pub(super) fn load_target_triple(config: &Config, triple: &str) -> CargoResult<TargetConfig> {
let runner: OptValue<PathAndArgs> = config.get(&format!("target.{}.runner", triple))?;
let rustflags: OptValue<StringList> = config.get(&format!("target.{}.rustflags", triple))?;
let linker: OptValue<ConfigRelativePath> = config.get(&format!("target.{}.linker", triple))?;
let target_key = ConfigKey::from_str(&format!("target.{}", triple));
let links_overrides = match config.get_table(&target_key)? {
Some(links) => parse_links_overrides(&target_key, links.val, config)?,
None => BTreeMap::new(),
};
Ok(TargetConfig {
runner,
rustflags,
linker,
links_overrides,
})
}
fn parse_links_overrides(
target_key: &ConfigKey,
links: HashMap<String, CV>,
config: &Config,
) -> CargoResult<BTreeMap<String, BuildOutput>> {
let extra_link_arg = config.cli_unstable().extra_link_arg;
let mut links_overrides = BTreeMap::new();
for (lib_name, value) in links {
match lib_name.as_str() {
"ar" | "linker" | "runner" | "rustflags" => continue,
_ => {}
}
let mut output = BuildOutput::default();
let table = value.table(&format!("{}.{}", target_key, lib_name))?.0;
let mut pairs = Vec::new();
for (k, value) in table {
pairs.push((k, value));
}
pairs.sort_by_key(|p| p.0);
for (key, value) in pairs {
match key.as_str() {
"rustc-flags" => {
let flags = value.string(key)?;
let whence = format!("target config `{}.{}` (in {})", target_key, key, flags.1);
let (paths, links) = BuildOutput::parse_rustc_flags(flags.0, &whence)?;
output.library_paths.extend(paths);
output.library_links.extend(links);
}
"rustc-link-lib" => {
let list = value.list(key)?;
output
.library_links
.extend(list.iter().map(|v| v.0.clone()));
}
"rustc-link-search" => {
let list = value.list(key)?;
output
.library_paths
.extend(list.iter().map(|v| PathBuf::from(&v.0)));
}
"rustc-link-arg-cdylib" | "rustc-cdylib-link-arg" => {
let args = value.list(key)?;
let args = args.iter().map(|v| (Some(LinkType::Cdylib), v.0.clone()));
output.linker_args.extend(args);
}
"rustc-link-arg-bins" => {
if extra_link_arg {
let args = value.list(key)?;
let args = args.iter().map(|v| (Some(LinkType::Bin), v.0.clone()));
output.linker_args.extend(args);
} else {
config.shell().warn(format!(
"target config `{}.{}` requires -Zextra-link-arg flag",
target_key, key
))?;
}
}
"rustc-link-arg" => {
if extra_link_arg {
let args = value.list(key)?;
let args = args.iter().map(|v| (None, v.0.clone()));
output.linker_args.extend(args);
} else {
config.shell().warn(format!(
"target config `{}.{}` requires -Zextra-link-arg flag",
target_key, key
))?;
}
}
"rustc-cfg" => {
let list = value.list(key)?;
output.cfgs.extend(list.iter().map(|v| v.0.clone()));
}
"rustc-env" => {
for (name, val) in value.table(key)?.0 {
let val = val.string(name)?.0;
output.env.push((name.clone(), val.to_string()));
}
}
"warning" | "rerun-if-changed" | "rerun-if-env-changed" => {
anyhow::bail!("`{}` is not supported in build script overrides", key);
}
_ => {
let val = value.string(key)?.0;
output.metadata.push((key.clone(), val.to_string()));
}
}
}
links_overrides.insert(lib_name, output);
}
Ok(links_overrides)
}