OLD | NEW |
---|---|
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/source_dir.h" | 5 #include "tools/gn/source_dir.h" |
6 | 6 |
7 #include "base/logging.h" | 7 #include "base/logging.h" |
8 #include "tools/gn/filesystem_utils.h" | 8 #include "tools/gn/filesystem_utils.h" |
9 #include "tools/gn/source_file.h" | 9 #include "tools/gn/source_file.h" |
10 | 10 |
(...skipping 23 matching lines...) Expand all Loading... | |
34 if (!EndsWithSlash(value_)) | 34 if (!EndsWithSlash(value_)) |
35 value_.push_back('/'); | 35 value_.push_back('/'); |
36 AssertValueSourceDirString(value_); | 36 AssertValueSourceDirString(value_); |
37 } | 37 } |
38 | 38 |
39 SourceDir::~SourceDir() { | 39 SourceDir::~SourceDir() { |
40 } | 40 } |
41 | 41 |
42 SourceFile SourceDir::ResolveRelativeFile( | 42 SourceFile SourceDir::ResolveRelativeFile( |
43 const base::StringPiece& p, | 43 const base::StringPiece& p, |
44 const base::StringPiece& source_root) const { | 44 const base::FilePath& source_root) const { |
45 SourceFile ret; | 45 SourceFile ret; |
46 | 46 |
47 // It's an error to resolve an empty string or one that is a directory | 47 // It's an error to resolve an empty string or one that is a directory |
48 // (indicated by a trailing slash) because this is the function that expects | 48 // (indicated by a trailing slash) because this is the function that expects |
49 // to return a file. | 49 // to return a file. |
50 if (p.empty() || (p.size() > 0 && p[p.size() - 1] == '/')) | 50 if (p.empty() || (p.size() > 0 && p[p.size() - 1] == '/')) |
51 return SourceFile(); | 51 return SourceFile(); |
52 if (p.size() >= 2 && p[0] == '/' && p[1] == '/') { | 52 if (p.size() >= 2 && p[0] == '/' && p[1] == '/') { |
53 // Source-relative. | 53 // Source-relative. |
54 ret.value_.assign(p.data(), p.size()); | 54 ret.value_.assign(p.data(), p.size()); |
55 NormalizePath(&ret.value_); | 55 NormalizePath(&ret.value_); |
56 return ret; | 56 return ret; |
57 } else if (IsPathAbsolute(p)) { | 57 } else if (IsPathAbsolute(p)) { |
58 if (source_root.empty() || | 58 if (source_root.empty() || |
59 !MakeAbsolutePathRelativeIfPossible(source_root, p, &ret.value_)) { | 59 !MakeAbsolutePathRelativeIfPossible(source_root.value(), p, |
60 &ret.value_)) { | |
60 #if defined(OS_WIN) | 61 #if defined(OS_WIN) |
61 // On Windows we'll accept "C:\foo" as an absolute path, which we want | 62 // On Windows we'll accept "C:\foo" as an absolute path, which we want |
62 // to convert to "/C:..." here. | 63 // to convert to "/C:..." here. |
63 if (p[0] != '/') | 64 if (p[0] != '/') |
64 ret.value_ = "/"; | 65 ret.value_ = "/"; |
65 #endif | 66 #endif |
66 ret.value_.append(p.data(), p.size()); | 67 ret.value_.append(p.data(), p.size()); |
67 } | 68 } |
68 NormalizePath(&ret.value_); | 69 NormalizePath(&ret.value_); |
69 return ret; | 70 return ret; |
70 } | 71 } |
71 | 72 |
73 if (!source_root.empty()) { | |
brettw
2014/10/07 23:55:06
Are these blocks necessary? Are you trying to hand
zeuthen
2014/10/08 15:21:06
I tried commenting this out and without it, the bu
| |
74 std::string absolute = Resolve(source_root).Append(p.as_string()).value(); | |
75 NormalizePath(&absolute); | |
76 if (!MakeAbsolutePathRelativeIfPossible(source_root.value(), absolute, | |
77 &ret.value_)) | |
78 ret.value_ = absolute; | |
79 return ret; | |
80 } | |
81 | |
82 // With no source_root_, there's nothing we can do about | |
83 // e.g. p=../../../path/to/file and value_=//source and we'll | |
84 // errornously return //file. | |
72 ret.value_.reserve(value_.size() + p.size()); | 85 ret.value_.reserve(value_.size() + p.size()); |
73 ret.value_.assign(value_); | 86 ret.value_.assign(value_); |
74 ret.value_.append(p.data(), p.size()); | 87 ret.value_.append(p.data(), p.size()); |
75 | |
76 NormalizePath(&ret.value_); | 88 NormalizePath(&ret.value_); |
77 return ret; | 89 return ret; |
78 } | 90 } |
79 | 91 |
80 SourceDir SourceDir::ResolveRelativeDir( | 92 SourceDir SourceDir::ResolveRelativeDir( |
81 const base::StringPiece& p, | 93 const base::StringPiece& p, |
82 const base::StringPiece& source_root) const { | 94 const base::FilePath& source_root) const { |
83 SourceDir ret; | 95 SourceDir ret; |
84 | 96 |
85 if (p.empty()) | 97 if (p.empty()) |
86 return ret; | 98 return ret; |
87 if (p.size() >= 2 && p[0] == '/' && p[1] == '/') { | 99 if (p.size() >= 2 && p[0] == '/' && p[1] == '/') { |
88 // Source-relative. | 100 // Source-relative. |
89 ret.value_.assign(p.data(), p.size()); | 101 ret.value_.assign(p.data(), p.size()); |
90 if (!EndsWithSlash(ret.value_)) | 102 if (!EndsWithSlash(ret.value_)) |
91 ret.value_.push_back('/'); | 103 ret.value_.push_back('/'); |
92 NormalizePath(&ret.value_); | 104 NormalizePath(&ret.value_); |
93 return ret; | 105 return ret; |
94 } else if (IsPathAbsolute(p)) { | 106 } else if (IsPathAbsolute(p)) { |
95 if (source_root.empty() || | 107 if (source_root.empty() || |
96 !MakeAbsolutePathRelativeIfPossible(source_root, p, &ret.value_)) { | 108 !MakeAbsolutePathRelativeIfPossible(source_root.value(), p, |
109 &ret.value_)) { | |
97 #if defined(OS_WIN) | 110 #if defined(OS_WIN) |
98 if (p[0] != '/') // See the file case for why we do this check. | 111 if (p[0] != '/') // See the file case for why we do this check. |
99 ret.value_ = "/"; | 112 ret.value_ = "/"; |
100 #endif | 113 #endif |
101 ret.value_.append(p.data(), p.size()); | 114 ret.value_.append(p.data(), p.size()); |
102 } | 115 } |
103 NormalizePath(&ret.value_); | 116 NormalizePath(&ret.value_); |
104 if (!EndsWithSlash(ret.value_)) | 117 if (!EndsWithSlash(ret.value_)) |
105 ret.value_.push_back('/'); | 118 ret.value_.push_back('/'); |
106 return ret; | 119 return ret; |
107 } | 120 } |
108 | 121 |
122 if (!source_root.empty()) { | |
123 std::string absolute = Resolve(source_root).Append(p.as_string()).value(); | |
124 NormalizePath(&absolute); | |
125 if (!MakeAbsolutePathRelativeIfPossible(source_root.value(), absolute, | |
126 &ret.value_)) | |
127 ret.value_ = absolute; | |
128 if (!EndsWithSlash(ret.value_)) | |
129 ret.value_.push_back('/'); | |
130 return ret; | |
131 } | |
132 | |
109 ret.value_.reserve(value_.size() + p.size()); | 133 ret.value_.reserve(value_.size() + p.size()); |
110 ret.value_.assign(value_); | 134 ret.value_.assign(value_); |
111 ret.value_.append(p.data(), p.size()); | 135 ret.value_.append(p.data(), p.size()); |
112 | 136 |
113 NormalizePath(&ret.value_); | 137 NormalizePath(&ret.value_); |
114 if (!EndsWithSlash(ret.value_)) | 138 if (!EndsWithSlash(ret.value_)) |
115 ret.value_.push_back('/'); | 139 ret.value_.push_back('/'); |
116 AssertValueSourceDirString(ret.value_); | 140 AssertValueSourceDirString(ret.value_); |
117 | 141 |
118 return ret; | 142 return ret; |
(...skipping 17 matching lines...) Expand all Loading... | |
136 // String the double-leading slash for source-relative paths. | 160 // String the double-leading slash for source-relative paths. |
137 converted.assign(&value_[2], value_.size() - 2); | 161 converted.assign(&value_[2], value_.size() - 2); |
138 return source_root.Append(UTF8ToFilePath(converted)) | 162 return source_root.Append(UTF8ToFilePath(converted)) |
139 .NormalizePathSeparatorsTo('/'); | 163 .NormalizePathSeparatorsTo('/'); |
140 } | 164 } |
141 | 165 |
142 void SourceDir::SwapValue(std::string* v) { | 166 void SourceDir::SwapValue(std::string* v) { |
143 value_.swap(*v); | 167 value_.swap(*v); |
144 AssertValueSourceDirString(value_); | 168 AssertValueSourceDirString(value_); |
145 } | 169 } |
OLD | NEW |