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 @@ -63,6 +63,30 @@ return Error::success(); } +Expected getDriverConfig(ArrayRef Args) { + StringRef Stem = sys::path::stem(ToolName); + auto Is = [=](StringRef Tool) { + // We need to recognize the following filenames: + // + // llvm-objcopy -> objcopy + // strip-10.exe -> strip + // powerpc64-unknown-freebsd13-objcopy -> objcopy + // llvm-install-name-tool -> install-name-tool + auto I = Stem.rfind_lower(Tool); + return I != StringRef::npos && + (I + Tool.size() == Stem.size() || !isAlnum(Stem[I + Tool.size()])); + }; + + if (Is("bitcode-strip") || Is("bitcode_strip")) + return parseBitcodeStripOptions(Args); + else if (Is("strip")) + return parseStripOptions(Args, reportWarning); + else if (Is("install-name-tool") || Is("install_name_tool")) + return parseInstallNameToolOptions(Args); + else + return parseObjcopyOptions(Args, reportWarning); +} + } // end namespace objcopy } // end namespace llvm @@ -309,34 +333,12 @@ namespace { -enum class ToolType { Objcopy, Strip, InstallNameTool, BitcodeStrip }; - } // anonymous namespace int main(int argc, char **argv) { InitLLVM X(argc, argv); ToolName = argv[0]; - StringRef Stem = sys::path::stem(ToolName); - auto Is = [=](StringRef Tool) { - // We need to recognize the following filenames: - // - // llvm-objcopy -> objcopy - // strip-10.exe -> strip - // powerpc64-unknown-freebsd13-objcopy -> objcopy - // llvm-install-name-tool -> install-name-tool - auto I = Stem.rfind_lower(Tool); - return I != StringRef::npos && - (I + Tool.size() == Stem.size() || !isAlnum(Stem[I + Tool.size()])); - }; - ToolType Tool = ToolType::Objcopy; - if (Is("bitcode-strip") || Is("bitcode_strip")) - Tool = ToolType::BitcodeStrip; - else if (Is("strip")) - Tool = ToolType::Strip; - else if (Is("install-name-tool") || Is("install_name_tool")) - Tool = ToolType::InstallNameTool; - // Expand response files. // TODO: Move these lines, which are copied from lib/Support/CommandLine.cpp, // into a separate function in the CommandLine library and call that function @@ -351,14 +353,8 @@ NewArgv); auto Args = makeArrayRef(NewArgv).drop_front(); - Expected DriverConfig = - (Tool == ToolType::Strip) - ? parseStripOptions(Args, reportWarning) - : ((Tool == ToolType::InstallNameTool) - ? parseInstallNameToolOptions(Args) - : ((Tool == ToolType::BitcodeStrip) - ? parseBitcodeStripOptions(Args) - : parseObjcopyOptions(Args, reportWarning))); + Expected DriverConfig = getDriverConfig(Args); + if (!DriverConfig) { logAllUnhandledErrors(DriverConfig.takeError(), WithColor::error(errs(), ToolName));