• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "gn/json_project_writer.h"
6 
7 #include <memory>
8 
9 #include "base/command_line.h"
10 #include "base/json/json_writer.h"
11 #include "base/strings/string_number_conversions.h"
12 #include "gn/builder.h"
13 #include "gn/commands.h"
14 #include "gn/deps_iterator.h"
15 #include "gn/desc_builder.h"
16 #include "gn/exec_process.h"
17 #include "gn/filesystem_utils.h"
18 #include "gn/scheduler.h"
19 #include "gn/settings.h"
20 
21 // Structure of JSON output file
22 // {
23 //   "build_settings" = {
24 //     "root_path" : "absolute path of project root",
25 //     "build_dir" : "build directory (project relative)",
26 //     "default_toolchain" : "name of default toolchain"
27 //   }
28 //   "targets" = {
29 //      "target x name" : { target x properties },
30 //      "target y name" : { target y properties },
31 //      ...
32 //    }
33 // }
34 // See desc_builder.cc for overview of target properties
35 
36 namespace {
37 
AddTargetDependencies(const Target * target,std::set<const Target * > * deps)38 void AddTargetDependencies(const Target* target,
39                            std::set<const Target*>* deps) {
40   for (const auto& pair : target->GetDeps(Target::DEPS_LINKED)) {
41     if (deps->find(pair.ptr) == deps->end()) {
42       deps->insert(pair.ptr);
43       AddTargetDependencies(pair.ptr, deps);
44     }
45   }
46 }
47 
48 // Filters targets according to filter string; Will also recursively
49 // add dependent targets.
FilterTargets(const BuildSettings * build_settings,std::vector<const Target * > & all_targets,std::vector<const Target * > * targets,const std::string & dir_filter_string,Err * err)50 bool FilterTargets(const BuildSettings* build_settings,
51                    std::vector<const Target*>& all_targets,
52                    std::vector<const Target*>* targets,
53                    const std::string& dir_filter_string,
54                    Err* err) {
55   if (dir_filter_string.empty()) {
56     *targets = all_targets;
57   } else {
58     targets->reserve(all_targets.size());
59     std::vector<LabelPattern> filters;
60     if (!commands::FilterPatternsFromString(build_settings, dir_filter_string,
61                                             &filters, err)) {
62       return false;
63     }
64     commands::FilterTargetsByPatterns(all_targets, filters, targets);
65 
66     std::set<const Target*> target_set(targets->begin(), targets->end());
67     for (const auto* target : *targets)
68       AddTargetDependencies(target, &target_set);
69 
70     targets->clear();
71     targets->insert(targets->end(), target_set.begin(), target_set.end());
72   }
73 
74   // Sort the list of targets per-label to get a consistent ordering of them
75   // in the generated project (and thus stability of the file generated).
76   std::sort(targets->begin(), targets->end(),
77             [](const Target* a, const Target* b) {
78               return a->label().name() < b->label().name();
79             });
80 
81   return true;
82 }
83 
InvokePython(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)84 bool InvokePython(const BuildSettings* build_settings,
85                   const base::FilePath& python_script_path,
86                   const std::string& python_script_extra_args,
87                   const base::FilePath& output_path,
88                   bool quiet,
89                   Err* err) {
90   const base::FilePath& python_path = build_settings->python_path();
91   base::CommandLine cmdline(python_path);
92   cmdline.AppendArg("--");
93   cmdline.AppendArgPath(python_script_path);
94   cmdline.AppendArgPath(output_path);
95   if (!python_script_extra_args.empty()) {
96     cmdline.AppendArg(python_script_extra_args);
97   }
98   base::FilePath startup_dir =
99       build_settings->GetFullPath(build_settings->build_dir());
100 
101   std::string output;
102   std::string stderr_output;
103 
104   int exit_code = 0;
105   if (!internal::ExecProcess(cmdline, startup_dir, &output, &stderr_output,
106                              &exit_code)) {
107     *err =
108         Err(Location(), "Could not execute python.",
109             "I was trying to execute \"" + FilePathToUTF8(python_path) + "\".");
110     return false;
111   }
112 
113   if (!quiet) {
114     printf("%s", output.c_str());
115     fprintf(stderr, "%s", stderr_output.c_str());
116   }
117 
118   if (exit_code != 0) {
119     *err = Err(Location(), "Python has quit with exit code " +
120                                base::IntToString(exit_code) + ".");
121     return false;
122   }
123 
124   return true;
125 }
126 
127 }  // namespace
128 
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,const std::string & dir_filter_string,bool quiet,Err * err)129 bool JSONProjectWriter::RunAndWriteFiles(
130     const BuildSettings* build_settings,
131     const Builder& builder,
132     const std::string& file_name,
133     const std::string& exec_script,
134     const std::string& exec_script_extra_args,
135     const std::string& dir_filter_string,
136     bool quiet,
137     Err* err) {
138   SourceFile output_file = build_settings->build_dir().ResolveRelativeFile(
139       Value(nullptr, file_name), err);
140   if (output_file.is_null()) {
141     return false;
142   }
143 
144   base::FilePath output_path = build_settings->GetFullPath(output_file);
145 
146   std::vector<const Target*> all_targets = builder.GetAllResolvedTargets();
147   std::vector<const Target*> targets;
148   if (!FilterTargets(build_settings, all_targets, &targets, dir_filter_string,
149                      err)) {
150     return false;
151   }
152 
153   std::string json = RenderJSON(build_settings, targets);
154   if (!ContentsEqual(output_path, json)) {
155     if (!WriteFileIfChanged(output_path, json, err)) {
156       return false;
157     }
158 
159     if (!exec_script.empty()) {
160       SourceFile script_file;
161       if (exec_script[0] != '/') {
162         // Relative path, assume the base is in build_dir.
163         script_file = build_settings->build_dir().ResolveRelativeFile(
164             Value(nullptr, exec_script), err);
165         if (script_file.is_null()) {
166           return false;
167         }
168       } else {
169         script_file = SourceFile(exec_script);
170       }
171       base::FilePath script_path = build_settings->GetFullPath(script_file);
172       return InvokePython(build_settings, script_path, exec_script_extra_args,
173                           output_path, quiet, err);
174     }
175   }
176 
177   return true;
178 }
179 
RenderJSON(const BuildSettings * build_settings,std::vector<const Target * > & all_targets)180 std::string JSONProjectWriter::RenderJSON(
181     const BuildSettings* build_settings,
182     std::vector<const Target*>& all_targets) {
183   Label default_toolchain_label;
184 
185   auto targets = std::make_unique<base::DictionaryValue>();
186   for (const auto* target : all_targets) {
187     if (default_toolchain_label.is_null())
188       default_toolchain_label = target->settings()->default_toolchain_label();
189     auto description =
190         DescBuilder::DescriptionForTarget(target, "", false, false, false);
191     // Outputs need to be asked for separately.
192     auto outputs = DescBuilder::DescriptionForTarget(target, "source_outputs",
193                                                      false, false, false);
194     base::DictionaryValue* outputs_value = nullptr;
195     if (outputs->GetDictionary("source_outputs", &outputs_value) &&
196         !outputs_value->empty()) {
197       description->MergeDictionary(outputs.get());
198     }
199     targets->SetWithoutPathExpansion(
200         target->label().GetUserVisibleName(default_toolchain_label),
201         std::move(description));
202   }
203 
204   auto settings = std::make_unique<base::DictionaryValue>();
205   settings->SetKey("root_path", base::Value(build_settings->root_path_utf8()));
206   settings->SetKey("build_dir",
207                    base::Value(build_settings->build_dir().value()));
208   settings->SetKey(
209       "default_toolchain",
210       base::Value(default_toolchain_label.GetUserVisibleName(false)));
211 
212   std::vector<base::FilePath> input_files;
213   g_scheduler->input_file_manager()->GetAllPhysicalInputFileNames(&input_files);
214 
215   // Other files read by the build.
216   std::vector<base::FilePath> other_files = g_scheduler->GetGenDependencies();
217 
218   // Sort the input files to order them deterministically.
219   // Additionally, remove duplicate filepaths that seem to creep in.
220   std::set<base::FilePath> fileset(input_files.begin(), input_files.end());
221   fileset.insert(other_files.begin(), other_files.end());
222 
223   base::ListValue inputs;
224   const auto &build_path = build_settings->root_path();
225   for (const auto& other_file : fileset) {
226     std::string file;
227     if (MakeAbsolutePathRelativeIfPossible(FilePathToUTF8(build_path),
228                                            FilePathToUTF8(other_file), &file)) {
229       inputs.Append(std::make_unique<base::Value>(std::move(file)));
230     }
231   }
232   settings->SetKey("gen_input_files", std::move(inputs));
233 
234   auto output = std::make_unique<base::DictionaryValue>();
235   output->SetWithoutPathExpansion("targets", std::move(targets));
236   output->SetWithoutPathExpansion("build_settings", std::move(settings));
237 
238   std::string s;
239   base::JSONWriter::WriteWithOptions(
240       *output.get(), base::JSONWriter::OPTIONS_PRETTY_PRINT, &s);
241   return s;
242 }
243