| OLD | NEW |
| 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include <stddef.h> | 5 #include <stddef.h> |
| 6 | 6 |
| 7 #include "tools/gn/build_settings.h" | 7 #include "tools/gn/build_settings.h" |
| 8 #include "tools/gn/filesystem_utils.h" | 8 #include "tools/gn/filesystem_utils.h" |
| 9 #include "tools/gn/functions.h" | 9 #include "tools/gn/functions.h" |
| 10 #include "tools/gn/parse_tree.h" | 10 #include "tools/gn/parse_tree.h" |
| (...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 119 | 119 |
| 120 return result; | 120 return result; |
| 121 } | 121 } |
| 122 | 122 |
| 123 } // namespace | 123 } // namespace |
| 124 | 124 |
| 125 const char kRebasePath[] = "rebase_path"; | 125 const char kRebasePath[] = "rebase_path"; |
| 126 const char kRebasePath_HelpShort[] = | 126 const char kRebasePath_HelpShort[] = |
| 127 "rebase_path: Rebase a file or directory to another location."; | 127 "rebase_path: Rebase a file or directory to another location."; |
| 128 const char kRebasePath_Help[] = | 128 const char kRebasePath_Help[] = |
| 129 "rebase_path: Rebase a file or directory to another location.\n" | 129 R"(rebase_path: Rebase a file or directory to another location. |
| 130 "\n" | 130 |
| 131 " converted = rebase_path(input,\n" | 131 converted = rebase_path(input, |
| 132 " new_base = \"\",\n" | 132 new_base = "", |
| 133 " current_base = \".\")\n" | 133 current_base = ".") |
| 134 "\n" | 134 |
| 135 " Takes a string argument representing a file name, or a list of such\n" | 135 Takes a string argument representing a file name, or a list of such strings |
| 136 " strings and converts it/them to be relative to a different base\n" | 136 and converts it/them to be relative to a different base directory. |
| 137 " directory.\n" | 137 |
| 138 "\n" | 138 When invoking the compiler or scripts, GN will automatically convert sources |
| 139 " When invoking the compiler or scripts, GN will automatically convert\n" | 139 and include directories to be relative to the build directory. However, if |
| 140 " sources and include directories to be relative to the build directory.\n" | 140 you're passing files directly in the "args" array or doing other manual |
| 141 " However, if you're passing files directly in the \"args\" array or\n" | 141 manipulations where GN doesn't know something is a file name, you will need |
| 142 " doing other manual manipulations where GN doesn't know something is\n" | 142 to convert paths to be relative to what your tool is expecting. |
| 143 " a file name, you will need to convert paths to be relative to what\n" | 143 |
| 144 " your tool is expecting.\n" | 144 The common case is to use this to convert paths relative to the current |
| 145 "\n" | 145 directory to be relative to the build directory (which will be the current |
| 146 " The common case is to use this to convert paths relative to the\n" | 146 directory when executing scripts). |
| 147 " current directory to be relative to the build directory (which will\n" | 147 |
| 148 " be the current directory when executing scripts).\n" | 148 If you want to convert a file path to be source-absolute (that is, beginning |
| 149 "\n" | 149 with a double slash like "//foo/bar"), you should use the get_path_info() |
| 150 " If you want to convert a file path to be source-absolute (that is,\n" | 150 function. This function won't work because it will always make relative |
| 151 " beginning with a double slash like \"//foo/bar\"), you should use\n" | 151 paths, and it needs to support making paths relative to the source root, so |
| 152 " the get_path_info() function. This function won't work because it will\n" | 152 can't also generate source-absolute paths without more special-cases. |
| 153 " always make relative paths, and it needs to support making paths\n" | 153 |
| 154 " relative to the source root, so can't also generate source-absolute\n" | 154 Arguments |
| 155 " paths without more special-cases.\n" | 155 |
| 156 "\n" | 156 input |
| 157 "Arguments\n" | 157 A string or list of strings representing file or directory names These |
| 158 "\n" | 158 can be relative paths ("foo/bar.txt"), system absolute paths |
| 159 " input\n" | 159 ("/foo/bar.txt"), or source absolute paths ("//foo/bar.txt"). |
| 160 " A string or list of strings representing file or directory names\n" | 160 |
| 161 " These can be relative paths (\"foo/bar.txt\"), system absolute\n" | 161 new_base |
| 162 " paths (\"/foo/bar.txt\"), or source absolute paths\n" | 162 The directory to convert the paths to be relative to. This can be an |
| 163 " (\"//foo/bar.txt\").\n" | 163 absolute path or a relative path (which will be treated as being relative |
| 164 "\n" | 164 to the current BUILD-file's directory). |
| 165 " new_base\n" | 165 |
| 166 " The directory to convert the paths to be relative to. This can be\n" | 166 As a special case, if new_base is the empty string (the default), all |
| 167 " an absolute path or a relative path (which will be treated\n" | 167 paths will be converted to system-absolute native style paths with system |
| 168 " as being relative to the current BUILD-file's directory).\n" | 168 path separators. This is useful for invoking external programs. |
| 169 "\n" | 169 |
| 170 " As a special case, if new_base is the empty string (the default),\n" | 170 current_base |
| 171 " all paths will be converted to system-absolute native style paths\n" | 171 Directory representing the base for relative paths in the input. If this |
| 172 " with system path separators. This is useful for invoking external\n" | 172 is not an absolute path, it will be treated as being relative to the |
| 173 " programs.\n" | 173 current build file. Use "." (the default) to convert paths from the |
| 174 "\n" | 174 current BUILD-file's directory. |
| 175 " current_base\n" | 175 |
| 176 " Directory representing the base for relative paths in the input.\n" | 176 Return value |
| 177 " If this is not an absolute path, it will be treated as being\n" | 177 |
| 178 " relative to the current build file. Use \".\" (the default) to\n" | 178 The return value will be the same type as the input value (either a string or |
| 179 " convert paths from the current BUILD-file's directory.\n" | 179 a list of strings). All relative and source-absolute file names will be |
| 180 "\n" | 180 converted to be relative to the requested output System-absolute paths will |
| 181 "Return value\n" | 181 be unchanged. |
| 182 "\n" | 182 |
| 183 " The return value will be the same type as the input value (either a\n" | 183 Whether an output path will end in a slash will match whether the |
| 184 " string or a list of strings). All relative and source-absolute file\n" | 184 corresponding input path ends in a slash. It will return "." or "./" |
| 185 " names will be converted to be relative to the requested output\n" | 185 (depending on whether the input ends in a slash) to avoid returning empty |
| 186 " System-absolute paths will be unchanged.\n" | 186 strings. This means if you want a root path ("//" or "/") not ending in a |
| 187 "\n" | 187 slash, you can add a dot ("//."). |
| 188 " Whether an output path will end in a slash will match whether the\n" | 188 |
| 189 " corresponding input path ends in a slash. It will return \".\" or\n" | 189 Example |
| 190 " \"./\" (depending on whether the input ends in a slash) to avoid\n" | 190 |
| 191 " returning empty strings. This means if you want a root path\n" | 191 # Convert a file in the current directory to be relative to the build |
| 192 " (\"//\" or \"/\") not ending in a slash, you can add a dot (\"//.\").\n" | 192 # directory (the current dir when executing compilers and scripts). |
| 193 "\n" | 193 foo = rebase_path("myfile.txt", root_build_dir) |
| 194 "Example\n" | 194 # might produce "../../project/myfile.txt". |
| 195 "\n" | 195 |
| 196 " # Convert a file in the current directory to be relative to the build\n" | 196 # Convert a file to be system absolute: |
| 197 " # directory (the current dir when executing compilers and scripts).\n" | 197 foo = rebase_path("myfile.txt") |
| 198 " foo = rebase_path(\"myfile.txt\", root_build_dir)\n" | 198 # Might produce "D:\\source\\project\\myfile.txt" on Windows or |
| 199 " # might produce \"../../project/myfile.txt\".\n" | 199 # "/home/you/source/project/myfile.txt" on Linux. |
| 200 "\n" | 200 |
| 201 " # Convert a file to be system absolute:\n" | 201 # Typical usage for converting to the build directory for a script. |
| 202 " foo = rebase_path(\"myfile.txt\")\n" | 202 action("myscript") { |
| 203 " # Might produce \"D:\\source\\project\\myfile.txt\" on Windows or\n" | 203 # Don't convert sources, GN will automatically convert these to be relative |
| 204 " # \"/home/you/source/project/myfile.txt\" on Linux.\n" | 204 # to the build directory when it constructs the command line for your |
| 205 "\n" | 205 # script. |
| 206 " # Typical usage for converting to the build directory for a script.\n" | 206 sources = [ "foo.txt", "bar.txt" ] |
| 207 " action(\"myscript\") {\n" | 207 |
| 208 " # Don't convert sources, GN will automatically convert these to be\n" | 208 # Extra file args passed manually need to be explicitly converted |
| 209 " # relative to the build directory when it constructs the command\n" | 209 # to be relative to the build directory: |
| 210 " # line for your script.\n" | 210 args = [ |
| 211 " sources = [ \"foo.txt\", \"bar.txt\" ]\n" | 211 "--data", |
| 212 "\n" | 212 rebase_path("//mything/data/input.dat", root_build_dir), |
| 213 " # Extra file args passed manually need to be explicitly converted\n" | 213 "--rel", |
| 214 " # to be relative to the build directory:\n" | 214 rebase_path("relative_path.txt", root_build_dir) |
| 215 " args = [\n" | 215 ] + rebase_path(sources, root_build_dir) |
| 216 " \"--data\",\n" | 216 } |
| 217 " rebase_path(\"//mything/data/input.dat\", root_build_dir),\n" | 217 )"; |
| 218 " \"--rel\",\n" | |
| 219 " rebase_path(\"relative_path.txt\", root_build_dir)\n" | |
| 220 " ] + rebase_path(sources, root_build_dir)\n" | |
| 221 " }\n"; | |
| 222 | 218 |
| 223 Value RunRebasePath(Scope* scope, | 219 Value RunRebasePath(Scope* scope, |
| 224 const FunctionCallNode* function, | 220 const FunctionCallNode* function, |
| 225 const std::vector<Value>& args, | 221 const std::vector<Value>& args, |
| 226 Err* err) { | 222 Err* err) { |
| 227 Value result; | 223 Value result; |
| 228 | 224 |
| 229 // Argument indices. | 225 // Argument indices. |
| 230 static const size_t kArgIndexInputs = 0; | 226 static const size_t kArgIndexInputs = 0; |
| 231 static const size_t kArgIndexDest = 1; | 227 static const size_t kArgIndexDest = 1; |
| (...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 290 } | 286 } |
| 291 return result; | 287 return result; |
| 292 } | 288 } |
| 293 | 289 |
| 294 *err = Err(function->function(), | 290 *err = Err(function->function(), |
| 295 "rebase_path requires a list or a string."); | 291 "rebase_path requires a list or a string."); |
| 296 return result; | 292 return result; |
| 297 } | 293 } |
| 298 | 294 |
| 299 } // namespace functions | 295 } // namespace functions |
| OLD | NEW |