1use std::path::Path;
4
5use rustc_span::{Span, Symbol};
6
7use crate::Session;
8use crate::config::{CrateType, OutFileName, OutputFilenames, OutputType};
9use crate::diagnostics::{
10 CrateNameEmpty, FileIsNotWriteable, InvalidCharacterInCrateName,
11 InvalidCharacterInCrateNameSuggestion,
12};
13
14pub fn out_filename(
15 sess: &Session,
16 crate_type: CrateType,
17 outputs: &OutputFilenames,
18 crate_name: Symbol,
19) -> OutFileName {
20 let default_filename = filename_for_input(sess, crate_type, crate_name, outputs);
21 let out_filename = outputs
22 .outputs
23 .get(&OutputType::Exe)
24 .and_then(|s| s.to_owned())
25 .or_else(|| outputs.single_output_file.clone())
26 .unwrap_or(default_filename);
27
28 if let OutFileName::Real(ref path) = out_filename {
29 check_file_is_writeable(path, sess);
30 }
31
32 out_filename
33}
34
35pub fn check_file_is_writeable(file: &Path, sess: &Session) {
39 if !is_writeable(file) {
40 sess.dcx().emit_fatal(FileIsNotWriteable { file });
41 }
42}
43
44fn is_writeable(p: &Path) -> bool {
45 match p.metadata() {
46 Err(..) => true,
47 Ok(m) => !m.permissions().readonly(),
48 }
49}
50
51pub fn validate_crate_name(sess: &Session, crate_name: Symbol, span: Option<Span>) {
60 let mut guar = None;
61
62 if crate_name.is_empty() {
63 guar = Some(sess.dcx().emit_err(CrateNameEmpty { span }));
64 }
65
66 for c in crate_name.as_str().chars() {
67 if c.is_alphanumeric() || c == '_' {
68 continue;
69 }
70 guar = Some(sess.dcx().emit_err(InvalidCharacterInCrateName {
71 span,
72 character: c,
73 crate_name,
74 suggestion: span.is_none().then(|| InvalidCharacterInCrateNameSuggestion {
75 suggested_name:
76 crate_name.as_str().replace(|c: char| c != '_' && !c.is_alphanumeric(), "_"),
77 }),
78 }));
79 }
80
81 if let Some(guar) = guar {
82 guar.raise_fatal();
83 }
84}
85
86pub fn filename_for_metadata(sess: &Session, outputs: &OutputFilenames) -> OutFileName {
87 let out_filename = outputs.path(OutputType::Metadata);
88 if let OutFileName::Real(ref path) = out_filename {
89 check_file_is_writeable(path, sess);
90 }
91 out_filename
92}
93
94pub fn filename_for_input(
95 sess: &Session,
96 crate_type: CrateType,
97 crate_name: Symbol,
98 outputs: &OutputFilenames,
99) -> OutFileName {
100 let libname = ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("{0}{1}", crate_name,
sess.opts.cg.extra_filename))
})format!("{}{}", crate_name, sess.opts.cg.extra_filename);
101
102 match crate_type {
103 CrateType::Rlib => {
104 OutFileName::Real(outputs.out_directory.join(&::alloc::__export::must_use({
::alloc::fmt::format(format_args!("lib{0}.rlib", libname))
})format!("lib{libname}.rlib")))
105 }
106 CrateType::Cdylib | CrateType::ProcMacro | CrateType::Dylib | CrateType::Sdylib => {
107 let (prefix, suffix) = (&sess.target.dll_prefix, &sess.target.dll_suffix);
108 OutFileName::Real(outputs.out_directory.join(&::alloc::__export::must_use({
::alloc::fmt::format(format_args!("{0}{1}{2}", prefix, libname,
suffix))
})format!("{prefix}{libname}{suffix}")))
109 }
110 CrateType::StaticLib => {
111 let (prefix, suffix) = sess.staticlib_components(false);
112 OutFileName::Real(outputs.out_directory.join(&::alloc::__export::must_use({
::alloc::fmt::format(format_args!("{0}{1}{2}", prefix, libname,
suffix))
})format!("{prefix}{libname}{suffix}")))
113 }
114 CrateType::Executable => {
115 let suffix = &sess.target.exe_suffix;
116 let out_filename = outputs.path(OutputType::Exe);
117 if let OutFileName::Real(ref path) = out_filename {
118 if suffix.is_empty() {
119 out_filename
120 } else {
121 OutFileName::Real(path.with_extension(&suffix[1..]))
122 }
123 } else {
124 out_filename
125 }
126 }
127 }
128}
129
130pub fn invalid_output_for_target(sess: &Session, crate_type: CrateType) -> bool {
132 if let CrateType::Cdylib | CrateType::Dylib | CrateType::ProcMacro = crate_type {
133 if !sess.target.dynamic_linking {
134 return true;
135 }
136 if sess.crt_static(Some(crate_type)) && !sess.target.crt_static_allows_dylibs {
137 return true;
138 }
139 }
140 if let CrateType::ProcMacro | CrateType::Dylib = crate_type
141 && sess.target.only_cdylib
142 {
143 return true;
144 }
145 if let CrateType::Executable = crate_type
146 && !sess.target.executables
147 {
148 return true;
149 }
150
151 false
152}