Ninja
msvc_helper-win32.cc
Go to the documentation of this file.
1 // Copyright 2011 Google Inc. All Rights Reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #include "msvc_helper.h"
16 
17 #include <windows.h>
18 
19 #include "util.h"
20 
21 using namespace std;
22 
23 namespace {
24 
25 string Replace(const string& input, const string& find, const string& replace) {
26  string result = input;
27  size_t start_pos = 0;
28  while ((start_pos = result.find(find, start_pos)) != string::npos) {
29  result.replace(start_pos, find.length(), replace);
30  start_pos += replace.length();
31  }
32  return result;
33 }
34 
35 } // anonymous namespace
36 
37 string EscapeForDepfile(const string& path) {
38  // Depfiles don't escape single \.
39  return Replace(path, " ", "\\ ");
40 }
41 
42 int CLWrapper::Run(const string& command, string* output) {
43  SECURITY_ATTRIBUTES security_attributes = {};
44  security_attributes.nLength = sizeof(SECURITY_ATTRIBUTES);
45  security_attributes.bInheritHandle = TRUE;
46 
47  // Must be inheritable so subprocesses can dup to children.
48  HANDLE nul =
49  CreateFileA("NUL", GENERIC_READ,
50  FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
51  &security_attributes, OPEN_EXISTING, 0, NULL);
52  if (nul == INVALID_HANDLE_VALUE)
53  Fatal("couldn't open nul");
54 
55  HANDLE stdout_read, stdout_write;
56  if (!CreatePipe(&stdout_read, &stdout_write, &security_attributes, 0))
57  Win32Fatal("CreatePipe");
58 
59  if (!SetHandleInformation(stdout_read, HANDLE_FLAG_INHERIT, 0))
60  Win32Fatal("SetHandleInformation");
61 
62  PROCESS_INFORMATION process_info = {};
63  STARTUPINFOA startup_info = {};
64  startup_info.cb = sizeof(STARTUPINFOA);
65  startup_info.hStdInput = nul;
66  startup_info.hStdError = ::GetStdHandle(STD_ERROR_HANDLE);
67  startup_info.hStdOutput = stdout_write;
68  startup_info.dwFlags |= STARTF_USESTDHANDLES;
69 
70  if (!CreateProcessA(NULL, (char*)command.c_str(), NULL, NULL,
71  /* inherit handles */ TRUE, 0,
72  env_block_, NULL,
73  &startup_info, &process_info)) {
74  Win32Fatal("CreateProcess");
75  }
76 
77  if (!CloseHandle(nul) ||
78  !CloseHandle(stdout_write)) {
79  Win32Fatal("CloseHandle");
80  }
81 
82  // Read all output of the subprocess.
83  DWORD read_len = 1;
84  while (read_len) {
85  char buf[64 << 10];
86  read_len = 0;
87  if (!::ReadFile(stdout_read, buf, sizeof(buf), &read_len, NULL) &&
88  GetLastError() != ERROR_BROKEN_PIPE) {
89  Win32Fatal("ReadFile");
90  }
91  output->append(buf, read_len);
92  }
93 
94  // Wait for it to exit and grab its exit code.
95  if (WaitForSingleObject(process_info.hProcess, INFINITE) == WAIT_FAILED)
96  Win32Fatal("WaitForSingleObject");
97  DWORD exit_code = 0;
98  if (!GetExitCodeProcess(process_info.hProcess, &exit_code))
99  Win32Fatal("GetExitCodeProcess");
100 
101  if (!CloseHandle(stdout_read) ||
102  !CloseHandle(process_info.hProcess) ||
103  !CloseHandle(process_info.hThread)) {
104  Win32Fatal("CloseHandle");
105  }
106 
107  return exit_code;
108 }
string EscapeForDepfile(const string &path)
Definition: hash_map.h:26
int Run(const std::string &command, std::string *output)
Start a process and gather its raw output.
void Fatal(const char *msg,...)
Log a fatal message and exit.
Definition: util.cc:67
int ReadFile(const string &path, string *contents, string *err)
Definition: util.cc:415