blob: 121a9db5d9931f137a4e8cb8647a989b3e3c6874 [file] [log] [blame]
// Copyright 2023 Google Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "build_config.h"
#include <inttypes.h>
#include "ipc_utils.h"
#include "process_utils.h"
#include "util.h"
std::string BuildConfig::status_format() const {
std::string result = "[%f/%t] ";
const char* env = environment.Get("NINJA_STATUS");
if (env)
result = env;
return result;
}
int BuildConfig::status_max_commands() const {
int result = 0;
const char* env = environment.Get("NINJA_STATUS_MAX_COMMANDS");
if (env) {
int count = atoi(env);
if (count < 0)
count = 0;
result = count;
}
return result;
}
int64_t BuildConfig::status_refresh_millis() const {
int64_t result = 100;
const char* env = environment.Get("NINJA_STATUS_REFRESH_MILLIS");
if (env) {
long long val = strtoll(env, NULL, 10);
result = static_cast<int64_t>(val);
}
return result;
}
bool BuildConfig::operator==(const BuildConfig& o) const {
return verbosity == o.verbosity && dry_run == o.dry_run &&
parallelism == o.parallelism &&
failures_allowed == o.failures_allowed &&
max_load_average == o.max_load_average &&
depfile_parser_options == o.depfile_parser_options &&
input_file == o.input_file && environment == o.environment;
}
std::string BuildConfig::ToEncodedString() const {
WireEncoder encoder;
encoder.Write(verbosity);
encoder.Write(dry_run);
encoder.Write(parallelism);
encoder.Write(failures_allowed);
encoder.Write(max_load_average);
// depfile_parser_options is empty!
encoder.Write(input_file);
encoder.Write(environment.ToEncodedString());
return encoder.TakeResult();
}
// static
BuildConfig BuildConfig::FromEncodedString(const std::string& str,
std::string* error) {
BuildConfig result;
WireDecoder decoder(str);
error->clear();
decoder.Read(result.verbosity);
decoder.Read(result.dry_run);
decoder.Read(result.parallelism);
decoder.Read(result.failures_allowed);
decoder.Read(result.max_load_average);
// depfile_parser_options is empty!
decoder.Read(result.input_file);
std::string encoded;
decoder.Read(encoded);
if (decoder.has_error()) {
*error = "Truncated BuildCOnfig encoded string";
return {};
}
result.environment = EnvironmentBlock::FromEncodedString(encoded, error);
if (!error->empty())
return {};
return result;
}
std::string BuildConfig::ToString() const {
std::string result = "verbosity=";
switch (verbosity) {
case QUIET:
result += "quiet";
break;
case NO_STATUS_UPDATE:
result += "no-status-upadte";
break;
case NORMAL:
result += "normal";
break;
case VERBOSE:
result += "verbose";
break;
default:
StringAppendFormat(result, "unknown(%d)", static_cast<int>(verbosity));
}
StringAppendFormat(
result,
" dry_run=%s parallelism=%d failures_allowed=%d max_load_average=%.2f",
dry_run ? "true" : "false", parallelism, failures_allowed,
max_load_average);
StringAppendFormat(result, "input_file=%s", input_file.c_str());
StringAppendFormat(
result,
" environment=%s", environment.AsString().c_str());
StringAppendFormat(
result,
" status_format=%s status_max_commands=%d status_refresh_millis=" PRId64,
status_format().c_str(), status_max_commands(), status_refresh_millis());
return result;
}