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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
use crate::config::{self, Input, OutputFilenames, OutputType};
use crate::Session;
use rustc_ast::{ast, attr};
use rustc_span::symbol::sym;
use rustc_span::Span;
use std::path::{Path, PathBuf};
pub fn out_filename(
sess: &Session,
crate_type: config::CrateType,
outputs: &OutputFilenames,
crate_name: &str,
) -> PathBuf {
let default_filename = filename_for_input(sess, crate_type, crate_name, outputs);
let out_filename = outputs
.outputs
.get(&OutputType::Exe)
.and_then(|s| s.to_owned())
.or_else(|| outputs.single_output_file.clone())
.unwrap_or(default_filename);
check_file_is_writeable(&out_filename, sess);
out_filename
}
pub fn check_file_is_writeable(file: &Path, sess: &Session) {
if !is_writeable(file) {
sess.fatal(&format!(
"output file {} is not writeable -- check its \
permissions",
file.display()
));
}
}
fn is_writeable(p: &Path) -> bool {
match p.metadata() {
Err(..) => true,
Ok(m) => !m.permissions().readonly(),
}
}
pub fn find_crate_name(sess: Option<&Session>, attrs: &[ast::Attribute], input: &Input) -> String {
let validate = |s: String, span: Option<Span>| {
validate_crate_name(sess, &s, span);
s
};
let attr_crate_name =
attr::find_by_name(attrs, sym::crate_name).and_then(|at| at.value_str().map(|s| (at, s)));
if let Some(sess) = sess {
if let Some(ref s) = sess.opts.crate_name {
if let Some((attr, name)) = attr_crate_name {
if name.as_str() != *s {
let msg = format!(
"`--crate-name` and `#[crate_name]` are \
required to match, but `{}` != `{}`",
s, name
);
sess.span_err(attr.span, &msg);
}
}
return validate(s.clone(), None);
}
}
if let Some((attr, s)) = attr_crate_name {
return validate(s.to_string(), Some(attr.span));
}
if let Input::File(ref path) = *input {
if let Some(s) = path.file_stem().and_then(|s| s.to_str()) {
if s.starts_with('-') {
let msg = format!(
"crate names cannot start with a `-`, but \
`{}` has a leading hyphen",
s
);
if let Some(sess) = sess {
sess.err(&msg);
}
} else {
return validate(s.replace("-", "_"), None);
}
}
}
"rust_out".to_string()
}
pub fn validate_crate_name(sess: Option<&Session>, s: &str, sp: Option<Span>) {
let mut err_count = 0;
{
let mut say = |s: &str| {
match (sp, sess) {
(_, None) => panic!("{}", s),
(Some(sp), Some(sess)) => sess.span_err(sp, s),
(None, Some(sess)) => sess.err(s),
}
err_count += 1;
};
if s.is_empty() {
say("crate name must not be empty");
}
for c in s.chars() {
if c.is_alphanumeric() {
continue;
}
if c == '_' {
continue;
}
say(&format!("invalid character `{}` in crate name: `{}`", c, s));
}
}
if err_count > 0 {
sess.unwrap().abort_if_errors();
}
}
pub fn filename_for_metadata(
sess: &Session,
crate_name: &str,
outputs: &OutputFilenames,
) -> PathBuf {
let libname = format!("{}{}", crate_name, sess.opts.cg.extra_filename);
let out_filename = outputs
.single_output_file
.clone()
.unwrap_or_else(|| outputs.out_directory.join(&format!("lib{}.rmeta", libname)));
check_file_is_writeable(&out_filename, sess);
out_filename
}
pub fn filename_for_input(
sess: &Session,
crate_type: config::CrateType,
crate_name: &str,
outputs: &OutputFilenames,
) -> PathBuf {
let libname = format!("{}{}", crate_name, sess.opts.cg.extra_filename);
match crate_type {
config::CrateType::Rlib => outputs.out_directory.join(&format!("lib{}.rlib", libname)),
config::CrateType::Cdylib | config::CrateType::ProcMacro | config::CrateType::Dylib => {
let (prefix, suffix) =
(&sess.target.target.options.dll_prefix, &sess.target.target.options.dll_suffix);
outputs.out_directory.join(&format!("{}{}{}", prefix, libname, suffix))
}
config::CrateType::Staticlib => {
let (prefix, suffix) = (
&sess.target.target.options.staticlib_prefix,
&sess.target.target.options.staticlib_suffix,
);
outputs.out_directory.join(&format!("{}{}{}", prefix, libname, suffix))
}
config::CrateType::Executable => {
let suffix = &sess.target.target.options.exe_suffix;
let out_filename = outputs.path(OutputType::Exe);
if suffix.is_empty() { out_filename } else { out_filename.with_extension(&suffix[1..]) }
}
}
}
pub fn default_output_for_target(sess: &Session) -> config::CrateType {
if !sess.target.target.options.executables {
config::CrateType::Staticlib
} else {
config::CrateType::Executable
}
}
pub fn invalid_output_for_target(sess: &Session, crate_type: config::CrateType) -> bool {
match crate_type {
config::CrateType::Cdylib | config::CrateType::Dylib | config::CrateType::ProcMacro => {
if !sess.target.target.options.dynamic_linking {
return true;
}
if sess.crt_static(Some(crate_type))
&& !sess.target.target.options.crt_static_allows_dylibs
{
return true;
}
}
_ => {}
}
if sess.target.target.options.only_cdylib {
match crate_type {
config::CrateType::ProcMacro | config::CrateType::Dylib => return true,
_ => {}
}
}
if !sess.target.target.options.executables {
if crate_type == config::CrateType::Executable {
return true;
}
}
false
}