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/logging.h" | 9 #include "base/logging.h" |
10 #include "base/strings/string_util.h" | 10 #include "base/strings/string_util.h" |
11 #include "base/strings/utf_string_conversions.h" | 11 #include "base/strings/utf_string_conversions.h" |
12 #include "build/build_config.h" | 12 #include "build/build_config.h" |
13 #include "tools/gn/location.h" | 13 #include "tools/gn/location.h" |
| 14 #include "tools/gn/settings.h" |
14 #include "tools/gn/source_dir.h" | 15 #include "tools/gn/source_dir.h" |
15 | 16 |
16 namespace { | 17 namespace { |
17 | 18 |
18 enum DotDisposition { | 19 enum DotDisposition { |
19 // The given dot is just part of a filename and is not special. | 20 // The given dot is just part of a filename and is not special. |
20 NOT_A_DIRECTORY, | 21 NOT_A_DIRECTORY, |
21 | 22 |
22 // The given dot is the current directory. | 23 // The given dot is the current directory. |
23 DIRECTORY_CUR, | 24 DIRECTORY_CUR, |
(...skipping 515 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
539 | 540 |
540 // Append any remaining unique input. | 541 // Append any remaining unique input. |
541 ret.append(&input[common_prefix_len], input.size() - common_prefix_len); | 542 ret.append(&input[common_prefix_len], input.size() - common_prefix_len); |
542 | 543 |
543 // If the result is still empty, the paths are the same. | 544 // If the result is still empty, the paths are the same. |
544 if (ret.empty()) | 545 if (ret.empty()) |
545 ret.push_back('.'); | 546 ret.push_back('.'); |
546 | 547 |
547 return ret; | 548 return ret; |
548 } | 549 } |
| 550 |
| 551 std::string DirectoryWithNoLastSlash(const SourceDir& dir) { |
| 552 std::string ret; |
| 553 |
| 554 if (dir.value().empty()) { |
| 555 // Just keep input the same. |
| 556 } else if (dir.value() == "/") { |
| 557 ret.assign("/."); |
| 558 } else if (dir.value() == "//") { |
| 559 ret.assign("//."); |
| 560 } else { |
| 561 ret.assign(dir.value()); |
| 562 ret.resize(ret.size() - 1); |
| 563 } |
| 564 return ret; |
| 565 } |
| 566 |
| 567 SourceDir GetToolchainOutputDir(const Settings* settings) { |
| 568 const OutputFile& toolchain_subdir = settings->toolchain_output_subdir(); |
| 569 |
| 570 std::string result = settings->build_settings()->build_dir().value(); |
| 571 if (!toolchain_subdir.value().empty()) |
| 572 result.append(toolchain_subdir.value()); |
| 573 |
| 574 return SourceDir(SourceDir::SWAP_IN, &result); |
| 575 } |
| 576 |
| 577 SourceDir GetToolchainGenDir(const Settings* settings) { |
| 578 const OutputFile& toolchain_subdir = settings->toolchain_output_subdir(); |
| 579 |
| 580 std::string result = settings->build_settings()->build_dir().value(); |
| 581 if (!toolchain_subdir.value().empty()) |
| 582 result.append(toolchain_subdir.value()); |
| 583 |
| 584 result.append("gen/"); |
| 585 return SourceDir(SourceDir::SWAP_IN, &result); |
| 586 } |
| 587 |
| 588 SourceDir GetOutputDirForSourceDir(const Settings* settings, |
| 589 const SourceDir& source_dir) { |
| 590 SourceDir toolchain = GetToolchainOutputDir(settings); |
| 591 |
| 592 std::string ret; |
| 593 toolchain.SwapValue(&ret); |
| 594 ret.append("obj/"); |
| 595 |
| 596 // The source dir should be source-absolute, so we trim off the two leading |
| 597 // slashes to append to the toolchain object directory. |
| 598 DCHECK(source_dir.is_source_absolute()); |
| 599 ret.append(&source_dir.value()[2], source_dir.value().size() - 2); |
| 600 |
| 601 return SourceDir(SourceDir::SWAP_IN, &ret); |
| 602 } |
| 603 |
| 604 SourceDir GetGenDirForSourceDir(const Settings* settings, |
| 605 const SourceDir& source_dir) { |
| 606 SourceDir toolchain = GetToolchainGenDir(settings); |
| 607 |
| 608 std::string ret; |
| 609 toolchain.SwapValue(&ret); |
| 610 |
| 611 // The source dir should be source-absolute, so we trim off the two leading |
| 612 // slashes to append to the toolchain object directory. |
| 613 DCHECK(source_dir.is_source_absolute()); |
| 614 ret.append(&source_dir.value()[2], source_dir.value().size() - 2); |
| 615 |
| 616 return SourceDir(SourceDir::SWAP_IN, &ret); |
| 617 } |
| 618 |
| 619 SourceDir GetTargetOutputDir(const Target* target) { |
| 620 return GetOutputDirForSourceDir(target->settings(), target->label().dir()); |
| 621 } |
| 622 |
| 623 SourceDir GetTargetGenDir(const Target* target) { |
| 624 return GetGenDirForSourceDir(target->settings(), target->label().dir()); |
| 625 } |
| 626 |
| 627 SourceDir GetCurrentOutputDir(const Scope* scope) { |
| 628 return GetOutputDirForSourceDir(scope->settings(), scope->GetSourceDir()); |
| 629 } |
| 630 |
| 631 SourceDir GetCurrentGenDir(const Scope* scope) { |
| 632 return GetGenDirForSourceDir(scope->settings(), scope->GetSourceDir()); |
| 633 } |
OLD | NEW |