kythe.io@v0.0.68-0.20240422202219-7225dbc01741/kythe/cxx/extractor/dump_bazel_artifacts.cc (about)

     1  /*
     2   * Copyright 2020 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  #include <cerrno>
    17  #include <cstring>
    18  #include <fstream>
    19  #include <iostream>
    20  #include <string>
    21  
    22  #include "absl/flags/flag.h"
    23  #include "absl/flags/parse.h"
    24  #include "absl/log/log.h"
    25  #include "absl/strings/string_view.h"
    26  #include "google/protobuf/io/zero_copy_stream_impl.h"
    27  #include "kythe/cxx/common/init.h"
    28  #include "kythe/cxx/extractor/bazel_artifact_reader.h"
    29  #include "kythe/cxx/extractor/bazel_event_reader.h"
    30  
    31  ABSL_FLAG(std::string, build_event_binary_file, "",
    32            "Bazel event protocol file to read");
    33  
    34  namespace kythe {
    35  namespace {
    36  
    37  absl::string_view Basename(absl::string_view path) {
    38    if (auto pos = path.rfind('/'); pos != path.npos) {
    39      return path.substr(pos + 1);
    40    }
    41    return path;
    42  }
    43  
    44  int DumpArtifacts(const std::string filename) {
    45    std::ifstream file(filename);
    46    if (!file.is_open()) {
    47      LOG(ERROR) << "Error opening " << filename << ": " << std::strerror(errno);
    48      return 1;
    49    }
    50  
    51    google::protobuf::io::IstreamInputStream input(&file);
    52    BazelEventReader events(&input);
    53    BazelArtifactReader artifacts(&events);
    54    for (; !artifacts.Done(); artifacts.Next()) {
    55      std::cout << artifacts.Ref().label << std::endl;
    56      for (const auto& [local_path, uri] : artifacts.Ref().files) {
    57        std::cout << "  " << Basename(uri) << std::endl;
    58      }
    59    }
    60    if (!artifacts.status().ok()) {
    61      LOG(ERROR) << artifacts.status();
    62    }
    63  
    64    return !artifacts.status().ok();
    65  }
    66  }  // namespace
    67  }  // namespace kythe
    68  
    69  int main(int argc, char** argv) {
    70    kythe::InitializeProgram(argv[0]);
    71    absl::ParseCommandLine(argc, argv);
    72    return kythe::DumpArtifacts(absl::GetFlag(FLAGS_build_event_binary_file));
    73  }