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

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

Issue 429423002: Refactor GN source expansions. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 6 years, 4 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 | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. 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 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/ninja_copy_target_writer.h" 5 #include "tools/gn/ninja_copy_target_writer.h"
6 6
7 #include "base/strings/string_util.h" 7 #include "base/strings/string_util.h"
8 #include "tools/gn/file_template.h"
9 #include "tools/gn/string_utils.h" 8 #include "tools/gn/string_utils.h"
9 #include "tools/gn/substitution_list.h"
10 #include "tools/gn/substitution_writer.h"
10 11
11 NinjaCopyTargetWriter::NinjaCopyTargetWriter(const Target* target, 12 NinjaCopyTargetWriter::NinjaCopyTargetWriter(const Target* target,
12 const Toolchain* toolchain, 13 const Toolchain* toolchain,
13 std::ostream& out) 14 std::ostream& out)
14 : NinjaTargetWriter(target, toolchain, out) { 15 : NinjaTargetWriter(target, toolchain, out) {
15 } 16 }
16 17
17 NinjaCopyTargetWriter::~NinjaCopyTargetWriter() { 18 NinjaCopyTargetWriter::~NinjaCopyTargetWriter() {
18 } 19 }
19 20
20 void NinjaCopyTargetWriter::Run() { 21 void NinjaCopyTargetWriter::Run() {
21 CHECK(target_->action_values().outputs().size() == 1); 22 CHECK(target_->action_values().outputs().list().size() == 1);
22 FileTemplate output_template = FileTemplate::GetForTargetOutputs(target_); 23 const SubstitutionList& output_subst_list =
24 target_->action_values().outputs();
25 CHECK_EQ(1u, output_subst_list.list().size())
26 << "Should have one entry exactly.";
27 const SubstitutionPattern& output_subst = output_subst_list.list()[0];
23 28
24 std::vector<OutputFile> output_files; 29 std::vector<OutputFile> output_files;
25 30
26 std::string rule_prefix = helper_.GetRulePrefix(target_->settings()); 31 std::string rule_prefix = helper_.GetRulePrefix(target_->settings());
27 32
28 // Note that we don't write implicit deps for copy steps. "copy" only 33 // Note that we don't write implicit deps for copy steps. "copy" only
29 // depends on the output files themselves, rather than having includes 34 // depends on the output files themselves, rather than having includes
30 // (the possibility of generated #includes is the main reason for implicit 35 // (the possibility of generated #includes is the main reason for implicit
31 // dependencies). 36 // dependencies).
32 // 37 //
33 // It would seem that specifying implicit dependencies on the deps of the 38 // It would seem that specifying implicit dependencies on the deps of the
34 // copy command would still be harmeless. But Chrome implements copy tools 39 // copy command would still be harmeless. But Chrome implements copy tools
35 // as hard links (much faster) which don't change the timestamp. If the 40 // as hard links (much faster) which don't change the timestamp. If the
36 // ninja rule looks like this: 41 // ninja rule looks like this:
37 // output: copy input | foo.stamp 42 // output: copy input | foo.stamp
38 // The copy will not make a new timestamp on the output file, but the 43 // The copy will not make a new timestamp on the output file, but the
39 // foo.stamp file generated from a previous step will have a new timestamp. 44 // foo.stamp file generated from a previous step will have a new timestamp.
40 // The copy rule will therefore look out-of-date to Ninja and the rule will 45 // The copy rule will therefore look out-of-date to Ninja and the rule will
41 // get rebuilt. 46 // get rebuilt.
42 // 47 //
43 // If this copy is copying a generated file, not listing the implicit 48 // If this copy is copying a generated file, not listing the implicit
44 // dependency will be fine as long as the input to the copy is properly 49 // dependency will be fine as long as the input to the copy is properly
45 // listed as the output from the step that generated it. 50 // listed as the output from the step that generated it.
46 // 51 //
47 // Moreover, doing this assumes that the copy step is always a simple 52 // Moreover, doing this assumes that the copy step is always a simple
48 // locally run command, so there is no need for a toolchain dependency. 53 // locally run command, so there is no need for a toolchain dependency.
49 for (size_t i = 0; i < target_->sources().size(); i++) { 54 for (size_t i = 0; i < target_->sources().size(); i++) {
50 const SourceFile& input_file = target_->sources()[i]; 55 const SourceFile& input_file = target_->sources()[i];
51 56
52 // Make the output file from the template. 57 OutputFile output_file =
53 std::vector<std::string> template_result; 58 SubstitutionWriter::ApplyPatternToSourceAsOutputFile(
54 output_template.Apply(input_file, &template_result); 59 target_->settings(), output_subst, input_file);
55 CHECK(template_result.size() == 1);
56
57 // All output files should be in the build directory, so we can rebase
58 // them just by trimming the prefix.
59 OutputFile output_file(
60 RemovePrefix(template_result[0],
61 settings_->build_settings()->build_dir().value()));
62 output_files.push_back(output_file); 60 output_files.push_back(output_file);
63 61
64 out_ << "build "; 62 out_ << "build ";
65 path_output_.WriteFile(out_, output_file); 63 path_output_.WriteFile(out_, output_file);
66 out_ << ": " << rule_prefix << "copy "; 64 out_ << ": " << rule_prefix << "copy ";
67 path_output_.WriteFile(out_, input_file); 65 path_output_.WriteFile(out_, input_file);
68 out_ << std::endl; 66 out_ << std::endl;
69 } 67 }
70 68
71 // Write out the rule for the target to copy all of them. 69 // Write out the rule for the target to copy all of them.
72 out_ << std::endl << "build "; 70 out_ << std::endl << "build ";
73 path_output_.WriteFile(out_, helper_.GetTargetOutputFile(target_)); 71 path_output_.WriteFile(out_, helper_.GetTargetOutputFile(target_));
74 out_ << ": " << rule_prefix << "stamp"; 72 out_ << ": " << rule_prefix << "stamp";
75 for (size_t i = 0; i < output_files.size(); i++) { 73 for (size_t i = 0; i < output_files.size(); i++) {
76 out_ << " "; 74 out_ << " ";
77 path_output_.WriteFile(out_, output_files[i]); 75 path_output_.WriteFile(out_, output_files[i]);
78 } 76 }
79 out_ << std::endl; 77 out_ << std::endl;
80 } 78 }
OLDNEW
« no previous file with comments | « tools/gn/ninja_action_target_writer_unittest.cc ('k') | tools/gn/ninja_copy_target_writer_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698