1d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch// Copyright (c) 2013 The Chromium Authors. All rights reserved. 2d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch// Use of this source code is governed by a BSD-style license that can be 3d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch// found in the LICENSE file. 4d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch 5d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch#ifndef TOOLS_GN_FILESYSTEM_UTILS_H_ 6d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch#define TOOLS_GN_FILESYSTEM_UTILS_H_ 7d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch 8d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch#include <string> 9d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch 10d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch#include "base/files/file_path.h" 11d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch#include "base/strings/string_piece.h" 12d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch#include "tools/gn/settings.h" 13d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch#include "tools/gn/target.h" 14d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch 15d3868032626d59662ff73b372b5d584c1d144c53Ben Murdochclass Err; 16d3868032626d59662ff73b372b5d584c1d144c53Ben Murdochclass Location; 17d3868032626d59662ff73b372b5d584c1d144c53Ben Murdochclass Value; 18d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch 19d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch// Returns the extension, not including the dot, for the given file type on the 20d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch// given system. 21d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch// 22d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch// Some targets make multiple files (like a .dll and an import library). This 23d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch// function returns the name of the file other targets should depend on and 24d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch// link to (so in this example, the import library). 25d3868032626d59662ff73b372b5d584c1d144c53Ben Murdochconst char* GetExtensionForOutputType(Target::OutputType type, 26d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch Settings::TargetOS os); 27d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch 2868043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)std::string FilePathToUTF8(const base::FilePath::StringType& str); 2968043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)inline std::string FilePathToUTF8(const base::FilePath& path) { 3068043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles) return FilePathToUTF8(path.value()); 3168043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)} 32d3868032626d59662ff73b372b5d584c1d144c53Ben Murdochbase::FilePath UTF8ToFilePath(const base::StringPiece& sp); 33d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch 34d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch// Extensions ----------------------------------------------------------------- 35d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch 36d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch// Returns the index of the extension (character after the last dot not after a 37d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch// slash). Returns std::string::npos if not found. Returns path.size() if the 38d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch// file ends with a dot. 39d3868032626d59662ff73b372b5d584c1d144c53Ben Murdochsize_t FindExtensionOffset(const std::string& path); 40d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch 41d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch// Returns a string piece pointing into the input string identifying the 42d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch// extension. Note that the input pointer must outlive the output. 43d3868032626d59662ff73b372b5d584c1d144c53Ben Murdochbase::StringPiece FindExtension(const std::string* path); 44d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch 45d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch// Filename parts ------------------------------------------------------------- 46d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch 47d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch// Returns the offset of the character following the last slash, or 48d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch// 0 if no slash was found. Returns path.size() if the path ends with a slash. 49d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch// Note that the input pointer must outlive the output. 50d3868032626d59662ff73b372b5d584c1d144c53Ben Murdochsize_t FindFilenameOffset(const std::string& path); 51d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch 52d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch// Returns a string piece pointing into the input string identifying the 53d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch// file name (following the last slash, including the extension). Note that the 54d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch// input pointer must outlive the output. 55d3868032626d59662ff73b372b5d584c1d144c53Ben Murdochbase::StringPiece FindFilename(const std::string* path); 56d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch 57d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch// Like FindFilename but does not include the extension. 58d3868032626d59662ff73b372b5d584c1d144c53Ben Murdochbase::StringPiece FindFilenameNoExtension(const std::string* path); 59d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch 60d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch// Removes everything after the last slash. The last slash, if any, will be 61d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch// preserved. 62d3868032626d59662ff73b372b5d584c1d144c53Ben Murdochvoid RemoveFilename(std::string* path); 63d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch 64a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)// Returns if the given character is a slash. This allows both slashes and 65a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)// backslashes for consistency between Posix and Windows (as opposed to 66a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)// FilePath::IsSeparator which is based on the current platform). 67a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)inline bool IsSlash(const char ch) { 68a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) return ch == '/' || ch == '\\'; 69a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)} 70a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) 71d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch// Returns true if the given path ends with a slash. 72d3868032626d59662ff73b372b5d584c1d144c53Ben Murdochbool EndsWithSlash(const std::string& s); 73d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch 74d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch// Path parts ----------------------------------------------------------------- 75d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch 76d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch// Returns a string piece pointing into the input string identifying the 77d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch// directory name of the given path, including the last slash. Note that the 78d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch// input pointer must outlive the output. 79d3868032626d59662ff73b372b5d584c1d144c53Ben Murdochbase::StringPiece FindDir(const std::string* path); 805c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu 815c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu// Returns the substring identifying the last component of the dir, or the 825c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu// empty substring if none. For example "//foo/bar/" -> "bar". 835c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liubase::StringPiece FindLastDirComponent(const SourceDir& dir); 84d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch 85d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch// Verifies that the given string references a file inside of the given 86d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch// directory. This is pretty stupid and doesn't handle "." and "..", etc., 87d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch// it is designed for a sanity check to keep people from writing output files 88d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch// to the source directory accidentally. 89d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch// 9003b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)// The origin will be blamed in the error. 91d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch// 92d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch// If the file isn't in the dir, returns false and sets the error. Otherwise 93d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch// returns true and leaves the error untouched. 94d3868032626d59662ff73b372b5d584c1d144c53Ben Murdochbool EnsureStringIsInOutputDir(const SourceDir& dir, 95d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch const std::string& str, 9603b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles) const ParseNode* origin, 97d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch Err* err); 98d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch 99d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch// ---------------------------------------------------------------------------- 100d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch 1013551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)// Returns true if the input string is absolute. Double-slashes at the 1023551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)// beginning are treated as source-relative paths. On Windows, this handles 1033551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)// paths of both the native format: "C:/foo" and ours "/C:/foo" 1043551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)bool IsPathAbsolute(const base::StringPiece& path); 1053551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) 1063551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)// Given an absolute path, checks to see if is it is inside the source root. 1073551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)// If it is, fills a source-absolute path into the given output and returns 1083551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)// true. If it isn't, clears the dest and returns false. 1093551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)// 1103551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)// The source_root should be a base::FilePath converted to UTF-8. On Windows, 1113551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)// it should begin with a "C:/" rather than being our SourceFile's style 1123551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)// ("/C:/"). The source root can end with a slash or not. 113424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)// 114424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)// Note that this does not attempt to normalize slashes in the output. 1153551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)bool MakeAbsolutePathRelativeIfPossible(const base::StringPiece& source_root, 1163551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) const base::StringPiece& path, 1173551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) std::string* dest); 1183551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) 119d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch// Converts a directory to its inverse (e.g. "/foo/bar/" -> "../../"). 120d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch// This will be the empty string for the root directories ("/" and "//"), and 121d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch// in all other cases, this is guaranteed to end in a slash. 122d3868032626d59662ff73b372b5d584c1d144c53Ben Murdochstd::string InvertDir(const SourceDir& dir); 123d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch 124d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch// Collapses "." and sequential "/"s and evaluates "..". 125d3868032626d59662ff73b372b5d584c1d144c53Ben Murdochvoid NormalizePath(std::string* path); 126d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch 127d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch// Converts slashes to backslashes for Windows. Keeps the string unchanged 128d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch// for other systems. 129d3868032626d59662ff73b372b5d584c1d144c53Ben Murdochvoid ConvertPathToSystem(std::string* path); 130d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch 13168043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)// Takes a source-absolute path (must begin with "//") and makes it relative 13268043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)// to the given directory, which also must be source-absolute. 13368043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)std::string RebaseSourceAbsolutePath(const std::string& input, 13468043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles) const SourceDir& dest_dir); 13568043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles) 136f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)// Returns the given directory with no terminating slash at the end, such that 137f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)// appending a slash and more stuff will produce a valid path. 138f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)// 139f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)// If the directory refers to either the source or system root, we'll append 140f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)// a "." so this remains valid. 141f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)std::string DirectoryWithNoLastSlash(const SourceDir& dir); 142f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) 143a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)// Returns the "best" SourceDir representing the given path. If it's inside the 144a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)// given source_root, a source-relative directory will be returned (e.g. 145a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)// "//foo/bar.cc". If it's outside of the source root, a system-absolute 146a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)// directory will be returned. 147a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)SourceDir SourceDirForPath(const base::FilePath& source_root, 148a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) const base::FilePath& path); 149a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) 150a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)// Like SourceDirForPath but returns the SourceDir representing the current 151a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)// directory. 152a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)SourceDir SourceDirForCurrentDirectory(const base::FilePath& source_root); 153a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) 154cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)// Given the label of a toolchain and whether that toolchain is the default 155cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)// toolchain, returns the name of the subdirectory for that toolchain's 156cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)// output. This will be the empty string to indicate that the toolchain outputs 157cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)// go in the root build directory. Otherwise, the result will end in a slash. 158cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)std::string GetOutputSubdirName(const Label& toolchain_label, bool is_default); 159cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) 160f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)// ----------------------------------------------------------------------------- 161f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) 162f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)// These functions return the various flavors of output and gen directories. 163f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)SourceDir GetToolchainOutputDir(const Settings* settings); 164cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)SourceDir GetToolchainOutputDir(const BuildSettings* build_settings, 165cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) const Label& label, bool is_default); 16603b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles) 167f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)SourceDir GetToolchainGenDir(const Settings* settings); 16803b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)OutputFile GetToolchainGenDirAsOutputFile(const Settings* settings); 169cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)SourceDir GetToolchainGenDir(const BuildSettings* build_settings, 17003b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles) const Label& toolchain_label, 17103b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles) bool is_default); 17203b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles) 173f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)SourceDir GetOutputDirForSourceDir(const Settings* settings, 174f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) const SourceDir& source_dir); 17503b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)OutputFile GetOutputDirForSourceDirAsOutputFile(const Settings* settings, 17603b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles) const SourceDir& source_dir); 17703b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles) 178f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)SourceDir GetGenDirForSourceDir(const Settings* settings, 17903b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles) const SourceDir& source_dir); 18003b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)OutputFile GetGenDirForSourceDirAsOutputFile(const Settings* settings, 18103b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles) const SourceDir& source_dir); 18203b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles) 183f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)SourceDir GetTargetOutputDir(const Target* target); 18403b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)OutputFile GetTargetOutputDirAsOutputFile(const Target* target); 185f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)SourceDir GetTargetGenDir(const Target* target); 18603b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)OutputFile GetTargetGenDirAsOutputFile(const Target* target); 18703b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles) 188f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)SourceDir GetCurrentOutputDir(const Scope* scope); 189f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)SourceDir GetCurrentGenDir(const Scope* scope); 190f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) 191d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch#endif // TOOLS_GN_FILESYSTEM_UTILS_H_ 192