Skip to content

Commit cb2802b

Browse files
committed
Construct OutputType using macro and print [=FILENAME] help info
Signed-off-by: xizheyin <[email protected]>
1 parent 4c20d17 commit cb2802b

File tree

5 files changed

+235
-119
lines changed

5 files changed

+235
-119
lines changed

compiler/rustc_session/src/config.rs

+198-112
Original file line numberDiff line numberDiff line change
@@ -568,124 +568,205 @@ impl FromStr for SplitDwarfKind {
568568
}
569569
}
570570

571-
#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug, PartialOrd, Ord, HashStable_Generic)]
572-
#[derive(Encodable, Decodable)]
573-
pub enum OutputType {
574-
/// This is the optimized bitcode, which could be either pre-LTO or non-LTO bitcode,
575-
/// depending on the specific request type.
576-
Bitcode,
577-
/// This is the summary or index data part of the ThinLTO bitcode.
578-
ThinLinkBitcode,
579-
Assembly,
580-
LlvmAssembly,
581-
Mir,
582-
Metadata,
583-
Object,
584-
Exe,
585-
DepInfo,
586-
}
571+
macro_rules! define_output_types {
572+
(
573+
$(
574+
$(#[doc = $doc:expr])*
575+
$Variant:ident => {
576+
shorthand: $shorthand:expr,
577+
extension: $extension:expr,
578+
description: $description:expr,
579+
default_filename: $default_filename:expr,
580+
is_text: $is_text:expr,
581+
compatible_with_cgus_and_single_output: $compatible:expr
582+
}
583+
),* $(,)?
584+
) => {
585+
#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug, PartialOrd, Ord, HashStable_Generic)]
586+
#[derive(Encodable, Decodable)]
587+
pub enum OutputType {
588+
$(
589+
$(#[doc = $doc])*
590+
$Variant,
591+
)*
592+
}
587593

588-
impl StableOrd for OutputType {
589-
const CAN_USE_UNSTABLE_SORT: bool = true;
590594

591-
// Trivial C-Style enums have a stable sort order across compilation sessions.
592-
const THIS_IMPLEMENTATION_HAS_BEEN_TRIPLE_CHECKED: () = ();
593-
}
595+
impl StableOrd for OutputType {
596+
const CAN_USE_UNSTABLE_SORT: bool = true;
594597

595-
impl<HCX: HashStableContext> ToStableHashKey<HCX> for OutputType {
596-
type KeyType = Self;
598+
// Trivial C-Style enums have a stable sort order across compilation sessions.
599+
const THIS_IMPLEMENTATION_HAS_BEEN_TRIPLE_CHECKED: () = ();
600+
}
597601

598-
fn to_stable_hash_key(&self, _: &HCX) -> Self::KeyType {
599-
*self
600-
}
601-
}
602+
impl<HCX: HashStableContext> ToStableHashKey<HCX> for OutputType {
603+
type KeyType = Self;
602604

603-
impl OutputType {
604-
fn is_compatible_with_codegen_units_and_single_output_file(&self) -> bool {
605-
match *self {
606-
OutputType::Exe | OutputType::DepInfo | OutputType::Metadata => true,
607-
OutputType::Bitcode
608-
| OutputType::ThinLinkBitcode
609-
| OutputType::Assembly
610-
| OutputType::LlvmAssembly
611-
| OutputType::Mir
612-
| OutputType::Object => false,
605+
fn to_stable_hash_key(&self, _: &HCX) -> Self::KeyType {
606+
*self
607+
}
613608
}
614-
}
615609

616-
pub fn shorthand(&self) -> &'static str {
617-
match *self {
618-
OutputType::Bitcode => "llvm-bc",
619-
OutputType::ThinLinkBitcode => "thin-link-bitcode",
620-
OutputType::Assembly => "asm",
621-
OutputType::LlvmAssembly => "llvm-ir",
622-
OutputType::Mir => "mir",
623-
OutputType::Object => "obj",
624-
OutputType::Metadata => "metadata",
625-
OutputType::Exe => "link",
626-
OutputType::DepInfo => "dep-info",
627-
}
628-
}
629-
630-
fn from_shorthand(shorthand: &str) -> Option<Self> {
631-
Some(match shorthand {
632-
"asm" => OutputType::Assembly,
633-
"llvm-ir" => OutputType::LlvmAssembly,
634-
"mir" => OutputType::Mir,
635-
"llvm-bc" => OutputType::Bitcode,
636-
"thin-link-bitcode" => OutputType::ThinLinkBitcode,
637-
"obj" => OutputType::Object,
638-
"metadata" => OutputType::Metadata,
639-
"link" => OutputType::Exe,
640-
"dep-info" => OutputType::DepInfo,
641-
_ => return None,
642-
})
643-
}
644610

645-
fn shorthands_display() -> String {
646-
format!(
647-
"`{}`, `{}`, `{}`, `{}`, `{}`, `{}`, `{}`, `{}`, `{}`",
648-
OutputType::Bitcode.shorthand(),
649-
OutputType::ThinLinkBitcode.shorthand(),
650-
OutputType::Assembly.shorthand(),
651-
OutputType::LlvmAssembly.shorthand(),
652-
OutputType::Mir.shorthand(),
653-
OutputType::Object.shorthand(),
654-
OutputType::Metadata.shorthand(),
655-
OutputType::Exe.shorthand(),
656-
OutputType::DepInfo.shorthand(),
657-
)
658-
}
611+
impl OutputType {
612+
pub fn iter_all() -> impl Iterator<Item = OutputType> {
613+
static ALL_VARIANTS: &[OutputType] = &[
614+
$(
615+
OutputType::$Variant,
616+
)*
617+
];
618+
ALL_VARIANTS.iter().copied()
619+
}
620+
621+
fn is_compatible_with_codegen_units_and_single_output_file(&self) -> bool {
622+
match *self {
623+
$(
624+
OutputType::$Variant => $compatible,
625+
)*
626+
}
627+
}
628+
629+
pub fn shorthand(&self) -> &'static str {
630+
match *self {
631+
$(
632+
OutputType::$Variant => $shorthand,
633+
)*
634+
}
635+
}
636+
637+
fn from_shorthand(shorthand: &str) -> Option<Self> {
638+
match shorthand {
639+
$(
640+
s if s == $shorthand => Some(OutputType::$Variant),
641+
)*
642+
_ => None,
643+
}
644+
}
645+
646+
fn shorthands_display() -> String {
647+
let shorthands = vec![
648+
$(
649+
format!("`{}`", $shorthand),
650+
)*
651+
];
652+
shorthands.join(", ")
653+
}
654+
655+
pub fn extension(&self) -> &'static str {
656+
match *self {
657+
$(
658+
OutputType::$Variant => $extension,
659+
)*
660+
}
661+
}
662+
663+
pub fn is_text_output(&self) -> bool {
664+
match *self {
665+
$(
666+
OutputType::$Variant => $is_text,
667+
)*
668+
}
669+
}
670+
671+
pub fn description(&self) -> &'static str {
672+
match *self {
673+
$(
674+
OutputType::$Variant => $description,
675+
)*
676+
}
677+
}
678+
679+
pub fn default_filename(&self) -> &'static str {
680+
match *self {
681+
$(
682+
OutputType::$Variant => $default_filename,
683+
)*
684+
}
685+
}
659686

660-
pub fn extension(&self) -> &'static str {
661-
match *self {
662-
OutputType::Bitcode => "bc",
663-
OutputType::ThinLinkBitcode => "indexing.o",
664-
OutputType::Assembly => "s",
665-
OutputType::LlvmAssembly => "ll",
666-
OutputType::Mir => "mir",
667-
OutputType::Object => "o",
668-
OutputType::Metadata => "rmeta",
669-
OutputType::DepInfo => "d",
670-
OutputType::Exe => "",
671-
}
672-
}
673687

674-
pub fn is_text_output(&self) -> bool {
675-
match *self {
676-
OutputType::Assembly
677-
| OutputType::LlvmAssembly
678-
| OutputType::Mir
679-
| OutputType::DepInfo => true,
680-
OutputType::Bitcode
681-
| OutputType::ThinLinkBitcode
682-
| OutputType::Object
683-
| OutputType::Metadata
684-
| OutputType::Exe => false,
685688
}
686689
}
687690
}
688691

