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 "tools/gn/filesystem_utils.h" | 5 #include "tools/gn/filesystem_utils.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 | 8 |
9 #include "base/file_util.h" | 9 #include "base/file_util.h" |
10 #include "base/logging.h" | 10 #include "base/logging.h" |
11 #include "base/strings/string_util.h" | 11 #include "base/strings/string_util.h" |
12 #include "base/strings/utf_string_conversions.h" | 12 #include "base/strings/utf_string_conversions.h" |
13 #include "build/build_config.h" | 13 #include "build/build_config.h" |
14 #include "tools/gn/file_template.h" | |
14 #include "tools/gn/location.h" | 15 #include "tools/gn/location.h" |
15 #include "tools/gn/settings.h" | 16 #include "tools/gn/settings.h" |
16 #include "tools/gn/source_dir.h" | 17 #include "tools/gn/source_dir.h" |
17 | 18 |
18 namespace { | 19 namespace { |
19 | 20 |
20 enum DotDisposition { | 21 enum DotDisposition { |
21 // The given dot is just part of a filename and is not special. | 22 // The given dot is just part of a filename and is not special. |
22 NOT_A_DIRECTORY, | 23 NOT_A_DIRECTORY, |
23 | 24 |
(...skipping 303 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
327 for (; cur >= 0; cur--) { | 328 for (; cur >= 0; cur--) { |
328 if (dir_string[cur] == '/') | 329 if (dir_string[cur] == '/') |
329 return base::StringPiece(&dir_string[cur + 1], end - cur - 1); | 330 return base::StringPiece(&dir_string[cur + 1], end - cur - 1); |
330 } | 331 } |
331 return base::StringPiece(&dir_string[0], end); | 332 return base::StringPiece(&dir_string[0], end); |
332 } | 333 } |
333 | 334 |
334 bool EnsureStringIsInOutputDir(const SourceDir& dir, | 335 bool EnsureStringIsInOutputDir(const SourceDir& dir, |
335 const std::string& str, | 336 const std::string& str, |
336 const Value& originating, | 337 const Value& originating, |
338 bool allow_templates, | |
337 Err* err) { | 339 Err* err) { |
338 // The last char of the dir will be a slash. We don't care if the input ends | |
339 // in a slash or not, so just compare up until there. | |
340 // | |
341 // This check will be wrong for all proper prefixes "e.g. "/output" will | 340 // This check will be wrong for all proper prefixes "e.g. "/output" will |
342 // match "/out" but we don't really care since this is just a sanity check. | 341 // match "/out" but we don't really care since this is just a sanity check. |
343 const std::string& dir_str = dir.value(); | 342 const std::string& dir_str = dir.value(); |
344 if (str.compare(0, dir_str.length() - 1, dir_str, 0, dir_str.length() - 1) | 343 if (str.compare(0, dir_str.length(), dir_str, 0, dir_str.length()) == 0) |
viettrungluu
2014/07/10 23:10:39
nit: the second 0, dir_str.length() are unnecessar
| |
345 != 0) { | 344 return true; // Output directory is hardcoded. |
346 *err = Err(originating, "File is not inside output directory.", | 345 |
347 "The given file should be in the output directory. Normally you would " | 346 if (allow_templates) { |
348 "specify\n\"$target_out_dir/foo\" or " | 347 // Allow the string to begin with any source expansion inside the output |
349 "\"$target_gen_dir/foo\". I interpreted this as\n\"" | 348 // directory. |
350 + str + "\"."); | 349 if (StartsWithASCII(str, FileTemplate::kSourceGenDir, true) || |
351 return false; | 350 StartsWithASCII(str, FileTemplate::kSourceOutDir, true)) |
351 return true; | |
352 } | 352 } |
353 return true; | 353 |
354 *err = Err(originating, "File is not inside output directory.", | |
355 "The given file should be in the output directory. Normally you would " | |
356 "specify\n\"$target_out_dir/foo\" or " | |
357 "\"$target_gen_dir/foo\". I interpreted this as\n\"" | |
358 + str + "\"."); | |
359 return false; | |
354 } | 360 } |
355 | 361 |
356 bool IsPathAbsolute(const base::StringPiece& path) { | 362 bool IsPathAbsolute(const base::StringPiece& path) { |
357 if (path.empty()) | 363 if (path.empty()) |
358 return false; | 364 return false; |
359 | 365 |
360 if (!IsSlash(path[0])) { | 366 if (!IsSlash(path[0])) { |
361 #if defined(OS_WIN) | 367 #if defined(OS_WIN) |
362 // Check for Windows system paths like "C:\foo". | 368 // Check for Windows system paths like "C:\foo". |
363 if (path.size() > 2 && path[1] == ':' && IsSlash(path[2])) | 369 if (path.size() > 2 && path[1] == ':' && IsSlash(path[2])) |
(...skipping 392 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
756 return GetGenDirForSourceDir(target->settings(), target->label().dir()); | 762 return GetGenDirForSourceDir(target->settings(), target->label().dir()); |
757 } | 763 } |
758 | 764 |
759 SourceDir GetCurrentOutputDir(const Scope* scope) { | 765 SourceDir GetCurrentOutputDir(const Scope* scope) { |
760 return GetOutputDirForSourceDir(scope->settings(), scope->GetSourceDir()); | 766 return GetOutputDirForSourceDir(scope->settings(), scope->GetSourceDir()); |
761 } | 767 } |
762 | 768 |
763 SourceDir GetCurrentGenDir(const Scope* scope) { | 769 SourceDir GetCurrentGenDir(const Scope* scope) { |
764 return GetGenDirForSourceDir(scope->settings(), scope->GetSourceDir()); | 770 return GetGenDirForSourceDir(scope->settings(), scope->GetSourceDir()); |
765 } | 771 } |
OLD | NEW |