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