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_
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"
29 SOURCE_O
, // Object files can be inputs, too. Also counts .obj.
32 SourceFileType
GetSourceFileType(const SourceFile
& file
);
34 // Returns the extension, not including the dot, for the given file type on the
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
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 // Returns the substring identifying the last component of the dir, or the
97 // empty substring if none. For example "//foo/bar/" -> "bar".
98 base::StringPiece
FindLastDirComponent(const SourceDir
& dir
);
100 // Verifies that the given string references a file inside of the given
101 // directory. This is pretty stupid and doesn't handle "." and "..", etc.,
102 // it is designed for a sanity check to keep people from writing output files
103 // to the source directory accidentally.
105 // The originating value will be blamed in the error.
107 // If the file isn't in the dir, returns false and sets the error. Otherwise
108 // returns true and leaves the error untouched.
109 bool EnsureStringIsInOutputDir(const SourceDir
& dir
,
110 const std::string
& str
,
111 const Value
& originating
,
114 // ----------------------------------------------------------------------------
116 // Returns true if the input string is absolute. Double-slashes at the
117 // beginning are treated as source-relative paths. On Windows, this handles
118 // paths of both the native format: "C:/foo" and ours "/C:/foo"
119 bool IsPathAbsolute(const base::StringPiece
& path
);
121 // Given an absolute path, checks to see if is it is inside the source root.
122 // If it is, fills a source-absolute path into the given output and returns
123 // true. If it isn't, clears the dest and returns false.
125 // The source_root should be a base::FilePath converted to UTF-8. On Windows,
126 // it should begin with a "C:/" rather than being our SourceFile's style
127 // ("/C:/"). The source root can end with a slash or not.
129 // Note that this does not attempt to normalize slashes in the output.
130 bool MakeAbsolutePathRelativeIfPossible(const base::StringPiece
& source_root
,
131 const base::StringPiece
& path
,
134 // Converts a directory to its inverse (e.g. "/foo/bar/" -> "../../").
135 // This will be the empty string for the root directories ("/" and "//"), and
136 // in all other cases, this is guaranteed to end in a slash.
137 std::string
InvertDir(const SourceDir
& dir
);
139 // Collapses "." and sequential "/"s and evaluates "..".
140 void NormalizePath(std::string
* path
);
142 // Converts slashes to backslashes for Windows. Keeps the string unchanged
143 // for other systems.
144 void ConvertPathToSystem(std::string
* path
);
146 // Takes a source-absolute path (must begin with "//") and makes it relative
147 // to the given directory, which also must be source-absolute.
148 std::string
RebaseSourceAbsolutePath(const std::string
& input
,
149 const SourceDir
& dest_dir
);
151 // Returns the given directory with no terminating slash at the end, such that
152 // appending a slash and more stuff will produce a valid path.
154 // If the directory refers to either the source or system root, we'll append
155 // a "." so this remains valid.
156 std::string
DirectoryWithNoLastSlash(const SourceDir
& dir
);
158 // Returns the "best" SourceDir representing the given path. If it's inside the
159 // given source_root, a source-relative directory will be returned (e.g.
160 // "//foo/bar.cc". If it's outside of the source root, a system-absolute
161 // directory will be returned.
162 SourceDir
SourceDirForPath(const base::FilePath
& source_root
,
163 const base::FilePath
& path
);
165 // Like SourceDirForPath but returns the SourceDir representing the current
167 SourceDir
SourceDirForCurrentDirectory(const base::FilePath
& source_root
);
169 // Given the label of a toolchain and whether that toolchain is the default
170 // toolchain, returns the name of the subdirectory for that toolchain's
171 // output. This will be the empty string to indicate that the toolchain outputs
172 // go in the root build directory. Otherwise, the result will end in a slash.
173 std::string
GetOutputSubdirName(const Label
& toolchain_label
, bool is_default
);
175 // -----------------------------------------------------------------------------
177 // These functions return the various flavors of output and gen directories.
178 SourceDir
GetToolchainOutputDir(const Settings
* settings
);
179 SourceDir
GetToolchainOutputDir(const BuildSettings
* build_settings
,
180 const Label
& label
, bool is_default
);
181 SourceDir
GetToolchainGenDir(const Settings
* settings
);
182 SourceDir
GetToolchainGenDir(const BuildSettings
* build_settings
,
183 const Label
& toolchain_label
, bool is_default
);
184 SourceDir
GetOutputDirForSourceDir(const Settings
* settings
,
185 const SourceDir
& source_dir
);
186 SourceDir
GetGenDirForSourceDir(const Settings
* settings
,
187 const SourceDir
& source_dir
);
188 SourceDir
GetTargetOutputDir(const Target
* target
);
189 SourceDir
GetTargetGenDir(const Target
* target
);
190 SourceDir
GetCurrentOutputDir(const Scope
* scope
);
191 SourceDir
GetCurrentGenDir(const Scope
* scope
);
193 #endif // TOOLS_GN_FILESYSTEM_UTILS_H_