kythe.io@v0.0.68-0.20240422202219-7225dbc01741/kythe/cxx/extractor/proto/proto_extractor_main.cc (about) 1 /* 2 * Copyright 2019 The Kythe Authors. All rights reserved. 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 // Standalone extractor for protobuf. Given a proto file, builds a kzip 18 // containing the proto file(s) and all dependencies. 19 // 20 // Usage: 21 // export KYTHE_OUTPUT_FILE=foo.kzip 22 // proto_extractor foo.proto 23 // proto_extractor foo.proto bar.proto 24 // proto_extractor foo.proto -- --proto_path dir/with/my/deps 25 26 #include <cstdlib> 27 #include <string> 28 #include <utility> 29 #include <vector> 30 31 #include "absl/flags/parse.h" 32 #include "absl/flags/usage.h" 33 #include "absl/log/check.h" 34 #include "absl/strings/match.h" 35 #include "absl/strings/string_view.h" 36 #include "kythe/cxx/common/index_writer.h" 37 #include "kythe/cxx/common/init.h" 38 #include "kythe/cxx/common/kzip_writer.h" 39 #include "kythe/cxx/extractor/proto/proto_extractor.h" 40 #include "kythe/cxx/indexer/proto/search_path.h" 41 #include "kythe/proto/analysis.pb.h" 42 43 namespace kythe { 44 namespace lang_proto { 45 namespace { 46 /// \brief Returns a kzip-based IndexWriter or dies. 47 IndexWriter OpenKzipWriterOrDie(absl::string_view path) { 48 auto writer = KzipWriter::Create(path); 49 CHECK(writer.ok()) << "Failed to open KzipWriter: " << writer.status(); 50 return std::move(*writer); 51 } 52 53 int main(int argc, char* argv[]) { 54 kythe::InitializeProgram(argv[0]); 55 absl::SetProgramUsageMessage( 56 R"(Standalone extractor for the Kythe Proto indexer. 57 Creates a Kzip containing the main proto file(s) and any dependencies. 58 59 Examples: 60 export KYTHE_OUTPUT_FILE=foo.kzip 61 proto_extractor foo.proto 62 proto_extractor foo.proto bar.proto 63 proto_extractor foo.proto -- --proto_path dir/with/my/deps")"); 64 std::vector<char*> remain = absl::ParseCommandLine(argc, argv); 65 std::vector<std::string> final_args(remain.begin() + 1, remain.end()); 66 67 ProtoExtractor extractor; 68 extractor.ConfigureFromEnv(); 69 70 const char* env_output_file = getenv("KYTHE_OUTPUT_FILE"); 71 CHECK(env_output_file != nullptr) 72 << "Please specify an output kzip file with the KYTHE_OUTPUT_FILE " 73 "environment variable."; 74 IndexWriter kzip_writer = OpenKzipWriterOrDie(env_output_file); 75 76 // Parse --proto_path and -I args into a set of path substitutions (search 77 // paths). The remaining arguments should be .proto files. 78 std::vector<std::string> proto_filenames; 79 ::kythe::lang_proto::ParsePathSubstitutions( 80 final_args, &extractor.path_substitutions, &proto_filenames); 81 for (const std::string& arg : proto_filenames) { 82 CHECK(absl::EndsWith(arg, ".proto")) 83 << "Invalid arg, expected a proto file: '" << arg << "'"; 84 } 85 CHECK(!proto_filenames.empty()) << "Expected 1+ .proto files."; 86 87 // Extract and save kzip. 88 proto::IndexedCompilation compilation; 89 *compilation.mutable_unit() = 90 extractor.ExtractProtos(proto_filenames, &kzip_writer); 91 auto digest = kzip_writer.WriteUnit(compilation); 92 CHECK(digest.ok()) << "Error writing unit to kzip: " << digest.status(); 93 CHECK(kzip_writer.Close().ok()); 94 95 return 0; 96 } 97 98 } // namespace 99 } // namespace lang_proto 100 } // namespace kythe 101 102 int main(int argc, char* argv[]) { return kythe::lang_proto::main(argc, argv); }