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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
|
// 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.
// Wrapper around cl that adds /showIncludes to command line, and uses that to
// generate .d files that match the style from gcc -MD.
//
// /showIncludes is equivalent to -MD, not -MMD, that is, system headers are
// included.
#include <windows.h>
#include <sstream>
#include <cmSystemTools.h>
#include <cmsys/Encoding.hxx>
// We don't want any wildcard expansion.
// See http://msdn.microsoft.com/en-us/library/zay8tzh6(v=vs.85).aspx
void _setargv() {}
static void Fatal(const char* msg, ...) {
va_list ap;
fprintf(stderr, "ninja: FATAL: ");
va_start(ap, msg);
vfprintf(stderr, msg, ap);
va_end(ap);
fprintf(stderr, "\n");
// On Windows, some tools may inject extra threads.
// exit() may block on locks held by those threads, so forcibly exit.
fflush(stderr);
fflush(stdout);
ExitProcess(1);
}
static void usage(const char* msg) {
Fatal("%s\n\nusage:\n "
"cmcldeps "
"<language C, CXX or RC> "
"<source file path> "
"<output path for *.d file> "
"<output path for *.obj file> "
"<prefix of /showIncludes> "
"<path to cl.exe> "
"<path to tool (cl or rc)> "
"<rest of command ...>\n", msg);
}
static std::string trimLeadingSpace(const std::string& cmdline) {
int i = 0;
for (; cmdline[i] == ' '; ++i)
;
return cmdline.substr(i);
}
static void replaceAll(std::string& str, const std::string& search,
const std::string& repl) {
std::string::size_type pos = 0;
while ((pos = str.find(search, pos)) != std::string::npos) {
str.replace(pos, search.size(), repl);
pos += repl.size();
}
}
bool startsWith(const std::string& str, const std::string& what) {
return str.compare(0, what.size(), what) == 0;
}
// Strips one argument from the cmdline and returns it. "surrounding quotes"
// are removed from the argument if there were any.
static std::string getArg(std::string& cmdline) {
std::string ret;
bool in_quoted = false;
unsigned int i = 0;
cmdline = trimLeadingSpace(cmdline);
for (;; ++i) {
if (i >= cmdline.size())
usage("Couldn't parse arguments.");
if (!in_quoted && cmdline[i] == ' ')
break; // "a b" "x y"
if (cmdline[i] == '"')
in_quoted = !in_quoted;
}
ret = cmdline.substr(0, i);
if (ret[0] == '"' && ret[i - 1] == '"')
ret = ret.substr(1, ret.size() - 2);
cmdline = cmdline.substr(i);
return ret;
}
static void parseCommandLine(LPWSTR wincmdline,
std::string& lang,
std::string& srcfile,
std::string& dfile,
std::string& objfile,
std::string& prefix,
std::string& clpath,
std::string& binpath,
std::string& rest) {
std::string cmdline = cmsys::Encoding::ToNarrow(wincmdline);
/* self */ getArg(cmdline);
lang = getArg(cmdline);
srcfile = getArg(cmdline);
dfile = getArg(cmdline);
objfile = getArg(cmdline);
prefix = getArg(cmdline);
clpath = getArg(cmdline);
binpath = getArg(cmdline);
rest = trimLeadingSpace(cmdline);
}
// Not all backslashes need to be escaped in a depfile, but it's easier that
// way. See the re2c grammar in ninja's source code for more info.
static void escapePath(std::string &path) {
replaceAll(path, "\\", "\\\\");
replaceAll(path, " ", "\\ ");
}
static void outputDepFile(const std::string& dfile, const std::string& objfile,
std::vector<std::string>& incs) {
if (dfile.empty())
return;
// strip duplicates
std::sort(incs.begin(), incs.end());
incs.erase(std::unique(incs.begin(), incs.end()), incs.end());
FILE* out = fopen(dfile.c_str(), "wb");
// FIXME should this be fatal or not? delete obj? delete d?
if (!out)
return;
std::string cwd = cmSystemTools::GetCurrentWorkingDirectory();
replaceAll(cwd, "/", "\\");
cwd += "\\";
std::string tmp = objfile;
escapePath(tmp);
fprintf(out, "%s: \\\n", tmp.c_str());
std::vector<std::string>::iterator it = incs.begin();
for (; it != incs.end(); ++it) {
tmp = *it;
// The paths need to match the ones used to identify build artifacts in the
// build.ninja file. Therefore we need to canonicalize the path to use
// backward slashes and relativize the path to the build directory.
replaceAll(tmp, "/", "\\");
if (startsWith(tmp, cwd))
tmp = tmp.substr(cwd.size());
escapePath(tmp);
fprintf(out, "%s \\\n", tmp.c_str());
}
fprintf(out, "\n");
fclose(out);
}
bool contains(const std::string& str, const std::string& what) {
return str.find(what) != std::string::npos;
}
std::string replace(const std::string& str, const std::string& what,
const std::string& replacement) {
size_t pos = str.find(what);
if (pos == std::string::npos)
return str;
std::string replaced = str;
return replaced.replace(pos, what.size(), replacement);
}
static int process( const std::string& srcfilename,
const std::string& dfile,
const std::string& objfile,
const std::string& prefix,
const std::string& cmd,
const std::string& dir = "",
bool quiet = false)
{
std::string output;
// break up command line into a vector
std::vector<std::string> args;
cmSystemTools::ParseWindowsCommandLine(cmd.c_str(), args);
// convert to correct vector type for RunSingleCommand
std::vector<cmStdString> command;
for(std::vector<std::string>::iterator i = args.begin();
i != args.end(); ++i)
{
command.push_back(i->c_str());
}
// run the command
int exit_code = 0;
bool run = cmSystemTools::RunSingleCommand(command, &output, &exit_code,
dir.c_str(), cmSystemTools::OUTPUT_NONE);
// process the include directives and output everything else
std::stringstream ss(output);
std::string line;
std::vector<std::string> includes;
bool isFirstLine = true; // cl prints always first the source filename
while (std::getline(ss, line)) {
if (startsWith(line, prefix)) {
std::string inc = trimLeadingSpace(line.substr(prefix.size()).c_str());
if (inc[inc.size() - 1] == '\r') // blech, stupid \r\n
inc = inc.substr(0, inc.size() - 1);
includes.push_back(inc);
} else {
if (!isFirstLine || !startsWith(line, srcfilename)) {
if (!quiet || exit_code != 0) {
fprintf(stdout, "%s\n", line.c_str());
}
} else {
isFirstLine = false;
}
}
}
// don't update .d until/unless we succeed compilation
if (run && exit_code == 0)
outputDepFile(dfile, objfile, includes);
return exit_code;
}
int main() {
// Use the Win32 API instead of argc/argv so we can avoid interpreting the
// rest of command line after the .d and .obj. Custom parsing seemed
// preferable to the ugliness you get into in trying to re-escape quotes for
// subprocesses, so by avoiding argc/argv, the subprocess is called with
// the same command line verbatim.
std::string lang, srcfile, dfile, objfile, prefix, cl, binpath, rest;
parseCommandLine(GetCommandLineW(), lang, srcfile, dfile, objfile,
prefix, cl, binpath, rest);
// needed to suppress filename output of msvc tools
std::string srcfilename;
{
std::string::size_type pos = srcfile.rfind("\\");
if (pos == std::string::npos) {
srcfilename = srcfile;
} else {
srcfilename = srcfile.substr(pos + 1);
}
}
std::string nol = " /nologo ";
std::string show = " /showIncludes ";
if (lang == "C" || lang == "CXX") {
return process(srcfilename, dfile, objfile, prefix,
binpath + nol + show + rest);
} else if (lang == "RC") {
// "misuse" cl.exe to get headers from .rc files
std::string clrest = rest;
// rc: /fo x.dir\x.rc.res -> cl: /out:x.dir\x.rc.res.dep.obj
clrest = replace(clrest, "/fo", "/out:");
clrest = replace(clrest, objfile, objfile + ".dep.obj ");
// rc: src\x\x.rc -> cl: /Tc src\x\x.rc
if (srcfile.find(" ") != std::string::npos)
srcfile = "\"" + srcfile + "\"";
clrest = replace(clrest, srcfile, "/Tc " + srcfile);
cl = "\"" + cl + "\" /P /DRC_INVOKED ";
// call cl in object dir so the .i is generated there
std::string objdir;
{
std::string::size_type pos = objfile.rfind("\\");
if (pos != std::string::npos) {
objdir = objfile.substr(0, pos);
}
}
// extract dependencies with cl.exe
int exit_code = process(srcfilename, dfile, objfile,
prefix, cl + nol + show + clrest, objdir, true);
if (exit_code != 0)
return exit_code;
// compile rc file with rc.exe
return process(srcfilename, "" , objfile, prefix, binpath + " " + rest);
}
usage("Invalid language specified.");
return 1;
}
|