Chromium Code Reviews| Index: tools/gn/filesystem_utils.cc |
| diff --git a/tools/gn/filesystem_utils.cc b/tools/gn/filesystem_utils.cc |
| index 56e472e7c8b88f1715129f5e7bbc059cb6056593..e7bc8b9622d9e7689e121897e2ac5f8333d5bcd8 100644 |
| --- a/tools/gn/filesystem_utils.cc |
| +++ b/tools/gn/filesystem_utils.cc |
| @@ -420,27 +420,6 @@ bool MakeAbsolutePathRelativeIfPossible(const base::StringPiece& source_root, |
| #endif |
| } |
| -std::string InvertDir(const SourceDir& path) { |
| - const std::string value = path.value(); |
| - if (value.empty()) |
| - return std::string(); |
| - |
| - DCHECK(value[0] == '/'); |
| - size_t begin_index = 1; |
| - |
| - // If the input begins with two slashes, skip over both (this is a |
| - // source-relative dir). These must be forward slashes only. |
| - if (value.size() > 1 && value[1] == '/') |
| - begin_index = 2; |
| - |
| - std::string ret; |
| - for (size_t i = begin_index; i < value.size(); i++) { |
| - if (IsSlash(value[i])) |
| - ret.append("../"); |
| - } |
| - return ret; |
| -} |
| - |
| void NormalizePath(std::string* path) { |
| char* pathbuf = path->empty() ? NULL : &(*path)[0]; |
| @@ -538,18 +517,13 @@ void ConvertPathToSystem(std::string* path) { |
| #endif |
| } |
| -std::string RebaseSourceAbsolutePath(const std::string& input, |
| - const SourceDir& dest_dir) { |
| - CHECK(input.size() >= 2 && input[0] == '/' && input[1] == '/') |
| - << "Input to rebase isn't source-absolute: " << input; |
| - CHECK(dest_dir.is_source_absolute()) |
| - << "Dir to rebase to isn't source-absolute: " << dest_dir.value(); |
| - |
| - const std::string& dest = dest_dir.value(); |
| +std::string MakeRelativePath(const std::string& input, |
| + const std::string& dest) { |
| + std::string ret; |
| // Skip the common prefixes of the source and dest as long as they end in |
| // a [back]slash. |
| - size_t common_prefix_len = 2; // The beginning two "//" are always the same. |
| + size_t common_prefix_len = 0; |
| size_t max_common_length = std::min(input.size(), dest.size()); |
| for (size_t i = common_prefix_len; i < max_common_length; i++) { |
| if (IsSlash(input[i]) && IsSlash(dest[i])) |
| @@ -559,7 +533,6 @@ std::string RebaseSourceAbsolutePath(const std::string& input, |
| } |
| // Invert the dest dir starting from the end of the common prefix. |
| - std::string ret; |
| for (size_t i = common_prefix_len; i < dest.size(); i++) { |
| if (IsSlash(dest[i])) |
| ret.append("../"); |
| @@ -575,6 +548,42 @@ std::string RebaseSourceAbsolutePath(const std::string& input, |
| return ret; |
| } |
| +std::string RebaseSourceAbsolutePath(const std::string& input, |
| + const SourceDir& dest_dir, |
| + const base::FilePath& source_root) { |
| + std::string ret; |
| + bool input_is_source_path = (input.size() >= 2 && |
| + input[0] == '/' && input[1] == '/'); |
| + |
| + if (!input_is_source_path || !dest_dir.is_source_absolute()) { |
| + std::string input_full; |
| + std::string dest_full; |
| + if (input_is_source_path) |
| + input_full = source_root.Append(input.substr(2)).value(); |
| + else |
| + input_full = input; |
| + if (dest_dir.is_source_absolute()) |
| + dest_full = source_root.Append(dest_dir.value().substr(2)).value(); |
| + else |
| + dest_full = dest_dir.value(); |
| + bool remove_slash = false; |
| + if (!EndsWithSlash(input_full)) { |
| + input_full.push_back('/'); |
| + remove_slash = true; |
| + } |
| + if (!EndsWithSlash(dest_full)) |
| + dest_full.push_back('/'); |
| + |
| + ret = MakeRelativePath(input_full, dest_full); |
| + if (remove_slash && ret.size() > 1) |
| + ret.pop_back(); |
|
brettw
2014/10/07 17:51:36
I get a compiler error that this function doesn't
zeuthen
2014/10/07 18:14:38
It's a C++-11-ism, sorry. I'll change it.
|
| + return ret; |
| + } |
| + |
| + ret = MakeRelativePath(input, dest_dir.value()); |
| + return ret; |
| +} |
| + |
| std::string DirectoryWithNoLastSlash(const SourceDir& dir) { |
| std::string ret; |
| @@ -697,6 +706,16 @@ OutputFile GetOutputDirForSourceDirAsOutputFile(const Settings* settings, |
| // slashes to append to the toolchain object directory. |
| result.value().append(&source_dir.value()[2], |
| source_dir.value().size() - 2); |
| + } else { |
| + // system-absolute |
| + const std::string& build_dir = |
| + settings->build_settings()->build_dir().value(); |
| + |
| + if (StartsWithASCII(source_dir.value(), build_dir, true)) { |
| + size_t build_dir_size = build_dir.size(); |
| + result.value().append(&source_dir.value()[build_dir_size], |
| + source_dir.value().size() - build_dir_size); |
| + } |
| } |
| return result; |
| } |