Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / tools / gn / function_exec_script.cc
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 "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"
22
23 namespace functions {
24
25 namespace {
26 const char kNoExecSwitch[] = "no-exec";
27 }  // namespace
28
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"
34     "\n"
35     "  exec_script(filename,\n"
36     "              arguments = [],\n"
37     "              input_conversion = \"\",\n"
38     "              file_dependencies = [])\n"
39     "\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"
42     "  exit code.\n"
43     "\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"
48     "\n"
49     "Arguments:\n"
50     "\n"
51     "  filename:\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"
54     "\n"
55     "  arguments:\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"
58     "\n"
59     "  input_conversion:\n"
60     "      Controls how the file is read and parsed.\n"
61     "      See \"gn help input_conversion\".\n"
62     "\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"
65     "\n"
66     "  dependencies:\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"
71     "\n"
72     "      The script itself will be an implicit dependency so you do not\n"
73     "      need to list it.\n"
74     "\n"
75     "Example:\n"
76     "\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"
80     "\n"
81     "  # This example just calls the script with no arguments and discards\n"
82     "  # the result.\n"
83     "  exec_script(\"//foo/bar/myscript.py\")\n";
84
85 Value RunExecScript(Scope* scope,
86                     const FunctionCallNode* function,
87                     const std::vector<Value>& args,
88                     Err* err) {
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.");
92     return Value();
93   }
94
95   const Settings* settings = scope->settings();
96   const BuildSettings* build_settings = settings->build_settings();
97   const SourceDir& cur_dir = scope->GetSourceDir();
98
99   // Find the python script to run.
100   if (!args[0].VerifyTypeIs(Value::STRING, err))
101     return Value();
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);
109   }
110
111   ScopedTrace trace(TraceItem::TRACE_SCRIPT_EXECUTE, script_source.value());
112   trace.SetToolchain(settings->toolchain_label());
113
114   // Add all dependencies of this script, including the script itself, to the
115   // build deps.
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))
120       return Value();
121
122     for (const auto& dep : deps_value.list_value()) {
123       if (!dep.VerifyTypeIs(Value::STRING, err))
124         return Value();
125       g_scheduler->AddGenDependency(
126           build_settings->GetFullPath(cur_dir.ResolveRelativeFile(
127               dep.string_value())));
128     }
129   }
130
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);
135
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))
140       return Value();
141     for (const auto& arg : script_args.list_value()) {
142       if (!arg.VerifyTypeIs(Value::STRING, err))
143         return Value();
144       cmdline.AppendArg(arg.string_value());
145     }
146   }
147
148   // Log command line for debugging help.
149   trace.SetCommandLine(cmdline);
150   base::TimeTicks begin_exec;
151   if (g_scheduler->verbose_logging()) {
152 #if defined(OS_WIN)
153     g_scheduler->Log("Pythoning",
154                      base::UTF16ToUTF8(cmdline.GetCommandLineString()));
155 #else
156     g_scheduler->Log("Pythoning", cmdline.GetCommandLineString());
157 #endif
158     begin_exec = base::TimeTicks::Now();
159   }
160
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.
167   //
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);
171
172   // Execute the process.
173   // TODO(brettw) set the environment block.
174   std::string output;
175   std::string stderr_output;
176   int exit_code = 0;
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) + "\".");
182       return Value();
183     }
184   }
185   if (g_scheduler->verbose_logging()) {
186     g_scheduler->Log("Pythoning", script_source.value() + " took " +
187         base::Int64ToString(
188             (base::TimeTicks::Now() - begin_exec).InMilliseconds()) +
189         "ms");
190   }
191
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);
196     if (!output.empty())
197       msg += " and printed out:\n\n" + output;
198     else
199       msg += ".";
200     if (!stderr_output.empty())
201       msg += "\nstderr:\n\n" + stderr_output;
202
203     *err = Err(function->function(), "Script returned non-zero exit code.",
204                msg);
205     return Value();
206   }
207
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);
211 }
212
213 }  // namespace functions