kythe.io@v0.0.68-0.20240422202219-7225dbc01741/kythe/cxx/extractor/bazel_artifact.h (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  
    17  #ifndef KYTHE_CXX_EXTRACTOR_BAZEL_ARTIFACT_H_
    18  #define KYTHE_CXX_EXTRACTOR_BAZEL_ARTIFACT_H_
    19  
    20  #include <iomanip>
    21  #include <iostream>
    22  #include <string>
    23  #include <tuple>
    24  #include <vector>
    25  
    26  #include "absl/strings/str_format.h"
    27  #include "absl/strings/str_join.h"
    28  
    29  namespace kythe {
    30  
    31  /// \brief A pair of local path and canonical URI for a given Bazel output file.
    32  /// Bazel typically provides both a workspace-local path to a file as well as a
    33  /// canonical URI and both can be useful to clients.
    34  struct BazelArtifactFile {
    35    std::string local_path;  ///< The workspace-relative path to the file.
    36    std::string uri;  ///< The canonical URI for the file. Clients should be able
    37                      ///< to handle at least `file:` and `data:` schemes.
    38  
    39    bool operator==(const BazelArtifactFile& other) const {
    40      return std::tie(local_path, uri) == std::tie(other.local_path, other.uri);
    41    }
    42  
    43    bool operator!=(const BazelArtifactFile& other) const {
    44      return !(*this == other);
    45    }
    46  
    47    template <typename H>
    48    friend H AbslHashValue(H h, const BazelArtifactFile& file) {
    49      return H::combine(std::move(h), file.local_path, file.uri);
    50    }
    51  
    52    template <typename Sink>
    53    friend void AbslStringify(Sink& sink, const BazelArtifactFile& file) {
    54      absl::Format(&sink, "BazelArtifactFile{.local_path = %v, .uri = %v}",
    55                   absl::FormatStreamed(std::quoted(file.local_path)),
    56                   absl::FormatStreamed(std::quoted(file.uri)));
    57    }
    58    friend std::ostream& operator<<(std::ostream& out,
    59                                    const BazelArtifactFile& file) {
    60      return (out << absl::StreamFormat("%v", file));
    61    }
    62  };
    63  
    64  /// \brief A list of extracted compilation units and the target which owns them.
    65  struct BazelArtifact {
    66    std::string label;  ///< The target from which these artifacts originate.
    67    std::vector<BazelArtifactFile>
    68        files;  ///< A list of paths and URIs at which the artifacts can be found.
    69  
    70    bool operator==(const BazelArtifact& other) const {
    71      return std::tie(label, files) == std::tie(other.label, other.files);
    72    }
    73  
    74    bool operator!=(const BazelArtifact& other) const {
    75      return !(*this == other);
    76    }
    77  
    78    template <typename H>
    79    friend H AbslHashValue(H h, const BazelArtifact& artifact) {
    80      return H::combine(std::move(h), artifact.label, artifact.files);
    81    }
    82  
    83    template <typename Sink>
    84    friend void AbslStringify(Sink& sink, const BazelArtifact& artifact) {
    85      absl::Format(&sink, "BazelArtifact{.label = %v, .files = { %s }}",
    86                   absl::FormatStreamed(std::quoted(artifact.label)),
    87                   absl::StrJoin(artifact.files, ", "));
    88    }
    89    friend std::ostream& operator<<(std::ostream& out,
    90                                    const BazelArtifact& artifact) {
    91      return (out << absl::StreamFormat("%v", artifact));
    92    }
    93  };
    94  
    95  }  // namespace kythe
    96  #endif  // KYTHE_CXX_EXTRACTOR_BAZEL_ARTIFACT_H_