filesystem_utils.h revision 116680a4aac90f2aa7413d9095a592090648e557
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 SOURCE_O, // Object files can be inputs, too. Also counts .obj. 30}; 31 32SourceFileType GetSourceFileType(const SourceFile& file); 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). 40const char* GetExtensionForOutputType(Target::OutputType type, 41 Settings::TargetOS os); 42 43std::string FilePathToUTF8(const base::FilePath::StringType& str); 44inline std::string FilePathToUTF8(const base::FilePath& path) { 45 return FilePathToUTF8(path.value()); 46} 47base::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. 54size_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. 58base::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. 65size_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. 70base::StringPiece FindFilename(const std::string* path); 71 72// Like FindFilename but does not include the extension. 73base::StringPiece FindFilenameNoExtension(const std::string* path); 74 75// Removes everything after the last slash. The last slash, if any, will be 76// preserved. 77void RemoveFilename(std::string* path); 78 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). 82inline bool IsSlash(const char ch) { 83 return ch == '/' || ch == '\\'; 84} 85 86// Returns true if the given path ends with a slash. 87bool EndsWithSlash(const std::string& s); 88 89// Path parts ----------------------------------------------------------------- 90 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. 94base::StringPiece FindDir(const std::string* path); 95 96// Returns the substring identifying the last component of the dir, or the 97// empty substring if none. For example "//foo/bar/" -> "bar". 98base::StringPiece FindLastDirComponent(const SourceDir& dir); 99 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. 104// 105// The originating value will be blamed in the error. 106// 107// Setting allow_templates indicates that the result will be put into a 108// FileTemplate and it will allow the strings to be prefixed with expansions 109// that would include the output path. 110// 111// If the file isn't in the dir, returns false and sets the error. Otherwise 112// returns true and leaves the error untouched. 113bool EnsureStringIsInOutputDir(const SourceDir& dir, 114 const std::string& str, 115 const Value& originating, 116 bool allow_templates, 117 Err* err); 118 119// ---------------------------------------------------------------------------- 120 121// Returns true if the input string is absolute. Double-slashes at the 122// beginning are treated as source-relative paths. On Windows, this handles 123// paths of both the native format: "C:/foo" and ours "/C:/foo" 124bool IsPathAbsolute(const base::StringPiece& path); 125 126// Given an absolute path, checks to see if is it is inside the source root. 127// If it is, fills a source-absolute path into the given output and returns 128// true. If it isn't, clears the dest and returns false. 129// 130// The source_root should be a base::FilePath converted to UTF-8. On Windows, 131// it should begin with a "C:/" rather than being our SourceFile's style 132// ("/C:/"). The source root can end with a slash or not. 133// 134// Note that this does not attempt to normalize slashes in the output. 135bool MakeAbsolutePathRelativeIfPossible(const base::StringPiece& source_root, 136 const base::StringPiece& path, 137 std::string* dest); 138 139// Converts a directory to its inverse (e.g. "/foo/bar/" -> "../../"). 140// This will be the empty string for the root directories ("/" and "//"), and 141// in all other cases, this is guaranteed to end in a slash. 142std::string InvertDir(const SourceDir& dir); 143 144// Collapses "." and sequential "/"s and evaluates "..". 145void NormalizePath(std::string* path); 146 147// Converts slashes to backslashes for Windows. Keeps the string unchanged 148// for other systems. 149void ConvertPathToSystem(std::string* path); 150 151// Takes a source-absolute path (must begin with "//") and makes it relative 152// to the given directory, which also must be source-absolute. 153std::string RebaseSourceAbsolutePath(const std::string& input, 154 const SourceDir& dest_dir); 155 156// Returns the given directory with no terminating slash at the end, such that 157// appending a slash and more stuff will produce a valid path. 158// 159// If the directory refers to either the source or system root, we'll append 160// a "." so this remains valid. 161std::string DirectoryWithNoLastSlash(const SourceDir& dir); 162 163// Returns the "best" SourceDir representing the given path. If it's inside the 164// given source_root, a source-relative directory will be returned (e.g. 165// "//foo/bar.cc". If it's outside of the source root, a system-absolute 166// directory will be returned. 167SourceDir SourceDirForPath(const base::FilePath& source_root, 168 const base::FilePath& path); 169 170// Like SourceDirForPath but returns the SourceDir representing the current 171// directory. 172SourceDir SourceDirForCurrentDirectory(const base::FilePath& source_root); 173 174// Given the label of a toolchain and whether that toolchain is the default 175// toolchain, returns the name of the subdirectory for that toolchain's 176// output. This will be the empty string to indicate that the toolchain outputs 177// go in the root build directory. Otherwise, the result will end in a slash. 178std::string GetOutputSubdirName(const Label& toolchain_label, bool is_default); 179 180// ----------------------------------------------------------------------------- 181 182// These functions return the various flavors of output and gen directories. 183SourceDir GetToolchainOutputDir(const Settings* settings); 184SourceDir GetToolchainOutputDir(const BuildSettings* build_settings, 185 const Label& label, bool is_default); 186SourceDir GetToolchainGenDir(const Settings* settings); 187SourceDir GetToolchainGenDir(const BuildSettings* build_settings, 188 const Label& toolchain_label, bool is_default); 189SourceDir GetOutputDirForSourceDir(const Settings* settings, 190 const SourceDir& source_dir); 191SourceDir GetGenDirForSourceDir(const Settings* settings, 192 const SourceDir& source_dir); 193SourceDir GetTargetOutputDir(const Target* target); 194SourceDir GetTargetGenDir(const Target* target); 195SourceDir GetCurrentOutputDir(const Scope* scope); 196SourceDir GetCurrentGenDir(const Scope* scope); 197 198#endif // TOOLS_GN_FILESYSTEM_UTILS_H_ 199