Skip to content

Commit

Permalink
Add structured parsing output from blazerc files.
Browse files Browse the repository at this point in the history
Introduces `OptionProcessor::GetBlazercOptions` method which returns a list of structs containing information about each option and its source file. The caller can than manipulate the list as needed before passing to `GetBlazercAndEnvCommandArgs`, which generates the `--rc_source` and `--default_override` flags from the structs.

PiperOrigin-RevId: 718414757
Change-Id: Idfadfb708ac7b37e89a9730fc254bbfd5985e97d
  • Loading branch information
Googler authored and copybara-github committed Jan 22, 2025
1 parent 21e133f commit 7c54196
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 47 deletions.
102 changes: 58 additions & 44 deletions src/main/cpp/option_processor.cc
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,9 @@
#include <iterator>
#include <memory>
#include <set>
#include <sstream>
#include <string>
#include <utility>
#include <vector>

#include "src/main/cpp/blaze_util.h"
#include "src/main/cpp/blaze_util_platform.h"
Expand All @@ -34,6 +35,11 @@
#include "src/main/cpp/util/path_platform.h"
#include "src/main/cpp/util/strings.h"
#include "src/main/cpp/workspace_layout.h"
#include "absl/container/flat_hash_map.h"
#include "absl/strings/str_cat.h"
#include "src/main/cpp/rc_file.h"
#include "src/main/cpp/startup_options.h"
#include "src/main/cpp/util/exit_code.h"

// On OSX, there apparently is no header that defines this.
#ifndef environ
Expand All @@ -47,7 +53,6 @@ extern char **environ;

namespace blaze {

using std::map;
using std::set;
using std::string;
using std::vector;
Expand Down Expand Up @@ -524,8 +529,9 @@ blaze_exit_code::ExitCode OptionProcessor::ParseOptions(
return parse_startup_options_exit_code;
}

parsed_blazercs_ = GetBlazercOptions(cwd, rc_file_ptrs);
blazerc_and_env_command_args_ = GetBlazercAndEnvCommandArgs(
cwd, rc_file_ptrs, blaze::internal::GetProcessedEnv());
cwd, parsed_blazercs_, blaze::internal::GetProcessedEnv());
return blaze_exit_code::SUCCESS;
}

Expand Down Expand Up @@ -595,46 +601,23 @@ blaze_exit_code::ExitCode OptionProcessor::ParseStartupOptions(
return startup_options_->ProcessArgs(rcstartup_flags, error);
}

