Home | History | Annotate | Download | only in gn
      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 // Returns the extension, not including the dot, for the given file type on the
     20 // given system.
     21 //
     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);
     27 
     28 std::string FilePathToUTF8(const base::FilePath::StringType& str);
     29 inline std::string FilePathToUTF8(const base::FilePath& path) {
     30   return FilePathToUTF8(path.value());
     31 }
     32 base::FilePath UTF8ToFilePath(const base::StringPiece& sp);
     33 
     34 // Extensions -----------------------------------------------------------------
     35 
     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);
     40 
     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);
     44 
     45 // Filename parts -------------------------------------------------------------
     46 
     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);
     51 
     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);
     56 
     57 // Like FindFilename but does not include the extension.
     58 base::StringPiece FindFilenameNoExtension(const std::string* path);
     59 
     60 // Removes everything after the last slash. The last slash, if any, will be
     61 // preserved.
     62 void RemoveFilename(std::string* path);
     63 
     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 == '\\';
     69 }
     70 
     71 // Returns true if the given path ends with a slash.
     72 bool EndsWithSlash(const std::string& s);
     73 
     74 // Path parts -----------------------------------------------------------------
     75 
     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);
     80 
     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);
     84 
     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.
     89 //
     90 // The origin will be blamed in the error.
     91 //
     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,
     97                                Err* err);
     98 
     99 // ----------------------------------------------------------------------------
    100 
    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);
    105 
    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.
    109 //
    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.
    113 //
    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,
    117                                         std::string* dest);
    118 
    119 // Converts a directory to its inverse (e.g. "/foo/bar/" -> "../../").
    120 // This will be the empty string for the root directories ("/" and "//"), and
    121 // in all other cases, this is guaranteed to end in a slash.
    122 std::string InvertDir(const SourceDir& dir);
    123 
    124 // Collapses "." and sequential "/"s and evaluates "..".
    125 void NormalizePath(std::string* path);
    126 
    127 // Converts slashes to backslashes for Windows. Keeps the string unchanged
    128 // for other systems.
    129 void ConvertPathToSystem(std::string* path);
    130 
    131 // Takes a source-absolute path (must begin with "//") and makes it relative
    132 // to the given directory, which also must be source-absolute.
    133 std::string RebaseSourceAbsolutePath(const std::string& input,
    134                                      const SourceDir& dest_dir);
    135 
    136 // Returns the given directory with no terminating slash at the end, such that
    137 // appending a slash and more stuff will produce a valid path.
    138 //
    139 // If the directory refers to either the source or system root, we'll append
    140 // a "." so this remains valid.
    141 std::string DirectoryWithNoLastSlash(const SourceDir& dir);
    142 
    143 // Returns the "best" SourceDir representing the given path. If it's inside the
    144 // given source_root, a source-relative directory will be returned (e.g.
    145 // "//foo/bar.cc". If it's outside of the source root, a system-absolute
    146 // directory will be returned.
    147 SourceDir SourceDirForPath(const base::FilePath& source_root,
    148                            const base::FilePath& path);
    149 
    150 // Like SourceDirForPath but returns the SourceDir representing the current
    151 // directory.
    152 SourceDir SourceDirForCurrentDirectory(const base::FilePath& source_root);
    153 
    154 // Given the label of a toolchain and whether that toolchain is the default
    155 // toolchain, returns the name of the subdirectory for that toolchain's
    156 // output. This will be the empty string to indicate that the toolchain outputs
    157 // go in the root build directory. Otherwise, the result will end in a slash.
    158 std::string GetOutputSubdirName(const Label& toolchain_label, bool is_default);
    159 
    160 // -----------------------------------------------------------------------------
    161 
    162 // These functions return the various flavors of output and gen directories.
    163 SourceDir GetToolchainOutputDir(const Settings* settings);
    164 SourceDir GetToolchainOutputDir(const BuildSettings* build_settings,
    165                                 const Label& label, bool is_default);
    166 
    167 SourceDir GetToolchainGenDir(const Settings* settings);
    168 OutputFile GetToolchainGenDirAsOutputFile(const Settings* settings);
    169 SourceDir GetToolchainGenDir(const BuildSettings* build_settings,
    170                              const Label& toolchain_label,
    171                              bool is_default);
    172 
    173 SourceDir GetOutputDirForSourceDir(const Settings* settings,
    174                                    const SourceDir& source_dir);
    175 OutputFile GetOutputDirForSourceDirAsOutputFile(const Settings* settings,
    176                                                 const SourceDir& source_dir);
    177 
    178 SourceDir GetGenDirForSourceDir(const Settings* settings,
    179                                  const SourceDir& source_dir);
    180 OutputFile GetGenDirForSourceDirAsOutputFile(const Settings* settings,
    181                                              const SourceDir& source_dir);
    182 
    183 SourceDir GetTargetOutputDir(const Target* target);
    184 OutputFile GetTargetOutputDirAsOutputFile(const Target* target);
    185 SourceDir GetTargetGenDir(const Target* target);
    186 OutputFile GetTargetGenDirAsOutputFile(const Target* target);
    187 
    188 SourceDir GetCurrentOutputDir(const Scope* scope);
    189 SourceDir GetCurrentGenDir(const Scope* scope);
    190 
    191 #endif  // TOOLS_GN_FILESYSTEM_UTILS_H_
    192