[email protected] | 96ea63d | 2013-07-30 10:17:07 | [diff] [blame] | 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. |
| 4 | |
| 5 | #include "tools/gn/filesystem_utils.h" |
| 6 | |
[email protected] | ea3690c | 2013-09-23 17:59:22 | [diff] [blame] | 7 | #include <algorithm> |
| 8 | |
[email protected] | a623db187 | 2014-02-19 19:11:17 | [diff] [blame] | 9 | #include "base/file_util.h" |
[email protected] | 96ea63d | 2013-07-30 10:17:07 | [diff] [blame] | 10 | #include "base/logging.h" |
[email protected] | f6b314d1 | 2013-08-23 21:07:47 | [diff] [blame] | 11 | #include "base/strings/string_util.h" |
[email protected] | 96ea63d | 2013-07-30 10:17:07 | [diff] [blame] | 12 | #include "base/strings/utf_string_conversions.h" |
| 13 | #include "build/build_config.h" |
| 14 | #include "tools/gn/location.h" |
[email protected] | 05b94b43 | 2013-11-22 22:09:55 | [diff] [blame] | 15 | #include "tools/gn/settings.h" |
[email protected] | 96ea63d | 2013-07-30 10:17:07 | [diff] [blame] | 16 | #include "tools/gn/source_dir.h" |
| 17 | |
| 18 | namespace { |
| 19 | |
| 20 | enum DotDisposition { |
| 21 | // The given dot is just part of a filename and is not special. |
| 22 | NOT_A_DIRECTORY, |
| 23 | |
| 24 | // The given dot is the current directory. |
| 25 | DIRECTORY_CUR, |
| 26 | |
| 27 | // The given dot is the first of a double dot that should take us up one. |
| 28 | DIRECTORY_UP |
| 29 | }; |
| 30 | |
| 31 | // When we find a dot, this function is called with the character following |
| 32 | // that dot to see what it is. The return value indicates what type this dot is |
| 33 | // (see above). This code handles the case where the dot is at the end of the |
| 34 | // input. |
| 35 | // |
| 36 | // |*consumed_len| will contain the number of characters in the input that |
| 37 | // express what we found. |
| 38 | DotDisposition ClassifyAfterDot(const std::string& path, |
| 39 | size_t after_dot, |
| 40 | size_t* consumed_len) { |
| 41 | if (after_dot == path.size()) { |
| 42 | // Single dot at the end. |
| 43 | *consumed_len = 1; |
| 44 | return DIRECTORY_CUR; |
| 45 | } |
[email protected] | 858ceda | 2014-02-27 23:26:40 | [diff] [blame] | 46 | if (IsSlash(path[after_dot])) { |
[email protected] | 96ea63d | 2013-07-30 10:17:07 | [diff] [blame] | 47 | // Single dot followed by a slash. |
| 48 | *consumed_len = 2; // Consume the slash |
| 49 | return DIRECTORY_CUR; |
| 50 | } |
| 51 | |
| 52 | if (path[after_dot] == '.') { |
| 53 | // Two dots. |
| 54 | if (after_dot + 1 == path.size()) { |
| 55 | // Double dot at the end. |
| 56 | *consumed_len = 2; |
| 57 | return DIRECTORY_UP; |
| 58 | } |
[email protected] | 858ceda | 2014-02-27 23:26:40 | [diff] [blame] | 59 | if (IsSlash(path[after_dot + 1])) { |
[email protected] | 96ea63d | 2013-07-30 10:17:07 | [diff] [blame] | 60 | // Double dot folowed by a slash. |
| 61 | *consumed_len = 3; |
| 62 | return DIRECTORY_UP; |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | // The dots are followed by something else, not a directory. |
| 67 | *consumed_len = 1; |
| 68 | return NOT_A_DIRECTORY; |
| 69 | } |
| 70 | |
[email protected] | f6b314d1 | 2013-08-23 21:07:47 | [diff] [blame] | 71 | #if defined(OS_WIN) |
| 72 | inline char NormalizeWindowsPathChar(char c) { |
| 73 | if (c == '/') |
| 74 | return '\\'; |
| 75 | return base::ToLowerASCII(c); |
| 76 | } |
| 77 | |
| 78 | // Attempts to do a case and slash-insensitive comparison of two 8-bit Windows |
| 79 | // paths. |
| 80 | bool AreAbsoluteWindowsPathsEqual(const base::StringPiece& a, |
| 81 | const base::StringPiece& b) { |
| 82 | if (a.size() != b.size()) |
| 83 | return false; |
| 84 | |
| 85 | // For now, just do a case-insensitive ASCII comparison. We could convert to |
| 86 | // UTF-16 and use ICU if necessary. Or maybe base::strcasecmp is good enough? |
| 87 | for (size_t i = 0; i < a.size(); i++) { |
| 88 | if (NormalizeWindowsPathChar(a[i]) != NormalizeWindowsPathChar(b[i])) |
| 89 | return false; |
| 90 | } |
| 91 | return true; |
| 92 | } |
| 93 | |
| 94 | bool DoesBeginWindowsDriveLetter(const base::StringPiece& path) { |
| 95 | if (path.size() < 3) |
| 96 | return false; |
| 97 | |
| 98 | // Check colon first, this will generally fail fastest. |
| 99 | if (path[1] != ':') |
| 100 | return false; |
| 101 | |
| 102 | // Check drive letter |
| 103 | if (!((path[0] >= 'A' && path[0] <= 'Z') || |
| 104 | path[0] >= 'a' && path[0] <= 'z')) |
| 105 | return false; |
| 106 | |
[email protected] | 858ceda | 2014-02-27 23:26:40 | [diff] [blame] | 107 | if (!IsSlash(path[2])) |
[email protected] | f6b314d1 | 2013-08-23 21:07:47 | [diff] [blame] | 108 | return false; |
| 109 | return true; |
| 110 | } |
| 111 | #endif |
| 112 | |
[email protected] | a623db187 | 2014-02-19 19:11:17 | [diff] [blame] | 113 | // A wrapper around FilePath.GetComponents that works the way we need. This is |
| 114 | // not super efficient since it does some O(n) transformations on the path. If |
| 115 | // this is called a lot, we might want to optimize. |
| 116 | std::vector<base::FilePath::StringType> GetPathComponents( |
| 117 | const base::FilePath& path) { |
| 118 | std::vector<base::FilePath::StringType> result; |
| 119 | path.GetComponents(&result); |
| 120 | |
| 121 | if (result.empty()) |
| 122 | return result; |
| 123 | |
| 124 | // GetComponents will preserve the "/" at the beginning, which confuses us. |
| 125 | // We don't expect to have relative paths in this function. |
| 126 | // Don't use IsSeparator since we always want to allow backslashes. |
| 127 | if (result[0] == FILE_PATH_LITERAL("/") || |
| 128 | result[0] == FILE_PATH_LITERAL("\\")) |
| 129 | result.erase(result.begin()); |
| 130 | |
| 131 | #if defined(OS_WIN) |
| 132 | // On Windows, GetComponents will give us [ "C:", "/", "foo" ], and we |
| 133 | // don't want the slash in there. This doesn't support input like "C:foo" |
| 134 | // which means foo relative to the current directory of the C drive but |
| 135 | // that's basically legacy DOS behavior we don't need to support. |
[email protected] | 858ceda | 2014-02-27 23:26:40 | [diff] [blame] | 136 | if (result.size() >= 2 && result[1].size() == 1 && IsSlash(result[1][0])) |
[email protected] | a623db187 | 2014-02-19 19:11:17 | [diff] [blame] | 137 | result.erase(result.begin() + 1); |
| 138 | #endif |
| 139 | |
| 140 | return result; |
| 141 | } |
| 142 | |
| 143 | // Provides the equivalent of == for filesystem strings, trying to do |
| 144 | // approximately the right thing with case. |
| 145 | bool FilesystemStringsEqual(const base::FilePath::StringType& a, |
| 146 | const base::FilePath::StringType& b) { |
| 147 | #if defined(OS_WIN) |
| 148 | // Assume case-insensitive filesystems on Windows. We use the CompareString |
| 149 | // function to do a case-insensitive comparison based on the current locale |
| 150 | // (we don't want GN to depend on ICU which is large and requires data |
| 151 | // files). This isn't perfect, but getting this perfectly right is very |
| 152 | // difficult and requires I/O, and this comparison should cover 99.9999% of |
| 153 | // all cases. |
| 154 | // |
| 155 | // Note: The documentation for CompareString says it runs fastest on |
| 156 | // null-terminated strings with -1 passed for the length, so we do that here. |
| 157 | // There should not be embedded nulls in filesystem strings. |
| 158 | return ::CompareString(LOCALE_USER_DEFAULT, LINGUISTIC_IGNORECASE, |
| 159 | a.c_str(), -1, b.c_str(), -1) == CSTR_EQUAL; |
| 160 | #else |
| 161 | // Assume case-sensitive filesystems on non-Windows. |
| 162 | return a == b; |
| 163 | #endif |
| 164 | } |
| 165 | |
[email protected] | f6b314d1 | 2013-08-23 21:07:47 | [diff] [blame] | 166 | } // namespace |
[email protected] | 96ea63d | 2013-07-30 10:17:07 | [diff] [blame] | 167 | |
[email protected] | 126d8b5 | 2014-04-07 22:17:35 | [diff] [blame] | 168 | SourceFileType GetSourceFileType(const SourceFile& file) { |
[email protected] | 96ea63d | 2013-07-30 10:17:07 | [diff] [blame] | 169 | base::StringPiece extension = FindExtension(&file.value()); |
| 170 | if (extension == "cc" || extension == "cpp" || extension == "cxx") |
| 171 | return SOURCE_CC; |
| 172 | if (extension == "h") |
| 173 | return SOURCE_H; |
| 174 | if (extension == "c") |
| 175 | return SOURCE_C; |
[email protected] | 126d8b5 | 2014-04-07 22:17:35 | [diff] [blame] | 176 | if (extension == "m") |
| 177 | return SOURCE_M; |
| 178 | if (extension == "mm") |
| 179 | return SOURCE_MM; |
| 180 | if (extension == "rc") |
| 181 | return SOURCE_RC; |
[email protected] | e8ab691 | 2014-04-21 20:54:51 | [diff] [blame] | 182 | if (extension == "S" || extension == "s") |
[email protected] | 126d8b5 | 2014-04-07 22:17:35 | [diff] [blame] | 183 | return SOURCE_S; |
[email protected] | c6f27f2 | 2013-08-21 21:44:59 | [diff] [blame] | 184 | |
[email protected] | 96ea63d | 2013-07-30 10:17:07 | [diff] [blame] | 185 | return SOURCE_UNKNOWN; |
| 186 | } |
| 187 | |
| 188 | const char* GetExtensionForOutputType(Target::OutputType type, |
| 189 | Settings::TargetOS os) { |
| 190 | switch (os) { |
[email protected] | 8338eea | 2013-08-05 23:08:12 | [diff] [blame] | 191 | case Settings::MAC: |
| 192 | switch (type) { |
[email protected] | 8338eea | 2013-08-05 23:08:12 | [diff] [blame] | 193 | case Target::EXECUTABLE: |
| 194 | return ""; |
| 195 | case Target::SHARED_LIBRARY: |
| 196 | return "dylib"; |
| 197 | case Target::STATIC_LIBRARY: |
| 198 | return "a"; |
[email protected] | 8338eea | 2013-08-05 23:08:12 | [diff] [blame] | 199 | default: |
| 200 | NOTREACHED(); |
| 201 | } |
| 202 | break; |
| 203 | |
[email protected] | 96ea63d | 2013-07-30 10:17:07 | [diff] [blame] | 204 | case Settings::WIN: |
| 205 | switch (type) { |
[email protected] | 96ea63d | 2013-07-30 10:17:07 | [diff] [blame] | 206 | case Target::EXECUTABLE: |
| 207 | return "exe"; |
| 208 | case Target::SHARED_LIBRARY: |
| 209 | return "dll.lib"; // Extension of import library. |
| 210 | case Target::STATIC_LIBRARY: |
| 211 | return "lib"; |
[email protected] | c0822d7f | 2013-08-13 17:10:56 | [diff] [blame] | 212 | default: |
| 213 | NOTREACHED(); |
| 214 | } |
| 215 | break; |
| 216 | |
| 217 | case Settings::LINUX: |
| 218 | switch (type) { |
| 219 | case Target::EXECUTABLE: |
| 220 | return ""; |
| 221 | case Target::SHARED_LIBRARY: |
| 222 | return "so"; |
| 223 | case Target::STATIC_LIBRARY: |
| 224 | return "a"; |
[email protected] | 96ea63d | 2013-07-30 10:17:07 | [diff] [blame] | 225 | default: |
| 226 | NOTREACHED(); |
| 227 | } |
| 228 | break; |
| 229 | |
| 230 | default: |
| 231 | NOTREACHED(); |
| 232 | } |
| 233 | return ""; |
| 234 | } |
| 235 | |
[email protected] | 1bcf001 | 2013-09-19 21:13:32 | [diff] [blame] | 236 | std::string FilePathToUTF8(const base::FilePath::StringType& str) { |
[email protected] | 96ea63d | 2013-07-30 10:17:07 | [diff] [blame] | 237 | #if defined(OS_WIN) |
[email protected] | 6c3bf03 | 2013-12-25 19:37:03 | [diff] [blame] | 238 | return base::WideToUTF8(str); |
[email protected] | 96ea63d | 2013-07-30 10:17:07 | [diff] [blame] | 239 | #else |
[email protected] | 1bcf001 | 2013-09-19 21:13:32 | [diff] [blame] | 240 | return str; |
[email protected] | 96ea63d | 2013-07-30 10:17:07 | [diff] [blame] | 241 | #endif |
| 242 | } |
| 243 | |
| 244 | base::FilePath UTF8ToFilePath(const base::StringPiece& sp) { |
| 245 | #if defined(OS_WIN) |
[email protected] | 6c3bf03 | 2013-12-25 19:37:03 | [diff] [blame] | 246 | return base::FilePath(base::UTF8ToWide(sp)); |
[email protected] | 96ea63d | 2013-07-30 10:17:07 | [diff] [blame] | 247 | #else |
| 248 | return base::FilePath(sp.as_string()); |
| 249 | #endif |
| 250 | } |
| 251 | |
| 252 | size_t FindExtensionOffset(const std::string& path) { |
| 253 | for (int i = static_cast<int>(path.size()); i >= 0; i--) { |
[email protected] | 858ceda | 2014-02-27 23:26:40 | [diff] [blame] | 254 | if (IsSlash(path[i])) |
[email protected] | 96ea63d | 2013-07-30 10:17:07 | [diff] [blame] | 255 | break; |
| 256 | if (path[i] == '.') |
| 257 | return i + 1; |
| 258 | } |
| 259 | return std::string::npos; |
| 260 | } |
| 261 | |
| 262 | base::StringPiece FindExtension(const std::string* path) { |
| 263 | size_t extension_offset = FindExtensionOffset(*path); |
| 264 | if (extension_offset == std::string::npos) |
| 265 | return base::StringPiece(); |
| 266 | return base::StringPiece(&path->data()[extension_offset], |
| 267 | path->size() - extension_offset); |
| 268 | } |
| 269 | |
| 270 | size_t FindFilenameOffset(const std::string& path) { |
| 271 | for (int i = static_cast<int>(path.size()) - 1; i >= 0; i--) { |
[email protected] | 858ceda | 2014-02-27 23:26:40 | [diff] [blame] | 272 | if (IsSlash(path[i])) |
[email protected] | 96ea63d | 2013-07-30 10:17:07 | [diff] [blame] | 273 | return i + 1; |
| 274 | } |
| 275 | return 0; // No filename found means everything was the filename. |
| 276 | } |
| 277 | |
| 278 | base::StringPiece FindFilename(const std::string* path) { |
| 279 | size_t filename_offset = FindFilenameOffset(*path); |
| 280 | if (filename_offset == 0) |
| 281 | return base::StringPiece(*path); // Everything is the file name. |
| 282 | return base::StringPiece(&(*path).data()[filename_offset], |
| 283 | path->size() - filename_offset); |
| 284 | } |
| 285 | |
| 286 | base::StringPiece FindFilenameNoExtension(const std::string* path) { |
| 287 | if (path->empty()) |
| 288 | return base::StringPiece(); |
| 289 | size_t filename_offset = FindFilenameOffset(*path); |
| 290 | size_t extension_offset = FindExtensionOffset(*path); |
| 291 | |
| 292 | size_t name_len; |
| 293 | if (extension_offset == std::string::npos) |
| 294 | name_len = path->size() - filename_offset; |
| 295 | else |
| 296 | name_len = extension_offset - filename_offset - 1; |
| 297 | |
| 298 | return base::StringPiece(&(*path).data()[filename_offset], name_len); |
| 299 | } |
| 300 | |
| 301 | void RemoveFilename(std::string* path) { |
| 302 | path->resize(FindFilenameOffset(*path)); |
| 303 | } |
| 304 | |
| 305 | bool EndsWithSlash(const std::string& s) { |
[email protected] | 858ceda | 2014-02-27 23:26:40 | [diff] [blame] | 306 | return !s.empty() && IsSlash(s[s.size() - 1]); |
[email protected] | 96ea63d | 2013-07-30 10:17:07 | [diff] [blame] | 307 | } |
| 308 | |
| 309 | base::StringPiece FindDir(const std::string* path) { |
| 310 | size_t filename_offset = FindFilenameOffset(*path); |
| 311 | if (filename_offset == 0u) |
| 312 | return base::StringPiece(); |
| 313 | return base::StringPiece(path->data(), filename_offset); |
| 314 | } |
| 315 | |
[email protected] | a3372c7 | 2014-04-28 22:39:26 | [diff] [blame^] | 316 | base::StringPiece FindLastDirComponent(const SourceDir& dir) { |
| 317 | const std::string& dir_string = dir.value(); |
| 318 | |
| 319 | if (dir_string.empty()) |
| 320 | return base::StringPiece(); |
| 321 | int cur = static_cast<int>(dir_string.size()) - 1; |
| 322 | DCHECK(dir_string[cur] == '/'); |
| 323 | int end = cur; |
| 324 | cur--; // Skip before the last slash. |
| 325 | |
| 326 | for (; cur >= 0; cur--) { |
| 327 | if (dir_string[cur] == '/') |
| 328 | return base::StringPiece(&dir_string[cur + 1], end - cur - 1); |
| 329 | } |
| 330 | return base::StringPiece(&dir_string[0], end); |
| 331 | } |
| 332 | |
[email protected] | 96ea63d | 2013-07-30 10:17:07 | [diff] [blame] | 333 | bool EnsureStringIsInOutputDir(const SourceDir& dir, |
| 334 | const std::string& str, |
| 335 | const Value& originating, |
| 336 | Err* err) { |
| 337 | // The last char of the dir will be a slash. We don't care if the input ends |
| 338 | // in a slash or not, so just compare up until there. |
| 339 | // |
| 340 | // This check will be wrong for all proper prefixes "e.g. "/output" will |
| 341 | // match "/out" but we don't really care since this is just a sanity check. |
| 342 | const std::string& dir_str = dir.value(); |
| 343 | if (str.compare(0, dir_str.length() - 1, dir_str, 0, dir_str.length() - 1) |
| 344 | != 0) { |
[email protected] | b8aa8124 | 2014-01-05 11:59:51 | [diff] [blame] | 345 | *err = Err(originating, "File is not inside output directory.", |
[email protected] | 96ea63d | 2013-07-30 10:17:07 | [diff] [blame] | 346 | "The given file should be in the output directory. Normally you would " |
[email protected] | b8aa8124 | 2014-01-05 11:59:51 | [diff] [blame] | 347 | "specify\n\"$target_out_dir/foo\" or " |
[email protected] | 96ea63d | 2013-07-30 10:17:07 | [diff] [blame] | 348 | "\"$target_gen_dir/foo\". I interpreted this as\n\"" |
| 349 | + str + "\"."); |
| 350 | return false; |
| 351 | } |
| 352 | return true; |
| 353 | } |
| 354 | |
[email protected] | ac1128e | 2013-08-23 00:26:56 | [diff] [blame] | 355 | bool IsPathAbsolute(const base::StringPiece& path) { |
| 356 | if (path.empty()) |
| 357 | return false; |
| 358 | |
[email protected] | 858ceda | 2014-02-27 23:26:40 | [diff] [blame] | 359 | if (!IsSlash(path[0])) { |
[email protected] | ac1128e | 2013-08-23 00:26:56 | [diff] [blame] | 360 | #if defined(OS_WIN) |
| 361 | // Check for Windows system paths like "C:\foo". |
[email protected] | 858ceda | 2014-02-27 23:26:40 | [diff] [blame] | 362 | if (path.size() > 2 && path[1] == ':' && IsSlash(path[2])) |
[email protected] | ac1128e | 2013-08-23 00:26:56 | [diff] [blame] | 363 | return true; |
| 364 | #endif |
| 365 | return false; // Doesn't begin with a slash, is relative. |
| 366 | } |
| 367 | |
[email protected] | 858ceda | 2014-02-27 23:26:40 | [diff] [blame] | 368 | // Double forward slash at the beginning means source-relative (we don't |
| 369 | // allow backslashes for denoting this). |
[email protected] | ac1128e | 2013-08-23 00:26:56 | [diff] [blame] | 370 | if (path.size() > 1 && path[1] == '/') |
[email protected] | 858ceda | 2014-02-27 23:26:40 | [diff] [blame] | 371 | return false; |
[email protected] | ac1128e | 2013-08-23 00:26:56 | [diff] [blame] | 372 | |
| 373 | return true; |
| 374 | } |
| 375 | |
| 376 | bool MakeAbsolutePathRelativeIfPossible(const base::StringPiece& source_root, |
| 377 | const base::StringPiece& path, |
| 378 | std::string* dest) { |
| 379 | DCHECK(IsPathAbsolute(source_root)); |
| 380 | DCHECK(IsPathAbsolute(path)); |
| 381 | |
| 382 | dest->clear(); |
| 383 | |
| 384 | if (source_root.size() > path.size()) |
| 385 | return false; // The source root is longer: the path can never be inside. |
| 386 | |
| 387 | #if defined(OS_WIN) |
[email protected] | 858ceda | 2014-02-27 23:26:40 | [diff] [blame] | 388 | // Source root should be canonical on Windows. Note that the initial slash |
| 389 | // must be forward slash, but that the other ones can be either forward or |
| 390 | // backward. |
[email protected] | ac1128e | 2013-08-23 00:26:56 | [diff] [blame] | 391 | DCHECK(source_root.size() > 2 && source_root[0] != '/' && |
[email protected] | 858ceda | 2014-02-27 23:26:40 | [diff] [blame] | 392 | source_root[1] == ':' && IsSlash(source_root[2])); |
[email protected] | f6b314d1 | 2013-08-23 21:07:47 | [diff] [blame] | 393 | |
| 394 | size_t after_common_index = std::string::npos; |
| 395 | if (DoesBeginWindowsDriveLetter(path)) { |
| 396 | // Handle "C:\foo" |
| 397 | if (AreAbsoluteWindowsPathsEqual(source_root, |
| 398 | path.substr(0, source_root.size()))) |
| 399 | after_common_index = source_root.size(); |
| 400 | else |
| 401 | return false; |
| 402 | } else if (path[0] == '/' && source_root.size() <= path.size() - 1 && |
| 403 | DoesBeginWindowsDriveLetter(path.substr(1))) { |
| 404 | // Handle "/C:/foo" |
| 405 | if (AreAbsoluteWindowsPathsEqual(source_root, |
| 406 | path.substr(1, source_root.size()))) |
| 407 | after_common_index = source_root.size() + 1; |
| 408 | else |
| 409 | return false; |
| 410 | } else { |
| 411 | return false; |
| 412 | } |
| 413 | |
| 414 | // If we get here, there's a match and after_common_index identifies the |
| 415 | // part after it. |
| 416 | |
| 417 | // The base may or may not have a trailing slash, so skip all slashes from |
| 418 | // the path after our prefix match. |
| 419 | size_t first_after_slash = after_common_index; |
[email protected] | 858ceda | 2014-02-27 23:26:40 | [diff] [blame] | 420 | while (first_after_slash < path.size() && IsSlash(path[first_after_slash])) |
[email protected] | f6b314d1 | 2013-08-23 21:07:47 | [diff] [blame] | 421 | first_after_slash++; |
| 422 | |
| 423 | dest->assign("//"); // Result is source root relative. |
| 424 | dest->append(&path.data()[first_after_slash], |
| 425 | path.size() - first_after_slash); |
| 426 | return true; |
| 427 | |
[email protected] | ac1128e | 2013-08-23 00:26:56 | [diff] [blame] | 428 | #else |
[email protected] | f6b314d1 | 2013-08-23 21:07:47 | [diff] [blame] | 429 | |
[email protected] | ac1128e | 2013-08-23 00:26:56 | [diff] [blame] | 430 | // On non-Windows this is easy. Since we know both are absolute, just do a |
| 431 | // prefix check. |
| 432 | if (path.substr(0, source_root.size()) == source_root) { |
[email protected] | ac1128e | 2013-08-23 00:26:56 | [diff] [blame] | 433 | // The base may or may not have a trailing slash, so skip all slashes from |
| 434 | // the path after our prefix match. |
| 435 | size_t first_after_slash = source_root.size(); |
[email protected] | 858ceda | 2014-02-27 23:26:40 | [diff] [blame] | 436 | while (first_after_slash < path.size() && IsSlash(path[first_after_slash])) |
[email protected] | ac1128e | 2013-08-23 00:26:56 | [diff] [blame] | 437 | first_after_slash++; |
| 438 | |
[email protected] | f6b314d1 | 2013-08-23 21:07:47 | [diff] [blame] | 439 | dest->assign("//"); // Result is source root relative. |
[email protected] | ac1128e | 2013-08-23 00:26:56 | [diff] [blame] | 440 | dest->append(&path.data()[first_after_slash], |
| 441 | path.size() - first_after_slash); |
| 442 | return true; |
| 443 | } |
[email protected] | ac1128e | 2013-08-23 00:26:56 | [diff] [blame] | 444 | return false; |
[email protected] | f6b314d1 | 2013-08-23 21:07:47 | [diff] [blame] | 445 | #endif |
[email protected] | ac1128e | 2013-08-23 00:26:56 | [diff] [blame] | 446 | } |
| 447 | |
[email protected] | 96ea63d | 2013-07-30 10:17:07 | [diff] [blame] | 448 | std::string InvertDir(const SourceDir& path) { |
| 449 | const std::string value = path.value(); |
| 450 | if (value.empty()) |
| 451 | return std::string(); |
| 452 | |
| 453 | DCHECK(value[0] == '/'); |
| 454 | size_t begin_index = 1; |
| 455 | |
| 456 | // If the input begins with two slashes, skip over both (this is a |
[email protected] | 858ceda | 2014-02-27 23:26:40 | [diff] [blame] | 457 | // source-relative dir). These must be forward slashes only. |
[email protected] | 96ea63d | 2013-07-30 10:17:07 | [diff] [blame] | 458 | if (value.size() > 1 && value[1] == '/') |
| 459 | begin_index = 2; |
| 460 | |
| 461 | std::string ret; |
| 462 | for (size_t i = begin_index; i < value.size(); i++) { |
[email protected] | 858ceda | 2014-02-27 23:26:40 | [diff] [blame] | 463 | if (IsSlash(value[i])) |
[email protected] | 96ea63d | 2013-07-30 10:17:07 | [diff] [blame] | 464 | ret.append("../"); |
| 465 | } |
| 466 | return ret; |
| 467 | } |
| 468 | |
| 469 | void NormalizePath(std::string* path) { |
| 470 | char* pathbuf = path->empty() ? NULL : &(*path)[0]; |
| 471 | |
| 472 | // top_index is the first character we can modify in the path. Anything |
| 473 | // before this indicates where the path is relative to. |
| 474 | size_t top_index = 0; |
| 475 | bool is_relative = true; |
| 476 | if (!path->empty() && pathbuf[0] == '/') { |
| 477 | is_relative = false; |
| 478 | |
| 479 | if (path->size() > 1 && pathbuf[1] == '/') { |
| 480 | // Two leading slashes, this is a path into the source dir. |
| 481 | top_index = 2; |
| 482 | } else { |
| 483 | // One leading slash, this is a system-absolute path. |
| 484 | top_index = 1; |
| 485 | } |
| 486 | } |
| 487 | |
| 488 | size_t dest_i = top_index; |
| 489 | for (size_t src_i = top_index; src_i < path->size(); /* nothing */) { |
| 490 | if (pathbuf[src_i] == '.') { |
[email protected] | 858ceda | 2014-02-27 23:26:40 | [diff] [blame] | 491 | if (src_i == 0 || IsSlash(pathbuf[src_i - 1])) { |
[email protected] | 96ea63d | 2013-07-30 10:17:07 | [diff] [blame] | 492 | // Slash followed by a dot, see if it's something special. |
| 493 | size_t consumed_len; |
| 494 | switch (ClassifyAfterDot(*path, src_i + 1, &consumed_len)) { |
| 495 | case NOT_A_DIRECTORY: |
| 496 | // Copy the dot to the output, it means nothing special. |
| 497 | pathbuf[dest_i++] = pathbuf[src_i++]; |
| 498 | break; |
| 499 | case DIRECTORY_CUR: |
| 500 | // Current directory, just skip the input. |
| 501 | src_i += consumed_len; |
| 502 | break; |
| 503 | case DIRECTORY_UP: |
| 504 | // Back up over previous directory component. If we're already |
| 505 | // at the top, preserve the "..". |
| 506 | if (dest_i > top_index) { |
| 507 | // The previous char was a slash, remove it. |
| 508 | dest_i--; |
| 509 | } |
| 510 | |
| 511 | if (dest_i == top_index) { |
| 512 | if (is_relative) { |
| 513 | // We're already at the beginning of a relative input, copy the |
| 514 | // ".." and continue. We need the trailing slash if there was |
| 515 | // one before (otherwise we're at the end of the input). |
| 516 | pathbuf[dest_i++] = '.'; |
| 517 | pathbuf[dest_i++] = '.'; |
| 518 | if (consumed_len == 3) |
| 519 | pathbuf[dest_i++] = '/'; |
| 520 | |
| 521 | // This also makes a new "root" that we can't delete by going |
| 522 | // up more levels. Otherwise "../.." would collapse to |
| 523 | // nothing. |
| 524 | top_index = dest_i; |
| 525 | } |
| 526 | // Otherwise we're at the beginning of an absolute path. Don't |
| 527 | // allow ".." to go up another level and just eat it. |
| 528 | } else { |
| 529 | // Just find the previous slash or the beginning of input. |
[email protected] | 858ceda | 2014-02-27 23:26:40 | [diff] [blame] | 530 | while (dest_i > 0 && !IsSlash(pathbuf[dest_i - 1])) |
[email protected] | 96ea63d | 2013-07-30 10:17:07 | [diff] [blame] | 531 | dest_i--; |
| 532 | } |
| 533 | src_i += consumed_len; |
| 534 | } |
| 535 | } else { |
| 536 | // Dot not preceeded by a slash, copy it literally. |
| 537 | pathbuf[dest_i++] = pathbuf[src_i++]; |
| 538 | } |
[email protected] | 858ceda | 2014-02-27 23:26:40 | [diff] [blame] | 539 | } else if (IsSlash(pathbuf[src_i])) { |
| 540 | if (src_i > 0 && IsSlash(pathbuf[src_i - 1])) { |
[email protected] | 96ea63d | 2013-07-30 10:17:07 | [diff] [blame] | 541 | // Two slashes in a row, skip over it. |
| 542 | src_i++; |
| 543 | } else { |
[email protected] | 858ceda | 2014-02-27 23:26:40 | [diff] [blame] | 544 | // Just one slash, copy it, normalizing to foward slash. |
| 545 | pathbuf[dest_i] = '/'; |
| 546 | dest_i++; |
| 547 | src_i++; |
[email protected] | 96ea63d | 2013-07-30 10:17:07 | [diff] [blame] | 548 | } |
| 549 | } else { |
| 550 | // Input nothing special, just copy it. |
| 551 | pathbuf[dest_i++] = pathbuf[src_i++]; |
| 552 | } |
| 553 | } |
| 554 | path->resize(dest_i); |
| 555 | } |
| 556 | |
| 557 | void ConvertPathToSystem(std::string* path) { |
| 558 | #if defined(OS_WIN) |
| 559 | for (size_t i = 0; i < path->size(); i++) { |
| 560 | if ((*path)[i] == '/') |
| 561 | (*path)[i] = '\\'; |
| 562 | } |
| 563 | #endif |
| 564 | } |
| 565 | |
[email protected] | ea3690c | 2013-09-23 17:59:22 | [diff] [blame] | 566 | std::string RebaseSourceAbsolutePath(const std::string& input, |
| 567 | const SourceDir& dest_dir) { |
| 568 | CHECK(input.size() >= 2 && input[0] == '/' && input[1] == '/') |
| 569 | << "Input to rebase isn't source-absolute: " << input; |
| 570 | CHECK(dest_dir.is_source_absolute()) |
| 571 | << "Dir to rebase to isn't source-absolute: " << dest_dir.value(); |
| 572 | |
| 573 | const std::string& dest = dest_dir.value(); |
| 574 | |
| 575 | // Skip the common prefixes of the source and dest as long as they end in |
| 576 | // a [back]slash. |
| 577 | size_t common_prefix_len = 2; // The beginning two "//" are always the same. |
| 578 | size_t max_common_length = std::min(input.size(), dest.size()); |
| 579 | for (size_t i = common_prefix_len; i < max_common_length; i++) { |
[email protected] | 858ceda | 2014-02-27 23:26:40 | [diff] [blame] | 580 | if (IsSlash(input[i]) && IsSlash(dest[i])) |
[email protected] | ea3690c | 2013-09-23 17:59:22 | [diff] [blame] | 581 | common_prefix_len = i + 1; |
| 582 | else if (input[i] != dest[i]) |
| 583 | break; |
| 584 | } |
| 585 | |
| 586 | // Invert the dest dir starting from the end of the common prefix. |
| 587 | std::string ret; |
| 588 | for (size_t i = common_prefix_len; i < dest.size(); i++) { |
[email protected] | 858ceda | 2014-02-27 23:26:40 | [diff] [blame] | 589 | if (IsSlash(dest[i])) |
[email protected] | ea3690c | 2013-09-23 17:59:22 | [diff] [blame] | 590 | ret.append("../"); |
| 591 | } |
| 592 | |
| 593 | // Append any remaining unique input. |
| 594 | ret.append(&input[common_prefix_len], input.size() - common_prefix_len); |
| 595 | |
| 596 | // If the result is still empty, the paths are the same. |
| 597 | if (ret.empty()) |
| 598 | ret.push_back('.'); |
| 599 | |
| 600 | return ret; |
| 601 | } |
[email protected] | 05b94b43 | 2013-11-22 22:09:55 | [diff] [blame] | 602 | |
| 603 | std::string DirectoryWithNoLastSlash(const SourceDir& dir) { |
| 604 | std::string ret; |
| 605 | |
| 606 | if (dir.value().empty()) { |
| 607 | // Just keep input the same. |
| 608 | } else if (dir.value() == "/") { |
| 609 | ret.assign("/."); |
| 610 | } else if (dir.value() == "//") { |
| 611 | ret.assign("//."); |
| 612 | } else { |
| 613 | ret.assign(dir.value()); |
| 614 | ret.resize(ret.size() - 1); |
| 615 | } |
| 616 | return ret; |
| 617 | } |
| 618 | |
[email protected] | a623db187 | 2014-02-19 19:11:17 | [diff] [blame] | 619 | SourceDir SourceDirForPath(const base::FilePath& source_root, |
| 620 | const base::FilePath& path) { |
| 621 | std::vector<base::FilePath::StringType> source_comp = |
| 622 | GetPathComponents(source_root); |
| 623 | std::vector<base::FilePath::StringType> path_comp = |
| 624 | GetPathComponents(path); |
| 625 | |
| 626 | // See if path is inside the source root by looking for each of source root's |
| 627 | // components at the beginning of path. |
| 628 | bool is_inside_source; |
| 629 | if (path_comp.size() < source_comp.size()) { |
| 630 | // Too small to fit. |
| 631 | is_inside_source = false; |
| 632 | } else { |
| 633 | is_inside_source = true; |
| 634 | for (size_t i = 0; i < source_comp.size(); i++) { |
| 635 | if (!FilesystemStringsEqual(source_comp[i], path_comp[i])) { |
| 636 | is_inside_source = false; |
| 637 | break; |
| 638 | } |
| 639 | } |
| 640 | } |
| 641 | |
| 642 | std::string result_str; |
| 643 | size_t initial_path_comp_to_use; |
| 644 | if (is_inside_source) { |
| 645 | // Construct a source-relative path beginning in // and skip all of the |
| 646 | // shared directories. |
| 647 | result_str = "//"; |
| 648 | initial_path_comp_to_use = source_comp.size(); |
| 649 | } else { |
| 650 | // Not inside source code, construct a system-absolute path. |
| 651 | result_str = "/"; |
| 652 | initial_path_comp_to_use = 0; |
| 653 | } |
| 654 | |
| 655 | for (size_t i = initial_path_comp_to_use; i < path_comp.size(); i++) { |
| 656 | result_str.append(FilePathToUTF8(path_comp[i])); |
| 657 | result_str.push_back('/'); |
| 658 | } |
| 659 | return SourceDir(result_str); |
| 660 | } |
| 661 | |
| 662 | SourceDir SourceDirForCurrentDirectory(const base::FilePath& source_root) { |
| 663 | base::FilePath cd; |
[email protected] | 37b3c199 | 2014-03-11 20:59:02 | [diff] [blame] | 664 | base::GetCurrentDirectory(&cd); |
[email protected] | a623db187 | 2014-02-19 19:11:17 | [diff] [blame] | 665 | return SourceDirForPath(source_root, cd); |
| 666 | } |
| 667 | |
[email protected] | 05b94b43 | 2013-11-22 22:09:55 | [diff] [blame] | 668 | SourceDir GetToolchainOutputDir(const Settings* settings) { |
| 669 | const OutputFile& toolchain_subdir = settings->toolchain_output_subdir(); |
| 670 | |
| 671 | std::string result = settings->build_settings()->build_dir().value(); |
| 672 | if (!toolchain_subdir.value().empty()) |
| 673 | result.append(toolchain_subdir.value()); |
| 674 | |
| 675 | return SourceDir(SourceDir::SWAP_IN, &result); |
| 676 | } |
| 677 | |
| 678 | SourceDir GetToolchainGenDir(const Settings* settings) { |
| 679 | const OutputFile& toolchain_subdir = settings->toolchain_output_subdir(); |
| 680 | |
| 681 | std::string result = settings->build_settings()->build_dir().value(); |
| 682 | if (!toolchain_subdir.value().empty()) |
| 683 | result.append(toolchain_subdir.value()); |
| 684 | |
| 685 | result.append("gen/"); |
| 686 | return SourceDir(SourceDir::SWAP_IN, &result); |
| 687 | } |
| 688 | |
| 689 | SourceDir GetOutputDirForSourceDir(const Settings* settings, |
| 690 | const SourceDir& source_dir) { |
| 691 | SourceDir toolchain = GetToolchainOutputDir(settings); |
| 692 | |
| 693 | std::string ret; |
| 694 | toolchain.SwapValue(&ret); |
| 695 | ret.append("obj/"); |
| 696 | |
| 697 | // The source dir should be source-absolute, so we trim off the two leading |
| 698 | // slashes to append to the toolchain object directory. |
| 699 | DCHECK(source_dir.is_source_absolute()); |
| 700 | ret.append(&source_dir.value()[2], source_dir.value().size() - 2); |
| 701 | |
| 702 | return SourceDir(SourceDir::SWAP_IN, &ret); |
| 703 | } |
| 704 | |
| 705 | SourceDir GetGenDirForSourceDir(const Settings* settings, |
| 706 | const SourceDir& source_dir) { |
| 707 | SourceDir toolchain = GetToolchainGenDir(settings); |
| 708 | |
| 709 | std::string ret; |
| 710 | toolchain.SwapValue(&ret); |
| 711 | |
| 712 | // The source dir should be source-absolute, so we trim off the two leading |
| 713 | // slashes to append to the toolchain object directory. |
| 714 | DCHECK(source_dir.is_source_absolute()); |
| 715 | ret.append(&source_dir.value()[2], source_dir.value().size() - 2); |
| 716 | |
| 717 | return SourceDir(SourceDir::SWAP_IN, &ret); |
| 718 | } |
| 719 | |
| 720 | SourceDir GetTargetOutputDir(const Target* target) { |
| 721 | return GetOutputDirForSourceDir(target->settings(), target->label().dir()); |
| 722 | } |
| 723 | |
| 724 | SourceDir GetTargetGenDir(const Target* target) { |
| 725 | return GetGenDirForSourceDir(target->settings(), target->label().dir()); |
| 726 | } |
| 727 | |
| 728 | SourceDir GetCurrentOutputDir(const Scope* scope) { |
| 729 | return GetOutputDirForSourceDir(scope->settings(), scope->GetSourceDir()); |
| 730 | } |
| 731 | |
| 732 | SourceDir GetCurrentGenDir(const Scope* scope) { |
| 733 | return GetGenDirForSourceDir(scope->settings(), scope->GetSourceDir()); |
| 734 | } |