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 15 class Err; 16 class Location; 17 class Value; 18 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, 29 }; 30 31 SourceFileType GetSourceFileType(const SourceFile& file, 32 Settings::TargetOS os); 33 34 // Returns the extension, not including the dot, for the given file type on the 35 // given system. 36 // 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); 42 43 std::string FilePathToUTF8(const base::FilePath::StringType& str); 44 inline std::string FilePathToUTF8(const base::FilePath& path) { 45 return FilePathToUTF8(path.value()); 46 } 47 base::FilePath UTF8ToFilePath(const base::StringPiece& sp); 48 49 // Extensions ----------------------------------------------------------------- 50 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); 55 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); 59 60 // Filename parts ------------------------------------------------------------- 61 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); 66 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); 71 72 // Like FindFilename but does not include the extension. 73 base::StringPiece FindFilenameNoExtension(const std::string* path); 74 75 // Removes everything after the last slash. The last slash, if any, will be 76 // preserved. 77 void RemoveFilename(std::string* path); 78 79 // Returns true if the given path ends with a slash. 80 bool EndsWithSlash(const std::string& s); 81 82 // Path parts ----------------------------------------------------------------- 83 84 // Returns a string piece pointing into the input string identifying the 85 // directory name of the given path, including the last slash. Note that the 86 // input pointer must outlive the output. 87 base::StringPiece FindDir(const std::string* path); 88 89 // Verifies that the given string references a file inside of the given 90 // directory. This is pretty stupid and doesn't handle "." and "..", etc., 91 // it is designed for a sanity check to keep people from writing output files 92 // to the source directory accidentally. 93 // 94 // The originating value will be blamed in the error. 95 // 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& dir, 99 const std::string& str, 100 const Value& originating, 101 Err* err); 102 103 // ---------------------------------------------------------------------------- 104 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); 109 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. 113 // 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. 117 // 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, 121 std::string* dest); 122 123 // Converts a directory to its inverse (e.g. "/foo/bar/" -> "../../"). 124 // This will be the empty string for the root directories ("/" and "//"), and 125 // in all other cases, this is guaranteed to end in a slash. 126 std::string InvertDir(const SourceDir& dir); 127 128 // Collapses "." and sequential "/"s and evaluates "..". 129 void NormalizePath(std::string* path); 130 131 // Converts slashes to backslashes for Windows. Keeps the string unchanged 132 // for other systems. 133 void ConvertPathToSystem(std::string* path); 134 std::string PathToSystem(const std::string& path); 135 136 // Takes a source-absolute path (must begin with "//") and makes it relative 137 // to the given directory, which also must be source-absolute. 138 std::string RebaseSourceAbsolutePath(const std::string& input, 139 const SourceDir& dest_dir); 140 141 // Returns the given directory with no terminating slash at the end, such that 142 // appending a slash and more stuff will produce a valid path. 143 // 144 // If the directory refers to either the source or system root, we'll append 145 // a "." so this remains valid. 146 std::string DirectoryWithNoLastSlash(const SourceDir& dir); 147 148 // ----------------------------------------------------------------------------- 149 150 // These functions return the various flavors of output and gen directories. 151 SourceDir GetToolchainOutputDir(const Settings* settings); 152 SourceDir GetToolchainGenDir(const Settings* settings); 153 SourceDir GetOutputDirForSourceDir(const Settings* settings, 154 const SourceDir& source_dir); 155 SourceDir GetGenDirForSourceDir(const Settings* settings, 156 const SourceDir& source_dir); 157 SourceDir GetTargetOutputDir(const Target* target); 158 SourceDir GetTargetGenDir(const Target* target); 159 SourceDir GetCurrentOutputDir(const Scope* scope); 160 SourceDir GetCurrentGenDir(const Scope* scope); 161 162 #endif // TOOLS_GN_FILESYSTEM_UTILS_H_ 163