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