Skip to content

Commit aca2170

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

File tree

4 files changed

+217
-119
lines changed

4 files changed

+217
-119
lines changed

compiler/rustc_session/src/config.rs

+194-112
Original file line numberDiff line numberDiff line change
@@ -568,124 +568,203 @@ 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+
$Variant:ident => {
575+
shorthand: $shorthand:expr,
576+
extension: $extension:expr,
577+
description: $description:expr,
578+
default_filename: $default_filename:expr,
579+
is_text: $is_text:expr,
580+
compatible_with_cgus_and_single_output: $compatible:expr
581+
}
582+
),* $(,)?
583+
) => {
584+
#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug, PartialOrd, Ord, HashStable_Generic)]
585+
#[derive(Encodable, Decodable)]
586+
pub enum OutputType {
587+
$(
588+
$Variant,
589+
)*
590+
}
587591

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

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

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

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

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,
603+
fn to_stable_hash_key(&self, _: &HCX) -> Self::KeyType {
604+
*self
605+
}
613606
}
614-
}
615607

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-
}
644608

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

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-
}
673685

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,
685686
}
686687
}
687688
}
688689

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

1652+
static EMIT_HELP: LazyLock<String> = LazyLock::new(|| {
1653+
let mut result = String::from("Each TYPE has a following default FILENAME:\n");
1654+
1655+
for output in OutputType::iter_all() {
1656+
result.push_str(&format!("* {} - {}\n", output.shorthand(), output.default_filename()));
1657+
}
1658+
1659+
result
1660+
});
1661+
15731662
/// Returns all rustc command line options, including metadata for
15741663
/// each option, such as whether the option is stable.
15751664
pub fn rustc_optgroups() -> Vec<RustcOptGroup> {
@@ -1616,14 +1705,7 @@ pub fn rustc_optgroups() -> Vec<RustcOptGroup> {
16161705
make_crate_type_option(),
16171706
opt(Stable, Opt, "", "crate-name", "Specify the name of the crate being built", "NAME"),
16181707
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-
),
1708+
opt(Stable, Multi, "", "emit", &EMIT_HELP, "TYPE[=FILENAME]"),
16271709
opt(Stable, Multi, "", "print", &PRINT_HELP, "INFO[=FILE]"),
16281710
opt(Stable, FlagMulti, "g", "", "Equivalent to -C debuginfo=2", ""),
16291711
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+
@@ -61,10 +61,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

+11-3
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,17 @@ 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]
30-
Comma separated list of types of output for the
31-
compiler to emit
29+
--emit TYPE[=FILENAME]
30+
Each TYPE has a following default FILENAME:
31+
* llvm-bc - CRATE_NAME.bc
32+
* thin-link-bitcode - CRATE_NAME.indexing.o
33+
* asm - CRATE_NAME.s
34+
* llvm-ir - CRATE_NAME.ll
35+
* mir - CRATE_NAME.mir
36+
* obj - CRATE_NAME.o
37+
* metadata - libCRATE_NAME.rmeta
38+
* link - (platform and crate-type dependent)
39+
* dep-info - CRATE_NAME.d
3240
--print INFO[=FILE]
3341
Compiler information to print on stdout (or to a file)
3442
INFO may be one of

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

+11-3
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,17 @@ 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]
30-
Comma separated list of types of output for the
31-
compiler to emit
29+
--emit TYPE[=FILENAME]
30+
Each TYPE has a following default FILENAME:
31+
* llvm-bc - CRATE_NAME.bc
32+
* thin-link-bitcode - CRATE_NAME.indexing.o
33+
* asm - CRATE_NAME.s
34+
* llvm-ir - CRATE_NAME.ll
35+
* mir - CRATE_NAME.mir
36+
* obj - CRATE_NAME.o
37+
* metadata - libCRATE_NAME.rmeta
38+
* link - (platform and crate-type dependent)
39+
* dep-info - CRATE_NAME.d
3240
--print INFO[=FILE]
3341
Compiler information to print on stdout (or to a file)
3442
INFO may be one of

0 commit comments

Comments
 (0)