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"
19 // Returns the extension, not including the dot, for the given file type on the
22 // Some targets make multiple files (like a .dll and an import library). This
23 // function returns the name of the file other targets should depend on and
24 // link to (so in this example, the import library).
25 const char* GetExtensionForOutputType(Target::OutputType type
,
26 Settings::TargetOS os
);
28 std::string
FilePathToUTF8(const base::FilePath::StringType
& str
);
29 inline std::string
FilePathToUTF8(const base::FilePath
& path
) {
30 return FilePathToUTF8(path
.value());
32 base::FilePath
UTF8ToFilePath(const base::StringPiece
& sp
);
34 // Extensions -----------------------------------------------------------------
36 // Returns the index of the extension (character after the last dot not after a
37 // slash). Returns std::string::npos if not found. Returns path.size() if the
38 // file ends with a dot.
39 size_t FindExtensionOffset(const std::string
& path
);
41 // Returns a string piece pointing into the input string identifying the
42 // extension. Note that the input pointer must outlive the output.
43 base::StringPiece
FindExtension(const std::string
* path
);
45 // Filename parts -------------------------------------------------------------
47 // Returns the offset of the character following the last slash, or
48 // 0 if no slash was found. Returns path.size() if the path ends with a slash.
49 // Note that the input pointer must outlive the output.
50 size_t FindFilenameOffset(const std::string
& path
);
52 // Returns a string piece pointing into the input string identifying the
53 // file name (following the last slash, including the extension). Note that the
54 // input pointer must outlive the output.
55 base::StringPiece
FindFilename(const std::string
* path
);
57 // Like FindFilename but does not include the extension.
58 base::StringPiece
FindFilenameNoExtension(const std::string
* path
);
60 // Removes everything after the last slash. The last slash, if any, will be
62 void RemoveFilename(std::string
* path
);
64 // Returns if the given character is a slash. This allows both slashes and
65 // backslashes for consistency between Posix and Windows (as opposed to
66 // FilePath::IsSeparator which is based on the current platform).
67 inline bool IsSlash(const char ch
) {
68 return ch
== '/' || ch
== '\\';
71 // Returns true if the given path ends with a slash.
72 bool EndsWithSlash(const std::string
& s
);
74 // Path parts -----------------------------------------------------------------
76 // Returns a string piece pointing into the input string identifying the
77 // directory name of the given path, including the last slash. Note that the
78 // input pointer must outlive the output.
79 base::StringPiece
FindDir(const std::string
* path
);
81 // Returns the substring identifying the last component of the dir, or the
82 // empty substring if none. For example "//foo/bar/" -> "bar".
83 base::StringPiece
FindLastDirComponent(const SourceDir
& dir
);
85 // Returns true if the given string is in the given output dir. This is pretty
86 // stupid and doesn't handle "." and "..", etc., it is designed for a sanity
87 // check to keep people from writing output files to the source directory
89 bool IsStringInOutputDir(const SourceDir
& output_dir
, const std::string
& str
);
91 // Verifies that the given string references a file inside of the given
92 // directory. This just uses IsStringInOutputDir above.
94 // The origin will be blamed in the error.
96 // If the file isn't in the dir, returns false and sets the error. Otherwise
97 // returns true and leaves the error untouched.
98 bool EnsureStringIsInOutputDir(const SourceDir
& output_dir
,
99 const std::string
& str
,
100 const ParseNode
* origin
,
103 // ----------------------------------------------------------------------------
105 // Returns true if the input string is absolute. Double-slashes at the
106 // beginning are treated as source-relative paths. On Windows, this handles
107 // paths of both the native format: "C:/foo" and ours "/C:/foo"
108 bool IsPathAbsolute(const base::StringPiece
& path
);
110 // Given an absolute path, checks to see if is it is inside the source root.
111 // If it is, fills a source-absolute path into the given output and returns
112 // true. If it isn't, clears the dest and returns false.
114 // The source_root should be a base::FilePath converted to UTF-8. On Windows,
115 // it should begin with a "C:/" rather than being our SourceFile's style
116 // ("/C:/"). The source root can end with a slash or not.
118 // Note that this does not attempt to normalize slashes in the output.
119 bool MakeAbsolutePathRelativeIfPossible(const base::StringPiece
& source_root
,
120 const base::StringPiece
& path
,
123 // Collapses "." and sequential "/"s and evaluates "..".
124 void NormalizePath(std::string
* path
);
126 // Converts slashes to backslashes for Windows. Keeps the string unchanged
127 // for other systems.
128 void ConvertPathToSystem(std::string
* path
);
130 // Takes a path, |input|, and makes it relative to the given directory
131 // |dest_dir|. Both inputs may be source-relative (e.g. begins with
132 // with "//") or may be absolute.
134 // If supplied, the |source_root| parameter is the absolute path to
135 // the source root and not end in a slash. Unless you know that the
136 // inputs are always source relative, this should be supplied.
137 std::string
RebasePath(
138 const std::string
& input
,
139 const SourceDir
& dest_dir
,
140 const base::StringPiece
& source_root
= base::StringPiece());
142 // Returns the given directory with no terminating slash at the end, such that
143 // appending a slash and more stuff will produce a valid path.
145 // If the directory refers to either the source or system root, we'll append
146 // a "." so this remains valid.
147 std::string
DirectoryWithNoLastSlash(const SourceDir
& dir
);
149 // Returns the "best" SourceDir representing the given path. If it's inside the
150 // given source_root, a source-relative directory will be returned (e.g.
151 // "//foo/bar.cc". If it's outside of the source root or the source root is
152 // empty, a system-absolute directory will be returned.
153 SourceDir
SourceDirForPath(const base::FilePath
& source_root
,
154 const base::FilePath
& path
);
156 // Like SourceDirForPath but returns the SourceDir representing the current
158 SourceDir
SourceDirForCurrentDirectory(const base::FilePath
& source_root
);
160 // Given the label of a toolchain and whether that toolchain is the default
161 // toolchain, returns the name of the subdirectory for that toolchain's
162 // output. This will be the empty string to indicate that the toolchain outputs
163 // go in the root build directory. Otherwise, the result will end in a slash.
164 std::string
GetOutputSubdirName(const Label
& toolchain_label
, bool is_default
);
166 // -----------------------------------------------------------------------------
168 // These functions return the various flavors of output and gen directories.
169 SourceDir
GetToolchainOutputDir(const Settings
* settings
);
170 SourceDir
GetToolchainOutputDir(const BuildSettings
* build_settings
,
171 const Label
& label
, bool is_default
);
173 SourceDir
GetToolchainGenDir(const Settings
* settings
);
174 OutputFile
GetToolchainGenDirAsOutputFile(const Settings
* settings
);
175 SourceDir
GetToolchainGenDir(const BuildSettings
* build_settings
,
176 const Label
& toolchain_label
,
179 SourceDir
GetOutputDirForSourceDir(const Settings
* settings
,
180 const SourceDir
& source_dir
);
181 OutputFile
GetOutputDirForSourceDirAsOutputFile(const Settings
* settings
,
182 const SourceDir
& source_dir
);
184 SourceDir
GetGenDirForSourceDir(const Settings
* settings
,
185 const SourceDir
& source_dir
);
186 OutputFile
GetGenDirForSourceDirAsOutputFile(const Settings
* settings
,
187 const SourceDir
& source_dir
);
189 SourceDir
GetTargetOutputDir(const Target
* target
);
190 OutputFile
GetTargetOutputDirAsOutputFile(const Target
* target
);
191 SourceDir
GetTargetGenDir(const Target
* target
);
192 OutputFile
GetTargetGenDirAsOutputFile(const Target
* target
);
194 SourceDir
GetCurrentOutputDir(const Scope
* scope
);
195 SourceDir
GetCurrentGenDir(const Scope
* scope
);
197 #endif // TOOLS_GN_FILESYSTEM_UTILS_H_