• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "tools/gn/gyp_script_target_writer.h"
6 
7 #include "tools/gn/builder_record.h"
8 #include "tools/gn/err.h"
9 #include "tools/gn/file_template.h"
10 #include "tools/gn/filesystem_utils.h"
11 #include "tools/gn/settings.h"
12 #include "tools/gn/target.h"
13 
14 // Write script targets as GYP actions that just invoke Ninja. This allows us
15 // to not have to worry about duplicating the precise GN script execution
16 // semantices in GYP for each platform (GYP varies a bit).
17 
GypScriptTargetWriter(const TargetGroup & group,const Toolchain * toolchain,const SourceDir & gyp_dir,std::ostream & out)18 GypScriptTargetWriter::GypScriptTargetWriter(const TargetGroup& group,
19                                              const Toolchain* toolchain,
20                                              const SourceDir& gyp_dir,
21                                              std::ostream& out)
22     : GypTargetWriter(group.debug->item()->AsTarget(), toolchain,
23                       gyp_dir, out) {
24 }
25 
~GypScriptTargetWriter()26 GypScriptTargetWriter::~GypScriptTargetWriter() {
27 }
28 
Run()29 void GypScriptTargetWriter::Run() {
30   int indent = 4;
31   std::string name = helper_.GetNameForTarget(target_);
32 
33   // Put the ninja build for this script target in this directory.
34   SourceDir ninja_dir(GetTargetOutputDir(target_).value() + name + "_ninja/");
35 
36   Indent(indent) << "{\n";
37 
38   Indent(indent + kExtraIndent) << "'target_name': '" << name << "',\n";
39   Indent(indent + kExtraIndent) << "'type': 'none',\n";
40   Indent(indent + kExtraIndent) << "'actions': [{\n";
41 
42   Indent(indent + kExtraIndent * 2) << "'action_name': '" << name
43                                     << " action',\n";
44 
45   Indent(indent + kExtraIndent * 2) << "'action': [\n";
46   Indent(indent + kExtraIndent * 3) << "'ninja',\n";
47   Indent(indent + kExtraIndent * 3) << "'-C', '";
48   path_output_.WriteDir(out_, ninja_dir, PathOutput::DIR_NO_LAST_SLASH);
49   out_ << "',\n";
50   Indent(indent + kExtraIndent * 3) << "'" << name << "',\n";
51   Indent(indent + kExtraIndent * 2) << "],\n";
52 
53   WriteActionInputs(indent + kExtraIndent * 2);
54   WriteActionOutputs(indent + kExtraIndent * 2);
55 
56   Indent(indent + kExtraIndent) << "}],\n";
57   Indent(indent) << "},\n";
58 }
59 
WriteActionInputs(int indent)60 void GypScriptTargetWriter::WriteActionInputs(int indent) {
61   Indent(indent) << "'inputs': [\n";
62 
63   // Write everything that should be considered an input for dependency
64   // purposes, which is all sources as well as the prereqs.
65   const Target::FileList& sources = target_->sources();
66   for (size_t i = 0; i < sources.size(); i++) {
67     Indent(indent + kExtraIndent) << "'";
68     path_output_.WriteFile(out_, sources[i]);
69     out_ << "',\n";
70   }
71 
72   const Target::FileList& prereqs = target_->source_prereqs();
73   for (size_t i = 0; i < prereqs.size(); i++) {
74     Indent(indent + kExtraIndent) << "'";
75     path_output_.WriteFile(out_, prereqs[i]);
76     out_ << "',\n";
77   }
78 
79   Indent(indent) << "],\n";
80 }
81 
WriteActionOutputs(int indent)82 void GypScriptTargetWriter::WriteActionOutputs(int indent) {
83   Indent(indent) << "'outputs': [\n";
84 
85   const Target::FileList& sources = target_->sources();
86   if (sources.empty()) {
87     // Just write outputs directly if there are no sources.
88     const Target::FileList& output = target_->script_values().outputs();
89     for (size_t output_i = 0; output_i < output.size(); output_i++) {
90       Indent(indent + kExtraIndent) << "'";
91       path_output_.WriteFile(out_, output[output_i]);
92       out_ << "',\n";
93     }
94   } else {
95     // There are sources, the outputs should be a template to apply to each.
96     FileTemplate output_template = FileTemplate::GetForTargetOutputs(target_);
97 
98     std::vector<std::string> output;
99     for (size_t source_i = 0; source_i < sources.size(); source_i++) {
100       output_template.ApplyString(sources[source_i].value(), &output);
101       for (size_t output_i = 0; output_i < output.size(); output_i++) {
102         Indent(indent + kExtraIndent) << "'";
103         path_output_.WriteFile(out_, SourceFile(output[output_i]));
104         out_ << "',\n";
105       }
106     }
107   }
108 
109   Indent(indent) << "],\n";
110 }
111