1 // Copyright 2017 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
11 use rustc::session::config::{self, OutputFilenames, Input, OutputType};
12 use rustc::session::Session;
13 use rustc::middle::cstore;
14 use std::path::PathBuf;
18 pub fn find_crate_name(sess: Option<&Session>,
19 attrs: &[ast::Attribute],
20 input: &Input) -> String {
21 let validate = |s: String, span: Option<Span>| {
22 cstore::validate_crate_name(sess, &s, span);
26 // Look in attributes 100% of the time to make sure the attribute is marked
27 // as used. After doing this, however, we still prioritize a crate name from
28 // the command line over one found in the #[crate_name] attribute. If we
29 // find both we ensure that they're the same later on as well.
30 let attr_crate_name = attrs.iter().find(|at| at.check_name("crate_name"))
31 .and_then(|at| at.value_str().map(|s| (at, s)));
33 if let Some(sess) = sess {
34 if let Some(ref s) = sess.opts.crate_name {
35 if let Some((attr, name)) = attr_crate_name {
37 let msg = format!("--crate-name and #[crate_name] are \
38 required to match, but `{}` != `{}`",
40 sess.span_err(attr.span, &msg);
43 return validate(s.clone(), None);
47 if let Some((attr, s)) = attr_crate_name {
48 return validate(s.to_string(), Some(attr.span));
50 if let Input::File(ref path) = *input {
51 if let Some(s) = path.file_stem().and_then(|s| s.to_str()) {
52 if s.starts_with("-") {
53 let msg = format!("crate names cannot start with a `-`, but \
54 `{}` has a leading hyphen", s);
55 if let Some(sess) = sess {
59 return validate(s.replace("-", "_"), None);
64 "rust_out".to_string()
67 pub fn filename_for_input(sess: &Session,
68 crate_type: config::CrateType,
70 outputs: &OutputFilenames) -> PathBuf {
71 let libname = format!("{}{}", crate_name, sess.opts.cg.extra_filename);
74 config::CrateTypeRlib => {
75 outputs.out_directory.join(&format!("lib{}.rlib", libname))
77 config::CrateTypeCdylib |
78 config::CrateTypeProcMacro |
79 config::CrateTypeDylib => {
80 let (prefix, suffix) = (&sess.target.target.options.dll_prefix,
81 &sess.target.target.options.dll_suffix);
82 outputs.out_directory.join(&format!("{}{}{}", prefix, libname,
85 config::CrateTypeStaticlib => {
86 let (prefix, suffix) = (&sess.target.target.options.staticlib_prefix,
87 &sess.target.target.options.staticlib_suffix);
88 outputs.out_directory.join(&format!("{}{}{}", prefix, libname,
91 config::CrateTypeExecutable => {
92 let suffix = &sess.target.target.options.exe_suffix;
93 let out_filename = outputs.path(OutputType::Exe);
94 if suffix.is_empty() {
95 out_filename.to_path_buf()
97 out_filename.with_extension(&suffix[1..])
103 /// Returns default crate type for target
105 /// Default crate type is used when crate type isn't provided neither
106 /// through cmd line arguments nor through crate attributes
108 /// It is CrateTypeExecutable for all platforms but iOS as there is no
109 /// way to run iOS binaries anyway without jailbreaking and
110 /// interaction with Rust code through static library is the only
112 pub fn default_output_for_target(sess: &Session) -> config::CrateType {
113 if !sess.target.target.options.executables {
114 config::CrateTypeStaticlib
116 config::CrateTypeExecutable
120 /// Checks if target supports crate_type as output
121 pub fn invalid_output_for_target(sess: &Session,
122 crate_type: config::CrateType) -> bool {
123 match (sess.target.target.options.dynamic_linking,
124 sess.target.target.options.executables, crate_type) {
125 (false, _, config::CrateTypeCdylib) |
126 (false, _, config::CrateTypeDylib) |
127 (false, _, config::CrateTypeProcMacro) => true,
128 (true, _, config::CrateTypeCdylib) |
129 (true, _, config::CrateTypeDylib) => sess.crt_static() &&
130 !sess.target.target.options.crt_static_allows_dylibs,
131 (_, false, config::CrateTypeExecutable) => true,