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/filesystem_utils.h" | 5 #include "tools/gn/filesystem_utils.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 | 8 |
9 #include "base/files/file_util.h" | 9 #include "base/files/file_util.h" |
10 #include "base/logging.h" | 10 #include "base/logging.h" |
(...skipping 359 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
370 | 370 |
371 dest->assign("//"); // Result is source root relative. | 371 dest->assign("//"); // Result is source root relative. |
372 dest->append(&path.data()[first_after_slash], | 372 dest->append(&path.data()[first_after_slash], |
373 path.size() - first_after_slash); | 373 path.size() - first_after_slash); |
374 return true; | 374 return true; |
375 } | 375 } |
376 return false; | 376 return false; |
377 #endif | 377 #endif |
378 } | 378 } |
379 | 379 |
380 void NormalizePath(std::string* path) { | 380 void NormalizePath(std::string* path, const base::StringPiece& source_root) { |
381 char* pathbuf = path->empty() ? nullptr : &(*path)[0]; | 381 char* pathbuf = path->empty() ? nullptr : &(*path)[0]; |
382 | 382 |
383 // top_index is the first character we can modify in the path. Anything | 383 // top_index is the first character we can modify in the path. Anything |
384 // before this indicates where the path is relative to. | 384 // before this indicates where the path is relative to. |
385 size_t top_index = 0; | 385 size_t top_index = 0; |
386 bool is_relative = true; | 386 bool is_relative = true; |
387 if (!path->empty() && pathbuf[0] == '/') { | 387 if (!path->empty() && pathbuf[0] == '/') { |
388 is_relative = false; | 388 is_relative = false; |
389 | 389 |
390 if (path->size() > 1 && pathbuf[1] == '/') { | 390 if (path->size() > 1 && pathbuf[1] == '/') { |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
426 // one before (otherwise we're at the end of the input). | 426 // one before (otherwise we're at the end of the input). |
427 pathbuf[dest_i++] = '.'; | 427 pathbuf[dest_i++] = '.'; |
428 pathbuf[dest_i++] = '.'; | 428 pathbuf[dest_i++] = '.'; |
429 if (consumed_len == 3) | 429 if (consumed_len == 3) |
430 pathbuf[dest_i++] = '/'; | 430 pathbuf[dest_i++] = '/'; |
431 | 431 |
432 // This also makes a new "root" that we can't delete by going | 432 // This also makes a new "root" that we can't delete by going |
433 // up more levels. Otherwise "../.." would collapse to | 433 // up more levels. Otherwise "../.." would collapse to |
434 // nothing. | 434 // nothing. |
435 top_index = dest_i; | 435 top_index = dest_i; |
| 436 } else if (top_index == 2 && !source_root.empty()) { |
| 437 // |path| was passed in as a source-absolute path. Prepend |
| 438 // |source_root| to make |path| absolute. |source_root| must not |
| 439 // end with a slash unless we are at root. |
| 440 DCHECK(source_root.size() == 1u || |
| 441 !IsSlash(source_root[source_root.size() - 1u])); |
| 442 size_t source_root_len = source_root.size(); |
| 443 |
| 444 #if defined(OS_WIN) |
| 445 // On Windows, if the source_root does not start with a slash, |
| 446 // append one here for consistency. |
| 447 if (!IsSlash(source_root[0])) { |
| 448 path->insert(0, "/" + source_root.as_string()); |
| 449 source_root_len++; |
| 450 } else { |
| 451 path->insert(0, source_root.data(), source_root_len); |
| 452 } |
| 453 |
| 454 // Normalize slashes in source root portion. |
| 455 for (size_t i = 0; i < source_root_len; ++i) { |
| 456 if ((*path)[i] == '\\') |
| 457 (*path)[i] = '/'; |
| 458 } |
| 459 #else |
| 460 path->insert(0, source_root.data(), source_root_len); |
| 461 #endif |
| 462 |
| 463 // |path| is now absolute, so |top_index| is 1. |dest_i| and |
| 464 // |src_i| should be incremented to keep the same relative |
| 465 // position. Comsume the leading "//" by decrementing |dest_i|. |
| 466 top_index = 1; |
| 467 pathbuf = &(*path)[0]; |
| 468 dest_i += source_root_len - 2; |
| 469 src_i += source_root_len; |
| 470 |
| 471 // Just find the previous slash or the beginning of input. |
| 472 while (dest_i > 0 && !IsSlash(pathbuf[dest_i - 1])) |
| 473 dest_i--; |
436 } | 474 } |
437 // Otherwise we're at the beginning of an absolute path. Don't | 475 // Otherwise we're at the beginning of a system-absolute path, or |
438 // allow ".." to go up another level and just eat it. | 476 // a source-absolute path for which we don't know the absolute |
| 477 // path. Don't allow ".." to go up another level, and just eat it. |
439 } else { | 478 } else { |
440 // Just find the previous slash or the beginning of input. | 479 // Just find the previous slash or the beginning of input. |
441 while (dest_i > 0 && !IsSlash(pathbuf[dest_i - 1])) | 480 while (dest_i > 0 && !IsSlash(pathbuf[dest_i - 1])) |
442 dest_i--; | 481 dest_i--; |
443 } | 482 } |
444 src_i += consumed_len; | 483 src_i += consumed_len; |
445 } | 484 } |
446 } else { | 485 } else { |
447 // Dot not preceeded by a slash, copy it literally. | 486 // Dot not preceeded by a slash, copy it literally. |
448 pathbuf[dest_i++] = pathbuf[src_i++]; | 487 pathbuf[dest_i++] = pathbuf[src_i++]; |
(...skipping 347 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
796 | 835 |
797 SourceDir GetCurrentOutputDir(const Scope* scope) { | 836 SourceDir GetCurrentOutputDir(const Scope* scope) { |
798 return GetOutputDirForSourceDirAsOutputFile( | 837 return GetOutputDirForSourceDirAsOutputFile( |
799 scope->settings(), scope->GetSourceDir()).AsSourceDir( | 838 scope->settings(), scope->GetSourceDir()).AsSourceDir( |
800 scope->settings()->build_settings()); | 839 scope->settings()->build_settings()); |
801 } | 840 } |
802 | 841 |
803 SourceDir GetCurrentGenDir(const Scope* scope) { | 842 SourceDir GetCurrentGenDir(const Scope* scope) { |
804 return GetGenDirForSourceDir(scope->settings(), scope->GetSourceDir()); | 843 return GetGenDirForSourceDir(scope->settings(), scope->GetSourceDir()); |
805 } | 844 } |
OLD | NEW |