1 use rustc::session::config::{self, OutputFilenames, Input, OutputType};
2 use rustc::session::Session;
3 use std::path::{Path, PathBuf};
4 use syntax::{ast, attr};
5 use syntax::symbol::sym;
8 pub fn out_filename(sess: &Session,
9 crate_type: config::CrateType,
10 outputs: &OutputFilenames,
13 let default_filename = filename_for_input(sess, crate_type, crate_name, outputs);
14 let out_filename = outputs.outputs.get(&OutputType::Exe)
15 .and_then(|s| s.to_owned())
16 .or_else(|| outputs.single_output_file.clone())
17 .unwrap_or(default_filename);
19 check_file_is_writeable(&out_filename, sess);
24 // Make sure files are writeable. Mac, FreeBSD, and Windows system linkers
25 // check this already -- however, the Linux linker will happily overwrite a
26 // read-only file. We should be consistent.
27 pub fn check_file_is_writeable(file: &Path, sess: &Session) {
28 if !is_writeable(file) {
29 sess.fatal(&format!("output file {} is not writeable -- check its \
30 permissions", file.display()));
34 fn is_writeable(p: &Path) -> bool {
37 Ok(m) => !m.permissions().readonly()
41 pub fn find_crate_name(sess: Option<&Session>,
42 attrs: &[ast::Attribute],
43 input: &Input) -> String {
44 let validate = |s: String, span: Option<Span>| {
45 rustc_metadata::validate_crate_name(sess, &s, span);
49 // Look in attributes 100% of the time to make sure the attribute is marked
50 // as used. After doing this, however, we still prioritize a crate name from
51 // the command line over one found in the #[crate_name] attribute. If we
52 // find both we ensure that they're the same later on as well.
53 let attr_crate_name = attr::find_by_name(attrs, sym::crate_name)
54 .and_then(|at| at.value_str().map(|s| (at, s)));
56 if let Some(sess) = sess {
57 if let Some(ref s) = sess.opts.crate_name {
58 if let Some((attr, name)) = attr_crate_name {
59 if name.as_str() != *s {
60 let msg = format!("`--crate-name` and `#[crate_name]` are \
61 required to match, but `{}` != `{}`",
63 sess.span_err(attr.span, &msg);
66 return validate(s.clone(), None);
70 if let Some((attr, s)) = attr_crate_name {
71 return validate(s.to_string(), Some(attr.span));
73 if let Input::File(ref path) = *input {
74 if let Some(s) = path.file_stem().and_then(|s| s.to_str()) {
75 if s.starts_with("-") {
76 let msg = format!("crate names cannot start with a `-`, but \
77 `{}` has a leading hyphen", s);
78 if let Some(sess) = sess {
82 return validate(s.replace("-", "_"), None);
87 "rust_out".to_string()
90 pub fn filename_for_metadata(sess: &Session,
92 outputs: &OutputFilenames) -> PathBuf {
93 let libname = format!("{}{}", crate_name, sess.opts.cg.extra_filename);
95 let out_filename = outputs.single_output_file.clone()
96 .unwrap_or_else(|| outputs.out_directory.join(&format!("lib{}.rmeta", libname)));
98 check_file_is_writeable(&out_filename, sess);
103 pub fn filename_for_input(sess: &Session,
104 crate_type: config::CrateType,
106 outputs: &OutputFilenames) -> PathBuf {
107 let libname = format!("{}{}", crate_name, sess.opts.cg.extra_filename);
110 config::CrateType::Rlib => {
111 outputs.out_directory.join(&format!("lib{}.rlib", libname))
113 config::CrateType::Cdylib |
114 config::CrateType::ProcMacro |
115 config::CrateType::Dylib => {
116 let (prefix, suffix) = (&sess.target.target.options.dll_prefix,
117 &sess.target.target.options.dll_suffix);
118 outputs.out_directory.join(&format!("{}{}{}", prefix, libname,
121 config::CrateType::Staticlib => {
122 let (prefix, suffix) = (&sess.target.target.options.staticlib_prefix,
123 &sess.target.target.options.staticlib_suffix);
124 outputs.out_directory.join(&format!("{}{}{}", prefix, libname,
127 config::CrateType::Executable => {
128 let suffix = &sess.target.target.options.exe_suffix;
129 let out_filename = outputs.path(OutputType::Exe);
130 if suffix.is_empty() {
133 out_filename.with_extension(&suffix[1..])
139 /// Returns default crate type for target
141 /// Default crate type is used when crate type isn't provided neither
142 /// through cmd line arguments nor through crate attributes
144 /// It is CrateType::Executable for all platforms but iOS as there is no
145 /// way to run iOS binaries anyway without jailbreaking and
146 /// interaction with Rust code through static library is the only
148 pub fn default_output_for_target(sess: &Session) -> config::CrateType {
149 if !sess.target.target.options.executables {
150 config::CrateType::Staticlib
152 config::CrateType::Executable
156 /// Checks if target supports crate_type as output
157 pub fn invalid_output_for_target(sess: &Session,
158 crate_type: config::CrateType) -> bool {
160 config::CrateType::Cdylib |
161 config::CrateType::Dylib |
162 config::CrateType::ProcMacro => {
163 if !sess.target.target.options.dynamic_linking {
166 if sess.crt_static() && !sess.target.target.options.crt_static_allows_dylibs {
172 if sess.target.target.options.only_cdylib {
174 config::CrateType::ProcMacro | config::CrateType::Dylib => return true,
178 if !sess.target.target.options.executables {
179 if crate_type == config::CrateType::Executable {