<webview>: Define API endpoints for webview.contextMenus API.
[chromium-blink-merge.git] / tools / gn / filesystem_utils.h
blobf720987050f27ab26acbb206a6b70d75ccf42dad
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
3 // found in the LICENSE file.
5 #ifndef TOOLS_GN_FILESYSTEM_UTILS_H_
6 #define TOOLS_GN_FILESYSTEM_UTILS_H_
8 #include <string>
10 #include "base/files/file_path.h"
11 #include "base/strings/string_piece.h"
12 #include "tools/gn/settings.h"
13 #include "tools/gn/target.h"
15 class Err;
16 class Location;
17 class Value;
19 enum SourceFileType {
20 SOURCE_UNKNOWN,
21 SOURCE_ASM,
22 SOURCE_C,
23 SOURCE_CC,
24 SOURCE_H,
25 SOURCE_M,
26 SOURCE_MM,
27 SOURCE_S,
28 SOURCE_RC,
31 SourceFileType GetSourceFileType(const SourceFile& file,
32 Settings::TargetOS os);
34 // Returns the extension, not including the dot, for the given file type on the
35 // given system.
37 // Some targets make multiple files (like a .dll and an import library). This
38 // function returns the name of the file other targets should depend on and
39 // link to (so in this example, the import library).
40 const char* GetExtensionForOutputType(Target::OutputType type,
41 Settings::TargetOS os);
43 std::string FilePathToUTF8(const base::FilePath::StringType& str);
44 inline std::string FilePathToUTF8(const base::FilePath& path) {
45 return FilePathToUTF8(path.value());
47 base::FilePath UTF8ToFilePath(const base::StringPiece& sp);
49 // Extensions -----------------------------------------------------------------
51 // Returns the index of the extension (character after the last dot not after a
52 // slash). Returns std::string::npos if not found. Returns path.size() if the
53 // file ends with a dot.
54 size_t FindExtensionOffset(const std::string& path);
56 // Returns a string piece pointing into the input string identifying the
57 // extension. Note that the input pointer must outlive the output.
58 base::StringPiece FindExtension(const std::string* path);
60 // Filename parts -------------------------------------------------------------
62 // Returns the offset of the character following the last slash, or
63 // 0 if no slash was found. Returns path.size() if the path ends with a slash.
64 // Note that the input pointer must outlive the output.
65 size_t FindFilenameOffset(const std::string& path);
67 // Returns a string piece pointing into the input string identifying the
68 // file name (following the last slash, including the extension). Note that the
69 // input pointer must outlive the output.
70 base::StringPiece FindFilename(const std::string* path);
72 // Like FindFilename but does not include the extension.
73 base::StringPiece FindFilenameNoExtension(const std::string* path);
75 // Removes everything after the last slash. The last slash, if any, will be
76 // preserved.
77 void RemoveFilename(std::string* path);
79 // Returns if the given character is a slash. This allows both slashes and
80 // backslashes for consistency between Posix and Windows (as opposed to
81 // FilePath::IsSeparator which is based on the current platform).
82 inline bool IsSlash(const char ch) {
83 return ch == '/' || ch == '\\';
86 // Returns true if the given path ends with a slash.
87 bool EndsWithSlash(const std::string& s);
89 // Path parts -----------------------------------------------------------------
91 // Returns a string piece pointing into the input string identifying the
92 // directory name of the given path, including the last slash. Note that the
93 // input pointer must outlive the output.
94 base::StringPiece FindDir(const std::string* path);
96 // Verifies that the given string references a file inside of the given
97 // directory. This is pretty stupid and doesn't handle "." and "..", etc.,
98 // it is designed for a sanity check to keep people from writing output files
99 // to the source directory accidentally.
101 // The originating value will be blamed in the error.
103 // If the file isn't in the dir, returns false and sets the error. Otherwise
104 // returns true and leaves the error untouched.
105 bool EnsureStringIsInOutputDir(const SourceDir& dir,
106 const std::string& str,
107 const Value& originating,
108 Err* err);
110 // ----------------------------------------------------------------------------
112 // Returns true if the input string is absolute. Double-slashes at the
113 // beginning are treated as source-relative paths. On Windows, this handles
114 // paths of both the native format: "C:/foo" and ours "/C:/foo"
115 bool IsPathAbsolute(const base::StringPiece& path);
117 // Given an absolute path, checks to see if is it is inside the source root.
118 // If it is, fills a source-absolute path into the given output and returns
119 // true. If it isn't, clears the dest and returns false.
121 // The source_root should be a base::FilePath converted to UTF-8. On Windows,
122 // it should begin with a "C:/" rather than being our SourceFile's style
123 // ("/C:/"). The source root can end with a slash or not.
125 // Note that this does not attempt to normalize slashes in the output.
126 bool MakeAbsolutePathRelativeIfPossible(const base::StringPiece& source_root,
127 const base::StringPiece& path,
128 std::string* dest);
130 // Converts a directory to its inverse (e.g. "/foo/bar/" -> "../../").
131 // This will be the empty string for the root directories ("/" and "//"), and
132 // in all other cases, this is guaranteed to end in a slash.
133 std::string InvertDir(const SourceDir& dir);
135 // Collapses "." and sequential "/"s and evaluates "..".
136 void NormalizePath(std::string* path);
138 // Converts slashes to backslashes for Windows. Keeps the string unchanged
139 // for other systems.
140 void ConvertPathToSystem(std::string* path);
141 std::string PathToSystem(const std::string& path);
143 // Takes a source-absolute path (must begin with "//") and makes it relative
144 // to the given directory, which also must be source-absolute.
145 std::string RebaseSourceAbsolutePath(const std::string& input,
146 const SourceDir& dest_dir);
148 // Returns the given directory with no terminating slash at the end, such that
149 // appending a slash and more stuff will produce a valid path.
151 // If the directory refers to either the source or system root, we'll append
152 // a "." so this remains valid.
153 std::string DirectoryWithNoLastSlash(const SourceDir& dir);
155 // Returns the "best" SourceDir representing the given path. If it's inside the
156 // given source_root, a source-relative directory will be returned (e.g.
157 // "//foo/bar.cc". If it's outside of the source root, a system-absolute
158 // directory will be returned.
159 SourceDir SourceDirForPath(const base::FilePath& source_root,
160 const base::FilePath& path);
162 // Like SourceDirForPath but returns the SourceDir representing the current
163 // directory.
164 SourceDir SourceDirForCurrentDirectory(const base::FilePath& source_root);
166 // -----------------------------------------------------------------------------
168 // These functions return the various flavors of output and gen directories.
169 SourceDir GetToolchainOutputDir(const Settings* settings);
170 SourceDir GetToolchainGenDir(const Settings* settings);
171 SourceDir GetOutputDirForSourceDir(const Settings* settings,
172 const SourceDir& source_dir);
173 SourceDir GetGenDirForSourceDir(const Settings* settings,
174 const SourceDir& source_dir);
175 SourceDir GetTargetOutputDir(const Target* target);
176 SourceDir GetTargetGenDir(const Target* target);
177 SourceDir GetCurrentOutputDir(const Scope* scope);
178 SourceDir GetCurrentGenDir(const Scope* scope);
180 #endif // TOOLS_GN_FILESYSTEM_UTILS_H_