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 // Verifies that the given string references a file inside of the given
86 // directory. This is pretty stupid and doesn't handle "." and "..", etc.,
87 // it is designed for a sanity check to keep people from writing output files
88 // to the source directory accidentally.
90 // The origin will be blamed in the error.
92 // If the file isn't in the dir, returns false and sets the error. Otherwise
93 // returns true and leaves the error untouched.
94 bool EnsureStringIsInOutputDir(const SourceDir
& dir
,
95 const std::string
& str
,
96 const ParseNode
* origin
,
99 // ----------------------------------------------------------------------------
101 // Returns true if the input string is absolute. Double-slashes at the
102 // beginning are treated as source-relative paths. On Windows, this handles
103 // paths of both the native format: "C:/foo" and ours "/C:/foo"
104 bool IsPathAbsolute(const base::StringPiece
& path
);
106 // Given an absolute path, checks to see if is it is inside the source root.
107 // If it is, fills a source-absolute path into the given output and returns
108 // true. If it isn't, clears the dest and returns false.
110 // The source_root should be a base::FilePath converted to UTF-8. On Windows,
111 // it should begin with a "C:/" rather than being our SourceFile's style
112 // ("/C:/"). The source root can end with a slash or not.
114 // Note that this does not attempt to normalize slashes in the output.
115 bool MakeAbsolutePathRelativeIfPossible(const base::StringPiece
& source_root
,
116 const base::StringPiece
& path
,
119 // Collapses "." and sequential "/"s and evaluates "..".
120 void NormalizePath(std::string
* path
);
122 // Converts slashes to backslashes for Windows. Keeps the string unchanged
123 // for other systems.
124 void ConvertPathToSystem(std::string
* path
);
126 // Takes a path, |input|, and makes it relative to the given directory
127 // |dest_dir|. Both inputs may be source-relative (e.g. begins with
128 // with "//") or may be absolute.
130 // If supplied, the |source_root| parameter is the absolute path to
131 // the source root and not end in a slash. Unless you know that the
132 // inputs are always source relative, this should be supplied.
133 std::string
RebasePath(
134 const std::string
& input
,
135 const SourceDir
& dest_dir
,
136 const base::StringPiece
& source_root
= base::StringPiece());
138 // Returns the given directory with no terminating slash at the end, such that
139 // appending a slash and more stuff will produce a valid path.
141 // If the directory refers to either the source or system root, we'll append
142 // a "." so this remains valid.
143 std::string
DirectoryWithNoLastSlash(const SourceDir
& dir
);
145 // Returns the "best" SourceDir representing the given path. If it's inside the
146 // given source_root, a source-relative directory will be returned (e.g.
147 // "//foo/bar.cc". If it's outside of the source root or the source root is
148 // empty, a system-absolute directory will be returned.
149 SourceDir
SourceDirForPath(const base::FilePath
& source_root
,
150 const base::FilePath
& path
);
152 // Like SourceDirForPath but returns the SourceDir representing the current
154 SourceDir
SourceDirForCurrentDirectory(const base::FilePath
& source_root
);
156 // Given the label of a toolchain and whether that toolchain is the default
157 // toolchain, returns the name of the subdirectory for that toolchain's
158 // output. This will be the empty string to indicate that the toolchain outputs
159 // go in the root build directory. Otherwise, the result will end in a slash.
160 std::string
GetOutputSubdirName(const Label
& toolchain_label
, bool is_default
);
162 // -----------------------------------------------------------------------------
164 // These functions return the various flavors of output and gen directories.
165 SourceDir
GetToolchainOutputDir(const Settings
* settings
);
166 SourceDir
GetToolchainOutputDir(const BuildSettings
* build_settings
,
167 const Label
& label
, bool is_default
);
169 SourceDir
GetToolchainGenDir(const Settings
* settings
);
170 OutputFile
GetToolchainGenDirAsOutputFile(const Settings
* settings
);
171 SourceDir
GetToolchainGenDir(const BuildSettings
* build_settings
,
172 const Label
& toolchain_label
,
175 SourceDir
GetOutputDirForSourceDir(const Settings
* settings
,
176 const SourceDir
& source_dir
);
177 OutputFile
GetOutputDirForSourceDirAsOutputFile(const Settings
* settings
,
178 const SourceDir
& source_dir
);
180 SourceDir
GetGenDirForSourceDir(const Settings
* settings
,
181 const SourceDir
& source_dir
);
182 OutputFile
GetGenDirForSourceDirAsOutputFile(const Settings
* settings
,
183 const SourceDir
& source_dir
);
185 SourceDir
GetTargetOutputDir(const Target
* target
);
186 OutputFile
GetTargetOutputDirAsOutputFile(const Target
* target
);
187 SourceDir
GetTargetGenDir(const Target
* target
);
188 OutputFile
GetTargetGenDirAsOutputFile(const Target
* target
);
190 SourceDir
GetCurrentOutputDir(const Scope
* scope
);
191 SourceDir
GetCurrentGenDir(const Scope
* scope
);
193 #endif // TOOLS_GN_FILESYSTEM_UTILS_H_