10#include "llvm/ADT/STLExtras.h"
11#include "llvm/ADT/SmallVector.h"
12#include "llvm/Support/CommandLine.h"
13#include "llvm/Support/DynamicLibrary.h"
14#include "llvm/Support/Error.h"
15#include "llvm/Support/FileSystem.h"
16#include "llvm/Support/FormatVariadic.h"
17#include "llvm/Support/Path.h"
18#include "llvm/Support/Process.h"
19#include "llvm/Support/WithColor.h"
20#include "llvm/Support/raw_ostream.h"
27namespace fs = llvm::sys::fs;
28namespace path = llvm::sys::path;
36namespace ErrorMessages {
38constexpr const char *CannotValidatePath =
"failed to validate path '{0}': {1}";
40constexpr const char *ExtensionNotSupplied =
"Extension not supplied";
42constexpr const char *NoFormatForExtension =
43 "No format registered for extension '{0}'";
45constexpr const char *PathDoesNotExist =
"Path does not exist";
47constexpr const char *PathIsNotAFile =
"Path is not a file";
49constexpr const char *OutputDirectoryMissing =
50 "Parent directory does not exist";
52constexpr const char *OutputDirectoryNotWritable =
53 "Parent directory is not writable";
55constexpr const char *FileAlreadyExists =
"File already exists";
57constexpr const char *FailedToLoadPlugin =
"failed to load plugin '{0}': {1}";
59constexpr const char *InvalidTargetTriple =
60 "invalid {0} '{1}': unrecognized architecture";
64llvm::StringRef ToolName;
65llvm::StringRef ToolVersion;
67void printVersion(llvm::raw_ostream &OS) {
68 OS << ToolName <<
" " << ToolVersion <<
"\n";
75SerializationFormat *getFormatForExtension(llvm::StringRef Extension) {
81 static llvm::SmallVector<
82 std::pair<std::string, std::unique_ptr<SerializationFormat>>, 4>
86 auto ReversedList = llvm::reverse(ExtensionFormatList);
87 auto It = llvm::find_if(ReversedList, [&](
const auto &Entry) {
90 if (It != ReversedList.end()) {
91 return It->second.get();
99 SerializationFormat *
Result = Format.get();
101 "makeFormat must return non-null for a registered extension");
103 ExtensionFormatList.emplace_back(Extension, std::move(Format));
108FormatFile fromPath(llvm::StringRef Path) {
109 llvm::StringRef
Extension = path::extension(Path);
111 fail(ErrorMessages::CannotValidatePath, Path,
112 ErrorMessages::ExtensionNotSupplied);
116 SerializationFormat *Format = getFormatForExtension(Extension);
118 std::string BadExtension =
119 llvm::formatv(ErrorMessages::NoFormatForExtension, Extension);
120 fail(ErrorMessages::CannotValidatePath, Path, BadExtension);
123 return {Path.str(), Format};
131 llvm::WithColor::error(llvm::errs(), ToolName) << Msg <<
"\n";
132 llvm::sys::Process::Exit(1);
136 std::string Message = llvm::toString(std::move(Err));
141 for (
const std::string &PluginPath : Paths) {
142 if (!fs::exists(PluginPath)) {
143 fail(ErrorMessages::FailedToLoadPlugin, PluginPath,
144 ErrorMessages::PathDoesNotExist);
147 if (llvm::sys::DynamicLibrary::LoadLibraryPermanently(PluginPath.c_str(),
149 fail(ErrorMessages::FailedToLoadPlugin, PluginPath, ErrMsg);
155 llvm::StringRef
Value) {
156 assert(!
Value.empty() &&
157 "parseTargetTripleOrFail: triple value cannot be empty");
160 llvm::Triple
T(llvm::Triple::normalize(
Value));
165 if (
T.getArch() == llvm::Triple::UnknownArch) {
166 fail(ErrorMessages::InvalidTargetTriple, FlagName,
Value);
173 llvm::cl::OptionCategory &Category,
174 llvm::StringRef ToolHeading) {
176 ToolName = path::stem(argv[0]);
179 ToolVersion = Version;
182 llvm::cl::HideUnrelatedOptions(Category);
185 llvm::cl::SetVersionPrinter(printVersion);
188 std::string Overview = (ToolHeading +
"\n").str();
189 llvm::cl::ParseCommandLineOptions(argc, argv, Overview);
194 if (!fs::exists(
Path)) {
195 fail(ErrorMessages::CannotValidatePath,
Path,
196 ErrorMessages::PathDoesNotExist);
199 if (!fs::is_regular_file(
Path)) {
200 fail(ErrorMessages::CannotValidatePath,
Path,
201 ErrorMessages::PathIsNotAFile);
204 return fromPath(
Path);
209 if (fs::exists(
Path)) {
210 fail(ErrorMessages::CannotValidatePath,
Path,
211 ErrorMessages::FileAlreadyExists);
214 llvm::StringRef ParentDir = path::parent_path(
Path);
215 llvm::StringRef DirToCheck = ParentDir.empty() ?
"." : ParentDir;
217 if (!fs::exists(DirToCheck)) {
218 fail(ErrorMessages::CannotValidatePath,
Path,
219 ErrorMessages::OutputDirectoryMissing);
222 if (fs::access(DirToCheck, fs::AccessMode::Write)) {
223 fail(ErrorMessages::CannotValidatePath,
Path,
224 ErrorMessages::OutputDirectoryNotWritable);
227 return fromPath(
Path);
Result
Implement __builtin_bit_cast and related operations.
@ OS
Indicates that the tracking object is a descendant of a referenced-counted OSObject,...
llvm::StringRef getToolName()
Returns the name of the running tool, as set by initTool().
std::unique_ptr< SerializationFormat > makeFormat(llvm::StringRef FormatName)
Try to instantiate a SerializationFormat with a given name.
llvm::Triple parseTargetTripleOrFail(llvm::StringRef FlagName, llvm::StringRef Value)
Parses and validates a target triple supplied on the command line.
void fail(const char *Msg)
void initTool(int argc, const char **argv, llvm::StringRef Version, llvm::cl::OptionCategory &Category, llvm::StringRef ToolHeading)
Sets ToolName, ToolVersion, and the version printer, hides unrelated command-line options,...
bool isFormatRegistered(llvm::StringRef FormatName)
Check if a SerializationFormat was registered with a given name.
void loadPlugins(llvm::ArrayRef< std::string > Paths)
const FunctionProtoType * T