OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "tools/gn/build_settings.h" | 5 #include "tools/gn/build_settings.h" |
6 #include "tools/gn/functions.h" | 6 #include "tools/gn/functions.h" |
7 #include "tools/gn/ninja_helper.h" | |
8 #include "tools/gn/parse_tree.h" | 7 #include "tools/gn/parse_tree.h" |
9 #include "tools/gn/settings.h" | 8 #include "tools/gn/settings.h" |
10 #include "tools/gn/substitution_writer.h" | 9 #include "tools/gn/substitution_writer.h" |
11 #include "tools/gn/target.h" | 10 #include "tools/gn/target.h" |
12 #include "tools/gn/value.h" | 11 #include "tools/gn/value.h" |
13 | 12 |
14 namespace functions { | 13 namespace functions { |
15 | 14 |
16 namespace { | |
17 | |
18 void GetOutputsForTarget(const Settings* settings, | |
19 const Target* target, | |
20 std::vector<SourceFile>* ret) { | |
21 switch (target->output_type()) { | |
22 case Target::ACTION: { | |
23 // Actions just use the output list with no substitution. | |
24 std::vector<SourceFile> sources; | |
25 sources.push_back(SourceFile()); | |
26 SubstitutionWriter::GetListAsSourceFiles( | |
27 settings, target->action_values().outputs(), ret); | |
28 break; | |
29 } | |
30 | |
31 case Target::COPY_FILES: | |
32 case Target::ACTION_FOREACH: | |
33 SubstitutionWriter::ApplyListToSources( | |
34 settings, target->action_values().outputs(), target->sources(), ret); | |
35 break; | |
36 | |
37 case Target::EXECUTABLE: | |
38 case Target::SHARED_LIBRARY: | |
39 case Target::STATIC_LIBRARY: | |
40 // Return the resulting binary file. Currently, fall through to the | |
41 // Ninja helper below which will compute the main output name. | |
42 // | |
43 // TODO(brettw) some targets have secondary files which should go into | |
44 // the list after the main (like shared libraries on Windows have an | |
45 // import library). | |
46 case Target::GROUP: | |
47 case Target::SOURCE_SET: { | |
48 // These return the stamp file, which is computed by the NinjaHelper. | |
49 NinjaHelper helper(settings->build_settings()); | |
50 OutputFile output_file = helper.GetTargetOutputFile(target); | |
51 | |
52 // The output file is relative to the build dir. | |
53 ret->push_back(SourceFile( | |
54 settings->build_settings()->build_dir().value() + | |
55 output_file.value())); | |
56 break; | |
57 } | |
58 | |
59 default: | |
60 NOTREACHED(); | |
61 } | |
62 } | |
63 | |
64 } // namespace | |
65 | |
66 const char kGetTargetOutputs[] = "get_target_outputs"; | 15 const char kGetTargetOutputs[] = "get_target_outputs"; |
67 const char kGetTargetOutputs_HelpShort[] = | 16 const char kGetTargetOutputs_HelpShort[] = |
68 "get_target_outputs: [file list] Get the list of outputs from a target."; | 17 "get_target_outputs: [file list] Get the list of outputs from a target."; |
69 const char kGetTargetOutputs_Help[] = | 18 const char kGetTargetOutputs_Help[] = |
70 "get_target_outputs: [file list] Get the list of outputs from a target.\n" | 19 "get_target_outputs: [file list] Get the list of outputs from a target.\n" |
71 "\n" | 20 "\n" |
72 " get_target_outputs(target_label)\n" | 21 " get_target_outputs(target_label)\n" |
73 "\n" | 22 "\n" |
74 " Returns a list of output files for the named target. The named target\n" | 23 " Returns a list of output files for the named target. The named target\n" |
75 " must have been previously defined in the current file before this\n" | 24 " must have been previously defined in the current file before this\n" |
76 " function is called (it can't reference targets in other files because\n" | 25 " function is called (it can't reference targets in other files because\n" |
77 " there isn't a defined execution order, and it obviously can't\n" | 26 " there isn't a defined execution order, and it obviously can't\n" |
78 " reference targets that are defined after the function call).\n" | 27 " reference targets that are defined after the function call).\n" |
79 "\n" | 28 "\n" |
| 29 " Only copy and action targets are supported. The outputs from binary\n" |
| 30 " targets will depend on the toolchain definition which won't\n" |
| 31 " necessarily have been loaded by the time a given line of code has run,\n" |
| 32 " and source sets and groups have no useful output file.\n" |
| 33 "\n" |
80 "Return value\n" | 34 "Return value\n" |
81 "\n" | 35 "\n" |
82 " The names in the resulting list will be absolute file paths (normally\n" | 36 " The names in the resulting list will be absolute file paths (normally\n" |
83 " like \"//out/Debug/bar.exe\", depending on the build directory).\n" | 37 " like \"//out/Debug/bar.exe\", depending on the build directory).\n" |
84 "\n" | 38 "\n" |
85 " action targets: this will just return the files specified in the\n" | 39 " action targets: this will just return the files specified in the\n" |
86 " \"outputs\" variable of the target.\n" | 40 " \"outputs\" variable of the target.\n" |
87 "\n" | 41 "\n" |
88 " action_foreach targets: this will return the result of applying\n" | 42 " action_foreach targets: this will return the result of applying\n" |
89 " the output template to the sources (see \"gn help source_expansion\").\n" | 43 " the output template to the sources (see \"gn help source_expansion\").\n" |
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
155 } | 109 } |
156 | 110 |
157 if (!target) { | 111 if (!target) { |
158 *err = Err(function, "Target not found in this context.", | 112 *err = Err(function, "Target not found in this context.", |
159 label.GetUserVisibleName(false) + | 113 label.GetUserVisibleName(false) + |
160 "\nwas not found. get_target_outputs() can only be used for targets\n" | 114 "\nwas not found. get_target_outputs() can only be used for targets\n" |
161 "previously defined in the current file."); | 115 "previously defined in the current file."); |
162 return Value(); | 116 return Value(); |
163 } | 117 } |
164 | 118 |
| 119 // Compute the output list. |
165 std::vector<SourceFile> files; | 120 std::vector<SourceFile> files; |
166 GetOutputsForTarget(scope->settings(), target, &files); | 121 if (target->output_type() == Target::ACTION) { |
| 122 // Actions just use the output list with no substitution. |
| 123 SubstitutionWriter::GetListAsSourceFiles( |
| 124 target->action_values().outputs(), &files); |
| 125 } else if (target->output_type() == Target::COPY_FILES || |
| 126 target->output_type() == Target::ACTION_FOREACH) { |
| 127 // Copy and foreach appllies the outputs to the sources. |
| 128 SubstitutionWriter::ApplyListToSources( |
| 129 target->settings(), target->action_values().outputs(), |
| 130 target->sources(), &files); |
| 131 } else { |
| 132 // Other types of targets are not supported. |
| 133 *err = Err(args[0], "Target is not an action, action_foreach, or copy.", |
| 134 "Only these target types are supported by get_target_outputs."); |
| 135 return Value(); |
| 136 } |
167 | 137 |
| 138 // Convert to Values. |
168 Value ret(function, Value::LIST); | 139 Value ret(function, Value::LIST); |
169 ret.list_value().reserve(files.size()); | 140 ret.list_value().reserve(files.size()); |
170 for (size_t i = 0; i < files.size(); i++) | 141 for (size_t i = 0; i < files.size(); i++) |
171 ret.list_value().push_back(Value(function, files[i].value())); | 142 ret.list_value().push_back(Value(function, files[i].value())); |
172 | 143 |
173 return ret; | 144 return ret; |
174 } | 145 } |
175 | 146 |
176 } // namespace functions | 147 } // namespace functions |
OLD | NEW |