| Index: tools/gn/json_project_writer.cc
|
| diff --git a/tools/gn/json_project_writer.cc b/tools/gn/json_project_writer.cc
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..00cee9a5229ada212547c9c66af7954e63b573bc
|
| --- /dev/null
|
| +++ b/tools/gn/json_project_writer.cc
|
| @@ -0,0 +1,138 @@
|
| +// Copyright (c) 2016 The Chromium Authors. All rights reserved.
|
| +// Use of this source code is governed by a BSD-style license that can be
|
| +// found in the LICENSE file.
|
| +
|
| +#include "tools/gn/json_project_writer.h"
|
| +
|
| +#include <iostream>
|
| +
|
| +#include "base/command_line.h"
|
| +#include "base/json/json_writer.h"
|
| +#include "base/memory/ptr_util.h"
|
| +#include "base/strings/string_number_conversions.h"
|
| +#include "tools/gn/builder.h"
|
| +#include "tools/gn/desc_builder.h"
|
| +#include "tools/gn/exec_process.h"
|
| +#include "tools/gn/filesystem_utils.h"
|
| +#include "tools/gn/settings.h"
|
| +
|
| +namespace {
|
| +
|
| +std::string render_json(const BuildSettings* build_settings,
|
| + const Builder* builder) {
|
| + std::vector<const Target*> all_targets = builder->GetAllResolvedTargets();
|
| +
|
| + Label default_toolchain_label;
|
| +
|
| + auto targets = base::WrapUnique(new base::DictionaryValue());
|
| + for (const auto* target : all_targets) {
|
| + if (default_toolchain_label.is_null())
|
| + default_toolchain_label = target->settings()->default_toolchain_label();
|
| + targets->Set(
|
| + target->label().GetUserVisibleName(default_toolchain_label),
|
| + DescBuilder::DescriptionForTarget(target, "", false, false, false));
|
| + }
|
| +
|
| + auto settings = base::WrapUnique(new base::DictionaryValue());
|
| + settings->SetString("root_path", build_settings->root_path_utf8());
|
| + settings->SetString("build_dir", build_settings->build_dir().value());
|
| + settings->SetString("default_toolchain",
|
| + default_toolchain_label.GetUserVisibleName(false));
|
| +
|
| + auto output = base::WrapUnique(new base::DictionaryValue());
|
| + output->Set("targets", std::move(targets));
|
| + output->Set("build_settings", std::move(settings));
|
| +
|
| + std::string s;
|
| + base::JSONWriter::WriteWithOptions(
|
| + *output.get(), base::JSONWriter::OPTIONS_PRETTY_PRINT, &s);
|
| + return s;
|
| +}
|
| +
|
| +bool invoke_python(const BuildSettings* build_settings,
|
| + const base::FilePath& python_script_path,
|
| + const std::string& python_script_extra_args,
|
| + const base::FilePath& output_path,
|
| + bool quiet,
|
| + Err* err) {
|
| + const base::FilePath& python_path = build_settings->python_path();
|
| + base::CommandLine cmdline(python_path);
|
| + cmdline.AppendArg("--");
|
| + cmdline.AppendArgPath(python_script_path);
|
| + cmdline.AppendArgPath(output_path);
|
| + if (!python_script_extra_args.empty()) {
|
| + cmdline.AppendArg(python_script_extra_args);
|
| + }
|
| + base::FilePath startup_dir =
|
| + build_settings->GetFullPath(build_settings->build_dir());
|
| +
|
| + std::string output;
|
| + std::string stderr_output;
|
| +
|
| + int exit_code = 0;
|
| + if (!internal::ExecProcess(cmdline, startup_dir, &output, &stderr_output,
|
| + &exit_code)) {
|
| + *err =
|
| + Err(Location(), "Could not execute python.",
|
| + "I was trying to execute \"" + FilePathToUTF8(python_path) + "\".");
|
| + return false;
|
| + }
|
| +
|
| + if (!quiet) {
|
| + std::cout << output;
|
| + std::cerr << stderr_output;
|
| + }
|
| +
|
| + if (exit_code != 0) {
|
| + *err = Err(Location(), "Python has quit with exit code " +
|
| + base::IntToString(exit_code) + ".");
|
| + return false;
|
| + }
|
| +
|
| + return true;
|
| +}
|
| +
|
| +} // namespace
|
| +
|
| +bool JSONProjectWriter::RunAndWriteFiles(
|
| + const BuildSettings* build_settings,
|
| + const Builder* builder,
|
| + const std::string& file_name,
|
| + const std::string& exec_script,
|
| + const std::string& exec_script_extra_args,
|
| + bool quiet,
|
| + Err* err) {
|
| + SourceFile output_file = build_settings->build_dir().ResolveRelativeFile(
|
| + Value(nullptr, file_name), err);
|
| + if (output_file.is_null()) {
|
| + return false;
|
| + }
|
| +
|
| + base::FilePath output_path = build_settings->GetFullPath(output_file);
|
| +
|
| + std::string json = render_json(build_settings, builder);
|
| + if (!ContentsEqual(output_path, json)) {
|
| + if (!WriteFileIfChanged(output_path, json, err)) {
|
| + return false;
|
| + }
|
| +
|
| + if (!exec_script.empty()) {
|
| + SourceFile script_file;
|
| + if (exec_script[0] != '/') {
|
| + // relative path, assume the base is in build_dir
|
| + script_file = build_settings->build_dir().ResolveRelativeFile(
|
| + Value(nullptr, exec_script), err);
|
| + if (script_file.is_null()) {
|
| + return false;
|
| + }
|
| + } else {
|
| + script_file = SourceFile(exec_script);
|
| + }
|
| + base::FilePath script_path = build_settings->GetFullPath(script_file);
|
| + return invoke_python(build_settings, script_path, exec_script_extra_args,
|
| + output_path, quiet, err);
|
| + }
|
| + }
|
| +
|
| + return true;
|
| +}
|
|
|