|
| 1 | +//===--- swift-dependency-tool.cpp - Convert binary swiftdeps to YAML -----===// |
| 2 | +// |
| 3 | +// This source file is part of the Swift.org open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2014 - 2020 Apple Inc. and the Swift project authors |
| 6 | +// Licensed under Apache License v2.0 with Runtime Library Exception |
| 7 | +// |
| 8 | +// See https://swift.org/LICENSE.txt for license information |
| 9 | +// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors |
| 10 | +// |
| 11 | +//===----------------------------------------------------------------------===// |
| 12 | + |
| 13 | +#include "swift/AST/FileSystem.h" |
| 14 | +#include "swift/AST/FineGrainedDependencies.h" |
| 15 | +#include "swift/AST/DiagnosticEngine.h" |
| 16 | +#include "swift/AST/FineGrainedDependencyFormat.h" |
| 17 | +#include "swift/Basic/SourceManager.h" |
| 18 | +#include "swift/Basic/LLVMInitialize.h" |
| 19 | +#include "llvm/Support/CommandLine.h" |
| 20 | +#include "llvm/Support/MemoryBuffer.h" |
| 21 | +#include "llvm/Support/YAMLParser.h" |
| 22 | + |
| 23 | +using namespace swift; |
| 24 | +using namespace fine_grained_dependencies; |
| 25 | + |
| 26 | +enum class ActionType : unsigned { |
| 27 | + None, |
| 28 | + BinaryToYAML, |
| 29 | + YAMLToBinary |
| 30 | +}; |
| 31 | + |
| 32 | +namespace options { |
| 33 | + |
| 34 | +static llvm::cl::OptionCategory Category("swift-dependency-tool Options"); |
| 35 | + |
| 36 | +static llvm::cl::opt<std::string> |
| 37 | +InputFilename("input-filename", |
| 38 | + llvm::cl::desc("Name of the input file"), |
| 39 | + llvm::cl::cat(Category)); |
| 40 | + |
| 41 | +static llvm::cl::opt<std::string> |
| 42 | +OutputFilename("output-filename", |
| 43 | + llvm::cl::desc("Name of the output file"), |
| 44 | + llvm::cl::cat(Category)); |
| 45 | + |
| 46 | +static llvm::cl::opt<ActionType> |
| 47 | +Action(llvm::cl::desc("Mode:"), llvm::cl::init(ActionType::None), |
| 48 | + llvm::cl::cat(Category), |
| 49 | + llvm::cl::values( |
| 50 | + clEnumValN(ActionType::BinaryToYAML, |
| 51 | + "to-yaml", "Convert new binary .swiftdeps format to YAML"), |
| 52 | + clEnumValN(ActionType::YAMLToBinary, |
| 53 | + "from-yaml", "Convert YAML to new binary .swiftdeps format"))); |
| 54 | + |
| 55 | +} |
| 56 | + |
| 57 | +int main(int argc, char *argv[]) { |
| 58 | + PROGRAM_START(argc, argv); |
| 59 | + INITIALIZE_LLVM(); |
| 60 | + |
| 61 | + llvm::cl::HideUnrelatedOptions(options::Category); |
| 62 | + llvm::cl::ParseCommandLineOptions(argc, argv, "Swift Dependency Tool\n"); |
| 63 | + |
| 64 | + SourceManager sourceMgr; |
| 65 | + DiagnosticEngine diags(sourceMgr); |
| 66 | + |
| 67 | + switch (options::Action) { |
| 68 | + case ActionType::None: { |
| 69 | + llvm::errs() << "action required\n"; |
| 70 | + llvm::cl::PrintHelpMessage(); |
| 71 | + return 1; |
| 72 | + } |
| 73 | + |
| 74 | + case ActionType::BinaryToYAML: { |
| 75 | + auto fg = SourceFileDepGraph::loadFromPath(options::InputFilename); |
| 76 | + if (!fg) { |
| 77 | + llvm::errs() << "Failed to read dependency file\n"; |
| 78 | + return 1; |
| 79 | + } |
| 80 | + |
| 81 | + bool hadError = |
| 82 | + withOutputFile(diags, options::OutputFilename, |
| 83 | + [&](llvm::raw_pwrite_stream &out) { |
| 84 | + out << fg->yamlProlog(/*hadError=*/false); |
| 85 | + llvm::yaml::Output yamlWriter(out); |
| 86 | + yamlWriter << *fg; |
| 87 | + return false; |
| 88 | + }); |
| 89 | + |
| 90 | + if (hadError) { |
| 91 | + llvm::errs() << "Failed to write YAML swiftdeps\n"; |
| 92 | + } |
| 93 | + break; |
| 94 | + } |
| 95 | + |
| 96 | + case ActionType::YAMLToBinary: { |
| 97 | + auto bufferOrError = llvm::MemoryBuffer::getFile(options::InputFilename); |
| 98 | + if (!bufferOrError) { |
| 99 | + llvm::errs() << "Failed to read dependency file\n"; |
| 100 | + return 1; |
| 101 | + } |
| 102 | + |
| 103 | + auto &buffer = *bufferOrError.get(); |
| 104 | + |
| 105 | + SourceFileDepGraph fg; |
| 106 | + llvm::yaml::Input yamlReader(llvm::MemoryBufferRef(buffer), nullptr); |
| 107 | + yamlReader >> fg; |
| 108 | + if (yamlReader.error()) { |
| 109 | + llvm::errs() << "Failed to parse YAML swiftdeps\n"; |
| 110 | + return 1; |
| 111 | + } |
| 112 | + |
| 113 | + if (writeFineGrainedDependencyGraph(diags, options::OutputFilename, fg)) { |
| 114 | + llvm::errs() << "Failed to write binary swiftdeps\n"; |
| 115 | + return 1; |
| 116 | + } |
| 117 | + |
| 118 | + break; |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + return 0; |
| 123 | +} |
0 commit comments