Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(47)

Side by Side Diff: tools/gn/function_to_build_path.cc

Issue 23606031: GN: Use build directory for CD for scripts (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: iFix docs, base build Created 7 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « tools/gn/function_exec_script.cc ('k') | tools/gn/function_to_build_path_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
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 #include "tools/gn/build_settings.h"
6 #include "tools/gn/functions.h"
7 #include "tools/gn/parse_tree.h"
8 #include "tools/gn/path_output.h"
9 #include "tools/gn/scope.h"
10 #include "tools/gn/settings.h"
11 #include "tools/gn/source_dir.h"
12 #include "tools/gn/source_file.h"
13 #include "tools/gn/value.h"
14
15 namespace functions {
16
17 namespace {
18
19 // Returns true if the given value looks like a directory, otherwise we'll
20 // assume it's a file.
21 bool ValueLooksLikeDir(const std::string& value) {
22 if (value.empty())
23 return true;
24 size_t value_size = value.size();
25
26 // Count the number of dots at the end of the string.
27 size_t num_dots = 0;
28 while (num_dots < value_size && value[value_size - num_dots - 1] == '.')
29 num_dots++;
30
31 if (num_dots == value.size())
32 return true; // String is all dots.
33
34 if (value[value_size - num_dots - 1] == '/' ||
35 value[value_size - num_dots - 1] == '\\')
36 return true; // String is a [back]slash followed by 0 or more dots.
37
38 // Anything else.
39 return false;
40 }
41
42 Value ConvertOneBuildPath(const Scope* scope,
43 const FunctionCallNode* function,
44 const Value& value,
45 const PathOutput& path_output,
46 Err* err) {
47 if (!value.VerifyTypeIs(Value::STRING, err))
48 return Value();
49
50 const std::string& string_value = value.string_value();
51
52 std::ostringstream buffer;
53 if (ValueLooksLikeDir(string_value)) {
54 SourceDir absolute =
55 scope->GetSourceDir().ResolveRelativeDir(string_value);
56 path_output.WriteDir(buffer, absolute, PathOutput::DIR_NO_LAST_SLASH);
57 } else {
58 SourceFile absolute =
59 scope->GetSourceDir().ResolveRelativeFile(string_value);
60 path_output.WriteFile(buffer, absolute);
61 }
62 return Value(function, buffer.str());
63 }
64
65 } // namespace
66
67 const char kToBuildPath[] = "to_build_path";
scottmg 2013/09/14 04:53:16 is this name from google3? i find it a bit confusi
brettw 2013/09/16 17:59:02 This is my own name. I agree the overlap of "build
68 const char kToBuildPath_Help[] =
69 "to_build_path: Rebase a file or directory to the build output dir.\n"
70 "\n"
71 " <converted> = to_build_path(<file_or_path_string_or_list>)\n"
72 "\n"
73 " Takes a string argument representing a file name, or a list of such\n"
74 " strings and converts it/them to be relative to the root build output\n"
75 " directory (which is the current directory when running scripts).\n"
76 "\n"
77 " The input can be:\n"
78 " - Paths relative to the BUILD file like \"foo.txt\".\n"
79 " - Source-root absolute paths like \"//foo/bar/foo.txt\".\n"
80 " - System absolute paths like \"/usr/include/foo.h\" or\n"
81 " \"/C:/foo/bar.h\" (these will be passed unchanged).\n"
82 " - A list of such values (the result will be a list of each item\n"
83 " converted as per the above description).\n"
84 "\n"
85 " Normally for sources and in cases where GN is providing file names\n"
86 " to a tool, the paths will automatically be converted to be relative\n"
87 " to the build directory. However, if you pass additional arguments,\n"
88 " GN won't know that the string is actually a file path. These will\n"
89 " need to be manually converted to be relative to the build dir using\n"
90 " to_build_path().\n"
91 "\n"
92 " Trailing slashes will not be reflected in the output.\n"
93 "\n"
94 " Additionally, on Windows, slashes will be converted to backslashes.\n"
95 "\n"
96 "Example:\n"
97 " custom(\"myscript\") {\n"
98 " # Don't use for sources, GN will automatically convert these since\n"
99 " # it knows they're files.\n"
100 " sources = [ \"foo.txt\", \"bar.txt\" ]\n"
scottmg 2013/09/14 04:53:16 this ended up very confusing in gyp, and there's s
brettw 2013/09/16 17:59:02 I think it's pretty clear here. Some things like s
101 "\n"
102 " # Extra file args passed manually need to be explicitly converted:\n"
103 " args = [ \"--data\", to_build_path(\"//mything/data/input.dat\"),\n"
104 " \"--rel\", to_build_path(\"relative_path.txt\") ]\n"
105 " }\n";
106
107 Value RunToBuildPath(Scope* scope,
108 const FunctionCallNode* function,
109 const std::vector<Value>& args,
110 Err* err) {
111 if (args.size() != 1) {
112 *err = Err(function->function(), "to_build_path takes one argument.");
113 return Value();
114 }
115
116 const Value& value = args[0];
117 PathOutput path_output(scope->settings()->build_settings()->build_dir(),
118 ESCAPE_NONE, true);
119
120 if (value.type() == Value::STRING) {
121 return ConvertOneBuildPath(scope, function, value, path_output, err);
122
123 } else if (value.type() == Value::LIST) {
124 Value ret(function, Value::LIST);
125 ret.list_value().reserve(value.list_value().size());
126
127 for (size_t i = 0; i < value.list_value().size(); i++) {
128 ret.list_value().push_back(
129 ConvertOneBuildPath(scope, function, value.list_value()[i],
130 path_output, err));
131 }
132 return ret;
133 }
134
135 *err = Err(function->function(),
136 "to_build_path requires a list or a string.");
137 return Value();
138 }
139
140 } // namespace functions
OLDNEW
« no previous file with comments | « tools/gn/function_exec_script.cc ('k') | tools/gn/function_to_build_path_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698