Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "tools/gn/err.h" | 5 #include "tools/gn/err.h" |
| 6 #include "tools/gn/filesystem_utils.h" | 6 #include "tools/gn/filesystem_utils.h" |
| 7 #include "tools/gn/functions.h" | 7 #include "tools/gn/functions.h" |
| 8 #include "tools/gn/parse_tree.h" | 8 #include "tools/gn/parse_tree.h" |
| 9 #include "tools/gn/scope.h" | 9 #include "tools/gn/scope.h" |
| 10 #include "tools/gn/value.h" | 10 #include "tools/gn/value.h" |
| 11 | 11 |
| 12 namespace functions { | 12 namespace functions { |
| 13 | 13 |
| 14 namespace { | 14 namespace { |
| 15 | 15 |
| 16 // Corresponds to the various values of "what" in the function call. | 16 // Corresponds to the various values of "what" in the function call. |
| 17 enum What { | 17 enum What { |
| 18 WHAT_FILE, | 18 WHAT_FILE, |
| 19 WHAT_NAME, | 19 WHAT_NAME, |
| 20 WHAT_EXTENSION, | 20 WHAT_EXTENSION, |
| 21 WHAT_DIR, | 21 WHAT_DIR, |
| 22 WHAT_ABSPATH, | 22 WHAT_ABSPATH, |
| 23 WHAT_GEN_DIR, | |
| 24 WHAT_OUT_DIR, | |
| 23 }; | 25 }; |
| 24 | 26 |
| 25 std::string GetOnePathInfo(const SourceDir& current_dir, | 27 // Returns the directory containing the input, regardless of whether the input |
|
cjhopman
2014/06/18 18:13:30
directory relative to current_dir?
| |
| 28 // is a directory or a file. | |
| 29 SourceDir DirForInput(const SourceDir& current_dir, | |
| 30 const std::string& input_string) { | |
| 31 if (!input_string.empty() && input_string[input_string.size() - 1] == '/') { | |
| 32 // Input is a directory. | |
| 33 return current_dir.ResolveRelativeDir(input_string); | |
| 34 } | |
| 35 | |
| 36 // Input is a directory. | |
|
cjhopman
2014/06/18 18:13:30
s/directory/file
| |
| 37 return current_dir.ResolveRelativeFile(input_string).GetDir(); | |
| 38 } | |
| 39 | |
| 40 std::string GetOnePathInfo(const Settings* settings, | |
| 41 const SourceDir& current_dir, | |
| 26 What what, | 42 What what, |
| 27 const Value& input, | 43 const Value& input, |
| 28 Err* err) { | 44 Err* err) { |
| 29 if (!input.VerifyTypeIs(Value::STRING, err)) | 45 if (!input.VerifyTypeIs(Value::STRING, err)) |
| 30 return std::string(); | 46 return std::string(); |
| 31 const std::string& input_string = input.string_value(); | 47 const std::string& input_string = input.string_value(); |
| 32 if (input_string.empty()) { | 48 if (input_string.empty()) { |
| 33 *err = Err(input, "Calling get_path_info on an empty string."); | 49 *err = Err(input, "Calling get_path_info on an empty string."); |
| 34 return std::string(); | 50 return std::string(); |
| 35 } | 51 } |
| (...skipping 19 matching lines...) Expand all Loading... | |
| 55 return std::string("."); | 71 return std::string("."); |
| 56 // Trim slash since this function doesn't return trailing slashes. The | 72 // Trim slash since this function doesn't return trailing slashes. The |
| 57 // times we don't do this are if the result is "/" and "//" since those | 73 // times we don't do this are if the result is "/" and "//" since those |
| 58 // slashes can't be trimmed. | 74 // slashes can't be trimmed. |
| 59 if (dir_incl_slash == "/") | 75 if (dir_incl_slash == "/") |
| 60 return std::string("/."); | 76 return std::string("/."); |
| 61 if (dir_incl_slash == "//") | 77 if (dir_incl_slash == "//") |
| 62 return std::string("//."); | 78 return std::string("//."); |
| 63 return dir_incl_slash.substr(0, dir_incl_slash.size() - 1).as_string(); | 79 return dir_incl_slash.substr(0, dir_incl_slash.size() - 1).as_string(); |
| 64 } | 80 } |
| 81 case WHAT_GEN_DIR: { | |
| 82 return DirectoryWithNoLastSlash( | |
| 83 GetGenDirForSourceDir(settings, | |
| 84 DirForInput(current_dir, input_string))); | |
| 85 } | |
| 86 case WHAT_OUT_DIR: { | |
| 87 return DirectoryWithNoLastSlash( | |
| 88 GetOutputDirForSourceDir(settings, | |
| 89 DirForInput(current_dir, input_string))); | |
| 90 } | |
| 65 case WHAT_ABSPATH: { | 91 case WHAT_ABSPATH: { |
| 66 if (!input_string.empty() && input_string[input_string.size() - 1] == '/') | 92 if (!input_string.empty() && input_string[input_string.size() - 1] == '/') |
| 67 return current_dir.ResolveRelativeDir(input_string).value(); | 93 return current_dir.ResolveRelativeDir(input_string).value(); |
| 68 else | 94 else |
| 69 return current_dir.ResolveRelativeFile(input_string).value(); | 95 return current_dir.ResolveRelativeFile(input_string).value(); |
| 70 } | 96 } |
| 71 default: | 97 default: |
| 72 NOTREACHED(); | 98 NOTREACHED(); |
| 73 return std::string(); | 99 return std::string(); |
| 74 } | 100 } |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 116 " The directory portion of the name, not including the slash.\n" | 142 " The directory portion of the name, not including the slash.\n" |
| 117 " \"foo/bar.txt\" => \"foo\"\n" | 143 " \"foo/bar.txt\" => \"foo\"\n" |
| 118 " \"//foo/bar\" => \"//foo\"\n" | 144 " \"//foo/bar\" => \"//foo\"\n" |
| 119 " \"foo\" => \".\"\n" | 145 " \"foo\" => \".\"\n" |
| 120 "\n" | 146 "\n" |
| 121 " The result will never end in a slash, so if the resulting\n" | 147 " The result will never end in a slash, so if the resulting\n" |
| 122 " is empty, the system (\"/\") or source (\"//\") roots, a \".\"\n" | 148 " is empty, the system (\"/\") or source (\"//\") roots, a \".\"\n" |
| 123 " will be appended such that it is always legal to append a slash\n" | 149 " will be appended such that it is always legal to append a slash\n" |
| 124 " and a filename and get a valid path.\n" | 150 " and a filename and get a valid path.\n" |
| 125 "\n" | 151 "\n" |
| 152 " \"out_dir\"\n" | |
| 153 " The output file directory corresponding to the path of the\n" | |
| 154 " given file, not including a trailing slash.\n" | |
| 155 " \"//foo/bar/baz.txt\" => \"//out/Default/obj/foo/bar\"\n" | |
| 156 | |
| 157 " \"gen_dir\"\n" | |
| 158 " The generated file directory corresponding to the path of the\n" | |
| 159 " given file, not including a trailing slash.\n" | |
| 160 " \"//foo/bar/baz.txt\" => \"//out/Default/gen/foo/bar\"\n" | |
| 161 "\n" | |
| 126 " \"abspath\"\n" | 162 " \"abspath\"\n" |
| 127 " The full absolute path name to the file or directory. It will be\n" | 163 " The full absolute path name to the file or directory. It will be\n" |
| 128 " resolved relative to the currebt directory, and then the source-\n" | 164 " resolved relative to the currebt directory, and then the source-\n" |
| 129 " absolute version will be returned. If the input is system-\n" | 165 " absolute version will be returned. If the input is system-\n" |
| 130 " absolute, the same input will be returned.\n" | 166 " absolute, the same input will be returned.\n" |
| 131 " \"foo/bar.txt\" => \"//mydir/foo/bar.txt\"\n" | 167 " \"foo/bar.txt\" => \"//mydir/foo/bar.txt\"\n" |
| 132 " \"foo/\" => \"//mydir/foo/\"\n" | 168 " \"foo/\" => \"//mydir/foo/\"\n" |
| 133 " \"//foo/bar\" => \"//foo/bar\" (already absolute)\n" | 169 " \"//foo/bar\" => \"//foo/bar\" (already absolute)\n" |
| 134 " \"/usr/include\" => \"/usr/include\" (already absolute)\n" | 170 " \"/usr/include\" => \"/usr/include\" (already absolute)\n" |
| 135 "\n" | 171 "\n" |
| (...skipping 25 matching lines...) Expand all Loading... | |
| 161 return Value(); | 197 return Value(); |
| 162 What what; | 198 What what; |
| 163 if (args[1].string_value() == "file") { | 199 if (args[1].string_value() == "file") { |
| 164 what = WHAT_FILE; | 200 what = WHAT_FILE; |
| 165 } else if (args[1].string_value() == "name") { | 201 } else if (args[1].string_value() == "name") { |
| 166 what = WHAT_NAME; | 202 what = WHAT_NAME; |
| 167 } else if (args[1].string_value() == "extension") { | 203 } else if (args[1].string_value() == "extension") { |
| 168 what = WHAT_EXTENSION; | 204 what = WHAT_EXTENSION; |
| 169 } else if (args[1].string_value() == "dir") { | 205 } else if (args[1].string_value() == "dir") { |
| 170 what = WHAT_DIR; | 206 what = WHAT_DIR; |
| 207 } else if (args[1].string_value() == "out_dir") { | |
| 208 what = WHAT_OUT_DIR; | |
| 209 } else if (args[1].string_value() == "gen_dir") { | |
| 210 what = WHAT_GEN_DIR; | |
| 171 } else if (args[1].string_value() == "abspath") { | 211 } else if (args[1].string_value() == "abspath") { |
| 172 what = WHAT_ABSPATH; | 212 what = WHAT_ABSPATH; |
| 173 } else { | 213 } else { |
| 174 *err = Err(args[1], "Unknown value for 'what'."); | 214 *err = Err(args[1], "Unknown value for 'what'."); |
| 175 return Value(); | 215 return Value(); |
| 176 } | 216 } |
| 177 | 217 |
| 178 const SourceDir& current_dir = scope->GetSourceDir(); | 218 const SourceDir& current_dir = scope->GetSourceDir(); |
| 179 if (args[0].type() == Value::STRING) { | 219 if (args[0].type() == Value::STRING) { |
| 180 return Value(function, GetOnePathInfo(current_dir, what, args[0], err)); | 220 return Value(function, GetOnePathInfo(scope->settings(), current_dir, what, |
| 221 args[0], err)); | |
| 181 } else if (args[0].type() == Value::LIST) { | 222 } else if (args[0].type() == Value::LIST) { |
| 182 const std::vector<Value>& input_list = args[0].list_value(); | 223 const std::vector<Value>& input_list = args[0].list_value(); |
| 183 Value result(function, Value::LIST); | 224 Value result(function, Value::LIST); |
| 184 for (size_t i = 0; i < input_list.size(); i++) { | 225 for (size_t i = 0; i < input_list.size(); i++) { |
| 185 result.list_value().push_back(Value(function, | 226 result.list_value().push_back(Value(function, |
| 186 GetOnePathInfo(current_dir, what, input_list[i], err))); | 227 GetOnePathInfo(scope->settings(), current_dir, what, |
| 228 input_list[i], err))); | |
| 187 if (err->has_error()) | 229 if (err->has_error()) |
| 188 return Value(); | 230 return Value(); |
| 189 } | 231 } |
| 190 return result; | 232 return result; |
| 191 } | 233 } |
| 192 | 234 |
| 193 *err = Err(args[0], "Path must be a string or a list of strings."); | 235 *err = Err(args[0], "Path must be a string or a list of strings."); |
| 194 return Value(); | 236 return Value(); |
| 195 } | 237 } |
| 196 | 238 |
| 197 } // namespace functions | 239 } // namespace functions |
| OLD | NEW |