// IMPORTANT: The options added here do not come from the user. In order for
// their source to be correctly tracked, the options must either be passed
// as --default_override=0, 0 being "client", or must be listed in
// BlazeOptionHandler.INTERNAL_COMMAND_OPTIONS!
std::vector<std::string> OptionProcessor::GetBlazercAndEnvCommandArgs(
const std::string& cwd,
const std::vector<RcFile*>& blazercs,
const std::vector<std::string>& env) {
// Provide terminal options as coming from the least important rc file.
std::vector<std::string> result = {
// LINT.IfChange
"--rc_source=client",
// LINT.ThenChange(src/main/java/com/google/devtools/common/options/GlobalRcUtils.java)
"--default_override=0:common=--isatty=" +
blaze_util::ToString(IsStandardTerminal()),
"--default_override=0:common=--terminal_columns=" +
blaze_util::ToString(GetTerminalColumns())};
std::vector<BlazercOption> OptionProcessor::GetBlazercOptions(
const std::string& cwd, const std::vector<RcFile*>& blazercs) {
std::vector<BlazercOption> result;

// Provide terminal options as least important options.
// LINT.IfChange
std::string terminal_rc_source = "client";
// LINT.ThenChange(src/main/java/com/google/devtools/common/options/GlobalRcUtils.java)
result.push_back({terminal_rc_source, "common",
"--isatty=" + blaze_util::ToString(IsStandardTerminal())});
result.push_back(
{terminal_rc_source, "common",
"--terminal_columns=" + blaze_util::ToString(GetTerminalColumns())});
if (IsEmacsTerminal()) {
result.push_back("--default_override=0:common=--emacs");
}

EnsurePythonPathOption(&result);

// Map .blazerc numbers to filenames. The indexes here start at 1 because #0
// is reserved the "client" options created by this function.
int cur_index = 1;
std::map<std::string, int> rcfile_indexes;
for (const auto* blazerc : blazercs) {
for (const std::string& source_path : blazerc->canonical_source_paths()) {
// Deduplicate the rc_source list because the same file might be included
// from multiple places.
if (rcfile_indexes.find(source_path) != rcfile_indexes.end()) continue;

result.push_back("--rc_source=" + blaze_util::ConvertPath(source_path));
rcfile_indexes[source_path] = cur_index;
cur_index++;
}
result.push_back({terminal_rc_source, "common", "--emacs"});
}

// Add RcOptions as default_overrides.
for (const auto* blazerc : blazercs) {
for (const auto& command_options : blazerc->options()) {
const string& command = command_options.first;
Expand All @@ -644,14 +627,45 @@ std::vector<std::string> OptionProcessor::GetBlazercAndEnvCommandArgs(
for (const RcOption& rcoption : command_options.second) {
const std::string& source_path =
blazerc->canonical_source_paths()[rcoption.source_index];
std::ostringstream oss;
oss << "--default_override=" << rcfile_indexes[source_path] << ':'
<< command << '=' << rcoption.option;
result.push_back(oss.str());
result.push_back({source_path, command, rcoption.option});
}
}
}

return result;
}

// IMPORTANT: The options added here do not come from the user. In order for
// their source to be correctly tracked, the options must either be passed
// as --default_override=0, 0 being "client", or must be listed in
// BlazeOptionHandler.INTERNAL_COMMAND_OPTIONS!
std::vector<std::string> OptionProcessor::GetBlazercAndEnvCommandArgs(
const std::string& cwd, const std::vector<BlazercOption>& blazerc_options,
const std::vector<std::string>& env) {
std::vector<std::string> result;
absl::flat_hash_map<std::string, int> rcfile_index;

// Fix first value of `--rc_source` to allow for user-defined
// `--default_override=0` values.
// LINT.IfChange
result.push_back("--rc_source=client");
rcfile_index["client"] = 0;
// LINT.ThenChange(src/main/java/com/google/devtools/common/options/GlobalRcUtils.java)
int cur_index = 1;
for (const auto& option : blazerc_options) {
if (rcfile_index.contains(option.source_path)) continue;
rcfile_index[option.source_path] = cur_index;
result.push_back(absl::StrCat("--rc_source=",
blaze_util::ConvertPath(option.source_path)));
++cur_index;
}
EnsurePythonPathOption(&result);
for (const auto& option : blazerc_options) {
result.push_back(
absl::StrCat("--default_override=", rcfile_index[option.source_path],
":", option.command, "=", option.option));
}

// Pass the client environment to the server.
for (const string& env_var : env) {
result.push_back("--client_env=" + env_var);
Expand Down
19 changes: 16 additions & 3 deletions src/main/cpp/option_processor.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
#ifndef BAZEL_SRC_MAIN_CPP_OPTION_PROCESSOR_H_
#define BAZEL_SRC_MAIN_CPP_OPTION_PROCESSOR_H_

#include <list>
#include <memory>
#include <string>
#include <utility>
Expand Down Expand Up @@ -49,6 +48,13 @@ struct CommandLine {
command_args(std::move(command_args_arg)) {}
};

// Represents an option parsed from a blazerc file.
struct BlazercOption {
std::string source_path;
std::string command;
std::string option;
};

// This class is responsible for parsing the command line of the Blaze binary,
// parsing blazerc files, and putting together the command that should be sent
// to the server.
Expand Down Expand Up @@ -117,11 +123,16 @@ class OptionProcessor {
// the failure. Otherwise, the server will handle any required logging.
void PrintStartupOptionsProvenanceMessage() const;

// Parse the files in `blazercs` and return all options that need to be passed
// to the server. The options are returned in the order they should be appear
// on the command line (later options have precedence over earlier ones).
static std::vector<BlazercOption> GetBlazercOptions(
const std::string& cwd, const std::vector<RcFile*>& blazercs);

// Constructs all synthetic command args that should be passed to the
// server to configure blazerc options and client environment.
static std::vector<std::string> GetBlazercAndEnvCommandArgs(
const std::string& cwd,
const std::vector<RcFile*>& blazercs,
const std::string& cwd, const std::vector<BlazercOption>& blazerc_options,
const std::vector<std::string>& env);

// Finds and parses the appropriate RcFiles:
Expand All @@ -144,6 +155,8 @@ class OptionProcessor {
// execution environment and the flags passed via the bazelrc files.
std::vector<std::string> blazerc_and_env_command_args_;

std::vector<BlazercOption> parsed_blazercs_;

// The command line constructed after calling ParseOptions.
std::unique_ptr<CommandLine> cmd_line_;

Expand Down

0 comments on commit 7c54196

Please sign in to comment.