2022-08-19 13:48:15 +00:00
|
|
|
use crate::errors;
|
2022-08-31 13:09:26 +00:00
|
|
|
use info;
|
2021-11-08 23:03:55 +00:00
|
|
|
use libloading::Library;
|
2021-06-25 09:56:14 +00:00
|
|
|
use rustc_ast as ast;
|
2020-03-12 23:07:58 +00:00
|
|
|
use rustc_codegen_ssa::traits::CodegenBackend;
|
Clean up config mess.
`parse_cfgspecs` and `parse_check_cfg` run very early, before the main
interner is running. They each use a short-lived interner and convert
all interned symbols to strings in their output data structures. Once
the main interner starts up, these data structures get converted into
new data structures that are identical except with the strings converted
to symbols.
All is not obvious from the current code, which is a mess, particularly
with inconsistent naming that obscures the parallel string/symbol data
structures. This commit clean things up a lot.
- The existing `CheckCfg` type is generic, allowing both
`CheckCfg<String>` and `CheckCfg<Symbol>` forms. This is really
useful, but it defaults to `String`. The commit removes the default so
we have to use `CheckCfg<String>` and `CheckCfg<Symbol>` explicitly,
which makes things clearer.
- Introduces `Cfg`, which is generic over `String` and `Symbol`, similar
to `CheckCfg`.
- Renames some things.
- `parse_cfgspecs` -> `parse_cfg`
- `CfgSpecs` -> `Cfg<String>`, plus it's used in more places, rather
than the underlying `FxHashSet` type.
- `CrateConfig` -> `Cfg<Symbol>`.
- `CrateCheckConfig` -> `CheckCfg<Symbol>`
- Adds some comments explaining the string-to-symbol conversions.
- `to_crate_check_config`, which converts `CheckCfg<String>` to
`CheckCfg<Symbol>`, is inlined and removed and combined with the
overly-general `CheckCfg::map_data` to produce
`CheckCfg::<String>::intern`.
- `build_configuration` now does the `Cfg<String>`-to-`Cfg<Symbol>`
conversion, so callers don't need to, which removes the need for
`to_crate_config`.
The diff for two of the fields in `Config` is a good example of the
improved clarity:
```
- pub crate_cfg: FxHashSet<(String, Option<String>)>,
- pub crate_check_cfg: CheckCfg,
+ pub crate_cfg: Cfg<String>,
+ pub crate_check_cfg: CheckCfg<String>,
```
Compare that with the diff for the corresponding fields in `ParseSess`,
and the relationship to `Config` is much clearer than before:
```
- pub config: CrateConfig,
- pub check_config: CrateCheckConfig,
+ pub config: Cfg<Symbol>,
+ pub check_config: CheckCfg<Symbol>,
```
2023-10-27 04:58:02 +00:00
|
|
|
use rustc_data_structures::fx::FxHashMap;
|
2020-10-31 11:01:54 +00:00
|
|
|
#[cfg(parallel_compiler)]
|
|
|
|
use rustc_data_structures::sync;
|
2018-12-08 19:30:23 +00:00
|
|
|
use rustc_errors::registry::Registry;
|
2021-09-17 20:08:56 +00:00
|
|
|
use rustc_parse::validate_attr;
|
2020-01-05 08:40:16 +00:00
|
|
|
use rustc_session as session;
|
Clean up config mess.
`parse_cfgspecs` and `parse_check_cfg` run very early, before the main
interner is running. They each use a short-lived interner and convert
all interned symbols to strings in their output data structures. Once
the main interner starts up, these data structures get converted into
new data structures that are identical except with the strings converted
to symbols.
All is not obvious from the current code, which is a mess, particularly
with inconsistent naming that obscures the parallel string/symbol data
structures. This commit clean things up a lot.
- The existing `CheckCfg` type is generic, allowing both
`CheckCfg<String>` and `CheckCfg<Symbol>` forms. This is really
useful, but it defaults to `String`. The commit removes the default so
we have to use `CheckCfg<String>` and `CheckCfg<Symbol>` explicitly,
which makes things clearer.
- Introduces `Cfg`, which is generic over `String` and `Symbol`, similar
to `CheckCfg`.
- Renames some things.
- `parse_cfgspecs` -> `parse_cfg`
- `CfgSpecs` -> `Cfg<String>`, plus it's used in more places, rather
than the underlying `FxHashSet` type.
- `CrateConfig` -> `Cfg<Symbol>`.
- `CrateCheckConfig` -> `CheckCfg<Symbol>`
- Adds some comments explaining the string-to-symbol conversions.
- `to_crate_check_config`, which converts `CheckCfg<String>` to
`CheckCfg<Symbol>`, is inlined and removed and combined with the
overly-general `CheckCfg::map_data` to produce
`CheckCfg::<String>::intern`.
- `build_configuration` now does the `Cfg<String>`-to-`Cfg<Symbol>`
conversion, so callers don't need to, which removes the need for
`to_crate_config`.
The diff for two of the fields in `Config` is a good example of the
improved clarity:
```
- pub crate_cfg: FxHashSet<(String, Option<String>)>,
- pub crate_check_cfg: CheckCfg,
+ pub crate_cfg: Cfg<String>,
+ pub crate_check_cfg: CheckCfg<String>,
```
Compare that with the diff for the corresponding fields in `ParseSess`,
and the relationship to `Config` is much clearer than before:
```
- pub config: CrateConfig,
- pub check_config: CrateCheckConfig,
+ pub config: Cfg<Symbol>,
+ pub check_config: CheckCfg<Symbol>,
```
2023-10-27 04:58:02 +00:00
|
|
|
use rustc_session::config::{
|
|
|
|
self, Cfg, CheckCfg, CrateType, OutFileName, OutputFilenames, OutputTypes,
|
|
|
|
};
|
2022-10-28 07:20:51 +00:00
|
|
|
use rustc_session::filesearch::sysroot_candidates;
|
2020-03-11 11:49:08 +00:00
|
|
|
use rustc_session::lint::{self, BuiltinLintDiagnostics, LintBuffer};
|
2023-06-22 21:56:09 +00:00
|
|
|
use rustc_session::{filesearch, output, Session};
|
2023-02-19 04:03:56 +00:00
|
|
|
use rustc_span::edit_distance::find_best_match_for_name;
|
2019-12-31 17:15:40 +00:00
|
|
|
use rustc_span::edition::Edition;
|
2020-05-27 18:34:17 +00:00
|
|
|
use rustc_span::source_map::FileLoader;
|
2020-01-01 18:30:57 +00:00
|
|
|
use rustc_span::symbol::{sym, Symbol};
|
2023-06-22 21:56:09 +00:00
|
|
|
use session::{CompilerIO, EarlyErrorHandler};
|
2018-12-08 19:30:23 +00:00
|
|
|
use std::env;
|
2020-10-15 12:23:43 +00:00
|
|
|
use std::env::consts::{DLL_PREFIX, DLL_SUFFIX};
|
2018-12-08 19:30:23 +00:00
|
|
|
use std::mem;
|
|
|
|
use std::path::{Path, PathBuf};
|
2020-10-15 12:23:43 +00:00
|
|
|
use std::sync::atomic::{AtomicBool, Ordering};
|
2023-10-16 20:11:57 +00:00
|
|
|
use std::sync::{Arc, OnceLock};
|
2020-11-18 18:02:31 +00:00
|
|
|
use std::thread;
|
2018-12-08 19:30:23 +00:00
|
|
|
|
2021-11-08 23:03:55 +00:00
|
|
|
/// Function pointer type that constructs a new CodegenBackend.
|
|
|
|
pub type MakeBackendFn = fn() -> Box<dyn CodegenBackend>;
|
|
|
|
|
2018-12-08 19:30:23 +00:00
|
|
|
/// Adds `target_feature = "..."` cfgs for a variety of platform
|
|
|
|
/// specific features (SSE, NEON etc.).
|
|
|
|
///
|
2020-07-07 15:12:44 +00:00
|
|
|
/// This is performed by checking whether a set of permitted features
|
2023-03-28 00:19:46 +00:00
|
|
|
/// is available on the target machine, by querying the codegen backend.
|
2018-12-08 19:30:23 +00:00
|
|
|
pub fn add_configuration(
|
Clean up config mess.
`parse_cfgspecs` and `parse_check_cfg` run very early, before the main
interner is running. They each use a short-lived interner and convert
all interned symbols to strings in their output data structures. Once
the main interner starts up, these data structures get converted into
new data structures that are identical except with the strings converted
to symbols.
All is not obvious from the current code, which is a mess, particularly
with inconsistent naming that obscures the parallel string/symbol data
structures. This commit clean things up a lot.
- The existing `CheckCfg` type is generic, allowing both
`CheckCfg<String>` and `CheckCfg<Symbol>` forms. This is really
useful, but it defaults to `String`. The commit removes the default so
we have to use `CheckCfg<String>` and `CheckCfg<Symbol>` explicitly,
which makes things clearer.
- Introduces `Cfg`, which is generic over `String` and `Symbol`, similar
to `CheckCfg`.
- Renames some things.
- `parse_cfgspecs` -> `parse_cfg`
- `CfgSpecs` -> `Cfg<String>`, plus it's used in more places, rather
than the underlying `FxHashSet` type.
- `CrateConfig` -> `Cfg<Symbol>`.
- `CrateCheckConfig` -> `CheckCfg<Symbol>`
- Adds some comments explaining the string-to-symbol conversions.
- `to_crate_check_config`, which converts `CheckCfg<String>` to
`CheckCfg<Symbol>`, is inlined and removed and combined with the
overly-general `CheckCfg::map_data` to produce
`CheckCfg::<String>::intern`.
- `build_configuration` now does the `Cfg<String>`-to-`Cfg<Symbol>`
conversion, so callers don't need to, which removes the need for
`to_crate_config`.
The diff for two of the fields in `Config` is a good example of the
improved clarity:
```
- pub crate_cfg: FxHashSet<(String, Option<String>)>,
- pub crate_check_cfg: CheckCfg,
+ pub crate_cfg: Cfg<String>,
+ pub crate_check_cfg: CheckCfg<String>,
```
Compare that with the diff for the corresponding fields in `ParseSess`,
and the relationship to `Config` is much clearer than before:
```
- pub config: CrateConfig,
- pub check_config: CrateCheckConfig,
+ pub config: Cfg<Symbol>,
+ pub check_config: CheckCfg<Symbol>,
```
2023-10-27 04:58:02 +00:00
|
|
|
cfg: &mut Cfg<Symbol>,
|
2020-02-12 15:48:03 +00:00
|
|
|
sess: &mut Session,
|
2018-12-08 19:30:23 +00:00
|
|
|
codegen_backend: &dyn CodegenBackend,
|
|
|
|
) {
|
2019-05-22 02:42:23 +00:00
|
|
|
let tf = sym::target_feature;
|
2018-12-08 19:30:23 +00:00
|
|
|
|
2022-07-11 13:26:58 +00:00
|
|
|
let unstable_target_features = codegen_backend.target_features(sess, true);
|
|
|
|
sess.unstable_target_features.extend(unstable_target_features.iter().cloned());
|
|
|
|
|
|
|
|
let target_features = codegen_backend.target_features(sess, false);
|
2020-02-12 15:48:03 +00:00
|
|
|
sess.target_features.extend(target_features.iter().cloned());
|
|
|
|
|
|
|
|
cfg.extend(target_features.into_iter().map(|feat| (tf, Some(feat))));
|
2018-12-08 19:30:23 +00:00
|
|
|
|
2020-05-01 20:32:00 +00:00
|
|
|
if sess.crt_static(None) {
|
2020-07-08 01:04:10 +00:00
|
|
|
cfg.insert((tf, Some(sym::crt_dash_static)));
|
2018-12-08 19:30:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-08 19:30:23 +00:00
|
|
|
pub fn create_session(
|
2023-06-22 21:56:09 +00:00
|
|
|
handler: &EarlyErrorHandler,
|
2018-12-08 19:30:23 +00:00
|
|
|
sopts: config::Options,
|
Clean up config mess.
`parse_cfgspecs` and `parse_check_cfg` run very early, before the main
interner is running. They each use a short-lived interner and convert
all interned symbols to strings in their output data structures. Once
the main interner starts up, these data structures get converted into
new data structures that are identical except with the strings converted
to symbols.
All is not obvious from the current code, which is a mess, particularly
with inconsistent naming that obscures the parallel string/symbol data
structures. This commit clean things up a lot.
- The existing `CheckCfg` type is generic, allowing both
`CheckCfg<String>` and `CheckCfg<Symbol>` forms. This is really
useful, but it defaults to `String`. The commit removes the default so
we have to use `CheckCfg<String>` and `CheckCfg<Symbol>` explicitly,
which makes things clearer.
- Introduces `Cfg`, which is generic over `String` and `Symbol`, similar
to `CheckCfg`.
- Renames some things.
- `parse_cfgspecs` -> `parse_cfg`
- `CfgSpecs` -> `Cfg<String>`, plus it's used in more places, rather
than the underlying `FxHashSet` type.
- `CrateConfig` -> `Cfg<Symbol>`.
- `CrateCheckConfig` -> `CheckCfg<Symbol>`
- Adds some comments explaining the string-to-symbol conversions.
- `to_crate_check_config`, which converts `CheckCfg<String>` to
`CheckCfg<Symbol>`, is inlined and removed and combined with the
overly-general `CheckCfg::map_data` to produce
`CheckCfg::<String>::intern`.
- `build_configuration` now does the `Cfg<String>`-to-`Cfg<Symbol>`
conversion, so callers don't need to, which removes the need for
`to_crate_config`.
The diff for two of the fields in `Config` is a good example of the
improved clarity:
```
- pub crate_cfg: FxHashSet<(String, Option<String>)>,
- pub crate_check_cfg: CheckCfg,
+ pub crate_cfg: Cfg<String>,
+ pub crate_check_cfg: CheckCfg<String>,
```
Compare that with the diff for the corresponding fields in `ParseSess`,
and the relationship to `Config` is much clearer than before:
```
- pub config: CrateConfig,
- pub check_config: CrateCheckConfig,
+ pub config: Cfg<Symbol>,
+ pub check_config: CheckCfg<Symbol>,
```
2023-10-27 04:58:02 +00:00
|
|
|
cfg: Cfg<String>,
|
|
|
|
check_cfg: CheckCfg<String>,
|
2022-10-13 09:13:02 +00:00
|
|
|
locale_resources: &'static [&'static str],
|
2018-12-08 19:30:23 +00:00
|
|
|
file_loader: Option<Box<dyn FileLoader + Send + Sync + 'static>>,
|
2022-12-07 09:24:00 +00:00
|
|
|
io: CompilerIO,
|
2018-12-08 19:30:23 +00:00
|
|
|
lint_caps: FxHashMap<lint::LintId, lint::Level>,
|
2020-09-08 11:44:41 +00:00
|
|
|
make_codegen_backend: Option<
|
|
|
|
Box<dyn FnOnce(&config::Options) -> Box<dyn CodegenBackend> + Send>,
|
|
|
|
>,
|
2019-11-15 18:41:50 +00:00
|
|
|
descriptions: Registry,
|
2023-03-03 22:25:18 +00:00
|
|
|
ice_file: Option<PathBuf>,
|
2023-10-16 20:11:57 +00:00
|
|
|
using_internal_features: Arc<AtomicBool>,
|
2023-07-03 11:11:27 +00:00
|
|
|
expanded_args: Vec<String>,
|
2022-10-07 05:20:20 +00:00
|
|
|
) -> (Session, Box<dyn CodegenBackend>) {
|
2020-09-08 11:44:41 +00:00
|
|
|
let codegen_backend = if let Some(make_codegen_backend) = make_codegen_backend {
|
|
|
|
make_codegen_backend(&sopts)
|
|
|
|
} else {
|
2023-06-22 21:56:09 +00:00
|
|
|
get_codegen_backend(
|
|
|
|
handler,
|
|
|
|
&sopts.maybe_sysroot,
|
|
|
|
sopts.unstable_opts.codegen_backend.as_deref(),
|
|
|
|
)
|
2020-09-08 11:44:41 +00:00
|
|
|
};
|
|
|
|
|
2020-09-17 10:01:12 +00:00
|
|
|
// target_override is documented to be called before init(), so this is okay
|
|
|
|
let target_override = codegen_backend.target_override(&sopts);
|
|
|
|
|
2022-04-06 03:16:07 +00:00
|
|
|
let bundle = match rustc_errors::fluent_bundle(
|
|
|
|
sopts.maybe_sysroot.clone(),
|
2022-10-28 07:20:51 +00:00
|
|
|
sysroot_candidates().to_vec(),
|
2022-07-06 12:44:47 +00:00
|
|
|
sopts.unstable_opts.translate_lang.clone(),
|
|
|
|
sopts.unstable_opts.translate_additional_ftl.as_deref(),
|
|
|
|
sopts.unstable_opts.translate_directionality_markers,
|
2022-04-06 03:16:07 +00:00
|
|
|
) {
|
|
|
|
Ok(bundle) => bundle,
|
|
|
|
Err(e) => {
|
2023-06-22 21:56:09 +00:00
|
|
|
handler.early_error(format!("failed to load fluent bundle: {e}"));
|
2022-04-06 03:16:07 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-10-17 13:11:26 +00:00
|
|
|
let mut locale_resources = Vec::from(locale_resources);
|
|
|
|
locale_resources.push(codegen_backend.locale_resource());
|
|
|
|
|
2020-05-27 18:34:17 +00:00
|
|
|
let mut sess = session::build_session(
|
2023-06-22 21:56:09 +00:00
|
|
|
handler,
|
2018-12-08 19:30:23 +00:00
|
|
|
sopts,
|
2022-12-07 09:24:00 +00:00
|
|
|
io,
|
2022-04-06 03:16:07 +00:00
|
|
|
bundle,
|
2018-12-08 19:30:23 +00:00
|
|
|
descriptions,
|
2022-10-13 09:13:02 +00:00
|
|
|
locale_resources,
|
2018-12-08 19:30:23 +00:00
|
|
|
lint_caps,
|
2020-03-31 05:17:15 +00:00
|
|
|
file_loader,
|
2020-09-17 10:01:12 +00:00
|
|
|
target_override,
|
2023-05-08 09:12:38 +00:00
|
|
|
rustc_version_str().unwrap_or("unknown"),
|
2023-03-03 22:25:18 +00:00
|
|
|
ice_file,
|
2023-10-16 20:11:57 +00:00
|
|
|
using_internal_features,
|
2023-07-03 11:11:27 +00:00
|
|
|
expanded_args,
|
2018-12-08 19:30:23 +00:00
|
|
|
);
|
|
|
|
|
2020-09-17 10:01:12 +00:00
|
|
|
codegen_backend.init(&sess);
|
2018-12-08 19:30:23 +00:00
|
|
|
|
Clean up config mess.
`parse_cfgspecs` and `parse_check_cfg` run very early, before the main
interner is running. They each use a short-lived interner and convert
all interned symbols to strings in their output data structures. Once
the main interner starts up, these data structures get converted into
new data structures that are identical except with the strings converted
to symbols.
All is not obvious from the current code, which is a mess, particularly
with inconsistent naming that obscures the parallel string/symbol data
structures. This commit clean things up a lot.
- The existing `CheckCfg` type is generic, allowing both
`CheckCfg<String>` and `CheckCfg<Symbol>` forms. This is really
useful, but it defaults to `String`. The commit removes the default so
we have to use `CheckCfg<String>` and `CheckCfg<Symbol>` explicitly,
which makes things clearer.
- Introduces `Cfg`, which is generic over `String` and `Symbol`, similar
to `CheckCfg`.
- Renames some things.
- `parse_cfgspecs` -> `parse_cfg`
- `CfgSpecs` -> `Cfg<String>`, plus it's used in more places, rather
than the underlying `FxHashSet` type.
- `CrateConfig` -> `Cfg<Symbol>`.
- `CrateCheckConfig` -> `CheckCfg<Symbol>`
- Adds some comments explaining the string-to-symbol conversions.
- `to_crate_check_config`, which converts `CheckCfg<String>` to
`CheckCfg<Symbol>`, is inlined and removed and combined with the
overly-general `CheckCfg::map_data` to produce
`CheckCfg::<String>::intern`.
- `build_configuration` now does the `Cfg<String>`-to-`Cfg<Symbol>`
conversion, so callers don't need to, which removes the need for
`to_crate_config`.
The diff for two of the fields in `Config` is a good example of the
improved clarity:
```
- pub crate_cfg: FxHashSet<(String, Option<String>)>,
- pub crate_check_cfg: CheckCfg,
+ pub crate_cfg: Cfg<String>,
+ pub crate_check_cfg: CheckCfg<String>,
```
Compare that with the diff for the corresponding fields in `ParseSess`,
and the relationship to `Config` is much clearer than before:
```
- pub config: CrateConfig,
- pub check_config: CrateCheckConfig,
+ pub config: Cfg<Symbol>,
+ pub check_config: CheckCfg<Symbol>,
```
2023-10-27 04:58:02 +00:00
|
|
|
let mut cfg = config::build_configuration(&sess, cfg);
|
2020-02-12 15:48:03 +00:00
|
|
|
add_configuration(&mut cfg, &mut sess, &*codegen_backend);
|
2021-09-29 00:39:30 +00:00
|
|
|
|
Clean up config mess.
`parse_cfgspecs` and `parse_check_cfg` run very early, before the main
interner is running. They each use a short-lived interner and convert
all interned symbols to strings in their output data structures. Once
the main interner starts up, these data structures get converted into
new data structures that are identical except with the strings converted
to symbols.
All is not obvious from the current code, which is a mess, particularly
with inconsistent naming that obscures the parallel string/symbol data
structures. This commit clean things up a lot.
- The existing `CheckCfg` type is generic, allowing both
`CheckCfg<String>` and `CheckCfg<Symbol>` forms. This is really
useful, but it defaults to `String`. The commit removes the default so
we have to use `CheckCfg<String>` and `CheckCfg<Symbol>` explicitly,
which makes things clearer.
- Introduces `Cfg`, which is generic over `String` and `Symbol`, similar
to `CheckCfg`.
- Renames some things.
- `parse_cfgspecs` -> `parse_cfg`
- `CfgSpecs` -> `Cfg<String>`, plus it's used in more places, rather
than the underlying `FxHashSet` type.
- `CrateConfig` -> `Cfg<Symbol>`.
- `CrateCheckConfig` -> `CheckCfg<Symbol>`
- Adds some comments explaining the string-to-symbol conversions.
- `to_crate_check_config`, which converts `CheckCfg<String>` to
`CheckCfg<Symbol>`, is inlined and removed and combined with the
overly-general `CheckCfg::map_data` to produce
`CheckCfg::<String>::intern`.
- `build_configuration` now does the `Cfg<String>`-to-`Cfg<Symbol>`
conversion, so callers don't need to, which removes the need for
`to_crate_config`.
The diff for two of the fields in `Config` is a good example of the
improved clarity:
```
- pub crate_cfg: FxHashSet<(String, Option<String>)>,
- pub crate_check_cfg: CheckCfg,
+ pub crate_cfg: Cfg<String>,
+ pub crate_check_cfg: CheckCfg<String>,
```
Compare that with the diff for the corresponding fields in `ParseSess`,
and the relationship to `Config` is much clearer than before:
```
- pub config: CrateConfig,
- pub check_config: CrateCheckConfig,
+ pub config: Cfg<Symbol>,
+ pub check_config: CheckCfg<Symbol>,
```
2023-10-27 04:58:02 +00:00
|
|
|
let mut check_cfg = check_cfg.intern();
|
2023-03-09 20:10:08 +00:00
|
|
|
check_cfg.fill_well_known(&sess.target);
|
2021-09-29 00:39:30 +00:00
|
|
|
|
Clean up config mess.
`parse_cfgspecs` and `parse_check_cfg` run very early, before the main
interner is running. They each use a short-lived interner and convert
all interned symbols to strings in their output data structures. Once
the main interner starts up, these data structures get converted into
new data structures that are identical except with the strings converted
to symbols.
All is not obvious from the current code, which is a mess, particularly
with inconsistent naming that obscures the parallel string/symbol data
structures. This commit clean things up a lot.
- The existing `CheckCfg` type is generic, allowing both
`CheckCfg<String>` and `CheckCfg<Symbol>` forms. This is really
useful, but it defaults to `String`. The commit removes the default so
we have to use `CheckCfg<String>` and `CheckCfg<Symbol>` explicitly,
which makes things clearer.
- Introduces `Cfg`, which is generic over `String` and `Symbol`, similar
to `CheckCfg`.
- Renames some things.
- `parse_cfgspecs` -> `parse_cfg`
- `CfgSpecs` -> `Cfg<String>`, plus it's used in more places, rather
than the underlying `FxHashSet` type.
- `CrateConfig` -> `Cfg<Symbol>`.
- `CrateCheckConfig` -> `CheckCfg<Symbol>`
- Adds some comments explaining the string-to-symbol conversions.
- `to_crate_check_config`, which converts `CheckCfg<String>` to
`CheckCfg<Symbol>`, is inlined and removed and combined with the
overly-general `CheckCfg::map_data` to produce
`CheckCfg::<String>::intern`.
- `build_configuration` now does the `Cfg<String>`-to-`Cfg<Symbol>`
conversion, so callers don't need to, which removes the need for
`to_crate_config`.
The diff for two of the fields in `Config` is a good example of the
improved clarity:
```
- pub crate_cfg: FxHashSet<(String, Option<String>)>,
- pub crate_check_cfg: CheckCfg,
+ pub crate_cfg: Cfg<String>,
+ pub crate_check_cfg: CheckCfg<String>,
```
Compare that with the diff for the corresponding fields in `ParseSess`,
and the relationship to `Config` is much clearer than before:
```
- pub config: CrateConfig,
- pub check_config: CrateCheckConfig,
+ pub config: Cfg<Symbol>,
+ pub check_config: CheckCfg<Symbol>,
```
2023-10-27 04:58:02 +00:00
|
|
|
// These configs use symbols, rather than strings.
|
2018-12-08 19:30:23 +00:00
|
|
|
sess.parse_sess.config = cfg;
|
2021-09-29 00:39:30 +00:00
|
|
|
sess.parse_sess.check_config = check_cfg;
|
2018-12-08 19:30:23 +00:00
|
|
|
|
2022-10-07 05:20:20 +00:00
|
|
|
(sess, codegen_backend)
|
2018-12-08 19:30:23 +00:00
|
|
|
}
|
|
|
|
|
2020-02-23 20:23:45 +00:00
|
|
|
const STACK_SIZE: usize = 8 * 1024 * 1024;
|
2018-12-08 19:30:23 +00:00
|
|
|
|
|
|
|
fn get_stack_size() -> Option<usize> {
|
|
|
|
// FIXME: Hacks on hacks. If the env is trying to override the stack size
|
|
|
|
// then *don't* set it explicitly.
|
2019-12-06 12:18:32 +00:00
|
|
|
env::var_os("RUST_MIN_STACK").is_none().then_some(STACK_SIZE)
|
2018-12-08 19:30:23 +00:00
|
|
|
}
|
|
|
|
|
2020-10-31 02:14:32 +00:00
|
|
|
pub(crate) fn run_in_thread_with_globals<F: FnOnce() -> R + Send, R: Send>(
|
2019-04-05 22:15:49 +00:00
|
|
|
edition: Edition,
|
2018-12-08 19:30:23 +00:00
|
|
|
f: F,
|
|
|
|
) -> R {
|
2022-10-09 23:50:14 +00:00
|
|
|
// The "thread pool" is a single spawned thread in the non-parallel
|
|
|
|
// compiler. We run on a spawned thread instead of the main thread (a) to
|
|
|
|
// provide control over the stack size, and (b) to increase similarity with
|
|
|
|
// the parallel compiler, in particular to ensure there is no accidental
|
|
|
|
// sharing of data between the main thread and the compilation thread
|
|
|
|
// (which might cause problems for the parallel compiler).
|
|
|
|
let mut builder = thread::Builder::new().name("rustc".to_string());
|
|
|
|
if let Some(size) = get_stack_size() {
|
|
|
|
builder = builder.stack_size(size);
|
|
|
|
}
|
2018-12-08 19:30:23 +00:00
|
|
|
|
2022-10-09 23:50:14 +00:00
|
|
|
// We build the session globals and run `f` on the spawned thread, because
|
|
|
|
// `SessionGlobals` does not impl `Send` in the non-parallel compiler.
|
|
|
|
thread::scope(|s| {
|
2022-10-07 20:43:15 +00:00
|
|
|
// `unwrap` is ok here because `spawn_scoped` only panics if the thread
|
|
|
|
// name contains null bytes.
|
|
|
|
let r = builder
|
|
|
|
.spawn_scoped(s, move || rustc_span::create_session_globals_then(edition, f))
|
|
|
|
.unwrap()
|
|
|
|
.join();
|
2020-07-07 03:15:02 +00:00
|
|
|
|
2022-10-07 20:43:15 +00:00
|
|
|
match r {
|
|
|
|
Ok(v) => v,
|
2022-10-09 23:50:14 +00:00
|
|
|
Err(e) => std::panic::resume_unwind(e),
|
2022-10-07 20:43:15 +00:00
|
|
|
}
|
|
|
|
})
|
2018-12-08 19:30:23 +00:00
|
|
|
}
|
|
|
|
|
2020-10-31 02:14:32 +00:00
|
|
|
#[cfg(not(parallel_compiler))]
|
|
|
|
pub(crate) fn run_in_thread_pool_with_globals<F: FnOnce() -> R + Send, R: Send>(
|
|
|
|
edition: Edition,
|
|
|
|
_threads: usize,
|
|
|
|
f: F,
|
|
|
|
) -> R {
|
|
|
|
run_in_thread_with_globals(edition, f)
|
|
|
|
}
|
|
|
|
|
2018-12-08 19:30:23 +00:00
|
|
|
#[cfg(parallel_compiler)]
|
2022-10-07 04:35:42 +00:00
|
|
|
pub(crate) fn run_in_thread_pool_with_globals<F: FnOnce() -> R + Send, R: Send>(
|
2019-04-05 22:15:49 +00:00
|
|
|
edition: Edition,
|
2019-09-30 20:27:28 +00:00
|
|
|
threads: usize,
|
2018-12-08 19:30:23 +00:00
|
|
|
f: F,
|
|
|
|
) -> R {
|
2020-10-31 02:14:32 +00:00
|
|
|
use rustc_data_structures::{jobserver, sync::FromDyn};
|
2022-10-09 23:50:14 +00:00
|
|
|
use rustc_middle::ty::tls;
|
2023-03-25 08:46:19 +00:00
|
|
|
use rustc_query_impl::QueryCtxt;
|
|
|
|
use rustc_query_system::query::{deadlock, QueryContext};
|
2022-10-09 23:50:14 +00:00
|
|
|
|
2020-10-31 11:01:54 +00:00
|
|
|
let registry = sync::Registry::new(threads);
|
2020-10-31 02:14:32 +00:00
|
|
|
|
|
|
|
if !sync::is_dyn_thread_safe() {
|
|
|
|
return run_in_thread_with_globals(edition, || {
|
|
|
|
// Register the thread for use with the `WorkerLocal` type.
|
|
|
|
registry.register();
|
|
|
|
|
|
|
|
f()
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-10-09 23:50:14 +00:00
|
|
|
let mut builder = rayon::ThreadPoolBuilder::new()
|
2019-09-12 19:34:43 +00:00
|
|
|
.thread_name(|_| "rustc".to_string())
|
2018-12-08 19:30:23 +00:00
|
|
|
.acquire_thread_handler(jobserver::acquire_thread)
|
|
|
|
.release_thread_handler(jobserver::release_thread)
|
2019-09-30 20:27:28 +00:00
|
|
|
.num_threads(threads)
|
2022-10-09 23:50:14 +00:00
|
|
|
.deadlock_handler(|| {
|
|
|
|
// On deadlock, creates a new thread and forwards information in thread
|
|
|
|
// locals to it. The new thread runs the deadlock handler.
|
2020-10-31 02:14:32 +00:00
|
|
|
let query_map = FromDyn::from(tls::with(|tcx| {
|
2023-03-25 08:46:19 +00:00
|
|
|
QueryCtxt::new(tcx)
|
2022-10-09 23:50:14 +00:00
|
|
|
.try_collect_active_jobs()
|
|
|
|
.expect("active jobs shouldn't be locked in deadlock handler")
|
2020-10-31 02:14:32 +00:00
|
|
|
}));
|
2023-03-25 01:12:13 +00:00
|
|
|
let registry = rayon_core::Registry::current();
|
2020-10-31 02:14:32 +00:00
|
|
|
thread::spawn(move || deadlock(query_map.into_inner(), ®istry));
|
2022-10-09 23:50:14 +00:00
|
|
|
});
|
2018-12-08 19:30:23 +00:00
|
|
|
if let Some(size) = get_stack_size() {
|
2022-10-09 23:50:14 +00:00
|
|
|
builder = builder.stack_size(size);
|
2018-12-08 19:30:23 +00:00
|
|
|
}
|
|
|
|
|
2022-10-09 23:50:14 +00:00
|
|
|
// We create the session globals on the main thread, then create the thread
|
|
|
|
// pool. Upon creation, each worker thread created gets a copy of the
|
|
|
|
// session globals in TLS. This is possible because `SessionGlobals` impls
|
|
|
|
// `Send` in the parallel compiler.
|
2021-05-05 19:31:25 +00:00
|
|
|
rustc_span::create_session_globals_then(edition, || {
|
|
|
|
rustc_span::with_session_globals(|session_globals| {
|
2020-10-31 02:14:32 +00:00
|
|
|
let session_globals = FromDyn::from(session_globals);
|
2022-10-09 23:50:14 +00:00
|
|
|
builder
|
|
|
|
.build_scoped(
|
|
|
|
// Initialize each new worker thread when created.
|
|
|
|
move |thread: rayon::ThreadBuilder| {
|
2020-10-31 11:01:54 +00:00
|
|
|
// Register the thread for use with the `WorkerLocal` type.
|
|
|
|
registry.register();
|
|
|
|
|
2020-10-31 02:14:32 +00:00
|
|
|
rustc_span::set_session_globals_then(session_globals.into_inner(), || {
|
|
|
|
thread.run()
|
|
|
|
})
|
2022-10-09 23:50:14 +00:00
|
|
|
},
|
|
|
|
// Run `f` on the first thread in the thread pool.
|
|
|
|
move |pool: &rayon::ThreadPool| pool.install(f),
|
|
|
|
)
|
|
|
|
.unwrap()
|
2018-12-08 19:30:23 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-06-22 21:56:09 +00:00
|
|
|
fn load_backend_from_dylib(handler: &EarlyErrorHandler, path: &Path) -> MakeBackendFn {
|
2021-11-08 23:03:55 +00:00
|
|
|
let lib = unsafe { Library::new(path) }.unwrap_or_else(|err| {
|
2022-12-19 09:31:55 +00:00
|
|
|
let err = format!("couldn't load codegen backend {path:?}: {err}");
|
2023-06-22 21:56:09 +00:00
|
|
|
handler.early_error(err);
|
2018-12-08 19:30:23 +00:00
|
|
|
});
|
2021-11-08 23:03:55 +00:00
|
|
|
|
|
|
|
let backend_sym = unsafe { lib.get::<MakeBackendFn>(b"__rustc_codegen_backend") }
|
|
|
|
.unwrap_or_else(|e| {
|
2022-12-19 09:31:55 +00:00
|
|
|
let err = format!("couldn't load codegen backend: {e}");
|
2023-06-22 21:56:09 +00:00
|
|
|
handler.early_error(err);
|
2021-11-08 23:03:55 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
// Intentionally leak the dynamic library. We can't ever unload it
|
|
|
|
// since the library can make things that will live arbitrarily long.
|
|
|
|
let backend_sym = unsafe { backend_sym.into_raw() };
|
|
|
|
mem::forget(lib);
|
|
|
|
|
|
|
|
*backend_sym
|
2018-12-08 19:30:23 +00:00
|
|
|
}
|
|
|
|
|
2021-05-29 13:14:05 +00:00
|
|
|
/// Get the codegen backend based on the name and specified sysroot.
|
|
|
|
///
|
|
|
|
/// A name of `None` indicates that the default backend should be used.
|
|
|
|
pub fn get_codegen_backend(
|
2023-06-22 21:56:09 +00:00
|
|
|
handler: &EarlyErrorHandler,
|
2021-05-29 13:14:05 +00:00
|
|
|
maybe_sysroot: &Option<PathBuf>,
|
|
|
|
backend_name: Option<&str>,
|
|
|
|
) -> Box<dyn CodegenBackend> {
|
2022-06-16 15:39:39 +00:00
|
|
|
static LOAD: OnceLock<unsafe fn() -> Box<dyn CodegenBackend>> = OnceLock::new();
|
2018-12-08 19:30:23 +00:00
|
|
|
|
2021-05-29 13:23:57 +00:00
|
|
|
let load = LOAD.get_or_init(|| {
|
2022-02-27 09:59:10 +00:00
|
|
|
let default_codegen_backend = option_env!("CFG_DEFAULT_CODEGEN_BACKEND").unwrap_or("llvm");
|
2020-10-15 12:23:43 +00:00
|
|
|
|
2022-02-27 09:59:10 +00:00
|
|
|
match backend_name.unwrap_or(default_codegen_backend) {
|
2023-06-22 21:56:09 +00:00
|
|
|
filename if filename.contains('.') => {
|
|
|
|
load_backend_from_dylib(handler, filename.as_ref())
|
|
|
|
}
|
2021-05-29 13:14:05 +00:00
|
|
|
#[cfg(feature = "llvm")]
|
|
|
|
"llvm" => rustc_codegen_llvm::LlvmCodegenBackend::new,
|
2023-06-22 21:56:09 +00:00
|
|
|
backend_name => get_codegen_sysroot(handler, maybe_sysroot, backend_name),
|
2018-12-08 19:30:23 +00:00
|
|
|
}
|
|
|
|
});
|
2021-05-29 13:23:57 +00:00
|
|
|
|
|
|
|
// SAFETY: In case of a builtin codegen backend this is safe. In case of an external codegen
|
|
|
|
// backend we hope that the backend links against the same rustc_driver version. If this is not
|
|
|
|
// the case, we get UB.
|
|
|
|
unsafe { load() }
|
2018-12-08 19:30:23 +00:00
|
|
|
}
|
|
|
|
|
2019-09-10 01:01:41 +00:00
|
|
|
// This is used for rustdoc, but it uses similar machinery to codegen backend
|
|
|
|
// loading, so we leave the code here. It is potentially useful for other tools
|
|
|
|
// that want to invoke the rustc binary while linking to rustc as well.
|
|
|
|
pub fn rustc_path<'a>() -> Option<&'a Path> {
|
2022-06-16 15:39:39 +00:00
|
|
|
static RUSTC_PATH: OnceLock<Option<PathBuf>> = OnceLock::new();
|
2019-09-10 01:01:41 +00:00
|
|
|
|
|
|
|
const BIN_PATH: &str = env!("RUSTC_INSTALL_BINDIR");
|
2018-12-08 19:30:23 +00:00
|
|
|
|
2022-11-16 21:58:58 +00:00
|
|
|
RUSTC_PATH.get_or_init(|| get_rustc_path_inner(BIN_PATH)).as_deref()
|
2019-09-10 01:01:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn get_rustc_path_inner(bin_path: &str) -> Option<PathBuf> {
|
2020-04-25 03:03:45 +00:00
|
|
|
sysroot_candidates().iter().find_map(|sysroot| {
|
|
|
|
let candidate = sysroot.join(bin_path).join(if cfg!(target_os = "windows") {
|
|
|
|
"rustc.exe"
|
|
|
|
} else {
|
|
|
|
"rustc"
|
|
|
|
});
|
|
|
|
candidate.exists().then_some(candidate)
|
|
|
|
})
|
2019-09-10 01:01:41 +00:00
|
|
|
}
|
|
|
|
|
2023-06-22 21:56:09 +00:00
|
|
|
fn get_codegen_sysroot(
|
|
|
|
handler: &EarlyErrorHandler,
|
|
|
|
maybe_sysroot: &Option<PathBuf>,
|
|
|
|
backend_name: &str,
|
|
|
|
) -> MakeBackendFn {
|
2020-10-15 12:23:43 +00:00
|
|
|
// For now we only allow this function to be called once as it'll dlopen a
|
|
|
|
// few things, which seems to work best if we only do that once. In
|
|
|
|
// general this assertion never trips due to the once guard in `get_codegen_backend`,
|
|
|
|
// but there's a few manual calls to this function in this file we protect
|
|
|
|
// against.
|
|
|
|
static LOADED: AtomicBool = AtomicBool::new(false);
|
|
|
|
assert!(
|
|
|
|
!LOADED.fetch_or(true, Ordering::SeqCst),
|
|
|
|
"cannot load the default codegen backend twice"
|
|
|
|
);
|
|
|
|
|
|
|
|
let target = session::config::host_triple();
|
|
|
|
let sysroot_candidates = sysroot_candidates();
|
|
|
|
|
2021-02-01 18:29:31 +00:00
|
|
|
let sysroot = maybe_sysroot
|
2020-10-15 12:23:43 +00:00
|
|
|
.iter()
|
2021-02-01 18:29:31 +00:00
|
|
|
.chain(sysroot_candidates.iter())
|
2020-10-15 12:23:43 +00:00
|
|
|
.map(|sysroot| {
|
2021-09-30 17:38:50 +00:00
|
|
|
filesearch::make_target_lib_path(sysroot, target).with_file_name("codegen-backends")
|
2020-10-15 12:23:43 +00:00
|
|
|
})
|
2020-11-04 12:48:50 +00:00
|
|
|
.find(|f| {
|
2020-10-15 12:23:43 +00:00
|
|
|
info!("codegen backend candidate: {}", f.display());
|
|
|
|
f.exists()
|
2020-11-04 12:48:50 +00:00
|
|
|
});
|
2020-10-15 12:23:43 +00:00
|
|
|
let sysroot = sysroot.unwrap_or_else(|| {
|
|
|
|
let candidates = sysroot_candidates
|
|
|
|
.iter()
|
|
|
|
.map(|p| p.display().to_string())
|
|
|
|
.collect::<Vec<_>>()
|
|
|
|
.join("\n* ");
|
|
|
|
let err = format!(
|
|
|
|
"failed to find a `codegen-backends` folder \
|
2022-12-19 09:31:55 +00:00
|
|
|
in the sysroot candidates:\n* {candidates}"
|
2020-10-15 12:23:43 +00:00
|
|
|
);
|
2023-06-22 21:56:09 +00:00
|
|
|
handler.early_error(err);
|
2020-10-15 12:23:43 +00:00
|
|
|
});
|
|
|
|
info!("probing {} for a codegen backend", sysroot.display());
|
|
|
|
|
|
|
|
let d = sysroot.read_dir().unwrap_or_else(|e| {
|
|
|
|
let err = format!(
|
|
|
|
"failed to load default codegen backend, couldn't \
|
|
|
|
read `{}`: {}",
|
|
|
|
sysroot.display(),
|
|
|
|
e
|
|
|
|
);
|
2023-06-22 21:56:09 +00:00
|
|
|
handler.early_error(err);
|
2020-10-15 12:23:43 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
let mut file: Option<PathBuf> = None;
|
|
|
|
|
2021-02-01 18:29:31 +00:00
|
|
|
let expected_names = &[
|
2022-11-09 08:52:59 +00:00
|
|
|
format!("rustc_codegen_{}-{}", backend_name, env!("CFG_RELEASE")),
|
2022-12-19 09:31:55 +00:00
|
|
|
format!("rustc_codegen_{backend_name}"),
|
2021-02-01 18:29:31 +00:00
|
|
|
];
|
2020-10-15 12:23:43 +00:00
|
|
|
for entry in d.filter_map(|e| e.ok()) {
|
|
|
|
let path = entry.path();
|
2022-02-18 23:48:49 +00:00
|
|
|
let Some(filename) = path.file_name().and_then(|s| s.to_str()) else { continue };
|
2020-10-15 12:23:43 +00:00
|
|
|
if !(filename.starts_with(DLL_PREFIX) && filename.ends_with(DLL_SUFFIX)) {
|
|
|
|
continue;
|
2019-09-10 01:01:41 +00:00
|
|
|
}
|
2020-10-15 12:23:43 +00:00
|
|
|
let name = &filename[DLL_PREFIX.len()..filename.len() - DLL_SUFFIX.len()];
|
2021-02-01 18:29:31 +00:00
|
|
|
if !expected_names.iter().any(|expected| expected == name) {
|
2020-10-15 12:23:43 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if let Some(ref prev) = file {
|
|
|
|
let err = format!(
|
|
|
|
"duplicate codegen backends found\n\
|
|
|
|
first: {}\n\
|
|
|
|
second: {}\n\
|
|
|
|
",
|
|
|
|
prev.display(),
|
|
|
|
path.display()
|
|
|
|
);
|
2023-06-22 21:56:09 +00:00
|
|
|
handler.early_error(err);
|
2020-10-15 12:23:43 +00:00
|
|
|
}
|
|
|
|
file = Some(path.clone());
|
2019-09-10 01:01:41 +00:00
|
|
|
}
|
|
|
|
|
2020-10-15 12:23:43 +00:00
|
|
|
match file {
|
2023-06-22 21:56:09 +00:00
|
|
|
Some(ref s) => load_backend_from_dylib(handler, s),
|
2020-10-15 12:23:43 +00:00
|
|
|
None => {
|
2022-12-19 09:31:55 +00:00
|
|
|
let err = format!("unsupported builtin codegen backend `{backend_name}`");
|
2023-06-22 21:56:09 +00:00
|
|
|
handler.early_error(err);
|
2020-10-15 12:23:43 +00:00
|
|
|
}
|
|
|
|
}
|
2019-09-10 01:01:41 +00:00
|
|
|
}
|
|
|
|
|
2020-07-30 01:27:50 +00:00
|
|
|
pub(crate) fn check_attr_crate_type(
|
2021-09-17 20:08:56 +00:00
|
|
|
sess: &Session,
|
2020-07-30 01:27:50 +00:00
|
|
|
attrs: &[ast::Attribute],
|
|
|
|
lint_buffer: &mut LintBuffer,
|
|
|
|
) {
|
2019-10-25 17:23:18 +00:00
|
|
|
// Unconditionally collect crate types from attributes to make them used
|
|
|
|
for a in attrs.iter() {
|
2021-07-29 17:00:41 +00:00
|
|
|
if a.has_name(sym::crate_type) {
|
2019-10-25 17:23:18 +00:00
|
|
|
if let Some(n) = a.value_str() {
|
2020-03-04 14:53:14 +00:00
|
|
|
if categorize_crate_type(n).is_some() {
|
2019-10-25 17:23:18 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-12-26 15:47:08 +00:00
|
|
|
if let ast::MetaItemKind::NameValue(spanned) = a.meta_kind().unwrap() {
|
2019-10-25 17:23:18 +00:00
|
|
|
let span = spanned.span;
|
Move lev_distance to rustc_ast, make non-generic
rustc_ast currently has a few dependencies on rustc_lexer. Ideally, an AST
would not have any dependency its lexer, for minimizing unnecessarily
design-time dependencies. Breaking this dependency would also have practical
benefits, since modifying rustc_lexer would not trigger a rebuild of rustc_ast.
This commit does not remove the rustc_ast --> rustc_lexer dependency,
but it does remove one of the sources of this dependency, which is the
code that handles fuzzy matching between symbol names for making suggestions
in diagnostics. Since that code depends only on Symbol, it is easy to move
it to rustc_span. It might even be best to move it to a separate crate,
since other tools such as Cargo use the same algorithm, and have simply
contain a duplicate of the code.
This changes the signature of find_best_match_for_name so that it is no
longer generic over its input. I checked the optimized binaries, and this
function was duplicated at nearly every call site, because most call sites
used short-lived iterator chains, generic over Map and such. But there's
no good reason for a function like this to be generic, since all it does
is immediately convert the generic input (the Iterator impl) to a concrete
Vec<Symbol>. This has all of the costs of generics (duplicated method bodies)
with no benefit.
Changing find_best_match_for_name to be non-generic removed about 10KB of
code from the optimized binary. I know it's a drop in the bucket, but we have
to start reducing binary size, and beginning to tame over-use of generics
is part of that.
2020-11-12 19:24:10 +00:00
|
|
|
let lev_candidate = find_best_match_for_name(
|
|
|
|
&CRATE_TYPES.iter().map(|(k, _)| *k).collect::<Vec<_>>(),
|
|
|
|
n,
|
|
|
|
None,
|
|
|
|
);
|
2019-10-25 17:23:18 +00:00
|
|
|
if let Some(candidate) = lev_candidate {
|
|
|
|
lint_buffer.buffer_lint_with_diagnostic(
|
|
|
|
lint::builtin::UNKNOWN_CRATE_TYPES,
|
|
|
|
ast::CRATE_NODE_ID,
|
|
|
|
span,
|
|
|
|
"invalid `crate_type` value",
|
2020-01-05 08:40:16 +00:00
|
|
|
BuiltinLintDiagnostics::UnknownCrateTypes(
|
2019-10-25 17:23:18 +00:00
|
|
|
span,
|
|
|
|
"did you mean".to_string(),
|
2022-12-19 09:31:55 +00:00
|
|
|
format!("\"{candidate}\""),
|
2019-10-25 17:23:18 +00:00
|
|
|
),
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
lint_buffer.buffer_lint(
|
|
|
|
lint::builtin::UNKNOWN_CRATE_TYPES,
|
|
|
|
ast::CRATE_NODE_ID,
|
|
|
|
span,
|
|
|
|
"invalid `crate_type` value",
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2021-09-17 20:08:56 +00:00
|
|
|
} else {
|
|
|
|
// This is here mainly to check for using a macro, such as
|
|
|
|
// #![crate_type = foo!()]. That is not supported since the
|
|
|
|
// crate type needs to be known very early in compilation long
|
|
|
|
// before expansion. Otherwise, validation would normally be
|
|
|
|
// caught in AstValidator (via `check_builtin_attribute`), but
|
|
|
|
// by the time that runs the macro is expanded, and it doesn't
|
|
|
|
// give an error.
|
|
|
|
validate_attr::emit_fatal_malformed_builtin_attribute(
|
|
|
|
&sess.parse_sess,
|
|
|
|
a,
|
|
|
|
sym::crate_type,
|
|
|
|
);
|
2019-10-25 17:23:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-01 22:30:23 +00:00
|
|
|
const CRATE_TYPES: &[(Symbol, CrateType)] = &[
|
|
|
|
(sym::rlib, CrateType::Rlib),
|
|
|
|
(sym::dylib, CrateType::Dylib),
|
|
|
|
(sym::cdylib, CrateType::Cdylib),
|
2019-10-25 17:23:18 +00:00
|
|
|
(sym::lib, config::default_lib_output()),
|
2020-05-01 22:30:23 +00:00
|
|
|
(sym::staticlib, CrateType::Staticlib),
|
|
|
|
(sym::proc_dash_macro, CrateType::ProcMacro),
|
|
|
|
(sym::bin, CrateType::Executable),
|
2019-10-25 17:23:18 +00:00
|
|
|
];
|
|
|
|
|
2020-05-01 22:30:23 +00:00
|
|
|
fn categorize_crate_type(s: Symbol) -> Option<CrateType> {
|
2019-10-25 17:23:18 +00:00
|
|
|
Some(CRATE_TYPES.iter().find(|(key, _)| *key == s)?.1)
|
|
|
|
}
|
|
|
|
|
2020-05-01 22:30:23 +00:00
|
|
|
pub fn collect_crate_types(session: &Session, attrs: &[ast::Attribute]) -> Vec<CrateType> {
|
2018-12-08 19:30:23 +00:00
|
|
|
// Unconditionally collect crate types from attributes to make them used
|
2020-05-01 22:30:23 +00:00
|
|
|
let attr_types: Vec<CrateType> = attrs
|
2018-12-08 19:30:23 +00:00
|
|
|
.iter()
|
|
|
|
.filter_map(|a| {
|
2021-07-29 17:00:41 +00:00
|
|
|
if a.has_name(sym::crate_type) {
|
2018-12-08 19:30:23 +00:00
|
|
|
match a.value_str() {
|
2019-10-25 17:23:18 +00:00
|
|
|
Some(s) => categorize_crate_type(s),
|
|
|
|
_ => None,
|
2018-12-08 19:30:23 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.collect();
|
|
|
|
|
|
|
|
// If we're generating a test executable, then ignore all other output
|
|
|
|
// styles at all other locations
|
|
|
|
if session.opts.test {
|
2020-05-01 22:30:23 +00:00
|
|
|
return vec![CrateType::Executable];
|
2018-12-08 19:30:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Only check command line flags if present. If no types are specified by
|
|
|
|
// command line, then reuse the empty `base` Vec to hold the types that
|
|
|
|
// will be found in crate attributes.
|
2022-07-25 12:02:39 +00:00
|
|
|
// JUSTIFICATION: before wrapper fn is available
|
2022-08-09 13:56:13 +00:00
|
|
|
#[allow(rustc::bad_opt_access)]
|
2018-12-08 19:30:23 +00:00
|
|
|
let mut base = session.opts.crate_types.clone();
|
|
|
|
if base.is_empty() {
|
|
|
|
base.extend(attr_types);
|
|
|
|
if base.is_empty() {
|
2020-03-12 23:07:58 +00:00
|
|
|
base.push(output::default_output_for_target(session));
|
2018-12-08 19:30:23 +00:00
|
|
|
} else {
|
|
|
|
base.sort();
|
|
|
|
base.dedup();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
base.retain(|crate_type| {
|
2022-08-19 13:48:15 +00:00
|
|
|
if output::invalid_output_for_target(session, *crate_type) {
|
|
|
|
session.emit_warning(errors::UnsupportedCrateTypeForTarget {
|
|
|
|
crate_type: *crate_type,
|
|
|
|
target_triple: &session.opts.target_triple,
|
|
|
|
});
|
|
|
|
false
|
|
|
|
} else {
|
|
|
|
true
|
2018-12-08 19:30:23 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
base
|
|
|
|
}
|
|
|
|
|
2023-02-26 20:27:27 +00:00
|
|
|
fn multiple_output_types_to_stdout(
|
|
|
|
output_types: &OutputTypes,
|
|
|
|
single_output_file_is_stdout: bool,
|
|
|
|
) -> bool {
|
2023-07-26 15:09:50 +00:00
|
|
|
use std::io::IsTerminal;
|
|
|
|
if std::io::stdout().is_terminal() {
|
2023-02-26 20:27:27 +00:00
|
|
|
// If stdout is a tty, check if multiple text output types are
|
|
|
|
// specified by `--emit foo=- --emit bar=-` or `-o - --emit foo,bar`
|
|
|
|
let named_text_types = output_types
|
|
|
|
.iter()
|
|
|
|
.filter(|(f, o)| f.is_text_output() && *o == &Some(OutFileName::Stdout))
|
|
|
|
.count();
|
|
|
|
let unnamed_text_types =
|
|
|
|
output_types.iter().filter(|(f, o)| f.is_text_output() && o.is_none()).count();
|
|
|
|
named_text_types > 1 || unnamed_text_types > 1 && single_output_file_is_stdout
|
|
|
|
} else {
|
|
|
|
// Otherwise, all the output types should be checked
|
|
|
|
let named_types =
|
|
|
|
output_types.values().filter(|o| *o == &Some(OutFileName::Stdout)).count();
|
|
|
|
let unnamed_types = output_types.values().filter(|o| o.is_none()).count();
|
|
|
|
named_types > 1 || unnamed_types > 1 && single_output_file_is_stdout
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-07 09:24:00 +00:00
|
|
|
pub fn build_output_filenames(attrs: &[ast::Attribute], sess: &Session) -> OutputFilenames {
|
2023-02-26 20:27:27 +00:00
|
|
|
if multiple_output_types_to_stdout(
|
|
|
|
&sess.opts.output_types,
|
|
|
|
sess.io.output_file == Some(OutFileName::Stdout),
|
|
|
|
) {
|
|
|
|
sess.emit_fatal(errors::MultipleOutputTypesToStdout);
|
|
|
|
}
|
2023-08-11 04:20:35 +00:00
|
|
|
|
|
|
|
let crate_name = sess
|
|
|
|
.opts
|
|
|
|
.crate_name
|
|
|
|
.clone()
|
|
|
|
.or_else(|| rustc_attr::find_crate_name(attrs).map(|n| n.to_string()));
|
|
|
|
|
2022-12-07 09:24:00 +00:00
|
|
|
match sess.io.output_file {
|
2018-12-08 19:30:23 +00:00
|
|
|
None => {
|
|
|
|
// "-" as input file will cause the parser to read from stdin so we
|
|
|
|
// have to make up a name
|
|
|
|
// We want to toss everything after the final '.'
|
2022-12-07 09:24:00 +00:00
|
|
|
let dirpath = sess.io.output_dir.clone().unwrap_or_default();
|
2018-12-08 19:30:23 +00:00
|
|
|
|
|
|
|
// If a crate name is present, we use it as the link name
|
2023-08-11 04:20:35 +00:00
|
|
|
let stem = crate_name.clone().unwrap_or_else(|| sess.io.input.filestem().to_owned());
|
2018-12-08 19:30:23 +00:00
|
|
|
|
2020-01-21 14:50:22 +00:00
|
|
|
OutputFilenames::new(
|
|
|
|
dirpath,
|
2023-08-11 04:20:35 +00:00
|
|
|
crate_name.unwrap_or_else(|| stem.replace('-', "_")),
|
2020-01-21 14:50:22 +00:00
|
|
|
stem,
|
|
|
|
None,
|
2022-12-07 09:24:00 +00:00
|
|
|
sess.io.temps_dir.clone(),
|
2020-01-21 14:50:22 +00:00
|
|
|
sess.opts.cg.extra_filename.clone(),
|
|
|
|
sess.opts.output_types.clone(),
|
|
|
|
)
|
2018-12-08 19:30:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Some(ref out_file) => {
|
|
|
|
let unnamed_output_types =
|
|
|
|
sess.opts.output_types.values().filter(|a| a.is_none()).count();
|
|
|
|
let ofile = if unnamed_output_types > 1 {
|
2022-08-19 13:48:15 +00:00
|
|
|
sess.emit_warning(errors::MultipleOutputTypesAdaption);
|
2018-12-08 19:30:23 +00:00
|
|
|
None
|
|
|
|
} else {
|
2019-06-25 20:31:05 +00:00
|
|
|
if !sess.opts.cg.extra_filename.is_empty() {
|
2022-08-19 13:48:15 +00:00
|
|
|
sess.emit_warning(errors::IgnoringExtraFilename);
|
2019-06-25 20:31:05 +00:00
|
|
|
}
|
2018-12-08 19:30:23 +00:00
|
|
|
Some(out_file.clone())
|
|
|
|
};
|
2022-12-07 09:24:00 +00:00
|
|
|
if sess.io.output_dir != None {
|
2022-08-19 13:48:15 +00:00
|
|
|
sess.emit_warning(errors::IgnoringOutDir);
|
2018-12-08 19:30:23 +00:00
|
|
|
}
|
|
|
|
|
2023-08-11 04:20:35 +00:00
|
|
|
let out_filestem =
|
|
|
|
out_file.filestem().unwrap_or_default().to_str().unwrap().to_string();
|
2020-01-21 14:50:22 +00:00
|
|
|
OutputFilenames::new(
|
|
|
|
out_file.parent().unwrap_or_else(|| Path::new("")).to_path_buf(),
|
2023-08-11 04:20:35 +00:00
|
|
|
crate_name.unwrap_or_else(|| out_filestem.replace('-', "_")),
|
|
|
|
out_filestem,
|
2020-01-21 14:50:22 +00:00
|
|
|
ofile,
|
2022-12-07 09:24:00 +00:00
|
|
|
sess.io.temps_dir.clone(),
|
2020-01-21 14:50:22 +00:00
|
|
|
sess.opts.cg.extra_filename.clone(),
|
|
|
|
sess.opts.output_types.clone(),
|
|
|
|
)
|
2018-12-08 19:30:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-09 08:52:59 +00:00
|
|
|
/// Returns a version string such as "1.46.0 (04488afe3 2020-08-24)" when invoked by an in-tree tool.
|
|
|
|
pub macro version_str() {
|
2020-10-15 12:23:43 +00:00
|
|
|
option_env!("CFG_VERSION")
|
|
|
|
}
|
|
|
|
|
2022-11-09 08:52:59 +00:00
|
|
|
/// Returns the version string for `rustc` itself (which may be different from a tool version).
|
|
|
|
pub fn rustc_version_str() -> Option<&'static str> {
|
|
|
|
version_str!()
|
2020-10-15 12:23:43 +00:00
|
|
|
}
|