Kadir Çetinkaya | 0769200 | 2024-02-28 07:10:05 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (C) 2024 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | // Driver for c++ extractor. Operates in two modes: |
| 18 | // - DEPS, scans build graph for active files and reports targets that need to |
| 19 | // be build for analyzing that file. |
| 20 | // - INPUTS, scans the source code for active files and returns all the sources |
| 21 | // required for analyzing that file. |
| 22 | // |
| 23 | // Uses stdin/stdout to take in requests and provide responses. |
| 24 | #include <unistd.h> |
| 25 | |
| 26 | #include <memory> |
| 27 | #include <utility> |
| 28 | |
| 29 | #include "analyzer.h" |
| 30 | #include "google/protobuf/message.h" |
| 31 | #include "ide_query.pb.h" |
| 32 | #include "llvm/ADT/StringRef.h" |
| 33 | #include "llvm/Support/CommandLine.h" |
| 34 | #include "llvm/Support/TargetSelect.h" |
| 35 | #include "llvm/Support/raw_ostream.h" |
| 36 | |
| 37 | namespace { |
| 38 | enum class OpMode { |
| 39 | DEPS = 0, |
| 40 | INPUTS = 1, |
| 41 | }; |
| 42 | llvm::cl::opt<OpMode> mode{ |
| 43 | "mode", |
| 44 | llvm::cl::values(clEnumValN(OpMode::DEPS, "deps", |
| 45 | "Figure out targets that need to be build"), |
| 46 | clEnumValN(OpMode::INPUTS, "inputs", |
| 47 | "Figure out generated files used")), |
| 48 | llvm::cl::desc("Print the list of headers to insert and remove"), |
| 49 | }; |
| 50 | |
| 51 | ide_query::IdeAnalysis ReturnError(llvm::StringRef message) { |
| 52 | ide_query::IdeAnalysis result; |
| 53 | result.mutable_status()->set_code(ide_query::Status::FAILURE); |
| 54 | result.mutable_status()->set_message(message.str()); |
| 55 | return result; |
| 56 | } |
| 57 | |
| 58 | } // namespace |
| 59 | |
| 60 | int main(int argc, char* argv[]) { |
| 61 | llvm::InitializeAllTargetInfos(); |
| 62 | llvm::cl::ParseCommandLineOptions(argc, argv); |
| 63 | |
| 64 | ide_query::RepoState state; |
| 65 | if (!state.ParseFromFileDescriptor(STDIN_FILENO)) { |
| 66 | llvm::errs() << "Failed to parse input!\n"; |
| 67 | return 1; |
| 68 | } |
| 69 | |
| 70 | std::unique_ptr<google::protobuf::Message> result; |
| 71 | switch (mode) { |
| 72 | case OpMode::DEPS: { |
| 73 | result = std::make_unique<ide_query::DepsResponse>( |
| 74 | tools::ide_query::cc_analyzer::GetDeps(std::move(state))); |
| 75 | break; |
| 76 | } |
| 77 | case OpMode::INPUTS: { |
| 78 | result = std::make_unique<ide_query::IdeAnalysis>( |
| 79 | tools::ide_query::cc_analyzer::GetBuildInputs(std::move(state))); |
| 80 | break; |
| 81 | } |
| 82 | default: |
| 83 | llvm::errs() << "Unknown operation mode!\n"; |
| 84 | return 1; |
| 85 | } |
| 86 | if (!result->SerializeToFileDescriptor(STDOUT_FILENO)) { |
| 87 | llvm::errs() << "Failed to serialize result!\n"; |
| 88 | return 1; |
| 89 | } |
| 90 | |
| 91 | return 0; |
| 92 | } |