2019-12-22 17:42:04 -05:00
|
|
|
use rustc::session::config::{self, Input, OutputFilenames, OutputType};
|
2017-08-12 10:48:57 +02:00
|
|
|
use rustc::session::Session;
|
2017-08-26 17:31:32 +02:00
|
|
|
use std::path::{Path, PathBuf};
|
2019-05-08 13:21:18 +10:00
|
|
|
use syntax::symbol::sym;
|
2019-12-22 17:42:04 -05:00
|
|
|
use syntax::{ast, attr};
|
2017-08-05 16:55:23 +02:00
|
|
|
use syntax_pos::Span;
|
|
|
|
|
2019-12-22 17:42:04 -05:00
|
|
|
pub fn out_filename(
|
|
|
|
sess: &Session,
|
|
|
|
crate_type: config::CrateType,
|
|
|
|
outputs: &OutputFilenames,
|
|
|
|
crate_name: &str,
|
|
|
|
) -> PathBuf {
|
2017-08-26 17:31:32 +02:00
|
|
|
let default_filename = filename_for_input(sess, crate_type, crate_name, outputs);
|
2019-12-22 17:42:04 -05:00
|
|
|
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);
|
2017-08-26 17:31:32 +02:00
|
|
|
|
|
|
|
check_file_is_writeable(&out_filename, sess);
|
|
|
|
|
|
|
|
out_filename
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make sure files are writeable. Mac, FreeBSD, and Windows system linkers
|
|
|
|
// check this already -- however, the Linux linker will happily overwrite a
|
|
|
|
// read-only file. We should be consistent.
|
|
|
|
pub fn check_file_is_writeable(file: &Path, sess: &Session) {
|
|
|
|
if !is_writeable(file) {
|
2019-12-22 17:42:04 -05:00
|
|
|
sess.fatal(&format!(
|
|
|
|
"output file {} is not writeable -- check its \
|
|
|
|
permissions",
|
|
|
|
file.display()
|
|
|
|
));
|
2017-08-26 17:31:32 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn is_writeable(p: &Path) -> bool {
|
|
|
|
match p.metadata() {
|
|
|
|
Err(..) => true,
|
2019-12-22 17:42:04 -05:00
|
|
|
Ok(m) => !m.permissions().readonly(),
|
2017-08-26 17:31:32 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-22 17:42:04 -05:00
|
|
|
pub fn find_crate_name(sess: Option<&Session>, attrs: &[ast::Attribute], input: &Input) -> String {
|
2017-08-05 16:55:23 +02:00
|
|
|
let validate = |s: String, span: Option<Span>| {
|
2019-02-08 21:06:07 +09:00
|
|
|
rustc_metadata::validate_crate_name(sess, &s, span);
|
2017-08-05 16:55:23 +02:00
|
|
|
s
|
|
|
|
};
|
|
|
|
|
|
|
|
// Look in attributes 100% of the time to make sure the attribute is marked
|
|
|
|
// as used. After doing this, however, we still prioritize a crate name from
|
|
|
|
// the command line over one found in the #[crate_name] attribute. If we
|
|
|
|
// find both we ensure that they're the same later on as well.
|
2019-12-22 17:42:04 -05:00
|
|
|
let attr_crate_name =
|
|
|
|
attr::find_by_name(attrs, sym::crate_name).and_then(|at| at.value_str().map(|s| (at, s)));
|
2017-08-05 16:55:23 +02:00
|
|
|
|
|
|
|
if let Some(sess) = sess {
|
|
|
|
if let Some(ref s) = sess.opts.crate_name {
|
|
|
|
if let Some((attr, name)) = attr_crate_name {
|
2019-05-07 16:03:44 +10:00
|
|
|
if name.as_str() != *s {
|
2019-12-22 17:42:04 -05:00
|
|
|
let msg = format!(
|
|
|
|
"`--crate-name` and `#[crate_name]` are \
|
2017-08-05 16:55:23 +02:00
|
|
|
required to match, but `{}` != `{}`",
|
2019-12-22 17:42:04 -05:00
|
|
|
s, name
|
|
|
|
);
|
2017-08-05 16:55:23 +02:00
|
|
|
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("-") {
|
2019-12-22 17:42:04 -05:00
|
|
|
let msg = format!(
|
|
|
|
"crate names cannot start with a `-`, but \
|
|
|
|
`{}` has a leading hyphen",
|
|
|
|
s
|
|
|
|
);
|
2017-08-05 16:55:23 +02:00
|
|
|
if let Some(sess) = sess {
|
|
|
|
sess.err(&msg);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return validate(s.replace("-", "_"), None);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
"rust_out".to_string()
|
|
|
|
}
|
|
|
|
|
2019-12-22 17:42:04 -05:00
|
|
|
pub fn filename_for_metadata(
|
|
|
|
sess: &Session,
|
|
|
|
crate_name: &str,
|
|
|
|
outputs: &OutputFilenames,
|
|
|
|
) -> PathBuf {
|
2018-09-22 22:24:32 +02:00
|
|
|
let libname = format!("{}{}", crate_name, sess.opts.cg.extra_filename);
|
|
|
|
|
2019-12-22 17:42:04 -05:00
|
|
|
let out_filename = outputs
|
|
|
|
.single_output_file
|
|
|
|
.clone()
|
2018-10-12 16:16:00 +02:00
|
|
|
.unwrap_or_else(|| outputs.out_directory.join(&format!("lib{}.rmeta", libname)));
|
2018-09-22 22:24:32 +02:00
|
|
|
|
|
|
|
check_file_is_writeable(&out_filename, sess);
|
|
|
|
|
|
|
|
out_filename
|
|
|
|
}
|
|
|
|
|
2019-12-22 17:42:04 -05:00
|
|
|
pub fn filename_for_input(
|
|
|
|
sess: &Session,
|
|
|
|
crate_type: config::CrateType,
|
|
|
|
crate_name: &str,
|
|
|
|
outputs: &OutputFilenames,
|
|
|
|
) -> PathBuf {
|
2017-08-05 16:55:23 +02:00
|
|
|
let libname = format!("{}{}", crate_name, sess.opts.cg.extra_filename);
|
|
|
|
|
|
|
|
match crate_type {
|
2019-12-22 17:42:04 -05:00
|
|
|
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))
|
2017-08-05 16:55:23 +02:00
|
|
|
}
|
2018-07-26 11:13:11 -06:00
|
|
|
config::CrateType::Staticlib => {
|
2019-12-22 17:42:04 -05:00
|
|
|
let (prefix, suffix) = (
|
|
|
|
&sess.target.target.options.staticlib_prefix,
|
|
|
|
&sess.target.target.options.staticlib_suffix,
|
|
|
|
);
|
|
|
|
outputs.out_directory.join(&format!("{}{}{}", prefix, libname, suffix))
|
2017-08-05 16:55:23 +02:00
|
|
|
}
|
2018-07-26 11:13:11 -06:00
|
|
|
config::CrateType::Executable => {
|
2017-08-05 16:55:23 +02:00
|
|
|
let suffix = &sess.target.target.options.exe_suffix;
|
|
|
|
let out_filename = outputs.path(OutputType::Exe);
|
2019-12-22 17:42:04 -05:00
|
|
|
if suffix.is_empty() { out_filename } else { out_filename.with_extension(&suffix[1..]) }
|
2017-08-05 16:55:23 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns default crate type for target
|
|
|
|
///
|
|
|
|
/// Default crate type is used when crate type isn't provided neither
|
|
|
|
/// through cmd line arguments nor through crate attributes
|
|
|
|
///
|
2018-07-26 11:13:11 -06:00
|
|
|
/// It is CrateType::Executable for all platforms but iOS as there is no
|
2017-08-05 16:55:23 +02:00
|
|
|
/// way to run iOS binaries anyway without jailbreaking and
|
|
|
|
/// interaction with Rust code through static library is the only
|
|
|
|
/// option for now
|
|
|
|
pub fn default_output_for_target(sess: &Session) -> config::CrateType {
|
|
|
|
if !sess.target.target.options.executables {
|
2018-07-26 11:13:11 -06:00
|
|
|
config::CrateType::Staticlib
|
2017-08-05 16:55:23 +02:00
|
|
|
} else {
|
2018-07-26 11:13:11 -06:00
|
|
|
config::CrateType::Executable
|
2017-08-05 16:55:23 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Checks if target supports crate_type as output
|
2019-12-22 17:42:04 -05:00
|
|
|
pub fn invalid_output_for_target(sess: &Session, crate_type: config::CrateType) -> bool {
|
2017-10-22 20:01:00 -07:00
|
|
|
match crate_type {
|
2019-12-22 17:42:04 -05:00
|
|
|
config::CrateType::Cdylib | config::CrateType::Dylib | config::CrateType::ProcMacro => {
|
2017-10-22 20:01:00 -07:00
|
|
|
if !sess.target.target.options.dynamic_linking {
|
2019-12-22 17:42:04 -05:00
|
|
|
return true;
|
2017-10-22 20:01:00 -07:00
|
|
|
}
|
|
|
|
if sess.crt_static() && !sess.target.target.options.crt_static_allows_dylibs {
|
2019-12-22 17:42:04 -05:00
|
|
|
return true;
|
2017-10-22 20:01:00 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => {}
|
2017-08-05 16:55:23 +02:00
|
|
|
}
|
2017-10-22 20:01:00 -07:00
|
|
|
if sess.target.target.options.only_cdylib {
|
|
|
|
match crate_type {
|
2018-07-26 11:13:11 -06:00
|
|
|
config::CrateType::ProcMacro | config::CrateType::Dylib => return true,
|
2017-10-22 20:01:00 -07:00
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !sess.target.target.options.executables {
|
2018-07-26 11:13:11 -06:00
|
|
|
if crate_type == config::CrateType::Executable {
|
2019-12-22 17:42:04 -05:00
|
|
|
return true;
|
2017-10-22 20:01:00 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
false
|
2017-08-05 16:55:23 +02:00
|
|
|
}
|