ccalvarin | ac69da0 | 2018-06-05 15:27:26 -0700 | [diff] [blame] | 1 | // Copyright 2018 The Bazel Authors. All rights reserved. |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | #ifndef BAZEL_SRC_MAIN_CPP_UTIL_PATH_PLATFORM_H_ |
| 15 | #define BAZEL_SRC_MAIN_CPP_UTIL_PATH_PLATFORM_H_ |
| 16 | |
| 17 | #include <string> |
| 18 | |
| 19 | namespace blaze_util { |
| 20 | |
| 21 | // Convert a path from Bazel internal form to underlying OS form. |
| 22 | // On Unixes this is an identity operation. |
| 23 | // On Windows, Bazel internal form is cygwin path, and underlying OS form |
| 24 | // is Windows path. |
| 25 | std::string ConvertPath(const std::string &path); |
| 26 | |
| 27 | // Converts `path` to a string that's safe to pass as path in a JVM flag. |
| 28 | // See https://github.com/bazelbuild/bazel/issues/2576 |
| 29 | std::string PathAsJvmFlag(const std::string &path); |
| 30 | |
| 31 | // Compares two absolute paths. Necessary because the same path can have |
| 32 | // multiple different names under msys2: "C:\foo\bar" or "C:/foo/bar" |
| 33 | // (Windows-style) and "/c/foo/bar" (msys2 style). Returns if the paths are |
| 34 | // equal. |
| 35 | bool CompareAbsolutePaths(const std::string &a, const std::string &b); |
| 36 | |
| 37 | // Split a path to dirname and basename parts. |
| 38 | std::pair<std::string, std::string> SplitPath(const std::string &path); |
| 39 | |
| 40 | bool IsDevNull(const char *path); |
| 41 | |
| 42 | // Returns true if `path` is the root directory or a Windows drive root. |
| 43 | bool IsRootDirectory(const std::string &path); |
| 44 | |
| 45 | // Returns true if `path` is absolute. |
| 46 | bool IsAbsolute(const std::string &path); |
| 47 | |
ccalvarin | 755278d | 2018-06-07 11:05:54 -0700 | [diff] [blame] | 48 | // Returns the given path in absolute form. Does not change paths that are |
| 49 | // already absolute. |
| 50 | // |
| 51 | // If called from working directory "/bar": |
| 52 | // MakeAbsolute("foo") --> "/bar/foo" |
| 53 | // MakeAbsolute("/foo") ---> "/foo" |
| 54 | // MakeAbsolute("C:/foo") ---> "C:/foo" |
| 55 | std::string MakeAbsolute(const std::string &path); |
| 56 | |
ccalvarin | d39de3a | 2018-06-19 08:52:08 -0700 | [diff] [blame] | 57 | // Returns the given path in absolute form, taking into account a possible |
| 58 | // starting environment variable on the windows platform, so that we can |
| 59 | // accept standard path variables like %USERPROFILE%. We do not support |
| 60 | // unix-style envvars here: recreating that logic is error-prone and not |
| 61 | // worthwhile, since they are less critical to standard paths as in Windows. |
| 62 | // |
| 63 | // MakeAbsolute("foo") in wd "/bar" --> "/bar/foo" |
| 64 | // MakeAbsolute("%USERPROFILE%/foo") --> "C:\Users\bazel-user\foo" |
| 65 | std::string MakeAbsoluteAndResolveWindowsEnvvars(const std::string &path); |
| 66 | |
ccalvarin | ac69da0 | 2018-06-05 15:27:26 -0700 | [diff] [blame] | 67 | // TODO(bazel-team) consider changing the path(_platform) header split to be a |
| 68 | // path.h and path_windows.h split, which would make it clearer what functions |
| 69 | // are included by an import statement. The downside to this gain in clarity |
| 70 | // is that this would add more complexity to the implementation file(s)? of |
| 71 | // path.h, which would have to have the platform-specific implementations. |
Loo Rong Jie | 4022bac | 2018-06-11 02:04:52 -0700 | [diff] [blame] | 72 | #if defined(_WIN32) || defined(__CYGWIN__) |
Yun Peng | 40f5a77 | 2018-06-25 05:35:50 -0700 | [diff] [blame] | 73 | bool IsDevNull(const wchar_t *path); |
| 74 | |
| 75 | bool IsAbsolute(const std::wstring &path); |
| 76 | |
ccalvarin | ac69da0 | 2018-06-05 15:27:26 -0700 | [diff] [blame] | 77 | const wchar_t *RemoveUncPrefixMaybe(const wchar_t *ptr); |
Yun Peng | 40f5a77 | 2018-06-25 05:35:50 -0700 | [diff] [blame] | 78 | |
ccalvarin | ac69da0 | 2018-06-05 15:27:26 -0700 | [diff] [blame] | 79 | void AddUncPrefixMaybe(std::wstring *path); |
| 80 | |
| 81 | std::pair<std::wstring, std::wstring> SplitPathW(const std::wstring &path); |
| 82 | |
| 83 | bool IsRootDirectoryW(const std::wstring &path); |
| 84 | |
Laszlo Csomor | ac54537 | 2018-09-14 05:32:42 -0700 | [diff] [blame] | 85 | namespace testing { |
Yun Peng | 4e578e6 | 2018-06-20 07:56:11 -0700 | [diff] [blame] | 86 | |
Laszlo Csomor | ac54537 | 2018-09-14 05:32:42 -0700 | [diff] [blame] | 87 | bool TestOnly_NormalizeWindowsPath(const std::string &path, |
| 88 | std::string *result); |
| 89 | |
Yun Peng | 4e578e6 | 2018-06-20 07:56:11 -0700 | [diff] [blame] | 90 | } |
ccalvarin | ac69da0 | 2018-06-05 15:27:26 -0700 | [diff] [blame] | 91 | |
| 92 | // Converts a UTF8-encoded `path` to a normalized, widechar Windows path. |
| 93 | // |
| 94 | // Returns true if conversion succeeded and sets the contents of `result` to it. |
| 95 | // |
| 96 | // The input `path` may be an absolute or relative Windows path. |
| 97 | // |
| 98 | // The returned path is normalized (see NormalizeWindowsPath). |
| 99 | // |
| 100 | // If `path` had a "\\?\" prefix then the function assumes it's already Windows |
| 101 | // style and converts it to wstring without any alterations. |
| 102 | // Otherwise `path` is normalized and converted to a Windows path and the result |
| 103 | // won't have a "\\?\" prefix even if it's longer than MAX_PATH (adding the |
| 104 | // prefix is the caller's responsibility). |
| 105 | // |
| 106 | // The method recognizes current-drive-relative Windows paths ("\foo") turning |
| 107 | // them into absolute paths ("c:\foo"). |
| 108 | bool AsWindowsPath(const std::string &path, std::wstring *result, |
| 109 | std::string *error); |
| 110 | |
Yun Peng | 4e578e6 | 2018-06-20 07:56:11 -0700 | [diff] [blame] | 111 | template <typename char_type> |
| 112 | bool AsWindowsPath(const std::basic_string<char_type> &path, |
| 113 | std::basic_string<char_type> *result, std::string *error); |
| 114 | |
| 115 | template <typename char_type> |
| 116 | bool AsWindowsPath(const char_type *path, std::basic_string<char_type> *result, |
| 117 | std::string *error) { |
| 118 | return AsWindowsPath(std::basic_string<char_type>(path), result, error); |
| 119 | } |
| 120 | |
| 121 | template <typename char_type> |
| 122 | bool AsAbsoluteWindowsPath(const std::basic_string<char_type> &path, |
| 123 | std::wstring *result, std::string *error); |
| 124 | |
| 125 | template <typename char_type> |
| 126 | bool AsAbsoluteWindowsPath(const char_type *path, std::wstring *result, |
| 127 | std::string *error) { |
| 128 | return AsAbsoluteWindowsPath(std::basic_string<char_type>(path), result, |
| 129 | error); |
| 130 | } |
| 131 | |
| 132 | // Explicit instantiate AsAbsoluteWindowsPath for char and wchar_t. |
| 133 | template bool AsAbsoluteWindowsPath<char>(const char *, std::wstring *, |
| 134 | std::string *); |
| 135 | template bool AsAbsoluteWindowsPath<wchar_t>(const wchar_t *, std::wstring *, |
| 136 | std::string *); |
ccalvarin | ac69da0 | 2018-06-05 15:27:26 -0700 | [diff] [blame] | 137 | |
| 138 | // Same as `AsWindowsPath`, but returns a lowercase 8dot3 style shortened path. |
| 139 | // Result will never have a UNC prefix, nor a trailing "/" or "\". |
| 140 | // Works also for non-existent paths; shortens as much of them as it can. |
| 141 | // Also works for non-existent drives. |
| 142 | bool AsShortWindowsPath(const std::string &path, std::string *result, |
| 143 | std::string *error); |
| 144 | |
| 145 | template <typename char_type> |
| 146 | bool IsPathSeparator(char_type ch); |
| 147 | |
| 148 | template <typename char_type> |
| 149 | bool HasDriveSpecifierPrefix(const char_type *ch); |
| 150 | |
Loo Rong Jie | 4022bac | 2018-06-11 02:04:52 -0700 | [diff] [blame] | 151 | #endif // defined(_WIN32) || defined(__CYGWIN__) |
ccalvarin | ac69da0 | 2018-06-05 15:27:26 -0700 | [diff] [blame] | 152 | } // namespace blaze_util |
| 153 | |
| 154 | #endif // BAZEL_SRC_MAIN_CPP_UTIL_PATH_PLATFORM_H_ |