cmake/Source/cmcldeps.cxx

295 lines
9.0 KiB
C++
Raw Normal View History

2012-08-04 10:26:08 +03:00
// Copyright 2011 Google Inc. All Rights Reserved.
2012-06-27 20:52:58 +03: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-08-04 10:26:08 +03: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-27 20:52:58 +03:00
2016-10-30 18:24:19 +01:00
#include <algorithm>
2016-07-09 11:21:54 +02:00
#include <sstream>
2020-02-01 23:06:01 +01:00
2016-07-09 11:21:54 +02:00
#include <windows.h>
2020-02-01 23:06:01 +01:00
#include "cmsys/Encoding.hxx"
2020-08-30 11:54:41 +02:00
#include "cmStringAlgorithms.h"
2020-02-01 23:06:01 +01:00
#include "cmSystemTools.h"
2012-08-04 10:26:08 +03:00
// We don't want any wildcard expansion.
// See http://msdn.microsoft.com/en-us/library/zay8tzh6(v=vs.85).aspx
2016-07-09 11:21:54 +02:00
void _setargv()
{
}
2012-06-27 20:52:58 +03:00
2016-07-09 11:21:54 +02:00
static void Fatal(const char* msg, ...)
{
2012-06-27 20:52:58 +03:00
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);
}
2016-07-09 11:21:54 +02:00
static void usage(const char* msg)
{
2012-06-27 20:52:58 +03:00
Fatal("%s\n\nusage:\n "
2016-07-09 11:21:54 +02:00
"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);
2012-06-27 20:52:58 +03:00
}
2020-08-30 11:54:41 +02:00
static cm::string_view trimLeadingSpace(cm::string_view cmdline)
2016-07-09 11:21:54 +02:00
{
2012-06-27 20:52:58 +03:00
int i = 0;
for (; cmdline[i] == ' '; ++i)
;
return cmdline.substr(i);
}
2013-11-03 12:27:13 +02:00
static void replaceAll(std::string& str, const std::string& search,
2016-07-09 11:21:54 +02:00
const std::string& repl)
{
2012-06-27 20:52:58 +03:00
std::string::size_type pos = 0;
while ((pos = str.find(search, pos)) != std::string::npos) {
str.replace(pos, search.size(), repl);
pos += repl.size();
}
}
// Strips one argument from the cmdline and returns it. "surrounding quotes"
// are removed from the argument if there were any.
2016-07-09 11:21:54 +02:00
static std::string getArg(std::string& cmdline)
{
2012-06-27 20:52:58 +03:00
bool in_quoted = false;
unsigned int i = 0;
2020-08-30 11:54:41 +02:00
cm::string_view cmdview = trimLeadingSpace(cmdline);
size_t spaceCnt = cmdline.size() - cmdview.size();
2012-06-27 20:52:58 +03:00
for (;; ++i) {
2020-08-30 11:54:41 +02:00
if (i >= cmdview.size())
2012-06-27 20:52:58 +03:00
usage("Couldn't parse arguments.");
2020-08-30 11:54:41 +02:00
if (!in_quoted && cmdview[i] == ' ')
2012-06-27 20:52:58 +03:00
break; // "a b" "x y"
2020-08-30 11:54:41 +02:00
if (cmdview[i] == '"')
2012-06-27 20:52:58 +03:00
in_quoted = !in_quoted;
}
2020-08-30 11:54:41 +02:00
cmdview = cmdview.substr(0, i);
if (cmdview[0] == '"' && cmdview[i - 1] == '"')
cmdview = cmdview.substr(1, i - 2);
std::string ret(cmdview);
cmdline.erase(0, spaceCnt + i);
2012-06-27 20:52:58 +03:00
return ret;
}
2016-07-09 11:21:54 +02:00
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)
{
2014-08-03 19:52:23 +02:00
std::string cmdline = cmsys::Encoding::ToNarrow(wincmdline);
2012-06-27 20:52:58 +03:00
/* self */ getArg(cmdline);
lang = getArg(cmdline);
srcfile = getArg(cmdline);
dfile = getArg(cmdline);
objfile = getArg(cmdline);
prefix = getArg(cmdline);
clpath = getArg(cmdline);
binpath = getArg(cmdline);
2020-08-30 11:54:41 +02:00
rest = std::string(trimLeadingSpace(cmdline));
2012-06-27 20:52:58 +03:00
}
2013-11-03 12:27:13 +02: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.
2016-07-09 11:21:54 +02:00
static void escapePath(std::string& path)
{
2013-11-03 12:27:13 +02:00
replaceAll(path, "\\", "\\\\");
replaceAll(path, " ", "\\ ");
}
2012-06-27 20:52:58 +03:00
static void outputDepFile(const std::string& dfile, const std::string& objfile,
2016-07-09 11:21:54 +02:00
std::vector<std::string>& incs)
{
2012-06-27 20:52:58 +03:00
if (dfile.empty())
return;
// strip duplicates
std::sort(incs.begin(), incs.end());
incs.erase(std::unique(incs.begin(), incs.end()), incs.end());
2014-08-03 19:52:23 +02:00
FILE* out = cmsys::SystemTools::Fopen(dfile.c_str(), "wb");
2012-06-27 20:52:58 +03:00
// FIXME should this be fatal or not? delete obj? delete d?
if (!out)
return;
2013-11-03 12:27:13 +02:00
std::string cwd = cmSystemTools::GetCurrentWorkingDirectory();
replaceAll(cwd, "/", "\\");
cwd += "\\";
2012-06-27 20:52:58 +03:00
std::string tmp = objfile;
2013-11-03 12:27:13 +02:00
escapePath(tmp);
2012-06-27 20:52:58 +03:00
fprintf(out, "%s: \\\n", tmp.c_str());
std::vector<std::string>::iterator it = incs.begin();
for (; it != incs.end(); ++it) {
tmp = *it;
2013-11-03 12:27:13 +02: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, "/", "\\");
2020-08-30 11:54:41 +02:00
if (cmHasPrefix(tmp, cwd))
tmp.erase(0, cwd.size());
2013-11-03 12:27:13 +02:00
escapePath(tmp);
2012-06-27 20:52:58 +03:00
fprintf(out, "%s \\\n", tmp.c_str());
}
fprintf(out, "\n");
fclose(out);
}
2020-08-30 11:54:41 +02:00
static int process(cm::string_view srcfilename, const std::string& dfile,
2016-07-09 11:21:54 +02:00
const std::string& objfile, const std::string& prefix,
const std::string& cmd, const std::string& dir = "",
bool quiet = false)
2012-08-04 10:26:08 +03:00
{
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
2015-04-27 22:25:09 +02:00
std::vector<std::string> command;
2016-07-09 11:21:54 +02:00
for (std::vector<std::string>::iterator i = args.begin(); i != args.end();
++i) {
2017-04-14 19:02:05 +02:00
command.push_back(*i);
2016-07-09 11:21:54 +02:00
}
2012-08-04 10:26:08 +03:00
// run the command
int exit_code = 0;
2016-07-09 11:21:54 +02:00
bool run =
cmSystemTools::RunSingleCommand(command, &output, &output, &exit_code,
dir.c_str(), cmSystemTools::OUTPUT_NONE);
2012-06-27 20:52:58 +03:00
// process the include directives and output everything else
2016-10-30 18:24:19 +01:00
std::istringstream ss(output);
2012-06-27 20:52:58 +03:00
std::string line;
std::vector<std::string> includes;
bool isFirstLine = true; // cl prints always first the source filename
while (std::getline(ss, line)) {
2020-08-30 11:54:41 +02:00
cm::string_view inc(line);
if (cmHasPrefix(inc, prefix)) {
inc = trimLeadingSpace(inc.substr(prefix.size()));
2019-11-11 23:01:05 +01:00
if (inc.back() == '\r') // blech, stupid \r\n
2012-06-27 20:52:58 +03:00
inc = inc.substr(0, inc.size() - 1);
2020-08-30 11:54:41 +02:00
includes.emplace_back(std::string(inc));
2012-06-27 20:52:58 +03:00
} else {
2020-08-30 11:54:41 +02:00
if (!isFirstLine || !cmHasPrefix(inc, srcfilename)) {
2012-08-04 10:26:08 +03:00
if (!quiet || exit_code != 0) {
2012-06-27 20:52:58 +03:00
fprintf(stdout, "%s\n", line.c_str());
}
} else {
isFirstLine = false;
}
}
}
// don't update .d until/unless we succeed compilation
2012-08-04 10:26:08 +03:00
if (run && exit_code == 0)
outputDepFile(dfile, objfile, includes);
2012-06-27 20:52:58 +03:00
2012-08-04 10:26:08 +03:00
return exit_code;
2012-06-27 20:52:58 +03:00
}
2016-07-09 11:21:54 +02:00
int main()
{
2012-06-27 20:52:58 +03:00
2013-11-03 12:27:13 +02:00
// Use the Win32 API instead of argc/argv so we can avoid interpreting the
2012-06-27 20:52:58 +03: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.
std::string lang, srcfile, dfile, objfile, prefix, cl, binpath, rest;
2016-07-09 11:21:54 +02:00
parseCommandLine(GetCommandLineW(), lang, srcfile, dfile, objfile, prefix,
cl, binpath, rest);
2012-06-27 20:52:58 +03:00
// needed to suppress filename output of msvc tools
2020-08-30 11:54:41 +02:00
cm::string_view srcfilename(srcfile);
std::string::size_type pos = srcfile.rfind('\\');
if (pos != std::string::npos) {
srcfilename = srcfilename.substr(pos + 1);
2013-03-16 19:13:01 +02:00
}
2012-06-27 20:52:58 +03:00
2023-07-02 19:51:09 +02:00
if (lang == "RC") {
2012-06-27 20:52:58 +03:00
// "misuse" cl.exe to get headers from .rc files
2023-07-02 19:51:09 +02:00
// Make sure there is at most one /nologo option.
bool const haveNologo = (rest.find("/nologo ") != std::string::npos ||
rest.find("-nologo ") != std::string::npos);
cmSystemTools::ReplaceString(rest, "-nologo ", " ");
cmSystemTools::ReplaceString(rest, "/nologo ", " ");
2012-06-27 20:52:58 +03:00
std::string clrest = rest;
2023-07-02 19:51:09 +02:00
if (haveNologo) {
rest = "/nologo " + rest;
}
// rc /fo X.dir\x.rc.res => cl -FoX.dir\x.rc.res.obj
// The object will not actually be written.
cmSystemTools::ReplaceString(clrest, "/fo ", " ");
cmSystemTools::ReplaceString(clrest, "-fo ", " ");
cmSystemTools::ReplaceString(clrest, objfile, "-Fo" + objfile + ".obj");
2012-08-04 10:26:08 +03:00
2023-07-02 19:51:09 +02:00
cl = "\"" + cl + "\" /P /DRC_INVOKED /nologo /showIncludes /TC ";
2012-06-27 20:52:58 +03:00
// call cl in object dir so the .i is generated there
std::string objdir;
2013-03-16 19:13:01 +02:00
{
2020-08-30 11:54:41 +02:00
pos = objfile.rfind("\\");
2016-07-09 11:21:54 +02:00
if (pos != std::string::npos) {
objdir = objfile.substr(0, pos);
}
2013-03-16 19:13:01 +02:00
}
2012-06-27 20:52:58 +03:00
// extract dependencies with cl.exe
2023-07-02 19:51:09 +02:00
int exit_code =
process(srcfilename, dfile, objfile, prefix, cl + clrest, objdir, true);
2012-08-04 10:26:08 +03:00
if (exit_code != 0)
return exit_code;
2012-06-27 20:52:58 +03:00
// compile rc file with rc.exe
2022-03-29 21:10:50 +02:00
return process(srcfilename, "", objfile, prefix, binpath + " " + rest,
std::string(), true);
2012-06-27 20:52:58 +03:00
}
usage("Invalid language specified.");
return 1;
}