Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(87)

Side by Side Diff: tools/gn/filesystem_utils.cc

Issue 1455203002: [GN] Add support to rebase_path to resolve paths above the source root. (Closed) Base URL: https://chromium.googlesource.com/chromium/src@master
Patch Set: . Created 5 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
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
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.
439 const size_t len = source_root.ends_with("/")
brettw 2015/12/01 01:21:22 I think we can know whether the source_root ends i
slan 2015/12/01 22:15:03 Done.
440 ? source_root.size() - 1
441 : source_root.size();
442 path->insert(0, source_root.data(), len);
443 pathbuf = &(*path)[0];
444
445 // |path| is now absolute, so |top_index| is 1. |dest_i| and
446 // |src_i| should be incremented to keep the same relative
447 // position. Comsume the leading "//" by decrementing |dest_i|.
448 top_index = 1;
449 dest_i += len - 2;
450 src_i += len;
451
452 // Just find the previous slash or the beginning of input.
453 while (dest_i > 0 && !IsSlash(pathbuf[dest_i - 1]))
454 dest_i--;
436 } 455 }
437 // Otherwise we're at the beginning of an absolute path. Don't 456 // Otherwise we're at the beginning of a system-absolute path, or
438 // allow ".." to go up another level and just eat it. 457 // a source-absolute path for which we don't know the absolute
458 // path. Don't allow ".." to go up another level, and just eat it.
439 } else { 459 } else {
440 // Just find the previous slash or the beginning of input. 460 // Just find the previous slash or the beginning of input.
441 while (dest_i > 0 && !IsSlash(pathbuf[dest_i - 1])) 461 while (dest_i > 0 && !IsSlash(pathbuf[dest_i - 1]))
442 dest_i--; 462 dest_i--;
443 } 463 }
444 src_i += consumed_len; 464 src_i += consumed_len;
445 } 465 }
446 } else { 466 } else {
447 // Dot not preceeded by a slash, copy it literally. 467 // Dot not preceeded by a slash, copy it literally.
448 pathbuf[dest_i++] = pathbuf[src_i++]; 468 pathbuf[dest_i++] = pathbuf[src_i++];
(...skipping 347 matching lines...) Expand 10 before | Expand all | Expand 10 after
796 816
797 SourceDir GetCurrentOutputDir(const Scope* scope) { 817 SourceDir GetCurrentOutputDir(const Scope* scope) {
798 return GetOutputDirForSourceDirAsOutputFile( 818 return GetOutputDirForSourceDirAsOutputFile(
799 scope->settings(), scope->GetSourceDir()).AsSourceDir( 819 scope->settings(), scope->GetSourceDir()).AsSourceDir(
800 scope->settings()->build_settings()); 820 scope->settings()->build_settings());
801 } 821 }
802 822
803 SourceDir GetCurrentGenDir(const Scope* scope) { 823 SourceDir GetCurrentGenDir(const Scope* scope) {
804 return GetGenDirForSourceDir(scope->settings(), scope->GetSourceDir()); 824 return GetGenDirForSourceDir(scope->settings(), scope->GetSourceDir());
805 } 825 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698