2012-06-27 20:28:12 +04:00
|
|
|
// Copyright 2011 Google Inc. All Rights Reserved.
|
2012-06-08 18:31:54 +04:00
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
|
|
|
|
|
2012-06-27 20:28:12 +04:00
|
|
|
// 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.
|
2012-06-15 15:46:18 +04:00
|
|
|
|
2016-04-29 16:40:20 +03:00
|
|
|
#include <cmSystemTools.h>
|
|
|
|
#include <cmsys/Encoding.hxx>
|
2012-06-08 18:31:54 +04:00
|
|
|
|
2012-06-27 20:28:12 +04:00
|
|
|
#include <sstream>
|
2016-04-29 17:53:13 +03:00
|
|
|
#include <windows.h>
|
2012-06-08 18:31:54 +04:00
|
|
|
|
2012-06-27 20:28:12 +04:00
|
|
|
// We don't want any wildcard expansion.
|
|
|
|
// See http://msdn.microsoft.com/en-us/library/zay8tzh6(v=vs.85).aspx
|
|
|
|
void _setargv() {}
|
2012-06-08 18:31:54 +04:00
|
|
|
|
|
|
|
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) {
|
2012-06-14 12:35:06 +04:00
|
|
|
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)> "
|
2012-06-08 18:31:54 +04:00
|
|
|
"<rest of command ...>\n", msg);
|
|
|
|
}
|
|
|
|
|
2012-06-16 14:31:50 +04:00
|
|
|
static std::string trimLeadingSpace(const std::string& cmdline) {
|
2012-06-08 18:31:54 +04:00
|
|
|
int i = 0;
|
|
|
|
for (; cmdline[i] == ' '; ++i)
|
|
|
|
;
|
|
|
|
return cmdline.substr(i);
|
|
|
|
}
|
|
|
|
|
2013-05-23 23:13:55 +04:00
|
|
|
static void replaceAll(std::string& str, const std::string& search,
|
|
|
|
const std::string& repl) {
|
2012-06-16 14:31:50 +04:00
|
|
|
std::string::size_type pos = 0;
|
|
|
|
while ((pos = str.find(search, pos)) != std::string::npos) {
|
2012-06-08 18:31:54 +04:00
|
|
|
str.replace(pos, search.size(), repl);
|
|
|
|
pos += repl.size();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-23 23:13:55 +04:00
|
|
|
bool startsWith(const std::string& str, const std::string& what) {
|
|
|
|
return str.compare(0, what.size(), what) == 0;
|
|
|
|
}
|
|
|
|
|
2012-06-08 18:31:54 +04:00
|
|
|
// Strips one argument from the cmdline and returns it. "surrounding quotes"
|
|
|
|
// are removed from the argument if there were any.
|
2012-06-16 14:31:50 +04:00
|
|
|
static std::string getArg(std::string& cmdline) {
|
|
|
|
std::string ret;
|
2012-06-08 18:31:54 +04:00
|
|
|
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] == ' ')
|
2012-06-10 17:22:01 +04:00
|
|
|
break; // "a b" "x y"
|
2012-06-08 18:31:54 +04:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2013-12-05 09:17:24 +04:00
|
|
|
static void parseCommandLine(LPWSTR wincmdline,
|
2012-06-16 14:31:50 +04:00
|
|
|
std::string& lang,
|
|
|
|
std::string& srcfile,
|
|
|
|
std::string& dfile,
|
|
|
|
std::string& objfile,
|
|
|
|
std::string& prefix,
|
|
|
|
std::string& clpath,
|
|
|
|
std::string& binpath,
|
|
|
|
std::string& rest) {
|
2013-12-05 09:17:24 +04:00
|
|
|
std::string cmdline = cmsys::Encoding::ToNarrow(wincmdline);
|
2012-06-08 18:31:54 +04:00
|
|
|
/* self */ getArg(cmdline);
|
2012-06-13 19:14:16 +04:00
|
|
|
lang = getArg(cmdline);
|
2012-06-08 18:31:54 +04:00
|
|
|
srcfile = getArg(cmdline);
|
|
|
|
dfile = getArg(cmdline);
|
|
|
|
objfile = getArg(cmdline);
|
|
|
|
prefix = getArg(cmdline);
|
|
|
|
clpath = getArg(cmdline);
|
2012-06-13 19:14:16 +04:00
|
|
|
binpath = getArg(cmdline);
|
2012-06-08 18:31:54 +04:00
|
|
|
rest = trimLeadingSpace(cmdline);
|
|
|
|
}
|
|
|
|
|
2013-05-23 23:13:55 +04:00
|
|
|
// 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, " ", "\\ ");
|
|
|
|
}
|
|
|
|
|
2012-06-16 14:31:50 +04:00
|
|
|
static void outputDepFile(const std::string& dfile, const std::string& objfile,
|
|
|
|
std::vector<std::string>& incs) {
|
2012-06-08 18:31:54 +04:00
|
|
|
|
2012-06-13 19:14:16 +04:00
|
|
|
if (dfile.empty())
|
|
|
|
return;
|
|
|
|
|
2012-06-08 18:31:54 +04:00
|
|
|
// strip duplicates
|
2012-06-17 12:08:57 +04:00
|
|
|
std::sort(incs.begin(), incs.end());
|
|
|
|
incs.erase(std::unique(incs.begin(), incs.end()), incs.end());
|
2012-06-08 18:31:54 +04:00
|
|
|
|
2014-01-04 09:47:13 +04:00
|
|
|
FILE* out = cmsys::SystemTools::Fopen(dfile.c_str(), "wb");
|
2012-06-08 18:31:54 +04:00
|
|
|
|
|
|
|
// FIXME should this be fatal or not? delete obj? delete d?
|
|
|
|
if (!out)
|
|
|
|
return;
|
2013-05-23 23:13:55 +04:00
|
|
|
std::string cwd = cmSystemTools::GetCurrentWorkingDirectory();
|
|
|
|
replaceAll(cwd, "/", "\\");
|
|
|
|
cwd += "\\";
|
2012-06-08 18:31:54 +04:00
|
|
|
|
2012-06-16 14:31:50 +04:00
|
|
|
std::string tmp = objfile;
|
2013-05-23 23:13:55 +04:00
|
|
|
escapePath(tmp);
|
2012-06-10 17:22:01 +04:00
|
|
|
fprintf(out, "%s: \\\n", tmp.c_str());
|
|
|
|
|
2012-06-16 14:40:59 +04:00
|
|
|
std::vector<std::string>::iterator it = incs.begin();
|
|
|
|
for (; it != incs.end(); ++it) {
|
|
|
|
tmp = *it;
|
2013-05-23 23:13:55 +04:00
|
|
|
// 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);
|
2012-06-08 18:31:54 +04:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2012-06-13 21:20:01 +04:00
|
|
|
std::string replace(const std::string& str, const std::string& what,
|
|
|
|
const std::string& replacement) {
|
2012-06-13 19:14:16 +04:00
|
|
|
size_t pos = str.find(what);
|
|
|
|
if (pos == std::string::npos)
|
|
|
|
return str;
|
|
|
|
std::string replaced = str;
|
|
|
|
return replaced.replace(pos, what.size(), replacement);
|
|
|
|
}
|
2012-06-08 18:31:54 +04:00
|
|
|
|
|
|
|
|
|
|
|
|
2012-06-16 14:31:50 +04:00
|
|
|
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 = "",
|
2012-06-27 20:28:12 +04:00
|
|
|
bool quiet = false)
|
|
|
|
{
|
|
|
|
std::string output;
|
|
|
|
// break up command line into a vector
|
|
|
|
std::vector<std::string> args;
|
2012-06-28 15:46:43 +04:00
|
|
|
cmSystemTools::ParseWindowsCommandLine(cmd.c_str(), args);
|
2012-06-27 20:28:12 +04:00
|
|
|
// convert to correct vector type for RunSingleCommand
|
2014-02-10 09:21:34 +04:00
|
|
|
std::vector<std::string> command;
|
2012-06-27 20:28:12 +04:00
|
|
|
for(std::vector<std::string>::iterator i = args.begin();
|
|
|
|
i != args.end(); ++i)
|
|
|
|
{
|
|
|
|
command.push_back(i->c_str());
|
|
|
|
}
|
|
|
|
// run the command
|
2012-06-28 15:46:43 +04:00
|
|
|
int exit_code = 0;
|
2015-04-20 22:36:57 +03:00
|
|
|
bool run = cmSystemTools::RunSingleCommand(command, &output, &output,
|
|
|
|
&exit_code, dir.c_str(),
|
|
|
|
cmSystemTools::OUTPUT_NONE);
|
2012-06-28 15:46:43 +04:00
|
|
|
|
2012-06-08 18:31:54 +04:00
|
|
|
// process the include directives and output everything else
|
2012-06-16 14:31:50 +04:00
|
|
|
std::stringstream ss(output);
|
|
|
|
std::string line;
|
|
|
|
std::vector<std::string> includes;
|
2012-06-08 18:31:54 +04:00
|
|
|
bool isFirstLine = true; // cl prints always first the source filename
|
2012-06-17 12:08:57 +04:00
|
|
|
while (std::getline(ss, line)) {
|
2012-06-08 18:31:54 +04:00
|
|
|
if (startsWith(line, prefix)) {
|
2012-06-16 14:31:50 +04:00
|
|
|
std::string inc = trimLeadingSpace(line.substr(prefix.size()).c_str());
|
2012-06-14 16:37:08 +04:00
|
|
|
if (inc[inc.size() - 1] == '\r') // blech, stupid \r\n
|
|
|
|
inc = inc.substr(0, inc.size() - 1);
|
|
|
|
includes.push_back(inc);
|
2012-06-08 18:31:54 +04:00
|
|
|
} else {
|
2012-06-14 12:35:06 +04:00
|
|
|
if (!isFirstLine || !startsWith(line, srcfilename)) {
|
2012-07-15 16:47:02 +04:00
|
|
|
if (!quiet || exit_code != 0) {
|
2012-06-13 19:14:16 +04:00
|
|
|
fprintf(stdout, "%s\n", line.c_str());
|
|
|
|
}
|
2012-06-08 18:31:54 +04:00
|
|
|
} else {
|
|
|
|
isFirstLine = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// don't update .d until/unless we succeed compilation
|
2012-06-28 15:46:43 +04:00
|
|
|
if (run && exit_code == 0)
|
|
|
|
outputDepFile(dfile, objfile, includes);
|
2012-06-08 18:31:54 +04:00
|
|
|
|
2012-06-28 15:46:43 +04:00
|
|
|
return exit_code;
|
2012-06-08 18:31:54 +04:00
|
|
|
}
|
2012-06-13 19:14:16 +04:00
|
|
|
|
|
|
|
|
|
|
|
int main() {
|
|
|
|
|
2013-05-04 16:53:26 +04:00
|
|
|
// Use the Win32 API instead of argc/argv so we can avoid interpreting the
|
2012-06-13 19:14:16 +04:00
|
|
|
// 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.
|
|
|
|
|
2012-06-16 14:31:50 +04:00
|
|
|
std::string lang, srcfile, dfile, objfile, prefix, cl, binpath, rest;
|
2013-12-05 09:17:24 +04:00
|
|
|
parseCommandLine(GetCommandLineW(), lang, srcfile, dfile, objfile,
|
2012-06-13 19:14:16 +04:00
|
|
|
prefix, cl, binpath, rest);
|
|
|
|
|
2012-06-14 12:35:06 +04:00
|
|
|
// needed to suppress filename output of msvc tools
|
2012-06-16 14:31:50 +04:00
|
|
|
std::string srcfilename;
|
2012-11-07 16:46:44 +04:00
|
|
|
{
|
2012-06-14 12:35:06 +04:00
|
|
|
std::string::size_type pos = srcfile.rfind("\\");
|
2012-09-04 22:23:53 +04:00
|
|
|
if (pos == std::string::npos) {
|
|
|
|
srcfilename = srcfile;
|
|
|
|
} else {
|
2012-06-14 12:35:06 +04:00
|
|
|
srcfilename = srcfile.substr(pos + 1);
|
|
|
|
}
|
2012-11-07 16:46:44 +04:00
|
|
|
}
|
2012-06-13 19:14:16 +04:00
|
|
|
|
2012-06-15 15:07:05 +04:00
|
|
|
std::string nol = " /nologo ";
|
2012-06-14 12:35:06 +04:00
|
|
|
std::string show = " /showIncludes ";
|
|
|
|
if (lang == "C" || lang == "CXX") {
|
|
|
|
return process(srcfilename, dfile, objfile, prefix,
|
2012-06-15 15:07:05 +04:00
|
|
|
binpath + nol + show + rest);
|
2012-06-14 12:35:06 +04:00
|
|
|
} else if (lang == "RC") {
|
2012-06-13 19:14:16 +04:00
|
|
|
// "misuse" cl.exe to get headers from .rc files
|
|
|
|
|
2012-06-16 14:31:50 +04:00
|
|
|
std::string clrest = rest;
|
2012-06-14 12:35:06 +04:00
|
|
|
// 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 ");
|
2012-07-16 18:48:32 +04:00
|
|
|
|
2012-06-14 12:35:06 +04:00
|
|
|
// rc: src\x\x.rc -> cl: /Tc src\x\x.rc
|
2012-07-16 18:48:32 +04:00
|
|
|
if (srcfile.find(" ") != std::string::npos)
|
|
|
|
srcfile = "\"" + srcfile + "\"";
|
2012-06-14 12:35:06 +04:00
|
|
|
clrest = replace(clrest, srcfile, "/Tc " + srcfile);
|
|
|
|
|
2012-06-15 20:56:34 +04:00
|
|
|
cl = "\"" + cl + "\" /P /DRC_INVOKED ";
|
|
|
|
|
|
|
|
// call cl in object dir so the .i is generated there
|
2012-06-16 14:31:50 +04:00
|
|
|
std::string objdir;
|
2012-11-07 16:46:44 +04:00
|
|
|
{
|
2012-06-15 20:56:34 +04:00
|
|
|
std::string::size_type pos = objfile.rfind("\\");
|
2012-06-16 14:31:50 +04:00
|
|
|
if (pos != std::string::npos) {
|
2012-06-15 20:56:34 +04:00
|
|
|
objdir = objfile.substr(0, pos);
|
|
|
|
}
|
2012-11-07 16:46:44 +04:00
|
|
|
}
|
2012-06-14 12:35:06 +04:00
|
|
|
|
|
|
|
// extract dependencies with cl.exe
|
2012-07-15 16:47:02 +04:00
|
|
|
int exit_code = process(srcfilename, dfile, objfile,
|
|
|
|
prefix, cl + nol + show + clrest, objdir, true);
|
|
|
|
|
|
|
|
if (exit_code != 0)
|
|
|
|
return exit_code;
|
2012-06-13 19:14:16 +04:00
|
|
|
|
2012-06-14 12:35:06 +04:00
|
|
|
// compile rc file with rc.exe
|
2012-06-15 19:52:17 +04:00
|
|
|
return process(srcfilename, "" , objfile, prefix, binpath + " " + rest);
|
2012-06-13 19:14:16 +04:00
|
|
|
}
|
|
|
|
|
2012-06-14 12:35:06 +04:00
|
|
|
usage("Invalid language specified.");
|
|
|
|
return 1;
|
2012-06-13 19:14:16 +04:00
|
|
|
}
|