// Copyright 2024 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

// copy from google3/devtools/cider/services/build/companion/ide_query.proto#1

syntax = "proto3";

package ide_query;

option go_package = "go.chromium.org/build/siso/toolsupport/ciderutil/proto";

// Indicates the success/failure for analysis.
message Status {
  option deprecated = true;

  enum Code {
    OK = 0;
    FAILURE = 1;
  }
  Code code = 1;
  // Details about the status, might be displayed to user.
  optional string message = 2;
}

message GeneratedFile {
  // Path to the file relative to build_out_dir.
  string path = 1;

  // The text of the generated file, if not provided contents will be read
  // from the path above in user's workstation.
  optional bytes contents = 2;
}

message SourceFile {
  option deprecated = true;

  // Path to the source file relative to repository root.
  string path = 1;

  // Working directory used by the build system. All the relative
  // paths in compiler_arguments should be relative to this path.
  // Relative to repository root.
  string working_dir = 2;

  // Compiler arguments to compile the source file. If multiple variants
  // of the module being compiled are possible, the query script will choose
  // one.
  repeated string compiler_arguments = 3;

  // Any generated files that are used in compiling the file.
  repeated GeneratedFile generated = 4;

  // Paths to all of the sources, like build files, code generators,
  // proto files etc. that were used during analysis. Used to figure
  // out when a set of build artifacts are stale and the query tool
  // must be re-run.
  // Relative to repository root.
  repeated string deps = 5;

  // Represents analysis status for this particular file. e.g. not part
  // of the build graph.
  optional Status status = 6;
}

message IdeAnalysis {
  // Directory that contains build outputs generated by the build system.
  // Relative to repository root.
  string build_out_dir = 1;
  // Working directory used by the build system.
  // Relative to repository root.
  string working_dir = 4;
  // Only set if the whole query failed.
  optional AnalysisError error = 5;
  // List of results, one per queried file.
  repeated AnalysisResult results = 6;
  // List of buildable units directly or indirectly references by the results.
  repeated BuildableUnit units = 7;

  repeated SourceFile sources = 2 [deprecated = true];
  // Status representing overall analysis.
  // Should fail only when no analysis can be performed.
  optional Status status = 3 [deprecated = true];
}

message AnalysisError {
  // Human readable error message.
  string error_message = 1;
}

message AnalysisResult {
  // Path to the source file that was queried, relative to repository root.
  string source_file_path = 1;
  // Indicates the success/failure for the query.
  message Status {
    enum Code {
      CODE_UNSPECIFIED = 0;
      CODE_OK = 1;
      CODE_NOT_FOUND = 2;  // no target or module found for the source file.
      CODE_BUILD_FAILED = 3;
    }
    Code code = 1;
    // Details about the status, might be displayed to user.
    optional string status_message = 2;
  }
  // Represents status for this result. e.g. not part of the build graph.
  Status status = 2;
  // ID of buildable unit that contains the source file.
  // The ide_query script can choose the most relevant unit from multiple
  // options.
  string unit_id = 3;
  // Invalidation rule to check if the result is still valid.
  Invalidation invalidation = 4;
}

enum Language {
  LANGUAGE_UNSPECIFIED = 0;
  LANGUAGE_JAVA = 1;  // also includes Kotlin
  LANGUAGE_CPP = 2;
}

message BuildableUnit {
  // Unique identifier of the buildable unit.
  //
  // Examples:
  //   - Java: module or target name, e.g. "framework-bluetooth" or
  //   "//third_party/hamcrest:hamcrest_java"
  //   - C++: source file, e.g. "path/to/file.cc"
  string id = 1;
  // Language of the unit.
  // Required for buildable units directly referenced by the AnalysisResult,
  // e.g. the unit associated with the compilation stage for the source file.
  Language language = 2;
  // Source files that are part of this unit.
  // Path to the file relative to working_dir.
  repeated string source_file_paths = 3;
  // Compiler arguments to compile the source files.
  repeated string compiler_arguments = 4;
  // List of generated files produced by this unit.
  repeated GeneratedFile generated_files = 5;
  // List of other BuildableUnits this unit depend on.
  repeated string dependency_ids = 6;
}

// Invalidation rule to check if the result is still valid.
// This should contain files/dirs that are not directly part of the build graph
// but still affect the result. For example BUILD files, directory to the
// toolchain or config files etc.
message Invalidation {
  // If any of these files change the result may become invalid.
  // Path to the file relative to repository root.
  repeated string file_paths = 1;

  message Wildcard {
    // Prefix of the file path (e.g. "path/to/")
    optional string prefix = 1;
    // Suffix of the file path (e.g. "Android.bp")
    optional string suffix = 2;
    // If false, the part of the path between the given `prefix` and `suffix`
    // should not contain directory separators ('/').
    optional bool can_cross_folder = 3;
  }
  // If any of these rules match a changed file the result may become invalid.
  repeated Wildcard wildcards = 4;
}
