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.
5 #include "tools/gn/path_output.h"
7 #include "build/build_config.h"
8 #include "tools/gn/filesystem_utils.h"
9 #include "tools/gn/output_file.h"
10 #include "tools/gn/string_utils.h"
12 PathOutput::PathOutput(const SourceDir
& current_dir
, EscapingMode escaping
)
13 : current_dir_(current_dir
) {
14 CHECK(current_dir
.is_source_absolute())
15 << "Currently this only supports writing to output directories inside "
16 "the source root. There needs to be some tweaks to PathOutput to make "
17 "doing this work correctly.";
18 inverse_current_dir_
= InvertDir(current_dir_
);
20 options_
.mode
= escaping
;
23 PathOutput::~PathOutput() {
26 void PathOutput::WriteFile(std::ostream
& out
, const SourceFile
& file
) const {
27 WritePathStr(out
, file
.value());
30 void PathOutput::WriteDir(std::ostream
& out
,
32 DirSlashEnding slash_ending
) const {
33 if (dir
.value() == "/") {
34 // Writing system root is always a slash (this will normally only come up
36 if (slash_ending
== DIR_NO_LAST_SLASH
)
40 } else if (dir
.value() == "//") {
41 // Writing out the source root.
42 if (slash_ending
== DIR_NO_LAST_SLASH
) {
43 // The inverse_current_dir_ will contain a [back]slash at the end, so we
44 // can't just write it out.
45 if (inverse_current_dir_
.empty()) {
48 out
.write(inverse_current_dir_
.c_str(),
49 inverse_current_dir_
.size() - 1);
52 if (inverse_current_dir_
.empty())
55 out
<< inverse_current_dir_
;
57 } else if (dir
== current_dir_
) {
58 // Writing the same directory. This needs special handling here since
59 // we need to output something else other than the input.
60 if (slash_ending
== DIR_INCLUDE_LAST_SLASH
)
64 } else if (slash_ending
== DIR_INCLUDE_LAST_SLASH
) {
65 WritePathStr(out
, dir
.value());
67 // DIR_NO_LAST_SLASH mode, just trim the last char.
68 WritePathStr(out
, base::StringPiece(dir
.value().data(),
69 dir
.value().size() - 1));
73 void PathOutput::WriteFile(std::ostream
& out
, const OutputFile
& file
) const {
74 // Here we assume that the path is already preprocessed.
75 EscapeStringToStream(out
, file
.value(), options_
);
78 void PathOutput::WriteDir(std::ostream
& out
,
79 const OutputFile
& file
,
80 DirSlashEnding slash_ending
) const {
81 DCHECK(file
.value().empty() ||
82 file
.value()[file
.value().size() - 1] == '/');
84 switch (slash_ending
) {
85 case DIR_INCLUDE_LAST_SLASH
:
86 EscapeStringToStream(out
, file
.value(), options_
);
88 case DIR_NO_LAST_SLASH
:
89 if (!file
.value().empty() &&
90 file
.value()[file
.value().size() - 1] == '/') {
91 // Trim trailing slash.
94 base::StringPiece(file
.value().data(), file
.value().size() - 1),
97 // Doesn't end with a slash, write the whole thing.
98 EscapeStringToStream(out
, file
.value(), options_
);
104 void PathOutput::WriteFile(std::ostream
& out
,
105 const base::FilePath
& file
) const {
106 // Assume native file paths are always absolute.
107 EscapeStringToStream(out
, FilePathToUTF8(file
), options_
);
110 void PathOutput::WriteSourceRelativeString(
112 const base::StringPiece
& str
) const {
113 if (options_
.mode
== ESCAPE_NINJA_COMMAND
) {
114 // Shell escaping needs an intermediate string since it may end up
115 // quoting the whole thing.
116 std::string intermediate
;
117 intermediate
.reserve(inverse_current_dir_
.size() + str
.size());
118 intermediate
.assign(inverse_current_dir_
.c_str(),
119 inverse_current_dir_
.size());
120 intermediate
.append(str
.data(), str
.size());
122 EscapeStringToStream(out
,
123 base::StringPiece(intermediate
.c_str(), intermediate
.size()),
126 // Ninja (and none) escaping can avoid the intermediate string and
127 // reprocessing of the inverse_current_dir_.
128 out
<< inverse_current_dir_
;
129 EscapeStringToStream(out
, str
, options_
);
133 void PathOutput::WritePathStr(std::ostream
& out
,
134 const base::StringPiece
& str
) const {
135 DCHECK(str
.size() > 0 && str
[0] == '/');
137 if (str
.substr(0, current_dir_
.value().size()) ==
138 base::StringPiece(current_dir_
.value())) {
139 // The current dir is a prefix of the output file, so we can strip the
140 // prefix and write out the result.
141 EscapeStringToStream(out
, str
.substr(current_dir_
.value().size()),
143 } else if (str
.size() >= 2 && str
[1] == '/') {
144 WriteSourceRelativeString(out
, str
.substr(2));
146 // Input begins with one slash, don't write the current directory since
147 // it's system-absolute.
149 // On Windows, trim the leading slash, since the input for absolute
150 // paths will look like "/C:/foo/bar.txt".
151 EscapeStringToStream(out
, str
.substr(1), options_
);
153 EscapeStringToStream(out
, str
, options_
);