692+
define_output_types! {
693+
#[doc = "This is the optimized bitcode, which could be either pre-LTO or non-LTO bitcode,"]
694+
#[doc = "depending on the specific request type."]
695+
Bitcode => {
696+
shorthand: "llvm-bc",
697+
extension: "bc",
698+
description: "Generates a binary file containing the LLVM bitcode",
699+
default_filename: "CRATE_NAME.bc",
700+
is_text: false,
701+
compatible_with_cgus_and_single_output: false
702+
},
703+
#[doc = "This is the summary or index data part of the ThinLTO bitcode."]
704+
ThinLinkBitcode => {
705+
shorthand: "thin-link-bitcode",
706+
extension: "indexing.o",
707+
description: "Generates the ThinLTO summary as bitcode",
708+
default_filename: "CRATE_NAME.indexing.o",
709+
is_text: false,
710+
compatible_with_cgus_and_single_output: false
711+
},
712+
Assembly => {
713+
shorthand: "asm",
714+
extension: "s",
715+
description: "Generates a file with the crate's assembly code",
716+
default_filename: "CRATE_NAME.s",
717+
is_text: true,
718+
compatible_with_cgus_and_single_output: false
719+
},
720+
LlvmAssembly => {
721+
shorthand: "llvm-ir",
722+
extension: "ll",
723+
description: "Generates a file containing LLVM IR",
724+
default_filename: "CRATE_NAME.ll",
725+
is_text: true,
726+
compatible_with_cgus_and_single_output: false
727+
},
728+
Mir => {
729+
shorthand: "mir",
730+
extension: "mir",
731+
description: "Generates a file containing rustc's mid-level intermediate representation",
732+
default_filename: "CRATE_NAME.mir",
733+
is_text: true,
734+
compatible_with_cgus_and_single_output: false
735+
},
736+
Object => {
737+
shorthand: "obj",
738+
extension: "o",
739+
description: "Generates a native object file",
740+
default_filename: "CRATE_NAME.o",
741+
is_text: false,
742+
compatible_with_cgus_and_single_output: false
743+
},
744+
Metadata => {
745+
shorthand: "metadata",
746+
extension: "rmeta",
747+
description: "Generates a file containing metadata about the crate",
748+
default_filename: "libCRATE_NAME.rmeta",
749+
is_text: false,
750+
compatible_with_cgus_and_single_output: true
751+
},
752+
Exe => {
753+
shorthand: "link",
754+
extension: "",
755+
description: "Generates the crates specified by --crate-type. This is the default if --emit is not specified",
756+
default_filename: "(platform and crate-type dependent)",
757+
is_text: false,
758+
compatible_with_cgus_and_single_output: true
759+
},
760+
DepInfo => {
761+
shorthand: "dep-info",
762+
extension: "d",
763+
description: "Generates a file with Makefile syntax that indicates all the source files that were loaded to generate the crate",
764+
default_filename: "CRATE_NAME.d",
765+
is_text: true,
766+
compatible_with_cgus_and_single_output: true
767+
},
768+
}
769+
689770
/// The type of diagnostics output to generate.
690771
#[derive(Clone, Copy, Debug, PartialEq, Eq, Default)]
691772
pub enum ErrorOutputType {
@@ -1570,6 +1651,18 @@ static PRINT_HELP: LazyLock<String> = LazyLock::new(|| {
15701651
)
15711652
});
15721653

