| 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/file_util.h" | 9 #include "base/file_util.h" |
| 10 #include "base/logging.h" | 10 #include "base/logging.h" |
| (...skipping 146 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 157 return ::CompareString(LOCALE_USER_DEFAULT, LINGUISTIC_IGNORECASE, | 157 return ::CompareString(LOCALE_USER_DEFAULT, LINGUISTIC_IGNORECASE, |
| 158 a.c_str(), -1, b.c_str(), -1) == CSTR_EQUAL; | 158 a.c_str(), -1, b.c_str(), -1) == CSTR_EQUAL; |
| 159 #else | 159 #else |
| 160 // Assume case-sensitive filesystems on non-Windows. | 160 // Assume case-sensitive filesystems on non-Windows. |
| 161 return a == b; | 161 return a == b; |
| 162 #endif | 162 #endif |
| 163 } | 163 } |
| 164 | 164 |
| 165 } // namespace | 165 } // namespace |
| 166 | 166 |
| 167 SourceFileType GetSourceFileType(const SourceFile& file) { | |
| 168 base::StringPiece extension = FindExtension(&file.value()); | |
| 169 if (extension == "cc" || extension == "cpp" || extension == "cxx") | |
| 170 return SOURCE_CC; | |
| 171 if (extension == "h") | |
| 172 return SOURCE_H; | |
| 173 if (extension == "c") | |
| 174 return SOURCE_C; | |
| 175 if (extension == "m") | |
| 176 return SOURCE_M; | |
| 177 if (extension == "mm") | |
| 178 return SOURCE_MM; | |
| 179 if (extension == "rc") | |
| 180 return SOURCE_RC; | |
| 181 if (extension == "S" || extension == "s") | |
| 182 return SOURCE_S; | |
| 183 if (extension == "o" || extension == "obj") | |
| 184 return SOURCE_O; | |
| 185 | |
| 186 return SOURCE_UNKNOWN; | |
| 187 } | |
| 188 | |
| 189 const char* GetExtensionForOutputType(Target::OutputType type, | 167 const char* GetExtensionForOutputType(Target::OutputType type, |
| 190 Settings::TargetOS os) { | 168 Settings::TargetOS os) { |
| 191 switch (os) { | 169 switch (os) { |
| 192 case Settings::MAC: | 170 case Settings::MAC: |
| 193 switch (type) { | 171 switch (type) { |
| 194 case Target::EXECUTABLE: | 172 case Target::EXECUTABLE: |
| 195 return ""; | 173 return ""; |
| 196 case Target::SHARED_LIBRARY: | 174 case Target::SHARED_LIBRARY: |
| 197 return "dylib"; | 175 return "dylib"; |
| 198 case Target::STATIC_LIBRARY: | 176 case Target::STATIC_LIBRARY: |
| (...skipping 127 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 326 | 304 |
| 327 for (; cur >= 0; cur--) { | 305 for (; cur >= 0; cur--) { |
| 328 if (dir_string[cur] == '/') | 306 if (dir_string[cur] == '/') |
| 329 return base::StringPiece(&dir_string[cur + 1], end - cur - 1); | 307 return base::StringPiece(&dir_string[cur + 1], end - cur - 1); |
| 330 } | 308 } |
| 331 return base::StringPiece(&dir_string[0], end); | 309 return base::StringPiece(&dir_string[0], end); |
| 332 } | 310 } |
| 333 | 311 |
| 334 bool EnsureStringIsInOutputDir(const SourceDir& dir, | 312 bool EnsureStringIsInOutputDir(const SourceDir& dir, |
| 335 const std::string& str, | 313 const std::string& str, |
| 336 const Value& originating, | 314 const ParseNode* origin, |
| 337 Err* err) { | 315 Err* err) { |
| 338 // This check will be wrong for all proper prefixes "e.g. "/output" will | 316 // This check will be wrong for all proper prefixes "e.g. "/output" will |
| 339 // match "/out" but we don't really care since this is just a sanity check. | 317 // match "/out" but we don't really care since this is just a sanity check. |
| 340 const std::string& dir_str = dir.value(); | 318 const std::string& dir_str = dir.value(); |
| 341 if (str.compare(0, dir_str.length(), dir_str) == 0) | 319 if (str.compare(0, dir_str.length(), dir_str) == 0) |
| 342 return true; // Output directory is hardcoded. | 320 return true; // Output directory is hardcoded. |
| 343 | 321 |
| 344 *err = Err(originating, "File is not inside output directory.", | 322 *err = Err(origin, "File is not inside output directory.", |
| 345 "The given file should be in the output directory. Normally you would " | 323 "The given file should be in the output directory. Normally you would " |
| 346 "specify\n\"$target_out_dir/foo\" or " | 324 "specify\n\"$target_out_dir/foo\" or " |
| 347 "\"$target_gen_dir/foo\". I interpreted this as\n\"" | 325 "\"$target_gen_dir/foo\". I interpreted this as\n\"" |
| 348 + str + "\"."); | 326 + str + "\"."); |
| 349 return false; | 327 return false; |
| 350 } | 328 } |
| 351 | 329 |
| 352 bool IsPathAbsolute(const base::StringPiece& path) { | 330 bool IsPathAbsolute(const base::StringPiece& path) { |
| 353 if (path.empty()) | 331 if (path.empty()) |
| 354 return false; | 332 return false; |
| (...skipping 311 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 666 // The default toolchain has no subdir. | 644 // The default toolchain has no subdir. |
| 667 if (is_default) | 645 if (is_default) |
| 668 return std::string(); | 646 return std::string(); |
| 669 | 647 |
| 670 // For now just assume the toolchain name is always a valid dir name. We may | 648 // For now just assume the toolchain name is always a valid dir name. We may |
| 671 // want to clean up the in the future. | 649 // want to clean up the in the future. |
| 672 return toolchain_label.name() + "/"; | 650 return toolchain_label.name() + "/"; |
| 673 } | 651 } |
| 674 | 652 |
| 675 SourceDir GetToolchainOutputDir(const Settings* settings) { | 653 SourceDir GetToolchainOutputDir(const Settings* settings) { |
| 676 const OutputFile& toolchain_subdir = settings->toolchain_output_subdir(); | 654 return settings->toolchain_output_subdir().AsSourceDir( |
| 677 | 655 settings->build_settings()); |
| 678 std::string result = settings->build_settings()->build_dir().value(); | |
| 679 if (!toolchain_subdir.value().empty()) | |
| 680 result.append(toolchain_subdir.value()); | |
| 681 | |
| 682 return SourceDir(SourceDir::SWAP_IN, &result); | |
| 683 } | 656 } |
| 684 | 657 |
| 685 SourceDir GetToolchainOutputDir(const BuildSettings* build_settings, | 658 SourceDir GetToolchainOutputDir(const BuildSettings* build_settings, |
| 686 const Label& toolchain_label, bool is_default) { | 659 const Label& toolchain_label, bool is_default) { |
| 687 std::string result = build_settings->build_dir().value(); | 660 std::string result = build_settings->build_dir().value(); |
| 688 result.append(GetOutputSubdirName(toolchain_label, is_default)); | 661 result.append(GetOutputSubdirName(toolchain_label, is_default)); |
| 689 return SourceDir(SourceDir::SWAP_IN, &result); | 662 return SourceDir(SourceDir::SWAP_IN, &result); |
| 690 } | 663 } |
| 691 | 664 |
| 692 SourceDir GetToolchainGenDir(const Settings* settings) { | 665 SourceDir GetToolchainGenDir(const Settings* settings) { |
| 693 const OutputFile& toolchain_subdir = settings->toolchain_output_subdir(); | 666 return GetToolchainGenDirAsOutputFile(settings).AsSourceDir( |
| 667 settings->build_settings()); |
| 668 } |
| 694 | 669 |
| 695 std::string result = settings->build_settings()->build_dir().value(); | 670 OutputFile GetToolchainGenDirAsOutputFile(const Settings* settings) { |
| 696 if (!toolchain_subdir.value().empty()) | 671 OutputFile result(settings->toolchain_output_subdir()); |
| 697 result.append(toolchain_subdir.value()); | 672 result.value().append("gen/"); |
| 698 | 673 return result; |
| 699 result.append("gen/"); | |
| 700 return SourceDir(SourceDir::SWAP_IN, &result); | |
| 701 } | 674 } |
| 702 | 675 |
| 703 SourceDir GetToolchainGenDir(const BuildSettings* build_settings, | 676 SourceDir GetToolchainGenDir(const BuildSettings* build_settings, |
| 704 const Label& toolchain_label, bool is_default) { | 677 const Label& toolchain_label, bool is_default) { |
| 705 std::string result = GetToolchainOutputDir( | 678 std::string result = GetToolchainOutputDir( |
| 706 build_settings, toolchain_label, is_default).value(); | 679 build_settings, toolchain_label, is_default).value(); |
| 707 result.append("gen/"); | 680 result.append("gen/"); |
| 708 return SourceDir(SourceDir::SWAP_IN, &result); | 681 return SourceDir(SourceDir::SWAP_IN, &result); |
| 709 } | 682 } |
| 710 | 683 |
| 711 SourceDir GetOutputDirForSourceDir(const Settings* settings, | 684 SourceDir GetOutputDirForSourceDir(const Settings* settings, |
| 712 const SourceDir& source_dir) { | 685 const SourceDir& source_dir) { |
| 713 SourceDir toolchain = GetToolchainOutputDir(settings); | 686 return GetOutputDirForSourceDirAsOutputFile(settings, source_dir).AsSourceDir( |
| 687 settings->build_settings()); |
| 688 } |
| 714 | 689 |
| 715 std::string ret; | 690 OutputFile GetOutputDirForSourceDirAsOutputFile(const Settings* settings, |
| 716 toolchain.SwapValue(&ret); | 691 const SourceDir& source_dir) { |
| 717 ret.append("obj/"); | 692 OutputFile result = settings->toolchain_output_subdir(); |
| 693 result.value().append("obj/"); |
| 718 | 694 |
| 719 if (source_dir.is_source_absolute()) { | 695 if (source_dir.is_source_absolute()) { |
| 720 // The source dir is source-absolute, so we trim off the two leading | 696 // The source dir is source-absolute, so we trim off the two leading |
| 721 // slashes to append to the toolchain object directory. | 697 // slashes to append to the toolchain object directory. |
| 722 ret.append(&source_dir.value()[2], source_dir.value().size() - 2); | 698 result.value().append(&source_dir.value()[2], |
| 699 source_dir.value().size() - 2); |
| 723 } | 700 } |
| 724 // (Put system-absolute stuff in the root obj directory.) | 701 return result; |
| 725 | |
| 726 return SourceDir(SourceDir::SWAP_IN, &ret); | |
| 727 } | 702 } |
| 728 | 703 |
| 729 SourceDir GetGenDirForSourceDir(const Settings* settings, | 704 SourceDir GetGenDirForSourceDir(const Settings* settings, |
| 730 const SourceDir& source_dir) { | 705 const SourceDir& source_dir) { |
| 731 SourceDir toolchain = GetToolchainGenDir(settings); | 706 return GetGenDirForSourceDirAsOutputFile(settings, source_dir).AsSourceDir( |
| 707 settings->build_settings()); |
| 708 } |
| 732 | 709 |
| 733 std::string ret; | 710 OutputFile GetGenDirForSourceDirAsOutputFile(const Settings* settings, |
| 734 toolchain.SwapValue(&ret); | 711 const SourceDir& source_dir) { |
| 712 OutputFile result = GetToolchainGenDirAsOutputFile(settings); |
| 735 | 713 |
| 736 if (source_dir.is_source_absolute()) { | 714 if (source_dir.is_source_absolute()) { |
| 737 // The source dir should be source-absolute, so we trim off the two leading | 715 // The source dir should be source-absolute, so we trim off the two leading |
| 738 // slashes to append to the toolchain object directory. | 716 // slashes to append to the toolchain object directory. |
| 739 DCHECK(source_dir.is_source_absolute()); | 717 DCHECK(source_dir.is_source_absolute()); |
| 740 ret.append(&source_dir.value()[2], source_dir.value().size() - 2); | 718 result.value().append(&source_dir.value()[2], |
| 719 source_dir.value().size() - 2); |
| 741 } | 720 } |
| 742 // (Put system-absolute stuff in the root gen directory.) | 721 return result; |
| 743 | |
| 744 return SourceDir(SourceDir::SWAP_IN, &ret); | |
| 745 } | 722 } |
| 746 | 723 |
| 747 SourceDir GetTargetOutputDir(const Target* target) { | 724 SourceDir GetTargetOutputDir(const Target* target) { |
| 748 return GetOutputDirForSourceDir(target->settings(), target->label().dir()); | 725 return GetOutputDirForSourceDirAsOutputFile( |
| 726 target->settings(), target->label().dir()).AsSourceDir( |
| 727 target->settings()->build_settings()); |
| 728 } |
| 729 |
| 730 OutputFile GetTargetOutputDirAsOutputFile(const Target* target) { |
| 731 return GetOutputDirForSourceDirAsOutputFile( |
| 732 target->settings(), target->label().dir()); |
| 749 } | 733 } |
| 750 | 734 |
| 751 SourceDir GetTargetGenDir(const Target* target) { | 735 SourceDir GetTargetGenDir(const Target* target) { |
| 752 return GetGenDirForSourceDir(target->settings(), target->label().dir()); | 736 return GetTargetGenDirAsOutputFile(target).AsSourceDir( |
| 737 target->settings()->build_settings()); |
| 738 } |
| 739 |
| 740 OutputFile GetTargetGenDirAsOutputFile(const Target* target) { |
| 741 return GetGenDirForSourceDirAsOutputFile( |
| 742 target->settings(), target->label().dir()); |
| 753 } | 743 } |
| 754 | 744 |
| 755 SourceDir GetCurrentOutputDir(const Scope* scope) { | 745 SourceDir GetCurrentOutputDir(const Scope* scope) { |
| 756 return GetOutputDirForSourceDir(scope->settings(), scope->GetSourceDir()); | 746 return GetOutputDirForSourceDirAsOutputFile( |
| 747 scope->settings(), scope->GetSourceDir()).AsSourceDir( |
| 748 scope->settings()->build_settings()); |
| 757 } | 749 } |
| 758 | 750 |
| 759 SourceDir GetCurrentGenDir(const Scope* scope) { | 751 SourceDir GetCurrentGenDir(const Scope* scope) { |
| 760 return GetGenDirForSourceDir(scope->settings(), scope->GetSourceDir()); | 752 return GetGenDirForSourceDir(scope->settings(), scope->GetSourceDir()); |
| 761 } | 753 } |
| OLD | NEW |