1 //! Related to out filenames of compilation (e.g. save analysis, binaries).
2 use crate::config::{CrateType, Input, OutputFilenames, OutputType};
3 use crate::Session;
4 use rustc_ast as ast;
5 use rustc_span::symbol::sym;
6 use rustc_span::Span;
7 use std::path::{Path, PathBuf};
8
out_filename( sess: &Session, crate_type: CrateType, outputs: &OutputFilenames, crate_name: &str, ) -> PathBuf9 pub fn out_filename(
10 sess: &Session,
11 crate_type: CrateType,
12 outputs: &OutputFilenames,
13 crate_name: &str,
14 ) -> PathBuf {
15 let default_filename = filename_for_input(sess, crate_type, crate_name, outputs);
16 let out_filename = outputs
17 .outputs
18 .get(&OutputType::Exe)
19 .and_then(|s| s.to_owned())
20 .or_else(|| outputs.single_output_file.clone())
21 .unwrap_or(default_filename);
22
23 check_file_is_writeable(&out_filename, sess);
24
25 out_filename
26 }
27
28 /// Make sure files are writeable. Mac, FreeBSD, and Windows system linkers
29 /// check this already -- however, the Linux linker will happily overwrite a
30 /// read-only file. We should be consistent.
check_file_is_writeable(file: &Path, sess: &Session)31 pub fn check_file_is_writeable(file: &Path, sess: &Session) {
32 if !is_writeable(file) {
33 sess.fatal(&format!(
34 "output file {} is not writeable -- check its \
35 permissions",
36 file.display()
37 ));
38 }
39 }
40
is_writeable(p: &Path) -> bool41 fn is_writeable(p: &Path) -> bool {
42 match p.metadata() {
43 Err(..) => true,
44 Ok(m) => !m.permissions().readonly(),
45 }
46 }
47
find_crate_name(sess: &Session, attrs: &[ast::Attribute], input: &Input) -> String48 pub fn find_crate_name(sess: &Session, attrs: &[ast::Attribute], input: &Input) -> String {
49 let validate = |s: String, span: Option<Span>| {
50 validate_crate_name(sess, &s, span);
51 s
52 };
53
54 // Look in attributes 100% of the time to make sure the attribute is marked
55 // as used. After doing this, however, we still prioritize a crate name from
56 // the command line over one found in the #[crate_name] attribute. If we
57 // find both we ensure that they're the same later on as well.
58 let attr_crate_name =
59 sess.find_by_name(attrs, sym::crate_name).and_then(|at| at.value_str().map(|s| (at, s)));
60
61 if let Some(ref s) = sess.opts.crate_name {
62 if let Some((attr, name)) = attr_crate_name {
63 if name.as_str() != *s {
64 let msg = format!(
65 "`--crate-name` and `#[crate_name]` are \
66 required to match, but `{}` != `{}`",
67 s, name
68 );
69 sess.span_err(attr.span, &msg);
70 }
71 }
72 return validate(s.clone(), None);
73 }
74
75 if let Some((attr, s)) = attr_crate_name {
76 return validate(s.to_string(), Some(attr.span));
77 }
78 if let Input::File(ref path) = *input {
79 if let Some(s) = path.file_stem().and_then(|s| s.to_str()) {
80 if s.starts_with('-') {
81 let msg = format!(
82 "crate names cannot start with a `-`, but \
83 `{}` has a leading hyphen",
84 s
85 );
86 sess.err(&msg);
87 } else {
88 return validate(s.replace("-", "_"), None);
89 }
90 }
91 }
92
93 "rust_out".to_string()
94 }
95
validate_crate_name(sess: &Session, s: &str, sp: Option<Span>)96 pub fn validate_crate_name(sess: &Session, s: &str, sp: Option<Span>) {
97 let mut err_count = 0;
98 {
99 let mut say = |s: &str| {
100 match sp {
101 Some(sp) => sess.span_err(sp, s),
102 None => sess.err(s),
103 }
104 err_count += 1;
105 };
106 if s.is_empty() {
107 say("crate name must not be empty");
108 }
109 for c in s.chars() {
110 if c.is_alphanumeric() {
111 continue;
112 }
113 if c == '_' {
114 continue;
115 }
116 say(&format!("invalid character `{}` in crate name: `{}`", c, s));
117 }
118 }
119
120 if err_count > 0 {
121 sess.abort_if_errors();
122 }
123 }
124
filename_for_metadata( sess: &Session, crate_name: &str, outputs: &OutputFilenames, ) -> PathBuf125 pub fn filename_for_metadata(
126 sess: &Session,
127 crate_name: &str,
128 outputs: &OutputFilenames,
129 ) -> PathBuf {
130 let libname = format!("{}{}", crate_name, sess.opts.cg.extra_filename);
131
132 let out_filename = outputs
133 .single_output_file
134 .clone()
135 .unwrap_or_else(|| outputs.out_directory.join(&format!("lib{}.rmeta", libname)));
136
137 check_file_is_writeable(&out_filename, sess);
138
139 out_filename
140 }
141
filename_for_input( sess: &Session, crate_type: CrateType, crate_name: &str, outputs: &OutputFilenames, ) -> PathBuf142 pub fn filename_for_input(
143 sess: &Session,
144 crate_type: CrateType,
145 crate_name: &str,
146 outputs: &OutputFilenames,
147 ) -> PathBuf {
148 let libname = format!("{}{}", crate_name, sess.opts.cg.extra_filename);
149
150 match crate_type {
151 CrateType::Rlib => outputs.out_directory.join(&format!("lib{}.rlib", libname)),
152 CrateType::Cdylib | CrateType::ProcMacro | CrateType::Dylib => {
153 let (prefix, suffix) = (&sess.target.dll_prefix, &sess.target.dll_suffix);
154 outputs.out_directory.join(&format!("{}{}{}", prefix, libname, suffix))
155 }
156 CrateType::Staticlib => {
157 let (prefix, suffix) = (&sess.target.staticlib_prefix, &sess.target.staticlib_suffix);
158 outputs.out_directory.join(&format!("{}{}{}", prefix, libname, suffix))
159 }
160 CrateType::Executable => {
161 let suffix = &sess.target.exe_suffix;
162 let out_filename = outputs.path(OutputType::Exe);
163 if suffix.is_empty() { out_filename } else { out_filename.with_extension(&suffix[1..]) }
164 }
165 }
166 }
167
168 /// Returns default crate type for target
169 ///
170 /// Default crate type is used when crate type isn't provided neither
171 /// through cmd line arguments nor through crate attributes
172 ///
173 /// It is CrateType::Executable for all platforms but iOS as there is no
174 /// way to run iOS binaries anyway without jailbreaking and
175 /// interaction with Rust code through static library is the only
176 /// option for now
default_output_for_target(sess: &Session) -> CrateType177 pub fn default_output_for_target(sess: &Session) -> CrateType {
178 if !sess.target.executables { CrateType::Staticlib } else { CrateType::Executable }
179 }
180
181 /// Checks if target supports crate_type as output
invalid_output_for_target(sess: &Session, crate_type: CrateType) -> bool182 pub fn invalid_output_for_target(sess: &Session, crate_type: CrateType) -> bool {
183 match crate_type {
184 CrateType::Cdylib | CrateType::Dylib | CrateType::ProcMacro => {
185 if !sess.target.dynamic_linking {
186 return true;
187 }
188 if sess.crt_static(Some(crate_type)) && !sess.target.crt_static_allows_dylibs {
189 return true;
190 }
191 }
192 _ => {}
193 }
194 if sess.target.only_cdylib {
195 match crate_type {
196 CrateType::ProcMacro | CrateType::Dylib => return true,
197 _ => {}
198 }
199 }
200 if !sess.target.executables && crate_type == CrateType::Executable {
201 return true;
202 }
203
204 false
205 }
206