1 // Copyright (c) 2013 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/ninja_binary_target_writer.h"
6 
7 #include <sstream>
8 
9 #include "base/strings/string_util.h"
10 #include "gn/config_values_extractors.h"
11 #include "gn/deps_iterator.h"
12 #include "gn/filesystem_utils.h"
13 #include "gn/general_tool.h"
14 #include "gn/ninja_c_binary_target_writer.h"
15 #include "gn/ninja_rust_binary_target_writer.h"
16 #include "gn/ninja_target_command_util.h"
17 #include "gn/ninja_utils.h"
18 #include "gn/settings.h"
19 #include "gn/string_utils.h"
20 #include "gn/substitution_writer.h"
21 #include "gn/target.h"
22 #include "gn/variables.h"
23 
24 namespace {
25 
26 // Returns the proper escape options for writing compiler and linker flags.
GetFlagOptions()27 EscapeOptions GetFlagOptions() {
28   EscapeOptions opts;
29   opts.mode = ESCAPE_NINJA_COMMAND;
30   return opts;
31 }
32 
33 }  // namespace
34 
NinjaBinaryTargetWriter(const Target * target,std::ostream & out)35 NinjaBinaryTargetWriter::NinjaBinaryTargetWriter(const Target* target,
36                                                  std::ostream& out)
37     : NinjaTargetWriter(target, out),
38       rule_prefix_(GetNinjaRulePrefixForToolchain(settings_)) {}
39 
40 NinjaBinaryTargetWriter::~NinjaBinaryTargetWriter() = default;
41 
Run()42 void NinjaBinaryTargetWriter::Run() {
43   if (target_->source_types_used().RustSourceUsed()) {
44     NinjaRustBinaryTargetWriter writer(target_, out_);
45     writer.Run();
46     return;
47   }
48 
49   NinjaCBinaryTargetWriter writer(target_, out_);
50   writer.Run();
51 }
52 
WriteInputsStampAndGetDep(size_t num_stamp_uses) const53 std::vector<OutputFile> NinjaBinaryTargetWriter::WriteInputsStampAndGetDep(
54     size_t num_stamp_uses) const {
55   CHECK(target_->toolchain()) << "Toolchain not set on target "
56                               << target_->label().GetUserVisibleName(true);
57 
58   UniqueVector<const SourceFile*> inputs;
59   for (ConfigValuesIterator iter(target_); !iter.done(); iter.Next()) {
60     for (const auto& input : iter.cur().inputs()) {
61       inputs.push_back(&input);
62     }
63   }
64 
65   if (inputs.size() == 0)
66     return std::vector<OutputFile>();  // No inputs
67 
68   // If we only have one input, return it directly instead of writing a stamp
69   // file for it.
70   if (inputs.size() == 1) {
71     return std::vector<OutputFile>{
72       OutputFile(settings_->build_settings(), *inputs[0])};
73   }
74 
75   std::vector<OutputFile> outs;
76   for (const SourceFile* source : inputs)
77     outs.push_back(OutputFile(settings_->build_settings(), *source));
78 
79   // If there are multiple inputs, but the stamp file would be referenced only
80   // once, don't write it but depend on the inputs directly.
81   if (num_stamp_uses == 1u)
82     return outs;
83 
84   // Make a stamp file.
85   OutputFile stamp_file =
86       GetBuildDirForTargetAsOutputFile(target_, BuildDirType::OBJ);
87   stamp_file.value().append(target_->label().name());
88   stamp_file.value().append(".inputs.stamp");
89 
90   out_ << "build ";
91   path_output_.WriteFile(out_, stamp_file);
92   out_ << ": " << GetNinjaRulePrefixForToolchain(settings_)
93        << GeneralTool::kGeneralToolStamp;
94 
95   // File inputs.
96   for (const auto* input : inputs) {
97     out_ << " ";
98     path_output_.WriteFile(out_, *input);
99   }
100 
101   out_ << std::endl;
102   return {stamp_file};
103 }
104 
WriteSourceSetStamp(const std::vector<OutputFile> & object_files)105 void NinjaBinaryTargetWriter::WriteSourceSetStamp(
106     const std::vector<OutputFile>& object_files) {
107   // The stamp rule for source sets is generally not used, since targets that
108   // depend on this will reference the object files directly. However, writing
109   // this rule allows the user to type the name of the target and get a build
110   // which can be convenient for development.
111   ClassifiedDeps classified_deps = GetClassifiedDeps();
112 
113   // The classifier should never put extra object files in a source sets: any
114   // source sets that we depend on should appear in our non-linkable deps
115   // instead.
116   DCHECK(classified_deps.extra_object_files.empty());
117 
118   std::vector<OutputFile> order_only_deps;
119   for (auto* dep : classified_deps.non_linkable_deps)
120     order_only_deps.push_back(dep->dependency_output_file());
121 
122   WriteStampForTarget(object_files, order_only_deps);
123 }
124 
125 NinjaBinaryTargetWriter::ClassifiedDeps
GetClassifiedDeps() const126 NinjaBinaryTargetWriter::GetClassifiedDeps() const {
127   ClassifiedDeps classified_deps;
128 
129   // Normal public/private deps.
130   for (const auto& pair : target_->GetDeps(Target::DEPS_LINKED)) {
131     ClassifyDependency(pair.ptr, &classified_deps);
132   }
133 
134   // Inherited libraries.
135   for (auto* inherited_target : target_->inherited_libraries().GetOrdered()) {
136     ClassifyDependency(inherited_target, &classified_deps);
137   }
138 
139   // Data deps.
140   for (const auto& data_dep_pair : target_->data_deps())
141     classified_deps.non_linkable_deps.push_back(data_dep_pair.ptr);
142 
143   return classified_deps;
144 }
145 
ClassifyDependency(const Target * dep,ClassifiedDeps * classified_deps) const146 void NinjaBinaryTargetWriter::ClassifyDependency(
147     const Target* dep,
148     ClassifiedDeps* classified_deps) const {
149   // Only the following types of outputs have libraries linked into them:
150   //  EXECUTABLE
151   //  SHARED_LIBRARY
152   //  _complete_ STATIC_LIBRARY
153   //
154   // Child deps of intermediate static libraries get pushed up the
155   // dependency tree until one of these is reached, and source sets
156   // don't link at all.
157   bool can_link_libs = target_->IsFinal();
158 
159   if (can_link_libs && dep->swift_values().builds_module())
160     classified_deps->swiftmodule_deps.push_back(dep);
161 
162   if (target_->source_types_used().RustSourceUsed() &&
163       (target_->output_type() == Target::RUST_LIBRARY ||
164        target_->output_type() == Target::STATIC_LIBRARY) &&
165       dep->IsLinkable()) {
166     // Rust libraries and static libraries aren't final, but need to have the
167     // link lines of all transitive deps specified.
168     classified_deps->linkable_deps.push_back(dep);
169   } else if (dep->output_type() == Target::SOURCE_SET ||
170              // If a complete static library depends on an incomplete static
171              // library, manually link in the object files of the dependent
172              // library as if it were a source set. This avoids problems with
173              // braindead tools such as ar which don't properly link dependent
174              // static libraries.
175              (target_->complete_static_lib() &&
176               (dep->output_type() == Target::STATIC_LIBRARY &&
177                !dep->complete_static_lib()))) {
178     // Source sets have their object files linked into final targets
179     // (shared libraries, executables, loadable modules, and complete static
180     // libraries). Intermediate static libraries and other source sets
181     // just forward the dependency, otherwise the files in the source
182     // set can easily get linked more than once which will cause
183     // multiple definition errors.
184     if (can_link_libs)
185       AddSourceSetFiles(dep, &classified_deps->extra_object_files);
186 
187     // Add the source set itself as a non-linkable dependency on the current
188     // target. This will make sure that anything the source set's stamp file
189     // depends on (like data deps) are also built before the current target
190     // can be complete. Otherwise, these will be skipped since this target
191     // will depend only on the source set's object files.
192     classified_deps->non_linkable_deps.push_back(dep);
193   } else if (target_->complete_static_lib() && dep->IsFinal()) {
194     classified_deps->non_linkable_deps.push_back(dep);
195   } else if (can_link_libs && dep->IsLinkable()) {
196     classified_deps->linkable_deps.push_back(dep);
197   } else if (dep->output_type() == Target::CREATE_BUNDLE &&
198              dep->bundle_data().is_framework()) {
199     classified_deps->framework_deps.push_back(dep);
200   } else {
201     classified_deps->non_linkable_deps.push_back(dep);
202   }
203 }
204 
AddSourceSetFiles(const Target * source_set,UniqueVector<OutputFile> * obj_files) const205 void NinjaBinaryTargetWriter::AddSourceSetFiles(
206     const Target* source_set,
207     UniqueVector<OutputFile>* obj_files) const {
208   std::vector<OutputFile> tool_outputs;  // Prevent allocation in loop.
209 
210   // Compute object files for all sources. Only link the first output from
211   // the tool if there are more than one.
212   for (const auto& source : source_set->sources()) {
213     const char* tool_name = Tool::kToolNone;
214     if (source_set->GetOutputFilesForSource(source, &tool_name, &tool_outputs))
215       obj_files->push_back(tool_outputs[0]);
216   }
217 
218   // Swift files may generate one object file per module or one per source file
219   // depending on how the compiler is invoked (whole module optimization).
220   if (source_set->source_types_used().SwiftSourceUsed()) {
221     const Tool* tool = source_set->toolchain()->GetToolForSourceTypeAsC(
222         SourceFile::SOURCE_SWIFT);
223 
224     std::vector<OutputFile> outputs;
225     SubstitutionWriter::ApplyListToLinkerAsOutputFile(
226         source_set, tool, tool->outputs(), &outputs);
227 
228     for (const OutputFile& output : outputs) {
229       SourceFile output_as_source =
230           output.AsSourceFile(source_set->settings()->build_settings());
231       if (output_as_source.IsObjectType()) {
232         obj_files->push_back(output);
233       }
234     }
235   }
236 
237   // Add MSVC precompiled header object files. GCC .gch files are not object
238   // files so they are omitted.
239   if (source_set->config_values().has_precompiled_headers()) {
240     if (source_set->source_types_used().Get(SourceFile::SOURCE_C)) {
241       const CTool* tool = source_set->toolchain()->GetToolAsC(CTool::kCToolCc);
242       if (tool && tool->precompiled_header_type() == CTool::PCH_MSVC) {
243         GetPCHOutputFiles(source_set, CTool::kCToolCc, &tool_outputs);
244         obj_files->Append(tool_outputs.begin(), tool_outputs.end());
245       }
246     }
247     if (source_set->source_types_used().Get(SourceFile::SOURCE_CPP)) {
248       const CTool* tool = source_set->toolchain()->GetToolAsC(CTool::kCToolCxx);
249       if (tool && tool->precompiled_header_type() == CTool::PCH_MSVC) {
250         GetPCHOutputFiles(source_set, CTool::kCToolCxx, &tool_outputs);
251         obj_files->Append(tool_outputs.begin(), tool_outputs.end());
252       }
253     }
254     if (source_set->source_types_used().Get(SourceFile::SOURCE_M)) {
255       const CTool* tool =
256           source_set->toolchain()->GetToolAsC(CTool::kCToolObjC);
257       if (tool && tool->precompiled_header_type() == CTool::PCH_MSVC) {
258         GetPCHOutputFiles(source_set, CTool::kCToolObjC, &tool_outputs);
259         obj_files->Append(tool_outputs.begin(), tool_outputs.end());
260       }
261     }
262     if (source_set->source_types_used().Get(SourceFile::SOURCE_MM)) {
263       const CTool* tool =
264           source_set->toolchain()->GetToolAsC(CTool::kCToolObjCxx);
265       if (tool && tool->precompiled_header_type() == CTool::PCH_MSVC) {
266         GetPCHOutputFiles(source_set, CTool::kCToolObjCxx, &tool_outputs);
267         obj_files->Append(tool_outputs.begin(), tool_outputs.end());
268       }
269     }
270   }
271 }
272 
WriteCompilerBuildLine(const std::vector<SourceFile> & sources,const std::vector<OutputFile> & extra_deps,const std::vector<OutputFile> & order_only_deps,const char * tool_name,const std::vector<OutputFile> & outputs)273 void NinjaBinaryTargetWriter::WriteCompilerBuildLine(
274     const std::vector<SourceFile>& sources,
275     const std::vector<OutputFile>& extra_deps,
276     const std::vector<OutputFile>& order_only_deps,
277     const char* tool_name,
278     const std::vector<OutputFile>& outputs) {
279   out_ << "build";
280   path_output_.WriteFiles(out_, outputs);
281 
282   out_ << ": " << rule_prefix_ << tool_name;
283   path_output_.WriteFiles(out_, sources);
284 
285   if (!extra_deps.empty()) {
286     out_ << " |";
287     path_output_.WriteFiles(out_, extra_deps);
288   }
289 
290   if (!order_only_deps.empty()) {
291     out_ << " ||";
292     path_output_.WriteFiles(out_, order_only_deps);
293   }
294   out_ << std::endl;
295 }
296 
WriteCustomLinkerFlags(std::ostream & out,const Tool * tool)297 void NinjaBinaryTargetWriter::WriteCustomLinkerFlags(
298     std::ostream& out,
299     const Tool* tool) {
300 
301   if (tool->AsC() || (tool->AsRust() && tool->AsRust()->MayLink())) {
302     // First the ldflags from the target and its config.
303     RecursiveTargetConfigStringsToStream(kRecursiveWriterKeepDuplicates,
304                                          target_, &ConfigValues::ldflags,
305                                          GetFlagOptions(), out);
306   }
307 }
308 
WriteLibrarySearchPath(std::ostream & out,const Tool * tool)309 void NinjaBinaryTargetWriter::WriteLibrarySearchPath(
310     std::ostream& out,
311     const Tool* tool) {
312   // Write library search paths that have been recursively pushed
313   // through the dependency tree.
314   const UniqueVector<SourceDir>& all_lib_dirs = target_->all_lib_dirs();
315   if (!all_lib_dirs.empty()) {
316     // Since we're passing these on the command line to the linker and not
317     // to Ninja, we need to do shell escaping.
318     PathOutput lib_path_output(path_output_.current_dir(),
319                                settings_->build_settings()->root_path_utf8(),
320                                ESCAPE_NINJA_COMMAND);
321     for (size_t i = 0; i < all_lib_dirs.size(); i++) {
322       out << " " << tool->lib_dir_switch();
323       lib_path_output.WriteDir(out, all_lib_dirs[i],
324                                PathOutput::DIR_NO_LAST_SLASH);
325     }
326   }
327 
328   const auto& all_framework_dirs = target_->all_framework_dirs();
329   if (!all_framework_dirs.empty()) {
330     // Since we're passing these on the command line to the linker and not
331     // to Ninja, we need to do shell escaping.
332     PathOutput framework_path_output(
333         path_output_.current_dir(),
334         settings_->build_settings()->root_path_utf8(), ESCAPE_NINJA_COMMAND);
335     for (size_t i = 0; i < all_framework_dirs.size(); i++) {
336       out << " " << tool->framework_dir_switch();
337       framework_path_output.WriteDir(out, all_framework_dirs[i],
338                                      PathOutput::DIR_NO_LAST_SLASH);
339     }
340   }
341 }
342 
WriteLinkerFlags(std::ostream & out,const Tool * tool,const SourceFile * optional_def_file)343 void NinjaBinaryTargetWriter::WriteLinkerFlags(
344     std::ostream& out,
345     const Tool* tool,
346     const SourceFile* optional_def_file) {
347   // First any ldflags
348   WriteCustomLinkerFlags(out, tool);
349   // Then the library search path
350   WriteLibrarySearchPath(out, tool);
351 
352   if (optional_def_file) {
353     out_ << " /DEF:";
354     path_output_.WriteFile(out, *optional_def_file);
355   }
356 }
357 
WriteLibs(std::ostream & out,const Tool * tool)358 void NinjaBinaryTargetWriter::WriteLibs(std::ostream& out, const Tool* tool) {
359   // Libraries that have been recursively pushed through the dependency tree.
360   // Since we're passing these on the command line to the linker and not
361   // to Ninja, we need to do shell escaping.
362   PathOutput lib_path_output(
363       path_output_.current_dir(), settings_->build_settings()->root_path_utf8(),
364       ESCAPE_NINJA_COMMAND);
365   EscapeOptions lib_escape_opts;
366   lib_escape_opts.mode = ESCAPE_NINJA_COMMAND;
367   const UniqueVector<LibFile>& all_libs = target_->all_libs();
368   for (size_t i = 0; i < all_libs.size(); i++) {
369     const LibFile& lib_file = all_libs[i];
370     const std::string& lib_value = lib_file.value();
371     if (lib_file.is_source_file()) {
372       out << " " << tool->linker_arg();
373       lib_path_output.WriteFile(out, lib_file.source_file());
374     } else {
375       out << " " << tool->lib_switch();
376       EscapeStringToStream(out, lib_value, lib_escape_opts);
377     }
378   }
379 }
380 
WriteFrameworks(std::ostream & out,const Tool * tool)381 void NinjaBinaryTargetWriter::WriteFrameworks(std::ostream& out,
382                                               const Tool* tool) {
383   // Frameworks that have been recursively pushed through the dependency tree.
384   FrameworksWriter writer(tool->framework_switch());
385   const auto& all_frameworks = target_->all_frameworks();
386   for (size_t i = 0; i < all_frameworks.size(); i++) {
387     writer(all_frameworks[i], out);
388   }
389 
390   FrameworksWriter weak_writer(tool->weak_framework_switch());
391   const auto& all_weak_frameworks = target_->all_weak_frameworks();
392   for (size_t i = 0; i < all_weak_frameworks.size(); i++) {
393     weak_writer(all_weak_frameworks[i], out);
394   }
395 }
396 
WriteSwiftModules(std::ostream & out,const Tool * tool,const std::vector<OutputFile> & swiftmodules)397 void NinjaBinaryTargetWriter::WriteSwiftModules(
398     std::ostream& out,
399     const Tool* tool,
400     const std::vector<OutputFile>& swiftmodules) {
401   // Since we're passing these on the command line to the linker and not
402   // to Ninja, we need to do shell escaping.
403   PathOutput swiftmodule_path_output(
404       path_output_.current_dir(), settings_->build_settings()->root_path_utf8(),
405       ESCAPE_NINJA_COMMAND);
406 
407   for (const OutputFile& swiftmodule : swiftmodules) {
408     out << " " << tool->swiftmodule_switch();
409     swiftmodule_path_output.WriteFile(out, swiftmodule);
410   }
411 }
412