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 base::FilePath script_path = build_settings->GetFullPath(script_source);
105 if (!build_settings->secondary_source_path().empty() &&
106 !base::PathExists(script_path)) {
107 // Fall back to secondary source root when the file doesn't exist.
108 script_path = build_settings->GetFullPathSecondary(script_source);
111 ScopedTrace trace(TraceItem::TRACE_SCRIPT_EXECUTE, script_source.value());
112 trace.SetToolchain(settings->toolchain_label());
114 // Add all dependencies of this script, including the script itself, to the
116 g_scheduler->AddGenDependency(script_path);
117 if (args.size() == 4) {
118 const Value& deps_value = args[3];
119 if (!deps_value.VerifyTypeIs(Value::LIST, err))
122 for (const auto& dep : deps_value.list_value()) {
123 if (!dep.VerifyTypeIs(Value::STRING, err))
125 g_scheduler->AddGenDependency(
126 build_settings->GetFullPath(cur_dir.ResolveRelativeFile(
127 dep.string_value())));
131 // Make the command line.
132 const base::FilePath& python_path = build_settings->python_path();
133 CommandLine cmdline(python_path);
134 cmdline.AppendArgPath(script_path);
136 if (args.size() >= 2) {
137 // Optional command-line arguments to the script.
138 const Value& script_args = args[1];
139 if (!script_args.VerifyTypeIs(Value::LIST, err))
141 for (const auto& arg : script_args.list_value()) {
142 if (!arg.VerifyTypeIs(Value::STRING, err))
144 cmdline.AppendArg(arg.string_value());
148 // Log command line for debugging help.
149 trace.SetCommandLine(cmdline);
150 base::TimeTicks begin_exec;
151 if (g_scheduler->verbose_logging()) {
153 g_scheduler->Log("Pythoning",
154 base::UTF16ToUTF8(cmdline.GetCommandLineString()));
156 g_scheduler->Log("Pythoning", cmdline.GetCommandLineString());
158 begin_exec = base::TimeTicks::Now();
161 base::FilePath startup_dir =
162 build_settings->GetFullPath(build_settings->build_dir());
163 // The first time a build is run, no targets will have been written so the
164 // build output directory won't exist. We need to make sure it does before
165 // running any scripts with this as its startup directory, although it will
166 // be relatively rare that the directory won't exist by the time we get here.
168 // If this shows up on benchmarks, we can cache whether we've done this
169 // or not and skip creating the directory.
170 base::CreateDirectory(startup_dir);
172 // Execute the process.
173 // TODO(brettw) set the environment block.
175 std::string stderr_output;
177 if (!CommandLine::ForCurrentProcess()->HasSwitch(kNoExecSwitch)) {
178 if (!internal::ExecProcess(
179 cmdline, startup_dir, &output, &stderr_output, &exit_code)) {
180 *err = Err(function->function(), "Could not execute python.",
181 "I was trying to execute \"" + FilePathToUTF8(python_path) + "\".");
185 if (g_scheduler->verbose_logging()) {
186 g_scheduler->Log("Pythoning", script_source.value() + " took " +
188 (base::TimeTicks::Now() - begin_exec).InMilliseconds()) +
192 if (exit_code != 0) {
193 std::string msg = "Current dir: " + FilePathToUTF8(startup_dir) +
194 "\nCommand: " + FilePathToUTF8(cmdline.GetCommandLineString()) +
195 "\nReturned " + base::IntToString(exit_code);
197 msg += " and printed out:\n\n" + output;
200 if (!stderr_output.empty())
201 msg += "\nstderr:\n\n" + stderr_output;
203 *err = Err(function->function(), "Script returned non-zero exit code.",
208 // Default to None value for the input conversion if unspecified.
209 return ConvertInputToValue(scope->settings(), output, function,
210 args.size() >= 3 ? args[2] : Value(), err);
213 } // namespace functions