1 // Copyright 2011 Google Inc. All Rights Reserved.
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
7 // http://www.apache.org/licenses/LICENSE-2.0
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.
15 #include "msvc_helper.h"
22 #include "includes_normalize.h"
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);
33 string Replace(const string& input, const string& find, const string& replace) {
34 string result = input;
36 while ((start_pos = result.find(find, start_pos)) != string::npos) {
37 result.replace(start_pos, find.length(), replace);
38 start_pos += replace.length();
43 } // anonymous namespace
45 string EscapeForDepfile(const string& path) {
46 // Depfiles don't escape single \.
47 return Replace(path, " ", "\\ ");
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) {
62 return line.substr(in - line.c_str());
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);
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");
85 string CLParser::Parse(const string& output, const string& deps_prefix) {
86 string filtered_output;
88 // Loop over all lines in the output to process them.
90 while (start < output.size()) {
91 size_t end = output.find_first_of("\r\n", start);
92 if (end == string::npos)
94 string line = output.substr(start, end - start);
96 string include = FilterShowIncludes(line, deps_prefix);
97 if (!include.empty()) {
100 if (!IncludesNormalize::Normalize(include, NULL, &normalized, &err)) {
101 Error("failed to normalize path: %s: %s\n", include.c_str(),
105 if (!IsSystemInclude(normalized))
106 includes_.insert(normalized);
107 } else if (FilterInputFilename(line)) {
109 // TODO: if we support compiling multiple output files in a single
110 // cl.exe invocation, we should stash the filename.
112 filtered_output.append(line);
113 filtered_output.append("\n");
116 if (end < output.size() && output[end] == '\r')
118 if (end < output.size() && output[end] == '\n')
123 return filtered_output;
126 int CLWrapper::Run(const string& command, string* output) {
127 SECURITY_ATTRIBUTES security_attributes = {};
128 security_attributes.nLength = sizeof(SECURITY_ATTRIBUTES);
129 security_attributes.bInheritHandle = TRUE;
131 // Must be inheritable so subprocesses can dup to children.
132 HANDLE nul = CreateFile("NUL", GENERIC_READ,
133 FILE_SHARE_READ | FILE_SHARE_WRITE |
135 &security_attributes, OPEN_EXISTING, 0, NULL);
136 if (nul == INVALID_HANDLE_VALUE)
137 Fatal("couldn't open nul");
139 HANDLE stdout_read, stdout_write;
140 if (!CreatePipe(&stdout_read, &stdout_write, &security_attributes, 0))
141 Win32Fatal("CreatePipe");
143 if (!SetHandleInformation(stdout_read, HANDLE_FLAG_INHERIT, 0))
144 Win32Fatal("SetHandleInformation");
146 PROCESS_INFORMATION process_info = {};
147 STARTUPINFO startup_info = {};
148 startup_info.cb = sizeof(STARTUPINFO);
149 startup_info.hStdInput = nul;
150 startup_info.hStdError = ::GetStdHandle(STD_ERROR_HANDLE);
151 startup_info.hStdOutput = stdout_write;
152 startup_info.dwFlags |= STARTF_USESTDHANDLES;
154 if (!CreateProcessA(NULL, (char*)command.c_str(), NULL, NULL,
155 /* inherit handles */ TRUE, 0,
157 &startup_info, &process_info)) {
158 Win32Fatal("CreateProcess");
161 if (!CloseHandle(nul) ||
162 !CloseHandle(stdout_write)) {
163 Win32Fatal("CloseHandle");
166 // Read all output of the subprocess.
171 if (!::ReadFile(stdout_read, buf, sizeof(buf), &read_len, NULL) &&
172 GetLastError() != ERROR_BROKEN_PIPE) {
173 Win32Fatal("ReadFile");
175 output->append(buf, read_len);
178 // Wait for it to exit and grab its exit code.
179 if (WaitForSingleObject(process_info.hProcess, INFINITE) == WAIT_FAILED)
180 Win32Fatal("WaitForSingleObject");
182 if (!GetExitCodeProcess(process_info.hProcess, &exit_code))
183 Win32Fatal("GetExitCodeProcess");
185 if (!CloseHandle(stdout_read) ||
186 !CloseHandle(process_info.hProcess) ||
187 !CloseHandle(process_info.hThread)) {
188 Win32Fatal("CloseHandle");