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/file_util.h" 7 #include "base/strings/string_number_conversions.h" 8 #include "build/build_config.h" 9 #include "tools/gn/err.h" 10 #include "tools/gn/filesystem_utils.h" 11 #include "tools/gn/functions.h" 12 #include "tools/gn/input_conversion.h" 13 #include "tools/gn/input_file.h" 14 #include "tools/gn/parse_tree.h" 15 #include "tools/gn/scheduler.h" 16 #include "tools/gn/value.h" 17 18 #if defined(OS_WIN) 19 #include <windows.h> 20 21 #include "base/win/scoped_handle.h" 22 #include "base/win/scoped_process_information.h" 23 #endif 24 25 namespace functions { 26 27 namespace { 28 29 #if defined(OS_WIN) 30 bool ExecProcess(const CommandLine& cmdline, 31 const base::FilePath& startup_dir, 32 std::string* std_out, 33 std::string* std_err, 34 int* exit_code) { 35 SECURITY_ATTRIBUTES sa_attr; 36 // Set the bInheritHandle flag so pipe handles are inherited. 37 sa_attr.nLength = sizeof(SECURITY_ATTRIBUTES); 38 sa_attr.bInheritHandle = TRUE; 39 sa_attr.lpSecurityDescriptor = NULL; 40 41 // Create the pipe for the child process's STDOUT. 42 HANDLE out_read = NULL; 43 HANDLE out_write = NULL; 44 if (!CreatePipe(&out_read, &out_write, &sa_attr, 0)) { 45 NOTREACHED() << "Failed to create pipe"; 46 return false; 47 } 48 base::win::ScopedHandle scoped_out_read(out_read); 49 base::win::ScopedHandle scoped_out_write(out_write); 50 51 // Create the pipe for the child process's STDERR. 52 HANDLE err_read = NULL; 53 HANDLE err_write = NULL; 54 if (!CreatePipe(&err_read, &err_write, &sa_attr, 0)) { 55 NOTREACHED() << "Failed to create pipe"; 56 return false; 57 } 58 base::win::ScopedHandle scoped_err_read(err_read); 59 base::win::ScopedHandle scoped_err_write(err_write); 60 61 // Ensure the read handle to the pipe for STDOUT/STDERR is not inherited. 62 if (!SetHandleInformation(out_read, HANDLE_FLAG_INHERIT, 0)) { 63 NOTREACHED() << "Failed to disabled pipe inheritance"; 64 return false; 65 } 66 if (!SetHandleInformation(err_read, HANDLE_FLAG_INHERIT, 0)) { 67 NOTREACHED() << "Failed to disabled pipe inheritance"; 68 return false; 69 } 70 71 base::FilePath::StringType cmdline_str(cmdline.GetCommandLineString()); 72 73 base::win::ScopedProcessInformation proc_info; 74 STARTUPINFO start_info = { 0 }; 75 76 start_info.cb = sizeof(STARTUPINFO); 77 start_info.hStdOutput = out_write; 78 // Keep the normal stdin. 79 start_info.hStdInput = GetStdHandle(STD_INPUT_HANDLE); 80 // FIXME(brettw) set stderr here when we actually read it below. 81 //start_info.hStdError = err_write; 82 start_info.hStdError = GetStdHandle(STD_ERROR_HANDLE); 83 start_info.dwFlags |= STARTF_USESTDHANDLES; 84 85 // Create the child process. 86 if (!CreateProcess(NULL, 87 &cmdline_str[0], 88 NULL, NULL, 89 TRUE, // Handles are inherited. 90 0, NULL, 91 startup_dir.value().c_str(), 92 &start_info, proc_info.Receive())) { 93 return false; 94 } 95 96 // Close our writing end of pipes now. Otherwise later read would not be able 97 // to detect end of child's output. 98 scoped_out_write.Close(); 99 scoped_err_write.Close(); 100 101 // Read output from the child process's pipe for STDOUT 102 const int kBufferSize = 1024; 103 char buffer[kBufferSize]; 104 105 // FIXME(brettw) read from stderr here! This is complicated because we want 106 // to read both of them at the same time, probably need overlapped I/O. 107 // Also uncomment start_info code above. 108 for (;;) { 109 DWORD bytes_read = 0; 110 BOOL success = ReadFile(out_read, buffer, kBufferSize, &bytes_read, NULL); 111 if (!success || bytes_read == 0) 112 break; 113 std_out->append(buffer, bytes_read); 114 } 115 116 // Let's wait for the process to finish. 117 WaitForSingleObject(proc_info.process_handle(), INFINITE); 118 119 DWORD dw_exit_code; 120 GetExitCodeProcess(proc_info.process_handle(), &dw_exit_code); 121 *exit_code = static_cast<int>(dw_exit_code); 122 123 return true; 124 } 125 #else 126 bool ExecProcess(const CommandLine& cmdline, 127 const base::FilePath& startup_dir, 128 std::string* std_out, 129 std::string* std_err, 130 int* exit_code) { 131 //NOTREACHED() << "Implement me."; 132 return false; 133 } 134 #endif 135 136 } // namespace 137 138 const char kExecScript[] = "exec_script"; 139 const char kExecScript_Help[] = 140 "exec_script: Synchronously run a script and return the output.\n" 141 "\n" 142 " exec_script(filename, arguments, input_conversion,\n" 143 " [file_dependencies])\n" 144 "\n" 145 " Runs the given script, returning the stdout of the script. The build\n" 146 " generation will fail if the script does not exist or returns a nonzero\n" 147 " exit code.\n" 148 "\n" 149 "Arguments:\n" 150 "\n" 151 " filename:\n" 152 " File name of python script to execute, relative to the build file.\n" 153 "\n" 154 " arguments:\n" 155 " A list of strings to be passed to the script as arguments.\n" 156 "\n" 157 " input_conversion:\n" 158 " Controls how the file is read and parsed.\n" 159 " See \"gn help input_conversion\".\n" 160 "\n" 161 " dependencies:\n" 162 " (Optional) A list of files that this script reads or otherwise\n" 163 " depends on. These dependencies will be added to the build result\n" 164 " such that if any of them change, the build will be regenerated and\n" 165 " the script will be re-run.\n" 166 "\n" 167 " The script itself will be an implicit dependency so you do not\n" 168 " need to list it.\n" 169 "\n" 170 "Example:\n" 171 "\n" 172 " all_lines = exec_script(\"myscript.py\", [some_input], \"list lines\",\n" 173 " [\"data_file.txt\"])\n"; 174 175 Value RunExecScript(Scope* scope, 176 const FunctionCallNode* function, 177 const std::vector<Value>& args, 178 Err* err) { 179 if (args.size() != 3 && args.size() != 4) { 180 *err = Err(function->function(), "Wrong number of args to write_file", 181 "I expected three or four arguments."); 182 return Value(); 183 } 184 185 const Settings* settings = scope->settings(); 186 const BuildSettings* build_settings = settings->build_settings(); 187 const SourceDir& cur_dir = SourceDirForFunctionCall(function); 188 189 // Find the python script to run. 190 if (!args[0].VerifyTypeIs(Value::STRING, err)) 191 return Value(); 192 SourceFile script_source = 193 cur_dir.ResolveRelativeFile(args[0].string_value()); 194 base::FilePath script_path = build_settings->GetFullPath(script_source); 195 if (!build_settings->secondary_source_path().empty() && 196 !base::PathExists(script_path)) { 197 // Fall back to secondary source root when the file doesn't exist. 198 script_path = build_settings->GetFullPathSecondary(script_source); 199 } 200 201 // Add all dependencies of this script, including the script itself, to the 202 // build deps. 203 g_scheduler->AddGenDependency(script_source); 204 if (args.size() == 4) { 205 const Value& deps_value = args[3]; 206 if (!deps_value.VerifyTypeIs(Value::LIST, err)) 207 return Value(); 208 209 for (size_t i = 0; i < deps_value.list_value().size(); i++) { 210 if (!deps_value.list_value()[0].VerifyTypeIs(Value::STRING, err)) 211 return Value(); 212 g_scheduler->AddGenDependency(cur_dir.ResolveRelativeFile( 213 deps_value.list_value()[0].string_value())); 214 } 215 } 216 217 // Make the command line. 218 const base::FilePath& python_path = build_settings->python_path(); 219 CommandLine cmdline(python_path); 220 cmdline.AppendArgPath(script_path); 221 222 const Value& script_args = args[1]; 223 if (!script_args.VerifyTypeIs(Value::LIST, err)) 224 return Value(); 225 for (size_t i = 0; i < script_args.list_value().size(); i++) { 226 if (!script_args.list_value()[i].VerifyTypeIs(Value::STRING, err)) 227 return Value(); 228 cmdline.AppendArg(script_args.list_value()[i].string_value()); 229 } 230 231 // Execute the process. 232 // TODO(brettw) set the environment block. 233 std::string output; 234 std::string stderr_output; // TODO(brettw) not hooked up, see above. 235 int exit_code = 0; 236 if (!ExecProcess(cmdline, build_settings->GetFullPath(cur_dir), 237 &output, &stderr_output, &exit_code)) { 238 *err = Err(function->function(), "Could not execute python.", 239 "I was trying to execute \"" + FilePathToUTF8(python_path) + "\"."); 240 return Value(); 241 } 242 243 // TODO(brettw) maybe we need stderr also for reasonable stack dumps. 244 if (exit_code != 0) { 245 std::string msg = 246 std::string("I was running \"") + FilePathToUTF8(script_path) + "\"\n" 247 "and it returned " + base::IntToString(exit_code); 248 if (!output.empty()) 249 msg += " and printed out:\n\n" + output; 250 else 251 msg += "."; 252 *err = Err(function->function(), "Script returned non-zero exit code.", 253 msg); 254 return Value(); 255 } 256 257 return ConvertInputToValue(output, function, args[2], err); 258 } 259 260 } // namespace functions 261