Change next_proto member type.
[chromium-blink-merge.git] / tools / gn / function_exec_script.cc
blob9d6521f0578113c345ef37f33c60a2d29004ec87
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.
5 #include "base/command_line.h"
6 #include "base/files/file_util.h"
7 #include "base/logging.h"
8 #include "base/strings/string_number_conversions.h"
9 #include "base/strings/utf_string_conversions.h"
10 #include "base/time/time.h"
11 #include "build/build_config.h"
12 #include "tools/gn/err.h"
13 #include "tools/gn/exec_process.h"
14 #include "tools/gn/filesystem_utils.h"
15 #include "tools/gn/functions.h"
16 #include "tools/gn/input_conversion.h"
17 #include "tools/gn/input_file.h"
18 #include "tools/gn/parse_tree.h"
19 #include "tools/gn/scheduler.h"
20 #include "tools/gn/trace.h"
21 #include "tools/gn/value.h"
23 namespace functions {
25 const char kExecScript[] = "exec_script";
26 const char kExecScript_HelpShort[] =
27 "exec_script: Synchronously run a script and return the output.";
28 const char kExecScript_Help[] =
29 "exec_script: Synchronously run a script and return the output.\n"
30 "\n"
31 " exec_script(filename,\n"
32 " arguments = [],\n"
33 " input_conversion = \"\",\n"
34 " file_dependencies = [])\n"
35 "\n"
36 " Runs the given script, returning the stdout of the script. The build\n"
37 " generation will fail if the script does not exist or returns a nonzero\n"
38 " exit code.\n"
39 "\n"
40 " The current directory when executing the script will be the root\n"
41 " build directory. If you are passing file names, you will want to use\n"
42 " the rebase_path() function to make file names relative to this\n"
43 " path (see \"gn help rebase_path\").\n"
44 "\n"
45 "Arguments:\n"
46 "\n"
47 " filename:\n"
48 " File name of python script to execute. Non-absolute names will\n"
49 " be treated as relative to the current build file.\n"
50 "\n"
51 " arguments:\n"
52 " A list of strings to be passed to the script as arguments.\n"
53 " May be unspecified or the empty list which means no arguments.\n"
54 "\n"
55 " input_conversion:\n"
56 " Controls how the file is read and parsed.\n"
57 " See \"gn help input_conversion\".\n"
58 "\n"
59 " If unspecified, defaults to the empty string which causes the\n"
60 " script result to be discarded. exec script will return None.\n"
61 "\n"
62 " dependencies:\n"
63 " (Optional) A list of files that this script reads or otherwise\n"
64 " depends on. These dependencies will be added to the build result\n"
65 " such that if any of them change, the build will be regenerated and\n"
66 " the script will be re-run.\n"
67 "\n"
68 " The script itself will be an implicit dependency so you do not\n"
69 " need to list it.\n"
70 "\n"
71 "Example:\n"
72 "\n"
73 " all_lines = exec_script(\n"
74 " \"myscript.py\", [some_input], \"list lines\",\n"
75 " [ rebase_path(\"data_file.txt\", root_build_dir) ])\n"
76 "\n"
77 " # This example just calls the script with no arguments and discards\n"
78 " # the result.\n"
79 " exec_script(\"//foo/bar/myscript.py\")\n";
81 Value RunExecScript(Scope* scope,
82 const FunctionCallNode* function,
83 const std::vector<Value>& args,
84 Err* err) {
85 if (args.size() < 1 || args.size() > 4) {
86 *err = Err(function->function(), "Wrong number of arguments to exec_script",
87 "I expected between one and four arguments.");
88 return Value();
91 const Settings* settings = scope->settings();
92 const BuildSettings* build_settings = settings->build_settings();
93 const SourceDir& cur_dir = scope->GetSourceDir();
95 // Find the python script to run.
96 if (!args[0].VerifyTypeIs(Value::STRING, err))
97 return Value();
98 SourceFile script_source =
99 cur_dir.ResolveRelativeFile(args[0].string_value(),
100 scope->settings()->build_settings()->root_path_utf8());
101 base::FilePath script_path = build_settings->GetFullPath(script_source);
102 if (!build_settings->secondary_source_path().empty() &&
103 !base::PathExists(script_path)) {
104 // Fall back to secondary source root when the file doesn't exist.
105 script_path = build_settings->GetFullPathSecondary(script_source);
108 ScopedTrace trace(TraceItem::TRACE_SCRIPT_EXECUTE, script_source.value());
109 trace.SetToolchain(settings->toolchain_label());
111 // Add all dependencies of this script, including the script itself, to the
112 // build deps.
113 g_scheduler->AddGenDependency(script_path);
114 if (args.size() == 4) {
115 const Value& deps_value = args[3];
116 if (!deps_value.VerifyTypeIs(Value::LIST, err))
117 return Value();
119 for (const auto& dep : deps_value.list_value()) {
120 if (!dep.VerifyTypeIs(Value::STRING, err))
121 return Value();
122 g_scheduler->AddGenDependency(
123 build_settings->GetFullPath(cur_dir.ResolveRelativeFile(
124 dep.string_value(),
125 scope->settings()->build_settings()->root_path_utf8())));
129 // Make the command line.
130 const base::FilePath& python_path = build_settings->python_path();
131 CommandLine cmdline(python_path);
132 cmdline.AppendArgPath(script_path);
134 if (args.size() >= 2) {
135 // Optional command-line arguments to the script.
136 const Value& script_args = args[1];
137 if (!script_args.VerifyTypeIs(Value::LIST, err))
138 return Value();
139 for (const auto& arg : script_args.list_value()) {
140 if (!arg.VerifyTypeIs(Value::STRING, err))
141 return Value();
142 cmdline.AppendArg(arg.string_value());
146 // Log command line for debugging help.
147 trace.SetCommandLine(cmdline);
148 base::TimeTicks begin_exec;
149 if (g_scheduler->verbose_logging()) {
150 #if defined(OS_WIN)
151 g_scheduler->Log("Pythoning",
152 base::UTF16ToUTF8(cmdline.GetCommandLineString()));
153 #else
154 g_scheduler->Log("Pythoning", cmdline.GetCommandLineString());
155 #endif
156 begin_exec = base::TimeTicks::Now();
159 base::FilePath startup_dir =
160 build_settings->GetFullPath(build_settings->build_dir());
161 // The first time a build is run, no targets will have been written so the
162 // build output directory won't exist. We need to make sure it does before
163 // running any scripts with this as its startup directory, although it will
164 // be relatively rare that the directory won't exist by the time we get here.
166 // If this shows up on benchmarks, we can cache whether we've done this
167 // or not and skip creating the directory.
168 base::CreateDirectory(startup_dir);
170 // Execute the process.
171 // TODO(brettw) set the environment block.
172 std::string output;
173 std::string stderr_output;
174 int exit_code = 0;
175 if (!internal::ExecProcess(
176 cmdline, startup_dir, &output, &stderr_output, &exit_code)) {
177 *err = Err(function->function(), "Could not execute python.",
178 "I was trying to execute \"" + FilePathToUTF8(python_path) + "\".");
179 return Value();
181 if (g_scheduler->verbose_logging()) {
182 g_scheduler->Log("Pythoning", script_source.value() + " took " +
183 base::Int64ToString(
184 (base::TimeTicks::Now() - begin_exec).InMilliseconds()) +
185 "ms");
188 if (exit_code != 0) {
189 std::string msg = "Current dir: " + FilePathToUTF8(startup_dir) +
190 "\nCommand: " + FilePathToUTF8(cmdline.GetCommandLineString()) +
191 "\nReturned " + base::IntToString(exit_code);
192 if (!output.empty())
193 msg += " and printed out:\n\n" + output;
194 else
195 msg += ".";
196 if (!stderr_output.empty())
197 msg += "\nstderr:\n\n" + stderr_output;
199 *err = Err(function->function(), "Script returned non-zero exit code.",
200 msg);
201 return Value();
204 // Default to None value for the input conversion if unspecified.
205 return ConvertInputToValue(scope->settings(), output, function,
206 args.size() >= 3 ? args[2] : Value(), err);
209 } // namespace functions