cmcldeps.cxx 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. // Copyright 2011 Google Inc. All Rights Reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. // Wrapper around cl that adds /showIncludes to command line, and uses that to
  15. // generate .d files that match the style from gcc -MD.
  16. //
  17. // /showIncludes is equivalent to -MD, not -MMD, that is, system headers are
  18. // included.
  19. #include <windows.h>
  20. #include <sstream>
  21. #include <cmSystemTools.h>
  22. // We don't want any wildcard expansion.
  23. // See http://msdn.microsoft.com/en-us/library/zay8tzh6(v=vs.85).aspx
  24. void _setargv() {}
  25. static void Fatal(const char* msg, ...) {
  26. va_list ap;
  27. fprintf(stderr, "ninja: FATAL: ");
  28. va_start(ap, msg);
  29. vfprintf(stderr, msg, ap);
  30. va_end(ap);
  31. fprintf(stderr, "\n");
  32. // On Windows, some tools may inject extra threads.
  33. // exit() may block on locks held by those threads, so forcibly exit.
  34. fflush(stderr);
  35. fflush(stdout);
  36. ExitProcess(1);
  37. }
  38. static void usage(const char* msg) {
  39. Fatal("%s\n\nusage:\n "
  40. "cmcldeps "
  41. "<language C, CXX or RC> "
  42. "<source file path> "
  43. "<output path for *.d file> "
  44. "<output path for *.obj file> "
  45. "<prefix of /showIncludes> "
  46. "<path to cl.exe> "
  47. "<path to tool (cl or rc)> "
  48. "<rest of command ...>\n", msg);
  49. }
  50. static std::string trimLeadingSpace(const std::string& cmdline) {
  51. int i = 0;
  52. for (; cmdline[i] == ' '; ++i)
  53. ;
  54. return cmdline.substr(i);
  55. }
  56. static void doEscape(std::string& str, const std::string& search,
  57. const std::string& repl) {
  58. std::string::size_type pos = 0;
  59. while ((pos = str.find(search, pos)) != std::string::npos) {
  60. str.replace(pos, search.size(), repl);
  61. pos += repl.size();
  62. }
  63. }
  64. // Strips one argument from the cmdline and returns it. "surrounding quotes"
  65. // are removed from the argument if there were any.
  66. static std::string getArg(std::string& cmdline) {
  67. std::string ret;
  68. bool in_quoted = false;
  69. unsigned int i = 0;
  70. cmdline = trimLeadingSpace(cmdline);
  71. for (;; ++i) {
  72. if (i >= cmdline.size())
  73. usage("Couldn't parse arguments.");
  74. if (!in_quoted && cmdline[i] == ' ')
  75. break; // "a b" "x y"
  76. if (cmdline[i] == '"')
  77. in_quoted = !in_quoted;
  78. }
  79. ret = cmdline.substr(0, i);
  80. if (ret[0] == '"' && ret[i - 1] == '"')
  81. ret = ret.substr(1, ret.size() - 2);
  82. cmdline = cmdline.substr(i);
  83. return ret;
  84. }
  85. static void parseCommandLine(LPTSTR wincmdline,
  86. std::string& lang,
  87. std::string& srcfile,
  88. std::string& dfile,
  89. std::string& objfile,
  90. std::string& prefix,
  91. std::string& clpath,
  92. std::string& binpath,
  93. std::string& rest) {
  94. std::string cmdline(wincmdline);
  95. /* self */ getArg(cmdline);
  96. lang = getArg(cmdline);
  97. srcfile = getArg(cmdline);
  98. dfile = getArg(cmdline);
  99. objfile = getArg(cmdline);
  100. prefix = getArg(cmdline);
  101. clpath = getArg(cmdline);
  102. binpath = getArg(cmdline);
  103. rest = trimLeadingSpace(cmdline);
  104. }
  105. static void outputDepFile(const std::string& dfile, const std::string& objfile,
  106. std::vector<std::string>& incs) {
  107. if (dfile.empty())
  108. return;
  109. // strip duplicates
  110. std::sort(incs.begin(), incs.end());
  111. incs.erase(std::unique(incs.begin(), incs.end()), incs.end());
  112. FILE* out = fopen(dfile.c_str(), "wb");
  113. // FIXME should this be fatal or not? delete obj? delete d?
  114. if (!out)
  115. return;
  116. std::string tmp = objfile;
  117. doEscape(tmp, " ", "\\ ");
  118. fprintf(out, "%s: \\\n", tmp.c_str());
  119. std::vector<std::string>::iterator it = incs.begin();
  120. for (; it != incs.end(); ++it) {
  121. tmp = *it;
  122. doEscape(tmp, "\\", "/");
  123. doEscape(tmp, " ", "\\ ");
  124. fprintf(out, "%s \\\n", tmp.c_str());
  125. }
  126. fprintf(out, "\n");
  127. fclose(out);
  128. }
  129. bool startsWith(const std::string& str, const std::string& what) {
  130. return str.compare(0, what.size(), what) == 0;
  131. }
  132. bool contains(const std::string& str, const std::string& what) {
  133. return str.find(what) != std::string::npos;
  134. }
  135. std::string replace(const std::string& str, const std::string& what,
  136. const std::string& replacement) {
  137. size_t pos = str.find(what);
  138. if (pos == std::string::npos)
  139. return str;
  140. std::string replaced = str;
  141. return replaced.replace(pos, what.size(), replacement);
  142. }
  143. static int process( const std::string& srcfilename,
  144. const std::string& dfile,
  145. const std::string& objfile,
  146. const std::string& prefix,
  147. const std::string& cmd,
  148. const std::string& dir = "",
  149. bool quiet = false)
  150. {
  151. std::string output;
  152. // break up command line into a vector
  153. std::vector<std::string> args;
  154. cmSystemTools::ParseWindowsCommandLine(cmd.c_str(), args);
  155. // convert to correct vector type for RunSingleCommand
  156. std::vector<cmStdString> command;
  157. for(std::vector<std::string>::iterator i = args.begin();
  158. i != args.end(); ++i)
  159. {
  160. command.push_back(i->c_str());
  161. }
  162. // run the command
  163. int exit_code = 0;
  164. bool run = cmSystemTools::RunSingleCommand(command, &output, &exit_code,
  165. dir.c_str(), cmSystemTools::OUTPUT_NONE);
  166. // process the include directives and output everything else
  167. std::stringstream ss(output);
  168. std::string line;
  169. std::vector<std::string> includes;
  170. bool isFirstLine = true; // cl prints always first the source filename
  171. while (std::getline(ss, line)) {
  172. if (startsWith(line, prefix)) {
  173. std::string inc = trimLeadingSpace(line.substr(prefix.size()).c_str());
  174. if (inc[inc.size() - 1] == '\r') // blech, stupid \r\n
  175. inc = inc.substr(0, inc.size() - 1);
  176. includes.push_back(inc);
  177. } else {
  178. if (!isFirstLine || !startsWith(line, srcfilename)) {
  179. if (!quiet || exit_code != 0) {
  180. fprintf(stdout, "%s\n", line.c_str());
  181. }
  182. } else {
  183. isFirstLine = false;
  184. }
  185. }
  186. }
  187. // don't update .d until/unless we succeed compilation
  188. if (run && exit_code == 0)
  189. outputDepFile(dfile, objfile, includes);
  190. return exit_code;
  191. }
  192. int main() {
  193. // Use the Win32 API instead of argc/argv so we can avoid interpreting the
  194. // rest of command line after the .d and .obj. Custom parsing seemed
  195. // preferable to the ugliness you get into in trying to re-escape quotes for
  196. // subprocesses, so by avoiding argc/argv, the subprocess is called with
  197. // the same command line verbatim.
  198. std::string lang, srcfile, dfile, objfile, prefix, cl, binpath, rest;
  199. parseCommandLine(GetCommandLine(), lang, srcfile, dfile, objfile,
  200. prefix, cl, binpath, rest);
  201. // needed to suppress filename output of msvc tools
  202. std::string srcfilename;
  203. {
  204. std::string::size_type pos = srcfile.rfind("\\");
  205. if (pos == std::string::npos) {
  206. srcfilename = srcfile;
  207. } else {
  208. srcfilename = srcfile.substr(pos + 1);
  209. }
  210. }
  211. std::string nol = " /nologo ";
  212. std::string show = " /showIncludes ";
  213. if (lang == "C" || lang == "CXX") {
  214. return process(srcfilename, dfile, objfile, prefix,
  215. binpath + nol + show + rest);
  216. } else if (lang == "RC") {
  217. // "misuse" cl.exe to get headers from .rc files
  218. std::string clrest = rest;
  219. // rc: /fo x.dir\x.rc.res -> cl: /out:x.dir\x.rc.res.dep.obj
  220. clrest = replace(clrest, "/fo", "/out:");
  221. clrest = replace(clrest, objfile, objfile + ".dep.obj ");
  222. // rc: src\x\x.rc -> cl: /Tc src\x\x.rc
  223. if (srcfile.find(" ") != std::string::npos)
  224. srcfile = "\"" + srcfile + "\"";
  225. clrest = replace(clrest, srcfile, "/Tc " + srcfile);
  226. cl = "\"" + cl + "\" /P /DRC_INVOKED ";
  227. // call cl in object dir so the .i is generated there
  228. std::string objdir;
  229. {
  230. std::string::size_type pos = objfile.rfind("\\");
  231. if (pos != std::string::npos) {
  232. objdir = objfile.substr(0, pos);
  233. }
  234. }
  235. // extract dependencies with cl.exe
  236. int exit_code = process(srcfilename, dfile, objfile,
  237. prefix, cl + nol + show + clrest, objdir, true);
  238. if (exit_code != 0)
  239. return exit_code;
  240. // compile rc file with rc.exe
  241. return process(srcfilename, "" , objfile, prefix, binpath + " " + rest);
  242. }
  243. usage("Invalid language specified.");
  244. return 1;
  245. }