summaryrefslogtreecommitdiffstats
path: root/src/msvc_helper-win32.cc
blob: fd9b671b43aeaf0c01bc931ec8686996b44cb528 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
// Copyright 2011 Google Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include "msvc_helper.h"

#include <stdio.h>
#include <string.h>
#include <windows.h>

#include "includes_normalize.h"
#include "util.h"

namespace {

/// Return true if \a input ends with \a needle.
bool EndsWith(const string& input, const string& needle) {
  return (input.size() >= needle.size() &&
          input.substr(input.size() - needle.size()) == needle);
}

string Replace(const string& input, const string& find, const string& replace) {
  string result = input;
  size_t start_pos = 0;
  while ((start_pos = result.find(find, start_pos)) != string::npos) {
    result.replace(start_pos, find.length(), replace);
    start_pos += replace.length();
  }
  return result;
}

string EscapeForDepfile(const string& path) {
  // Depfiles don't escape single \.
  return Replace(path, " ", "\\ ");
}

}  // anonymous namespace

// static
string CLWrapper::FilterShowIncludes(const string& line) {
  static const char kMagicPrefix[] = "Note: including file: ";
  const char* in = line.c_str();
  const char* end = in + line.size();

  if (end - in > (int)sizeof(kMagicPrefix) - 1 &&
      memcmp(in, kMagicPrefix, sizeof(kMagicPrefix) - 1) == 0) {
    in += sizeof(kMagicPrefix) - 1;
    while (*in == ' ')
      ++in;
    return line.substr(in - line.c_str());
  }
  return "";
}

// static
bool CLWrapper::IsSystemInclude(const string& path) {
  // TODO: this is a heuristic, perhaps there's a better way?
  return (path.find("program files") != string::npos ||
          path.find("microsoft visual studio") != string::npos);
}

// static
bool CLWrapper::FilterInputFilename(const string& line) {
  // TODO: other extensions, like .asm?
  return EndsWith(line, ".c") ||
      EndsWith(line, ".cc") ||
      EndsWith(line, ".cxx") ||
      EndsWith(line, ".cpp");
}

int CLWrapper::Run(const string& command, string* extra_output) {
  SECURITY_ATTRIBUTES security_attributes = {};
  security_attributes.nLength = sizeof(SECURITY_ATTRIBUTES);
  security_attributes.bInheritHandle = TRUE;

  // Must be inheritable so subprocesses can dup to children.
  HANDLE nul = CreateFile("NUL", GENERIC_READ,
                          FILE_SHARE_READ | FILE_SHARE_WRITE |
                          FILE_SHARE_DELETE,
                          &security_attributes, OPEN_EXISTING, 0, NULL);
  if (nul == INVALID_HANDLE_VALUE)
    Fatal("couldn't open nul");

  HANDLE stdout_read, stdout_write;
  if (!CreatePipe(&stdout_read, &stdout_write, &security_attributes, 0))
    Win32Fatal("CreatePipe");

  if (!SetHandleInformation(stdout_read, HANDLE_FLAG_INHERIT, 0))
    Win32Fatal("SetHandleInformation");

  PROCESS_INFORMATION process_info = {};
  STARTUPINFO startup_info = {};
  startup_info.cb = sizeof(STARTUPINFO);
  startup_info.hStdInput = nul;
  startup_info.hStdError = stdout_write;
  startup_info.hStdOutput = stdout_write;
  startup_info.dwFlags |= STARTF_USESTDHANDLES;

  if (!CreateProcessA(NULL, (char*)command.c_str(), NULL, NULL,
                      /* inherit handles */ TRUE, 0,
                      env_block_, NULL,
                      &startup_info, &process_info)) {
    Win32Fatal("CreateProcess");
  }

  if (!CloseHandle(nul) ||
      !CloseHandle(stdout_write)) {
    Win32Fatal("CloseHandle");
  }

  // Read output of the subprocess and parse it.
  string output;
  DWORD read_len = 1;
  while (read_len) {
    char buf[64 << 10];
    read_len = 0;
    if (!::ReadFile(stdout_read, buf, sizeof(buf), &read_len, NULL) &&
        GetLastError() != ERROR_BROKEN_PIPE) {
      Win32Fatal("ReadFile");
    }
    output.append(buf, read_len);

    // Loop over all lines in the output and process them.
    for (;;) {
      size_t ofs = output.find_first_of("\r\n");
      if (ofs == string::npos)
        break;
      string line = output.substr(0, ofs);

      string include = FilterShowIncludes(line);
      if (!include.empty()) {
        include = IncludesNormalize::Normalize(include, NULL);
        if (!IsSystemInclude(include))
          includes_.insert(include);
      } else if (FilterInputFilename(line)) {
        // Drop it.
        // TODO: if we support compiling multiple output files in a single
        // cl.exe invocation, we should stash the filename.
      } else {
        if (extra_output) {
          extra_output->append(line);
          extra_output->append("\n");
        } else {
          printf("%s\n", line.c_str());
        }
      }

      if (ofs < output.size() && output[ofs] == '\r')
        ++ofs;
      if (ofs < output.size() && output[ofs] == '\n')
        ++ofs;
      output = output.substr(ofs);
    }
  }

  if (WaitForSingleObject(process_info.hProcess, INFINITE) == WAIT_FAILED)
    Win32Fatal("WaitForSingleObject");

  DWORD exit_code = 0;
  if (!GetExitCodeProcess(process_info.hProcess, &exit_code))
    Win32Fatal("GetExitCodeProcess");

  if (!CloseHandle(stdout_read) ||
      !CloseHandle(process_info.hProcess) ||
      !CloseHandle(process_info.hThread)) {
    Win32Fatal("CloseHandle");
  }

  return exit_code;
}

vector<string> CLWrapper::GetEscapedResult() {
  vector<string> result;
  for (set<string>::iterator i = includes_.begin(); i != includes_.end(); ++i) {
    result.push_back(EscapeForDepfile(*i));
  }
  return result;
}