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 <algorithm>
18 #include <stdio.h>
19 #include <string.h>
20 #include <windows.h>
21 
22 #include "includes_normalize.h"
23 #include "util.h"
24 
25 namespace {
26 
27 /// Return true if \a input ends with \a needle.
28 bool EndsWith(const string& input, const string& needle) {
29  return (input.size() >= needle.size() &&
30  input.substr(input.size() - needle.size()) == needle);
31 }
32 
33 string Replace(const string& input, const string& find, const string& replace) {
34  string result = input;
35  size_t start_pos = 0;
36  while ((start_pos = result.find(find, start_pos)) != string::npos) {
37  result.replace(start_pos, find.length(), replace);
38  start_pos += replace.length();
39  }
40  return result;
41 }
42 
43 } // anonymous namespace
44 
45 string EscapeForDepfile(const string& path) {
46  // Depfiles don't escape single \.
47  return Replace(path, " ", "\\ ");
48 }
49 
50 // static
51 string CLParser::FilterShowIncludes(const string& line,
52  const string& deps_prefix) {
53  const string kDepsPrefixEnglish = "Note: including file: ";
54  const char* in = line.c_str();
55  const char* end = in + line.size();
56  const string& prefix = deps_prefix.empty() ? kDepsPrefixEnglish : deps_prefix;
57  if (end - in > (int)prefix.size() &&
58  memcmp(in, prefix.c_str(), (int)prefix.size()) == 0) {
59  in += prefix.size();
60  while (*in == ' ')
61  ++in;
62  return line.substr(in - line.c_str());
63  }
64  return "";
65 }
66 
67 // static
68 bool CLParser::IsSystemInclude(string path) {
69  transform(path.begin(), path.end(), path.begin(), ::tolower);
70  // TODO: this is a heuristic, perhaps there's a better way?
71  return (path.find("program files") != string::npos ||
72  path.find("microsoft visual studio") != string::npos);
73 }
74 
75 // static
76 bool CLParser::FilterInputFilename(string line) {
77  transform(line.begin(), line.end(), line.begin(), ::tolower);
78  // TODO: other extensions, like .asm?
79  return EndsWith(line, ".c") ||
80  EndsWith(line, ".cc") ||
81  EndsWith(line, ".cxx") ||
82  EndsWith(line, ".cpp");
83 }
84 
85 string CLParser::Parse(const string& output, const string& deps_prefix) {
86  string filtered_output;
87 
88  // Loop over all lines in the output to process them.
89  size_t start = 0;
90  while (start < output.size()) {
91  size_t end = output.find_first_of("\r\n", start);
92  if (end == string::npos)
93  end = output.size();
94  string line = output.substr(start, end - start);
95 
96  string include = FilterShowIncludes(line, deps_prefix);
97  if (!include.empty()) {
98  include = IncludesNormalize::Normalize(include, NULL);
99  if (!IsSystemInclude(include))
100  includes_.insert(include);
101  } else if (FilterInputFilename(line)) {
102  // Drop it.
103  // TODO: if we support compiling multiple output files in a single
104  // cl.exe invocation, we should stash the filename.
105  } else {
106  filtered_output.append(line);
107  filtered_output.append("\n");
108  }
109 
110  if (end < output.size() && output[end] == '\r')
111  ++end;
112  if (end < output.size() && output[end] == '\n')
113  ++end;
114  start = end;
115  }
116 
117  return filtered_output;
118 }
119 
120 int CLWrapper::Run(const string& command, string* output) {
121  SECURITY_ATTRIBUTES security_attributes = {};
122  security_attributes.nLength = sizeof(SECURITY_ATTRIBUTES);
123  security_attributes.bInheritHandle = TRUE;
124 
125  // Must be inheritable so subprocesses can dup to children.
126  HANDLE nul = CreateFile("NUL", GENERIC_READ,
127  FILE_SHARE_READ | FILE_SHARE_WRITE |
128  FILE_SHARE_DELETE,
129  &security_attributes, OPEN_EXISTING, 0, NULL);
130  if (nul == INVALID_HANDLE_VALUE)
131  Fatal("couldn't open nul");
132 
133  HANDLE stdout_read, stdout_write;
134  if (!CreatePipe(&stdout_read, &stdout_write, &security_attributes, 0))
135  Win32Fatal("CreatePipe");
136 
137  if (!SetHandleInformation(stdout_read, HANDLE_FLAG_INHERIT, 0))
138  Win32Fatal("SetHandleInformation");
139 
140  PROCESS_INFORMATION process_info = {};
141  STARTUPINFO startup_info = {};
142  startup_info.cb = sizeof(STARTUPINFO);
143  startup_info.hStdInput = nul;
144  startup_info.hStdError = ::GetStdHandle(STD_ERROR_HANDLE);
145  startup_info.hStdOutput = stdout_write;
146  startup_info.dwFlags |= STARTF_USESTDHANDLES;
147 
148  if (!CreateProcessA(NULL, (char*)command.c_str(), NULL, NULL,
149  /* inherit handles */ TRUE, 0,
150  env_block_, NULL,
151  &startup_info, &process_info)) {
152  Win32Fatal("CreateProcess");
153  }
154 
155  if (!CloseHandle(nul) ||
156  !CloseHandle(stdout_write)) {
157  Win32Fatal("CloseHandle");
158  }
159 
160  // Read all output of the subprocess.
161  DWORD read_len = 1;
162  while (read_len) {
163  char buf[64 << 10];
164  read_len = 0;
165  if (!::ReadFile(stdout_read, buf, sizeof(buf), &read_len, NULL) &&
166  GetLastError() != ERROR_BROKEN_PIPE) {
167  Win32Fatal("ReadFile");
168  }
169  output->append(buf, read_len);
170  }
171 
172  // Wait for it to exit and grab its exit code.
173  if (WaitForSingleObject(process_info.hProcess, INFINITE) == WAIT_FAILED)
174  Win32Fatal("WaitForSingleObject");
175  DWORD exit_code = 0;
176  if (!GetExitCodeProcess(process_info.hProcess, &exit_code))
177  Win32Fatal("GetExitCodeProcess");
178 
179  if (!CloseHandle(stdout_read) ||
180  !CloseHandle(process_info.hProcess) ||
181  !CloseHandle(process_info.hThread)) {
182  Win32Fatal("CloseHandle");
183  }
184 
185  return exit_code;
186 }
static bool FilterInputFilename(string line)
Parse a line of cl.exe output and return true if it looks like it&#39;s printing an input filename...
static bool IsSystemInclude(string path)
Return true if a mentioned include file is a system path.
IN IN HANDLE
static string FilterShowIncludes(const string &line, const string &deps_prefix)
Parse a line of cl.exe output and extract /showIncludes info.
static string Normalize(const string &input, const char *relative_to)
Normalize by fixing slashes style, fixing redundant .
int ReadFile(const string &path, string *contents, string *err)
Read a file to a string (in text mode: with CRLF conversion on Windows).
Definition: util.cc:282
int Run(const string &command, string *output)
Start a process and gather its raw output.
void Fatal(const char *msg,...)
Log a fatal message and exit.
Definition: util.cc:52
set< string > includes_
Definition: msvc_helper.h:47
string Parse(const string &output, const string &deps_prefix)
Parse the full output of cl, returning the output (if any) that should printed.
string EscapeForDepfile(const string &path)
IN DWORD