Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(11)

Side by Side Diff: tools/gn/json_project_writer.cc

Issue 2064533002: [GN] Add JSON project writer (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: [GN] fix include order Created 4 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright (c) 2016 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "tools/gn/json_project_writer.h"
6
7 #include <iostream>
8
9 #include "base/command_line.h"
10 #include "base/json/json_writer.h"
11 #include "base/memory/ptr_util.h"
12 #include "base/strings/string_number_conversions.h"
13 #include "tools/gn/builder.h"
14 #include "tools/gn/desc_builder.h"
15 #include "tools/gn/exec_process.h"
16 #include "tools/gn/filesystem_utils.h"
17 #include "tools/gn/settings.h"
18
19 namespace {
20
21 std::string render_json(const BuildSettings* build_settings,
22 const Builder* builder) {
23 std::vector<const Target*> all_targets = builder->GetAllResolvedTargets();
24
25 Label default_toolchain_label;
26
27 auto targets = base::WrapUnique(new base::DictionaryValue());
28 for (const auto* target : all_targets) {
29 if (default_toolchain_label.is_null())
30 default_toolchain_label = target->settings()->default_toolchain_label();
31 targets->Set(
32 target->label().GetUserVisibleName(default_toolchain_label),
33 DescBuilder::DescriptionForTarget(target, "", false, false, false));
34 }
35
36 auto settings = base::WrapUnique(new base::DictionaryValue());
37 settings->SetString("root_path", build_settings->root_path_utf8());
38 settings->SetString("build_dir", build_settings->build_dir().value());
39 settings->SetString("default_toolchain",
40 default_toolchain_label.GetUserVisibleName(false));
41
42 auto output = base::WrapUnique(new base::DictionaryValue());
43 output->Set("targets", std::move(targets));
44 output->Set("build_settings", std::move(settings));
45
46 std::string s;
47 base::JSONWriter::WriteWithOptions(
48 *output.get(), base::JSONWriter::OPTIONS_PRETTY_PRINT, &s);
49 return s;
50 }
51
52 bool invoke_python(const BuildSettings* build_settings,
53 const base::FilePath& python_script_path,
54 const std::string& python_script_extra_args,
55 const base::FilePath& output_path,
56 bool quiet,
57 Err* err) {
58 const base::FilePath& python_path = build_settings->python_path();
59 base::CommandLine cmdline(python_path);
60 cmdline.AppendArg("--");
61 cmdline.AppendArgPath(python_script_path);
62 cmdline.AppendArgPath(output_path);
63 if (!python_script_extra_args.empty()) {
64 cmdline.AppendArg(python_script_extra_args);
65 }
66 base::FilePath startup_dir =
67 build_settings->GetFullPath(build_settings->build_dir());
68
69 std::string output;
70 std::string stderr_output;
71
72 int exit_code = 0;
73 if (!internal::ExecProcess(cmdline, startup_dir, &output, &stderr_output,
74 &exit_code)) {
75 *err =
76 Err(Location(), "Could not execute python.",
77 "I was trying to execute \"" + FilePathToUTF8(python_path) + "\".");
78 return false;
79 }
80
81 if (!quiet) {
82 std::cout << output;
83 std::cerr << stderr_output;
84 }
85
86 if (exit_code != 0) {
87 *err = Err(Location(), "Python has quit with exit code " +
88 base::IntToString(exit_code) + ".");
89 return false;
90 }
91
92 return true;
93 }
94
95 } // namespace
96
97 bool JSONProjectWriter::RunAndWriteFiles(
98 const BuildSettings* build_settings,
99 const Builder* builder,
100 const std::string& file_name,
101 const std::string& exec_script,
102 const std::string& exec_script_extra_args,
103 bool quiet,
104 Err* err) {
105 SourceFile output_file = build_settings->build_dir().ResolveRelativeFile(
106 Value(nullptr, file_name), err);
107 if (output_file.is_null()) {
108 return false;
109 }
110
111 base::FilePath output_path = build_settings->GetFullPath(output_file);
112
113 std::string json = render_json(build_settings, builder);
114 if (!ContentsEqual(output_path, json)) {
115 if (!WriteFileIfChanged(output_path, json, err)) {
116 return false;
117 }
118
119 if (!exec_script.empty()) {
120 SourceFile script_file;
121 if (exec_script[0] != '/') {
122 // relative path, assume the base is in build_dir
123 script_file = build_settings->build_dir().ResolveRelativeFile(
124 Value(nullptr, exec_script), err);
125 if (script_file.is_null()) {
126 return false;
127 }
128 } else {
129 script_file = SourceFile(exec_script);
130 }
131 base::FilePath script_path = build_settings->GetFullPath(script_file);
132 return invoke_python(build_settings, script_path, exec_script_extra_args,
133 output_path, quiet, err);
134 }
135 }
136
137 return true;
138 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698