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"
26 const char kNoExecSwitch
[] = "no-exec";
29 const char kExecScript
[] = "exec_script";
30 const char kExecScript_HelpShort
[] =
31 "exec_script: Synchronously run a script and return the output.";
32 const char kExecScript_Help
[] =
33 "exec_script: Synchronously run a script and return the output.\n"
35 " exec_script(filename,\n"
37 " input_conversion = \"\",\n"
38 " file_dependencies = [])\n"
40 " Runs the given script, returning the stdout of the script. The build\n"
41 " generation will fail if the script does not exist or returns a nonzero\n"
44 " The current directory when executing the script will be the root\n"
45 " build directory. If you are passing file names, you will want to use\n"
46 " the rebase_path() function to make file names relative to this\n"
47 " path (see \"gn help rebase_path\").\n"
52 " File name of python script to execute. Non-absolute names will\n"
53 " be treated as relative to the current build file.\n"
56 " A list of strings to be passed to the script as arguments.\n"
57 " May be unspecified or the empty list which means no arguments.\n"
59 " input_conversion:\n"
60 " Controls how the file is read and parsed.\n"
61 " See \"gn help input_conversion\".\n"
63 " If unspecified, defaults to the empty string which causes the\n"
64 " script result to be discarded. exec script will return None.\n"
67 " (Optional) A list of files that this script reads or otherwise\n"
68 " depends on. These dependencies will be added to the build result\n"
69 " such that if any of them change, the build will be regenerated and\n"
70 " the script will be re-run.\n"
72 " The script itself will be an implicit dependency so you do not\n"
77 " all_lines = exec_script(\n"
78 " \"myscript.py\", [some_input], \"list lines\",\n"
79 " [ rebase_path(\"data_file.txt\", root_build_dir) ])\n"
81 " # This example just calls the script with no arguments and discards\n"
83 " exec_script(\"//foo/bar/myscript.py\")\n";
85 Value
RunExecScript(Scope
* scope
,
86 const FunctionCallNode
* function
,
87 const std::vector
<Value
>& args
,
89 if (args
.size() < 1 || args
.size() > 4) {
90 *err
= Err(function
->function(), "Wrong number of arguments to exec_script",
91 "I expected between one and four arguments.");
95 const Settings
* settings
= scope
->settings();
96 const BuildSettings
* build_settings
= settings
->build_settings();
97 const SourceDir
& cur_dir
= scope
->GetSourceDir();
99 // Find the python script to run.
100 if (!args
[0].VerifyTypeIs(Value::STRING
, err
))
102 SourceFile script_source
=
103 cur_dir
.ResolveRelativeFile(args
[0].string_value(),
104 scope
->settings()->build_settings()->root_path_utf8());
105 base::FilePath script_path
= build_settings
->GetFullPath(script_source
);
106 if (!build_settings
->secondary_source_path().empty() &&
107 !base::PathExists(script_path
)) {
108 // Fall back to secondary source root when the file doesn't exist.
109 script_path
= build_settings
->GetFullPathSecondary(script_source
);
112 ScopedTrace
trace(TraceItem::TRACE_SCRIPT_EXECUTE
, script_source
.value());
113 trace
.SetToolchain(settings
->toolchain_label());
115 // Add all dependencies of this script, including the script itself, to the
117 g_scheduler
->AddGenDependency(script_path
);
118 if (args
.size() == 4) {
119 const Value
& deps_value
= args
[3];
120 if (!deps_value
.VerifyTypeIs(Value::LIST
, err
))
123 for (const auto& dep
: deps_value
.list_value()) {
124 if (!dep
.VerifyTypeIs(Value::STRING
, err
))
126 g_scheduler
->AddGenDependency(
127 build_settings
->GetFullPath(cur_dir
.ResolveRelativeFile(
129 scope
->settings()->build_settings()->root_path_utf8())));
133 // Make the command line.
134 const base::FilePath
& python_path
= build_settings
->python_path();
135 CommandLine
cmdline(python_path
);
136 cmdline
.AppendArgPath(script_path
);
138 if (args
.size() >= 2) {
139 // Optional command-line arguments to the script.
140 const Value
& script_args
= args
[1];
141 if (!script_args
.VerifyTypeIs(Value::LIST
, err
))
143 for (const auto& arg
: script_args
.list_value()) {
144 if (!arg
.VerifyTypeIs(Value::STRING
, err
))
146 cmdline
.AppendArg(arg
.string_value());
150 // Log command line for debugging help.
151 trace
.SetCommandLine(cmdline
);
152 base::TimeTicks begin_exec
;
153 if (g_scheduler
->verbose_logging()) {
155 g_scheduler
->Log("Pythoning",
156 base::UTF16ToUTF8(cmdline
.GetCommandLineString()));
158 g_scheduler
->Log("Pythoning", cmdline
.GetCommandLineString());
160 begin_exec
= base::TimeTicks::Now();
163 base::FilePath startup_dir
=
164 build_settings
->GetFullPath(build_settings
->build_dir());
165 // The first time a build is run, no targets will have been written so the
166 // build output directory won't exist. We need to make sure it does before
167 // running any scripts with this as its startup directory, although it will
168 // be relatively rare that the directory won't exist by the time we get here.
170 // If this shows up on benchmarks, we can cache whether we've done this
171 // or not and skip creating the directory.
172 base::CreateDirectory(startup_dir
);
174 // Execute the process.
175 // TODO(brettw) set the environment block.
177 std::string stderr_output
;
179 if (!CommandLine::ForCurrentProcess()->HasSwitch(kNoExecSwitch
)) {
180 if (!internal::ExecProcess(
181 cmdline
, startup_dir
, &output
, &stderr_output
, &exit_code
)) {
182 *err
= Err(function
->function(), "Could not execute python.",
183 "I was trying to execute \"" + FilePathToUTF8(python_path
) + "\".");
187 if (g_scheduler
->verbose_logging()) {
188 g_scheduler
->Log("Pythoning", script_source
.value() + " took " +
190 (base::TimeTicks::Now() - begin_exec
).InMilliseconds()) +
194 if (exit_code
!= 0) {
195 std::string msg
= "Current dir: " + FilePathToUTF8(startup_dir
) +
196 "\nCommand: " + FilePathToUTF8(cmdline
.GetCommandLineString()) +
197 "\nReturned " + base::IntToString(exit_code
);
199 msg
+= " and printed out:\n\n" + output
;
202 if (!stderr_output
.empty())
203 msg
+= "\nstderr:\n\n" + stderr_output
;
205 *err
= Err(function
->function(), "Script returned non-zero exit code.",
210 // Default to None value for the input conversion if unspecified.
211 return ConvertInputToValue(scope
->settings(), output
, function
,
212 args
.size() >= 3 ? args
[2] : Value(), err
);
215 } // namespace functions