diff --git a/llvm/include/llvm/Support/Host.h b/llvm/include/llvm/Support/Host.h --- a/llvm/include/llvm/Support/Host.h +++ b/llvm/include/llvm/Support/Host.h @@ -19,6 +19,7 @@ class MallocAllocator; class StringRef; template class StringMap; +class raw_ostream; namespace sys { @@ -54,6 +55,10 @@ /// \return - True on success. bool getHostCPUFeatures(StringMap &Features); + /// This is a function compatible with cl::AddExtraVersionPrinter, which adds + /// info about the current target triple and detected CPU. + void printDefaultTargetAndDetectedCPU(raw_ostream &OS); + namespace detail { /// Helper functions to extract HostCPUName from /proc/cpuinfo on linux. StringRef getHostCPUNameForPowerPC(StringRef ProcCpuinfoContent); diff --git a/llvm/lib/MC/TargetRegistry.cpp b/llvm/lib/MC/TargetRegistry.cpp --- a/llvm/lib/MC/TargetRegistry.cpp +++ b/llvm/lib/MC/TargetRegistry.cpp @@ -123,6 +123,7 @@ } array_pod_sort(Targets.begin(), Targets.end(), TargetArraySortFn); + OS << "\n"; OS << " Registered Targets:\n"; for (const auto &Target : Targets) { OS << " " << Target.first; diff --git a/llvm/lib/Support/CommandLine.cpp b/llvm/lib/Support/CommandLine.cpp --- a/llvm/lib/Support/CommandLine.cpp +++ b/llvm/lib/Support/CommandLine.cpp @@ -28,7 +28,6 @@ #include "llvm/ADT/StringExtras.h" #include "llvm/ADT/StringMap.h" #include "llvm/ADT/StringRef.h" -#include "llvm/ADT/Triple.h" #include "llvm/ADT/Twine.h" #include "llvm/Config/config.h" #include "llvm/Support/ConvertUTF.h" @@ -36,7 +35,6 @@ #include "llvm/Support/Error.h" #include "llvm/Support/ErrorHandling.h" #include "llvm/Support/FileSystem.h" -#include "llvm/Support/Host.h" #include "llvm/Support/ManagedStatic.h" #include "llvm/Support/MemoryBuffer.h" #include "llvm/Support/Path.h" @@ -1358,9 +1356,11 @@ bool cl::expandResponseFiles(int Argc, const char *const *Argv, const char *EnvVar, StringSaver &Saver, SmallVectorImpl &NewArgv) { - auto Tokenize = Triple(sys::getProcessTriple()).isOSWindows() - ? cl::TokenizeWindowsCommandLine - : cl::TokenizeGNUCommandLine; +#ifdef _WIN32 + auto Tokenize = cl::TokenizeWindowsCommandLine; +#else + auto Tokenize = cl::TokenizeGNUCommandLine; +#endif // The environment variable specifies initial options. if (EnvVar) if (llvm::Optional EnvValue = sys::Process::GetEnv(EnvVar)) @@ -1504,9 +1504,12 @@ // Expand response files. SmallVector newArgv(argv, argv + argc); BumpPtrAllocator A; - ExpansionContext ECtx(A, Triple(sys::getProcessTriple()).isOSWindows() - ? cl::TokenizeWindowsCommandLine - : cl::TokenizeGNUCommandLine); +#ifdef _WIN32 + auto Tokenize = cl::TokenizeWindowsCommandLine; +#else + auto Tokenize = cl::TokenizeGNUCommandLine; +#endif + ExpansionContext ECtx(A, Tokenize); if (Error Err = ECtx.expandResponseFiles(newArgv)) { *Errs << toString(std::move(Err)) << '\n'; return false; @@ -2535,7 +2538,7 @@ namespace { class VersionPrinter { public: - void print() { + void print(std::vector ExtraPrinters = {}) { raw_ostream &OS = outs(); #ifdef PACKAGE_VENDOR OS << PACKAGE_VENDOR << " "; @@ -2551,15 +2554,14 @@ #ifndef NDEBUG OS << " with assertions"; #endif -#if LLVM_VERSION_PRINTER_SHOW_HOST_TARGET_INFO - std::string CPU = std::string(sys::getHostCPUName()); - if (CPU == "generic") - CPU = "(unknown)"; - OS << ".\n" - << " Default target: " << sys::getDefaultTargetTriple() << '\n' - << " Host CPU: " << CPU; -#endif - OS << '\n'; + OS << ".\n"; + + // Iterate over any registered extra printers and call them to add further + // information. + if (!ExtraPrinters.empty()) { + for (const auto &I : ExtraPrinters) + I(outs()); + } } void operator=(bool OptionWasSpecified); }; @@ -2686,15 +2688,7 @@ CommonOptions->OverrideVersionPrinter(outs()); exit(0); } - print(); - - // Iterate over any registered extra printers and call them to add further - // information. - if (!CommonOptions->ExtraVersionPrinters.empty()) { - outs() << '\n'; - for (const auto &I : CommonOptions->ExtraVersionPrinters) - I(outs()); - } + print(CommonOptions->ExtraVersionPrinters); exit(0); } @@ -2749,7 +2743,7 @@ /// Utility function for printing version number. void cl::PrintVersionMessage() { - CommonOptions->VersionPrinterInstance.print(); + CommonOptions->VersionPrinterInstance.print(CommonOptions->ExtraVersionPrinters); } void cl::SetVersionPrinter(VersionPrinterTy func) { diff --git a/llvm/lib/Support/Host.cpp b/llvm/lib/Support/Host.cpp --- a/llvm/lib/Support/Host.cpp +++ b/llvm/lib/Support/Host.cpp @@ -1847,3 +1847,13 @@ return PT.str(); } + +void sys::printDefaultTargetAndDetectedCPU(raw_ostream &OS) { +#if LLVM_VERSION_PRINTER_SHOW_HOST_TARGET_INFO + std::string CPU = std::string(sys::getHostCPUName()); + if (CPU == "generic") + CPU = "(unknown)"; + OS << " Default target: " << sys::getDefaultTargetTriple() << '\n' + << " Host CPU: " << CPU << '\n'; +#endif +} diff --git a/llvm/tools/dsymutil/dsymutil.cpp b/llvm/tools/dsymutil/dsymutil.cpp --- a/llvm/tools/dsymutil/dsymutil.cpp +++ b/llvm/tools/dsymutil/dsymutil.cpp @@ -549,6 +549,8 @@ int dsymutil_main(int argc, char **argv) { InitLLVM X(argc, argv); + cl::AddExtraVersionPrinter(sys::printDefaultTargetAndDetectedCPU); + // Parse arguments. DsymutilOptTable T; unsigned MAI; diff --git a/llvm/tools/llc/llc.cpp b/llvm/tools/llc/llc.cpp --- a/llvm/tools/llc/llc.cpp +++ b/llvm/tools/llc/llc.cpp @@ -373,6 +373,8 @@ // Initialize debugging passes. initializeScavengerTestPass(*Registry); + // Register the Target and CPU printer for --version. + cl::AddExtraVersionPrinter(sys::printDefaultTargetAndDetectedCPU); // Register the target printer for --version. cl::AddExtraVersionPrinter(TargetRegistry::printRegisteredTargetsForVersion); diff --git a/llvm/tools/llvm-ar/llvm-ar.cpp b/llvm/tools/llvm-ar/llvm-ar.cpp --- a/llvm/tools/llvm-ar/llvm-ar.cpp +++ b/llvm/tools/llvm-ar/llvm-ar.cpp @@ -1460,6 +1460,8 @@ llvm::InitializeAllTargetMCs(); llvm::InitializeAllAsmParsers(); + cl::AddExtraVersionPrinter(sys::printDefaultTargetAndDetectedCPU); + Stem = sys::path::stem(ToolName); auto Is = [](StringRef Tool) { // We need to recognize the following filenames. diff --git a/llvm/tools/llvm-dwarfdump/llvm-dwarfdump.cpp b/llvm/tools/llvm-dwarfdump/llvm-dwarfdump.cpp --- a/llvm/tools/llvm-dwarfdump/llvm-dwarfdump.cpp +++ b/llvm/tools/llvm-dwarfdump/llvm-dwarfdump.cpp @@ -687,6 +687,9 @@ llvm::InitializeAllTargetInfos(); llvm::InitializeAllTargetMCs(); + // Register the Target and CPU printer for --version. + cl::AddExtraVersionPrinter(sys::printDefaultTargetAndDetectedCPU); + HideUnrelatedOptions( {&DwarfDumpCategory, &SectionCategory, &getColorCategory()}); cl::ParseCommandLineOptions( diff --git a/llvm/tools/llvm-dwarfutil/llvm-dwarfutil.cpp b/llvm/tools/llvm-dwarfutil/llvm-dwarfutil.cpp --- a/llvm/tools/llvm-dwarfutil/llvm-dwarfutil.cpp +++ b/llvm/tools/llvm-dwarfutil/llvm-dwarfutil.cpp @@ -454,6 +454,8 @@ InitLLVM X(Argc, Argv); ToolName = Argv[0]; + cl::AddExtraVersionPrinter(sys::printDefaultTargetAndDetectedCPU); + // Parse arguments. DwarfutilOptTable T; unsigned MAI; diff --git a/llvm/tools/llvm-gsymutil/llvm-gsymutil.cpp b/llvm/tools/llvm-gsymutil/llvm-gsymutil.cpp --- a/llvm/tools/llvm-gsymutil/llvm-gsymutil.cpp +++ b/llvm/tools/llvm-gsymutil/llvm-gsymutil.cpp @@ -439,6 +439,9 @@ llvm::InitializeAllTargets(); + // Register the Target and CPU printer for --version. + cl::AddExtraVersionPrinter(sys::printDefaultTargetAndDetectedCPU); + const char *Overview = "A tool for dumping, searching and creating GSYM files.\n\n" "Specify one or more GSYM paths as arguments to dump all of the " diff --git a/llvm/tools/llvm-libtool-darwin/llvm-libtool-darwin.cpp b/llvm/tools/llvm-libtool-darwin/llvm-libtool-darwin.cpp --- a/llvm/tools/llvm-libtool-darwin/llvm-libtool-darwin.cpp +++ b/llvm/tools/llvm-libtool-darwin/llvm-libtool-darwin.cpp @@ -20,6 +20,7 @@ #include "llvm/Object/MachOUniversalWriter.h" #include "llvm/Object/ObjectFile.h" #include "llvm/Support/CommandLine.h" +#include "llvm/Support/Host.h" #include "llvm/Support/InitLLVM.h" #include "llvm/Support/LineIterator.h" #include "llvm/Support/TargetSelect.h" @@ -709,6 +710,7 @@ int main(int Argc, char **Argv) { InitLLVM X(Argc, Argv); + cl::AddExtraVersionPrinter(sys::printDefaultTargetAndDetectedCPU); cl::HideUnrelatedOptions({&LibtoolCategory, &getColorCategory()}); Expected ConfigOrErr = parseCommandLine(Argc, Argv); if (!ConfigOrErr) { diff --git a/llvm/tools/llvm-lto/llvm-lto.cpp b/llvm/tools/llvm-lto/llvm-lto.cpp --- a/llvm/tools/llvm-lto/llvm-lto.cpp +++ b/llvm/tools/llvm-lto/llvm-lto.cpp @@ -45,8 +45,8 @@ #include "llvm/Support/SourceMgr.h" #include "llvm/Support/TargetSelect.h" #include "llvm/Support/ToolOutputFile.h" -#include "llvm/Support/raw_ostream.h" #include "llvm/Support/WithColor.h" +#include "llvm/Support/raw_ostream.h" #include "llvm/Target/TargetOptions.h" #include #include diff --git a/llvm/tools/llvm-mc-assemble-fuzzer/llvm-mc-assemble-fuzzer.cpp b/llvm/tools/llvm-mc-assemble-fuzzer/llvm-mc-assemble-fuzzer.cpp --- a/llvm/tools/llvm-mc-assemble-fuzzer/llvm-mc-assemble-fuzzer.cpp +++ b/llvm/tools/llvm-mc-assemble-fuzzer/llvm-mc-assemble-fuzzer.cpp @@ -280,6 +280,9 @@ LLVMInitializeAllTargetMCs(); LLVMInitializeAllAsmParsers(); + // Register the Target and CPU printer for --version. + cl::AddExtraVersionPrinter(sys::printDefaultTargetAndDetectedCPU); + cl::ParseCommandLineOptions(*argc, OriginalArgv); // Rebuild the argv without the arguments llvm-mc-fuzzer consumed so that diff --git a/llvm/tools/llvm-mc-disassemble-fuzzer/llvm-mc-disassemble-fuzzer.cpp b/llvm/tools/llvm-mc-disassemble-fuzzer/llvm-mc-disassemble-fuzzer.cpp --- a/llvm/tools/llvm-mc-disassemble-fuzzer/llvm-mc-disassemble-fuzzer.cpp +++ b/llvm/tools/llvm-mc-disassemble-fuzzer/llvm-mc-disassemble-fuzzer.cpp @@ -108,6 +108,9 @@ LLVMInitializeAllTargetMCs(); LLVMInitializeAllDisassemblers(); + // Register the Target and CPU printer for --version. + cl::AddExtraVersionPrinter(sys::printDefaultTargetAndDetectedCPU); + cl::ParseCommandLineOptions(*argc, OriginalArgv); // Rebuild the argv without the arguments llvm-mc-fuzzer consumed so that diff --git a/llvm/tools/llvm-mc/llvm-mc.cpp b/llvm/tools/llvm-mc/llvm-mc.cpp --- a/llvm/tools/llvm-mc/llvm-mc.cpp +++ b/llvm/tools/llvm-mc/llvm-mc.cpp @@ -355,6 +355,9 @@ llvm::InitializeAllAsmParsers(); llvm::InitializeAllDisassemblers(); + // Register the Target and CPU printer for --version. + cl::AddExtraVersionPrinter(sys::printDefaultTargetAndDetectedCPU); + // Register the target printer for --version. cl::AddExtraVersionPrinter(TargetRegistry::printRegisteredTargetsForVersion); diff --git a/llvm/tools/llvm-mca/llvm-mca.cpp b/llvm/tools/llvm-mca/llvm-mca.cpp --- a/llvm/tools/llvm-mca/llvm-mca.cpp +++ b/llvm/tools/llvm-mca/llvm-mca.cpp @@ -322,6 +322,9 @@ InitializeAllAsmParsers(); InitializeAllTargetMCAs(); + // Register the Target and CPU printer for --version. + cl::AddExtraVersionPrinter(sys::printDefaultTargetAndDetectedCPU); + // Enable printing of available targets when flag --version is specified. cl::AddExtraVersionPrinter(TargetRegistry::printRegisteredTargetsForVersion); diff --git a/llvm/tools/llvm-nm/llvm-nm.cpp b/llvm/tools/llvm-nm/llvm-nm.cpp --- a/llvm/tools/llvm-nm/llvm-nm.cpp +++ b/llvm/tools/llvm-nm/llvm-nm.cpp @@ -2299,6 +2299,7 @@ int llvm_nm_main(int argc, char **argv) { InitLLVM X(argc, argv); + cl::AddExtraVersionPrinter(sys::printDefaultTargetAndDetectedCPU); BumpPtrAllocator A; StringSaver Saver(A); NmOptTable Tbl; diff --git a/llvm/tools/llvm-objcopy/llvm-objcopy.cpp b/llvm/tools/llvm-objcopy/llvm-objcopy.cpp --- a/llvm/tools/llvm-objcopy/llvm-objcopy.cpp +++ b/llvm/tools/llvm-objcopy/llvm-objcopy.cpp @@ -225,6 +225,9 @@ int llvm_objcopy_main(int argc, char **argv) { InitLLVM X(argc, argv); + // Register the Target and CPU printer for --version. + cl::AddExtraVersionPrinter(sys::printDefaultTargetAndDetectedCPU); + ToolName = argv[0]; // Expand response files. diff --git a/llvm/tools/llvm-objdump/llvm-objdump.cpp b/llvm/tools/llvm-objdump/llvm-objdump.cpp --- a/llvm/tools/llvm-objdump/llvm-objdump.cpp +++ b/llvm/tools/llvm-objdump/llvm-objdump.cpp @@ -69,6 +69,7 @@ #include "llvm/Option/ArgList.h" #include "llvm/Option/Option.h" #include "llvm/Support/Casting.h" +#include "llvm/Support/CommandLine.h" #include "llvm/Support/Debug.h" #include "llvm/Support/Errc.h" #include "llvm/Support/FileSystem.h" @@ -3116,6 +3117,9 @@ using namespace llvm; InitLLVM X(argc, argv); + // Register the Target and CPU printer for --version. + cl::AddExtraVersionPrinter(sys::printDefaultTargetAndDetectedCPU); + ToolName = argv[0]; std::unique_ptr T; OptSpecifier Unknown, HelpFlag, HelpHiddenFlag, VersionFlag; @@ -3143,6 +3147,9 @@ HelpFlag = OBJDUMP_help; HelpHiddenFlag = OBJDUMP_help_hidden; VersionFlag = OBJDUMP_version; + // Register the Known Targets Printer for `--version`. + cl::AddExtraVersionPrinter( + TargetRegistry::printRegisteredTargetsForVersion); } BumpPtrAllocator A; @@ -3167,10 +3174,6 @@ if (InputArgs.hasArg(VersionFlag)) { cl::PrintVersionMessage(); - if (!Is("otool")) { - outs() << '\n'; - TargetRegistry::printRegisteredTargetsForVersion(outs()); - } return 0; } diff --git a/llvm/tools/llvm-profgen/llvm-profgen.cpp b/llvm/tools/llvm-profgen/llvm-profgen.cpp --- a/llvm/tools/llvm-profgen/llvm-profgen.cpp +++ b/llvm/tools/llvm-profgen/llvm-profgen.cpp @@ -145,6 +145,9 @@ InitializeAllTargetMCs(); InitializeAllDisassemblers(); + // Register the Target and CPU printer for --version. + cl::AddExtraVersionPrinter(sys::printDefaultTargetAndDetectedCPU); + cl::HideUnrelatedOptions({&ProfGenCategory, &getColorCategory()}); cl::ParseCommandLineOptions(argc, argv, "llvm SPGO profile generator\n"); validateCommandLine(); diff --git a/llvm/tools/llvm-rtdyld/llvm-rtdyld.cpp b/llvm/tools/llvm-rtdyld/llvm-rtdyld.cpp --- a/llvm/tools/llvm-rtdyld/llvm-rtdyld.cpp +++ b/llvm/tools/llvm-rtdyld/llvm-rtdyld.cpp @@ -1016,6 +1016,9 @@ llvm::InitializeAllTargetMCs(); llvm::InitializeAllDisassemblers(); + // Register the Target and CPU printer for --version. + cl::AddExtraVersionPrinter(sys::printDefaultTargetAndDetectedCPU); + cl::HideUnrelatedOptions({&RTDyldCategory, &getColorCategory()}); cl::ParseCommandLineOptions(argc, argv, "llvm MC-JIT tool\n"); diff --git a/llvm/tools/opt/opt.cpp b/llvm/tools/opt/opt.cpp --- a/llvm/tools/opt/opt.cpp +++ b/llvm/tools/opt/opt.cpp @@ -509,6 +509,9 @@ PluginList.emplace_back(Plugin.get()); }); + // Register the Target and CPU printer for --version. + cl::AddExtraVersionPrinter(sys::printDefaultTargetAndDetectedCPU); + cl::ParseCommandLineOptions(argc, argv, "llvm .bc -> .bc modular optimizer and analysis printer\n");