| 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 26 matching lines...) Expand all Loading... |
| 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::StringPiece& source_root) const { |
| 45 SourceFile ret; | 45 SourceFile ret; |
| 46 | 46 |
| 47 DCHECK(source_root.empty() || !source_root.ends_with("/")); |
| 48 |
| 47 // It's an error to resolve an empty string or one that is a directory | 49 // 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 | 50 // (indicated by a trailing slash) because this is the function that expects |
| 49 // to return a file. | 51 // to return a file. |
| 50 if (p.empty() || (p.size() > 0 && p[p.size() - 1] == '/')) | 52 if (p.empty() || (p.size() > 0 && p[p.size() - 1] == '/')) |
| 51 return SourceFile(); | 53 return SourceFile(); |
| 52 if (p.size() >= 2 && p[0] == '/' && p[1] == '/') { | 54 if (p.size() >= 2 && p[0] == '/' && p[1] == '/') { |
| 53 // Source-relative. | 55 // Source-relative. |
| 54 ret.value_.assign(p.data(), p.size()); | 56 ret.value_.assign(p.data(), p.size()); |
| 55 NormalizePath(&ret.value_); | 57 NormalizePath(&ret.value_); |
| 56 return ret; | 58 return ret; |
| 57 } else if (IsPathAbsolute(p)) { | 59 } else if (IsPathAbsolute(p)) { |
| 58 if (source_root.empty() || | 60 if (source_root.empty() || |
| 59 !MakeAbsolutePathRelativeIfPossible(source_root, p, &ret.value_)) { | 61 !MakeAbsolutePathRelativeIfPossible(source_root, p, &ret.value_)) { |
| 60 #if defined(OS_WIN) | 62 #if defined(OS_WIN) |
| 61 // On Windows we'll accept "C:\foo" as an absolute path, which we want | 63 // On Windows we'll accept "C:\foo" as an absolute path, which we want |
| 62 // to convert to "/C:..." here. | 64 // to convert to "/C:..." here. |
| 63 if (p[0] != '/') | 65 if (p[0] != '/') |
| 64 ret.value_ = "/"; | 66 ret.value_ = "/"; |
| 65 #endif | 67 #endif |
| 66 ret.value_.append(p.data(), p.size()); | 68 ret.value_.append(p.data(), p.size()); |
| 67 } | 69 } |
| 68 NormalizePath(&ret.value_); | 70 NormalizePath(&ret.value_); |
| 69 return ret; | 71 return ret; |
| 70 } | 72 } |
| 71 | 73 |
| 74 if (!source_root.empty()) { |
| 75 std::string absolute = |
| 76 Resolve(base::FilePath(source_root.data())) |
| 77 .Append(p.as_string()).value(); |
| 78 NormalizePath(&absolute); |
| 79 if (!MakeAbsolutePathRelativeIfPossible(source_root, absolute, |
| 80 &ret.value_)) |
| 81 ret.value_ = absolute; |
| 82 return ret; |
| 83 } |
| 84 |
| 85 // With no source_root_, there's nothing we can do about |
| 86 // e.g. p=../../../path/to/file and value_=//source and we'll |
| 87 // errornously return //file. |
| 72 ret.value_.reserve(value_.size() + p.size()); | 88 ret.value_.reserve(value_.size() + p.size()); |
| 73 ret.value_.assign(value_); | 89 ret.value_.assign(value_); |
| 74 ret.value_.append(p.data(), p.size()); | 90 ret.value_.append(p.data(), p.size()); |
| 75 | 91 |
| 76 NormalizePath(&ret.value_); | 92 NormalizePath(&ret.value_); |
| 77 return ret; | 93 return ret; |
| 78 } | 94 } |
| 79 | 95 |
| 80 SourceDir SourceDir::ResolveRelativeDir( | 96 SourceDir SourceDir::ResolveRelativeDir( |
| 81 const base::StringPiece& p, | 97 const base::StringPiece& p, |
| 82 const base::StringPiece& source_root) const { | 98 const base::StringPiece& source_root) const { |
| 83 SourceDir ret; | 99 SourceDir ret; |
| 84 | 100 |
| 101 DCHECK(source_root.empty() || !source_root.ends_with("/")); |
| 102 |
| 85 if (p.empty()) | 103 if (p.empty()) |
| 86 return ret; | 104 return ret; |
| 87 if (p.size() >= 2 && p[0] == '/' && p[1] == '/') { | 105 if (p.size() >= 2 && p[0] == '/' && p[1] == '/') { |
| 88 // Source-relative. | 106 // Source-relative. |
| 89 ret.value_.assign(p.data(), p.size()); | 107 ret.value_.assign(p.data(), p.size()); |
| 90 if (!EndsWithSlash(ret.value_)) | 108 if (!EndsWithSlash(ret.value_)) |
| 91 ret.value_.push_back('/'); | 109 ret.value_.push_back('/'); |
| 92 NormalizePath(&ret.value_); | 110 NormalizePath(&ret.value_); |
| 93 return ret; | 111 return ret; |
| 94 } else if (IsPathAbsolute(p)) { | 112 } else if (IsPathAbsolute(p)) { |
| 95 if (source_root.empty() || | 113 if (source_root.empty() || |
| 96 !MakeAbsolutePathRelativeIfPossible(source_root, p, &ret.value_)) { | 114 !MakeAbsolutePathRelativeIfPossible(source_root, p, &ret.value_)) { |
| 97 #if defined(OS_WIN) | 115 #if defined(OS_WIN) |
| 98 if (p[0] != '/') // See the file case for why we do this check. | 116 if (p[0] != '/') // See the file case for why we do this check. |
| 99 ret.value_ = "/"; | 117 ret.value_ = "/"; |
| 100 #endif | 118 #endif |
| 101 ret.value_.append(p.data(), p.size()); | 119 ret.value_.append(p.data(), p.size()); |
| 102 } | 120 } |
| 103 NormalizePath(&ret.value_); | 121 NormalizePath(&ret.value_); |
| 104 if (!EndsWithSlash(ret.value_)) | 122 if (!EndsWithSlash(ret.value_)) |
| 105 ret.value_.push_back('/'); | 123 ret.value_.push_back('/'); |
| 106 return ret; | 124 return ret; |
| 107 } | 125 } |
| 108 | 126 |
| 127 if (!source_root.empty()) { |
| 128 std::string absolute = |
| 129 Resolve(base::FilePath(source_root.data())) |
| 130 .Append(p.as_string()).value(); |
| 131 NormalizePath(&absolute); |
| 132 if (!MakeAbsolutePathRelativeIfPossible(source_root, absolute, &ret.value_)) |
| 133 ret.value_ = absolute; |
| 134 if (!EndsWithSlash(ret.value_)) |
| 135 ret.value_.push_back('/'); |
| 136 return ret; |
| 137 } |
| 138 |
| 109 ret.value_.reserve(value_.size() + p.size()); | 139 ret.value_.reserve(value_.size() + p.size()); |
| 110 ret.value_.assign(value_); | 140 ret.value_.assign(value_); |
| 111 ret.value_.append(p.data(), p.size()); | 141 ret.value_.append(p.data(), p.size()); |
| 112 | 142 |
| 113 NormalizePath(&ret.value_); | 143 NormalizePath(&ret.value_); |
| 114 if (!EndsWithSlash(ret.value_)) | 144 if (!EndsWithSlash(ret.value_)) |
| 115 ret.value_.push_back('/'); | 145 ret.value_.push_back('/'); |
| 116 AssertValueSourceDirString(ret.value_); | 146 AssertValueSourceDirString(ret.value_); |
| 117 | 147 |
| 118 return ret; | 148 return ret; |
| (...skipping 17 matching lines...) Expand all Loading... |
| 136 // String the double-leading slash for source-relative paths. | 166 // String the double-leading slash for source-relative paths. |
| 137 converted.assign(&value_[2], value_.size() - 2); | 167 converted.assign(&value_[2], value_.size() - 2); |
| 138 return source_root.Append(UTF8ToFilePath(converted)) | 168 return source_root.Append(UTF8ToFilePath(converted)) |
| 139 .NormalizePathSeparatorsTo('/'); | 169 .NormalizePathSeparatorsTo('/'); |
| 140 } | 170 } |
| 141 | 171 |
| 142 void SourceDir::SwapValue(std::string* v) { | 172 void SourceDir::SwapValue(std::string* v) { |
| 143 value_.swap(*v); | 173 value_.swap(*v); |
| 144 AssertValueSourceDirString(value_); | 174 AssertValueSourceDirString(value_); |
| 145 } | 175 } |
| OLD | NEW |