1654+
static EMIT_HELP: LazyLock<String> = LazyLock::new(|| {
1655+
let mut result =
1656+
String::from("Comma separated list of types of output for the compiler to emit.\n");
1657+
result.push_str("Each TYPE has a following default FILENAME:\n");
1658+
1659+
for output in OutputType::iter_all() {
1660+
result.push_str(&format!("* {} - {}\n", output.shorthand(), output.default_filename()));
1661+
}
1662+
1663+
result
1664+
});
1665+
15731666
/// Returns all rustc command line options, including metadata for
15741667
/// each option, such as whether the option is stable.
15751668
pub fn rustc_optgroups() -> Vec<RustcOptGroup> {
@@ -1616,14 +1709,7 @@ pub fn rustc_optgroups() -> Vec<RustcOptGroup> {
16161709
make_crate_type_option(),
16171710
opt(Stable, Opt, "", "crate-name", "Specify the name of the crate being built", "NAME"),
16181711
opt(Stable, Opt, "", "edition", &EDITION_STRING, EDITION_NAME_LIST),
1619-
opt(
1620-
Stable,
1621-
Multi,
1622-
"",
1623-
"emit",
1624-
"Comma separated list of types of output for the compiler to emit",
1625-
"[asm|llvm-bc|llvm-ir|obj|metadata|link|dep-info|mir]",
1626-
),
1712+
opt(Stable, Multi, "", "emit", &EMIT_HELP, "TYPE[=FILENAME]"),
16271713
opt(Stable, Multi, "", "print", &PRINT_HELP, "INFO[=FILE]"),
16281714
opt(Stable, FlagMulti, "g", "", "Equivalent to -C debuginfo=2", ""),
16291715
opt(Stable, FlagMulti, "O", "", "Equivalent to -C opt-level=3", ""),

tests/run-make/rustc-help/help-v.diff

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
@@ -53,10 +53,27 @@
1+
@@ -63,10 +63,27 @@
22
Set a codegen option
33
-V, --version Print version info and exit
44
-v, --verbose Use verbose output

tests/run-make/rustc-help/help-v.stdout

+12-2
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,19 @@ Options:
2626
Specify which edition of the compiler to use when
2727
compiling code. The default is 2015 and the latest
2828
stable edition is 2024.
29-
--emit [asm|llvm-bc|llvm-ir|obj|metadata|link|dep-info|mir]
29+
--emit TYPE[=FILENAME]
3030
Comma separated list of types of output for the
31-
compiler to emit
31+
compiler to emit.
32+
Each TYPE has a following default FILENAME:
33+
* llvm-bc - CRATE_NAME.bc
34+
* thin-link-bitcode - CRATE_NAME.indexing.o
35+
* asm - CRATE_NAME.s
36+
* llvm-ir - CRATE_NAME.ll
37+
* mir - CRATE_NAME.mir
38+
* obj - CRATE_NAME.o
39+
* metadata - libCRATE_NAME.rmeta
40+
* link - (platform and crate-type dependent)
41+
* dep-info - CRATE_NAME.d
3242
--print INFO[=FILE]
3343
Compiler information to print on stdout (or to a file)
3444
INFO may be one of

tests/run-make/rustc-help/help.stdout

+12-2
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,19 @@ Options:
2626
Specify which edition of the compiler to use when
2727
compiling code. The default is 2015 and the latest
2828
stable edition is 2024.
29-
--emit [asm|llvm-bc|llvm-ir|obj|metadata|link|dep-info|mir]
29+
--emit TYPE[=FILENAME]
3030
Comma separated list of types of output for the
31-
compiler to emit
31+
compiler to emit.
32+
Each TYPE has a following default FILENAME:
33+
* llvm-bc - CRATE_NAME.bc
34+
* thin-link-bitcode - CRATE_NAME.indexing.o
35+
* asm - CRATE_NAME.s
36+
* llvm-ir - CRATE_NAME.ll
37+
* mir - CRATE_NAME.mir
38+
* obj - CRATE_NAME.o
39+
* metadata - libCRATE_NAME.rmeta
40+
* link - (platform and crate-type dependent)
41+
* dep-info - CRATE_NAME.d
3242
--print INFO[=FILE]
3343
Compiler information to print on stdout (or to a file)
3444
INFO may be one of

0 commit comments

Comments
 (0)