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();
47 return Replace(path,
" ",
"\\ ");
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());
69 transform(path.begin(), path.end(), path.begin(), ::tolower);
71 return (path.find(
"program files") != string::npos ||
72 path.find(
"microsoft visual studio") != string::npos);
77 transform(line.begin(), line.end(), line.begin(), ::tolower);
79 return EndsWith(line,
".c") ||
80 EndsWith(line,
".cc") ||
81 EndsWith(line,
".cxx") ||
82 EndsWith(line,
".cpp");
86 string filtered_output;
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);
97 if (!include.empty()) {
106 filtered_output.append(line);
107 filtered_output.append(
"\n");
110 if (end < output.size() && output[end] ==
'\r')
112 if (end < output.size() && output[end] ==
'\n')
117 return filtered_output;
121 SECURITY_ATTRIBUTES security_attributes = {};
122 security_attributes.nLength =
sizeof(SECURITY_ATTRIBUTES);
123 security_attributes.bInheritHandle = TRUE;
126 HANDLE nul = CreateFile(
"NUL", GENERIC_READ,
127 FILE_SHARE_READ | FILE_SHARE_WRITE |
129 &security_attributes, OPEN_EXISTING, 0, NULL);
130 if (nul == INVALID_HANDLE_VALUE)
131 Fatal(
"couldn't open nul");
133 HANDLE stdout_read, stdout_write;
134 if (!CreatePipe(&stdout_read, &stdout_write, &security_attributes, 0))
135 Win32Fatal(
"CreatePipe");
137 if (!SetHandleInformation(stdout_read, HANDLE_FLAG_INHERIT, 0))
138 Win32Fatal(
"SetHandleInformation");
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;
148 if (!CreateProcessA(NULL, (
char*)command.c_str(), NULL, NULL,
151 &startup_info, &process_info)) {
152 Win32Fatal(
"CreateProcess");
155 if (!CloseHandle(nul) ||
156 !CloseHandle(stdout_write)) {
157 Win32Fatal(
"CloseHandle");
165 if (!::
ReadFile(stdout_read, buf,
sizeof(buf), &read_len, NULL) &&
166 GetLastError() != ERROR_BROKEN_PIPE) {
167 Win32Fatal(
"ReadFile");
169 output->append(buf, read_len);
173 if (WaitForSingleObject(process_info.hProcess, INFINITE) == WAIT_FAILED)
174 Win32Fatal(
"WaitForSingleObject");
176 if (!GetExitCodeProcess(process_info.hProcess, &exit_code))
177 Win32Fatal(
"GetExitCodeProcess");
179 if (!CloseHandle(stdout_read) ||
180 !CloseHandle(process_info.hProcess) ||
181 !CloseHandle(process_info.hThread)) {
182 Win32Fatal(
"CloseHandle");
static bool FilterInputFilename(string line)
Parse a line of cl.exe output and return true if it looks like it's printing an input filename...
static bool IsSystemInclude(string path)
Return true if a mentioned include file is a system path.
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).
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.
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)