cmcmd.cxx 60 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799
  1. /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. file Copyright.txt or https://cmake.org/licensing for details. */
  3. #include "cmcmd.h"
  4. #include "cmAlgorithms.h"
  5. #include "cmGlobalGenerator.h"
  6. #include "cmLocalGenerator.h"
  7. #include "cmMakefile.h"
  8. #include "cmQtAutoGenerators.h"
  9. #include "cmStateDirectory.h"
  10. #include "cmStateSnapshot.h"
  11. #include "cmSystemTools.h"
  12. #include "cmUtils.hxx"
  13. #include "cmVersion.h"
  14. #include "cm_auto_ptr.hxx"
  15. #include "cmake.h"
  16. #if defined(CMAKE_BUILD_WITH_CMAKE)
  17. #include "cmDependsFortran.h" // For -E cmake_copy_f90_mod callback.
  18. #include "cmServer.h"
  19. #include "cmServerConnection.h"
  20. #endif
  21. #if defined(CMAKE_BUILD_WITH_CMAKE) && defined(_WIN32)
  22. #include "bindexplib.h"
  23. #endif
  24. #if defined(CMAKE_BUILD_WITH_CMAKE) && defined(_WIN32) && !defined(__CYGWIN__)
  25. #include "cmVisualStudioWCEPlatformParser.h"
  26. #endif
  27. #include "cmsys/Directory.hxx"
  28. #include "cmsys/FStream.hxx"
  29. #include "cmsys/Process.h"
  30. #include "cmsys/Terminal.h"
  31. #include <algorithm>
  32. #include <functional>
  33. #include <iostream>
  34. #include <map>
  35. #include <sstream>
  36. #include <stdio.h>
  37. #include <stdlib.h>
  38. #include <time.h>
  39. #include <utility>
  40. class cmConnection;
  41. int cmcmd_cmake_ninja_depends(std::vector<std::string>::const_iterator argBeg,
  42. std::vector<std::string>::const_iterator argEnd);
  43. int cmcmd_cmake_ninja_dyndep(std::vector<std::string>::const_iterator argBeg,
  44. std::vector<std::string>::const_iterator argEnd);
  45. void CMakeCommandUsage(const char* program)
  46. {
  47. std::ostringstream errorStream;
  48. #ifdef CMAKE_BUILD_WITH_CMAKE
  49. /* clang-format off */
  50. errorStream
  51. << "cmake version " << cmVersion::GetCMakeVersion() << "\n";
  52. /* clang-format on */
  53. #else
  54. /* clang-format off */
  55. errorStream
  56. << "cmake bootstrap\n";
  57. /* clang-format on */
  58. #endif
  59. // If you add new commands, change here,
  60. // and in cmakemain.cxx in the options table
  61. /* clang-format off */
  62. errorStream
  63. << "Usage: " << program << " -E <command> [arguments...]\n"
  64. << "Available commands: \n"
  65. << " capabilities - Report capabilities built into cmake "
  66. "in JSON format\n"
  67. << " chdir dir cmd [args...] - run command in a given directory\n"
  68. << " compare_files file1 file2 - check if file1 is same as file2\n"
  69. << " copy <file>... destination - copy files to destination "
  70. "(either file or directory)\n"
  71. << " copy_directory <dir>... destination - copy content of <dir>... "
  72. "directories to 'destination' directory\n"
  73. << " copy_if_different <file>... destination - copy files if it has "
  74. "changed\n"
  75. << " echo [<string>...] - displays arguments as text\n"
  76. << " echo_append [<string>...] - displays arguments as text but no new "
  77. "line\n"
  78. << " env [--unset=NAME]... [NAME=VALUE]... COMMAND [ARG]...\n"
  79. << " - run command in a modified environment\n"
  80. << " environment - display the current environment\n"
  81. << " make_directory <dir>... - create parent and <dir> directories\n"
  82. << " md5sum <file>... - create MD5 checksum of files\n"
  83. << " sha1sum <file>... - create SHA1 checksum of files\n"
  84. << " sha224sum <file>... - create SHA224 checksum of files\n"
  85. << " sha256sum <file>... - create SHA256 checksum of files\n"
  86. << " sha384sum <file>... - create SHA384 checksum of files\n"
  87. << " sha512sum <file>... - create SHA512 checksum of files\n"
  88. << " remove [-f] <file>... - remove the file(s), use -f to force "
  89. "it\n"
  90. << " remove_directory dir - remove a directory and its contents\n"
  91. << " rename oldname newname - rename a file or directory "
  92. "(on one volume)\n"
  93. << " server - start cmake in server mode\n"
  94. << " sleep <number>... - sleep for given number of seconds\n"
  95. << " tar [cxt][vf][zjJ] file.tar [file/dir1 file/dir2 ...]\n"
  96. << " - create or extract a tar or zip archive\n"
  97. << " time command [args...] - run command and return elapsed time\n"
  98. << " touch file - touch a file.\n"
  99. << " touch_nocreate file - touch a file but do not create it.\n"
  100. #if defined(_WIN32) && !defined(__CYGWIN__)
  101. << "Available on Windows only:\n"
  102. << " delete_regv key - delete registry value\n"
  103. << " env_vs8_wince sdkname - displays a batch file which sets the "
  104. "environment for the provided Windows CE SDK installed in VS2005\n"
  105. << " env_vs9_wince sdkname - displays a batch file which sets the "
  106. "environment for the provided Windows CE SDK installed in VS2008\n"
  107. << " write_regv key value - write registry value\n"
  108. #else
  109. << "Available on UNIX only:\n"
  110. << " create_symlink old new - create a symbolic link new -> old\n"
  111. #endif
  112. ;
  113. /* clang-format on */
  114. cmSystemTools::Error(errorStream.str().c_str());
  115. }
  116. static bool cmTarFilesFrom(std::string const& file,
  117. std::vector<std::string>& files)
  118. {
  119. if (cmSystemTools::FileIsDirectory(file)) {
  120. std::ostringstream e;
  121. e << "-E tar --files-from= file '" << file << "' is a directory";
  122. cmSystemTools::Error(e.str().c_str());
  123. return false;
  124. }
  125. cmsys::ifstream fin(file.c_str());
  126. if (!fin) {
  127. std::ostringstream e;
  128. e << "-E tar --files-from= file '" << file << "' not found";
  129. cmSystemTools::Error(e.str().c_str());
  130. return false;
  131. }
  132. std::string line;
  133. while (cmSystemTools::GetLineFromStream(fin, line)) {
  134. if (line.empty()) {
  135. continue;
  136. }
  137. if (cmHasLiteralPrefix(line, "--add-file=")) {
  138. files.push_back(line.substr(11));
  139. } else if (cmHasLiteralPrefix(line, "-")) {
  140. std::ostringstream e;
  141. e << "-E tar --files-from='" << file << "' file invalid line:\n"
  142. << line << "\n";
  143. cmSystemTools::Error(e.str().c_str());
  144. return false;
  145. } else {
  146. files.push_back(line);
  147. }
  148. }
  149. return true;
  150. }
  151. int cmcmd::HandleIWYU(const std::string& runCmd, const std::string&,
  152. const std::vector<std::string>& orig_cmd)
  153. {
  154. // Construct the iwyu command line by taking what was given
  155. // and adding all the arguments we give to the compiler.
  156. std::vector<std::string> iwyu_cmd;
  157. cmSystemTools::ExpandListArgument(runCmd, iwyu_cmd, true);
  158. iwyu_cmd.insert(iwyu_cmd.end(), orig_cmd.begin() + 1, orig_cmd.end());
  159. // Run the iwyu command line. Capture its stderr and hide its stdout.
  160. // Ignore its return code because the tool always returns non-zero.
  161. std::string stdErr;
  162. int ret;
  163. if (!cmSystemTools::RunSingleCommand(iwyu_cmd, nullptr, &stdErr, &ret,
  164. nullptr, cmSystemTools::OUTPUT_NONE)) {
  165. std::cerr << "Error running '" << iwyu_cmd[0] << "': " << stdErr << "\n";
  166. return 1;
  167. }
  168. // Warn if iwyu reported anything.
  169. if (stdErr.find("should remove these lines:") != std::string::npos ||
  170. stdErr.find("should add these lines:") != std::string::npos) {
  171. std::cerr << "Warning: include-what-you-use reported diagnostics:\n"
  172. << stdErr << "\n";
  173. }
  174. // always return 0 we don't want to break the compile
  175. return 0;
  176. }
  177. int cmcmd::HandleTidy(const std::string& runCmd, const std::string& sourceFile,
  178. const std::vector<std::string>& orig_cmd)
  179. {
  180. // Construct the clang-tidy command line by taking what was given
  181. // and adding our compiler command line. The clang-tidy tool will
  182. // automatically skip over the compiler itself and extract the
  183. // options.
  184. int ret;
  185. std::vector<std::string> tidy_cmd;
  186. cmSystemTools::ExpandListArgument(runCmd, tidy_cmd, true);
  187. tidy_cmd.push_back(sourceFile);
  188. tidy_cmd.push_back("--");
  189. tidy_cmd.insert(tidy_cmd.end(), orig_cmd.begin(), orig_cmd.end());
  190. // Run the tidy command line. Capture its stdout and hide its stderr.
  191. std::string stdOut;
  192. std::string stdErr;
  193. if (!cmSystemTools::RunSingleCommand(tidy_cmd, &stdOut, &stdErr, &ret,
  194. nullptr, cmSystemTools::OUTPUT_NONE)) {
  195. std::cerr << "Error running '" << tidy_cmd[0] << "': " << stdErr << "\n";
  196. return 1;
  197. }
  198. // Output the stdout from clang-tidy to stderr
  199. std::cerr << stdOut;
  200. // If clang-tidy exited with an error do the same.
  201. if (ret != 0) {
  202. std::cerr << stdErr;
  203. }
  204. return ret;
  205. }
  206. int cmcmd::HandleLWYU(const std::string& runCmd, const std::string&,
  207. const std::vector<std::string>&)
  208. {
  209. // Construct the ldd -r -u (link what you use lwyu) command line
  210. // ldd -u -r lwuy target
  211. std::vector<std::string> lwyu_cmd;
  212. lwyu_cmd.push_back("ldd");
  213. lwyu_cmd.push_back("-u");
  214. lwyu_cmd.push_back("-r");
  215. lwyu_cmd.push_back(runCmd);
  216. // Run the ldd -u -r command line.
  217. // Capture its stdout and hide its stderr.
  218. // Ignore its return code because the tool always returns non-zero
  219. // if there are any warnings, but we just want to warn.
  220. std::string stdOut;
  221. std::string stdErr;
  222. int ret;
  223. if (!cmSystemTools::RunSingleCommand(lwyu_cmd, &stdOut, &stdErr, &ret,
  224. nullptr, cmSystemTools::OUTPUT_NONE)) {
  225. std::cerr << "Error running '" << lwyu_cmd[0] << "': " << stdErr << "\n";
  226. return 1;
  227. }
  228. // Output the stdout from ldd -r -u to stderr
  229. // Warn if lwyu reported anything.
  230. if (stdOut.find("Unused direct dependencies:") != std::string::npos) {
  231. std::cerr << "Warning: " << stdOut;
  232. }
  233. return 0;
  234. }
  235. int cmcmd::HandleCppLint(const std::string& runCmd,
  236. const std::string& sourceFile,
  237. const std::vector<std::string>&)
  238. {
  239. // Construct the cpplint command line.
  240. std::vector<std::string> cpplint_cmd;
  241. cmSystemTools::ExpandListArgument(runCmd, cpplint_cmd, true);
  242. cpplint_cmd.push_back(sourceFile);
  243. // Run the cpplint command line. Capture its output.
  244. std::string stdOut;
  245. int ret;
  246. if (!cmSystemTools::RunSingleCommand(cpplint_cmd, &stdOut, &stdOut, &ret,
  247. nullptr, cmSystemTools::OUTPUT_NONE)) {
  248. std::cerr << "Error running '" << cpplint_cmd[0] << "': " << stdOut
  249. << "\n";
  250. return 1;
  251. }
  252. // Output the output from cpplint to stderr
  253. std::cerr << stdOut;
  254. return ret;
  255. }
  256. int cmcmd::HandleCppCheck(const std::string& runCmd,
  257. const std::string& sourceFile,
  258. const std::vector<std::string>& orig_cmd)
  259. {
  260. // Construct the cpplint command line.
  261. std::vector<std::string> cppcheck_cmd;
  262. cmSystemTools::ExpandListArgument(runCmd, cppcheck_cmd, true);
  263. // extract all the -D, -U, and -I options from the compile line
  264. for (auto const& opt : orig_cmd) {
  265. if (opt.size() > 2) {
  266. if ((opt[0] == '-') &&
  267. ((opt[1] == 'D') || (opt[1] == 'I') || (opt[1] == 'U'))) {
  268. cppcheck_cmd.push_back(opt);
  269. // convert cl / options to - options if needed
  270. #if defined(_WIN32)
  271. } else if ((opt[0] == '/') &&
  272. ((opt[1] == 'D') || (opt[1] == 'I') || (opt[1] == 'U'))) {
  273. std::string optcopy = opt;
  274. optcopy[0] = '-';
  275. cppcheck_cmd.push_back(optcopy);
  276. #endif
  277. }
  278. }
  279. }
  280. // add the source file
  281. cppcheck_cmd.push_back(sourceFile);
  282. // Run the cpplint command line. Capture its output.
  283. std::string stdOut;
  284. std::string stdErr;
  285. int ret;
  286. if (!cmSystemTools::RunSingleCommand(cppcheck_cmd, &stdOut, &stdErr, &ret,
  287. nullptr, cmSystemTools::OUTPUT_NONE)) {
  288. std::cerr << "Error running '" << cppcheck_cmd[0] << "': " << stdOut
  289. << "\n";
  290. return 1;
  291. }
  292. std::cerr << stdOut;
  293. // Output the output from cpplint to stderr
  294. if (stdErr.find("(error)") != std::string::npos ||
  295. stdErr.find("(warning)") != std::string::npos ||
  296. stdErr.find("(style)") != std::string::npos ||
  297. stdErr.find("(performance)") != std::string::npos ||
  298. stdErr.find("(portability)") != std::string::npos ||
  299. stdErr.find("(information)") != std::string::npos) {
  300. std::cerr << "Warning: cppcheck reported diagnostics:\n";
  301. }
  302. std::cerr << stdErr;
  303. // ignore errors so build continues
  304. return 0;
  305. }
  306. // called when args[0] == "__run_co_compile"
  307. int cmcmd::HandleCoCompileCommands(std::vector<std::string>& args)
  308. {
  309. // initialize a map from command option to handler function
  310. std::map<std::string,
  311. std::function<int(const std::string&, const std::string&,
  312. const std::vector<std::string>&)>>
  313. coCompileTypes;
  314. auto a1 = std::placeholders::_1;
  315. auto a2 = std::placeholders::_2;
  316. auto a3 = std::placeholders::_3;
  317. // create a map from option to handler function for option
  318. // if the option does not call the original command then it will need
  319. // to set runOriginalCmd to false later in this function
  320. coCompileTypes["--iwyu="] = std::bind(&cmcmd::HandleIWYU, a1, a2, a3);
  321. coCompileTypes["--tidy="] = std::bind(&cmcmd::HandleTidy, a1, a2, a3);
  322. coCompileTypes["--lwyu="] = std::bind(&cmcmd::HandleLWYU, a1, a2, a3);
  323. coCompileTypes["--cpplint="] = std::bind(&cmcmd::HandleCppLint, a1, a2, a3);
  324. coCompileTypes["--cppcheck="] =
  325. std::bind(&cmcmd::HandleCppCheck, a1, a2, a3);
  326. // copy the command options to a vector of strings
  327. std::vector<std::string> commandOptions;
  328. for (const auto& i : coCompileTypes) {
  329. commandOptions.push_back(i.first);
  330. }
  331. std::string runCmd; // command to be run from --thing=command
  332. std::string sourceFile; // store --source=
  333. std::string commandFound; // the command that was in the args list
  334. std::vector<std::string> orig_cmd;
  335. bool doing_options = true;
  336. for (std::string::size_type cc = 2; cc < args.size(); cc++) {
  337. std::string const& arg = args[cc];
  338. // if the arg is -- then the rest of the args after
  339. // go into orig_cmd
  340. if (arg == "--") {
  341. doing_options = false;
  342. } else if (doing_options) {
  343. bool optionFound = false;
  344. // check arg against all the commandOptions
  345. for (auto const& command : commandOptions) {
  346. if (arg.compare(0, command.size(), command) == 0) {
  347. optionFound = true;
  348. runCmd = arg.substr(command.size());
  349. commandFound = command;
  350. }
  351. }
  352. // check arg with --source=
  353. if (cmHasLiteralPrefix(arg, "--source=")) {
  354. sourceFile = arg.substr(9);
  355. optionFound = true;
  356. }
  357. // if it was not a commandOptions or --source then error
  358. if (!optionFound) {
  359. std::cerr << "__run_co_compile given unknown argument: " << arg
  360. << "\n";
  361. return 1;
  362. }
  363. } else { // if not doing_options then push to orig_cmd
  364. orig_cmd.push_back(arg);
  365. }
  366. }
  367. if (commandFound.empty()) {
  368. std::cerr << "__run_co_compile missing command to run. Looking for one of "
  369. "the following:\n";
  370. for (const auto& i : commandOptions) {
  371. std::cerr << i << "\n";
  372. }
  373. return 1;
  374. }
  375. // Default is to run the original command found after -- if the option
  376. // does not need to do that, it should be specified here, currently only
  377. // lwyu does that.
  378. bool runOriginalCmd = true;
  379. if (commandFound == "--lwyu=") {
  380. runOriginalCmd = false;
  381. }
  382. if (runOriginalCmd && orig_cmd.empty()) {
  383. std::cerr << "__run_co_compile missing compile command after --\n";
  384. return 1;
  385. }
  386. // call the command handler here
  387. int ret = coCompileTypes[commandFound](runCmd, sourceFile, orig_cmd);
  388. // if the command returns non-zero then return and fail.
  389. // for commands that do not want to break the build, they should return
  390. // 0 no matter what.
  391. if (ret != 0) {
  392. return ret;
  393. }
  394. // if there is no original command to run return now
  395. if (!runOriginalCmd) {
  396. return ret;
  397. }
  398. // Now run the real compiler command and return its result value
  399. if (!cmSystemTools::RunSingleCommand(orig_cmd, nullptr, nullptr, &ret,
  400. nullptr,
  401. cmSystemTools::OUTPUT_PASSTHROUGH)) {
  402. std::cerr << "Error running '" << orig_cmd[0] << "'\n";
  403. return 1;
  404. }
  405. // return the return value from the original compiler command
  406. return ret;
  407. }
  408. int cmcmd::ExecuteCMakeCommand(std::vector<std::string>& args)
  409. {
  410. // IF YOU ADD A NEW COMMAND, DOCUMENT IT ABOVE and in cmakemain.cxx
  411. if (args.size() > 1) {
  412. // Copy file
  413. if (args[1] == "copy" && args.size() > 3) {
  414. // If multiple source files specified,
  415. // then destination must be directory
  416. if ((args.size() > 4) &&
  417. (!cmSystemTools::FileIsDirectory(args[args.size() - 1]))) {
  418. std::cerr << "Error: Target (for copy command) \""
  419. << args[args.size() - 1] << "\" is not a directory.\n";
  420. return 1;
  421. }
  422. // If error occurs we want to continue copying next files.
  423. bool return_value = false;
  424. for (std::string::size_type cc = 2; cc < args.size() - 1; cc++) {
  425. if (!cmSystemTools::cmCopyFile(args[cc].c_str(),
  426. args[args.size() - 1].c_str())) {
  427. std::cerr << "Error copying file \"" << args[cc] << "\" to \""
  428. << args[args.size() - 1] << "\".\n";
  429. return_value = true;
  430. }
  431. }
  432. return return_value;
  433. }
  434. // Copy file if different.
  435. if (args[1] == "copy_if_different" && args.size() > 3) {
  436. // If multiple source files specified,
  437. // then destination must be directory
  438. if ((args.size() > 4) &&
  439. (!cmSystemTools::FileIsDirectory(args[args.size() - 1]))) {
  440. std::cerr << "Error: Target (for copy_if_different command) \""
  441. << args[args.size() - 1] << "\" is not a directory.\n";
  442. return 1;
  443. }
  444. // If error occurs we want to continue copying next files.
  445. bool return_value = false;
  446. for (std::string::size_type cc = 2; cc < args.size() - 1; cc++) {
  447. if (!cmSystemTools::CopyFileIfDifferent(
  448. args[cc].c_str(), args[args.size() - 1].c_str())) {
  449. std::cerr << "Error copying file (if different) from \"" << args[cc]
  450. << "\" to \"" << args[args.size() - 1] << "\".\n";
  451. return_value = true;
  452. }
  453. }
  454. return return_value;
  455. }
  456. // Copy directory content
  457. if (args[1] == "copy_directory" && args.size() > 3) {
  458. // If error occurs we want to continue copying next files.
  459. bool return_value = false;
  460. for (std::string::size_type cc = 2; cc < args.size() - 1; cc++) {
  461. if (!cmSystemTools::CopyADirectory(args[cc], args[args.size() - 1])) {
  462. std::cerr << "Error copying directory from \"" << args[cc]
  463. << "\" to \"" << args[args.size() - 1] << "\".\n";
  464. return_value = true;
  465. }
  466. }
  467. return return_value;
  468. }
  469. // Rename a file or directory
  470. if (args[1] == "rename" && args.size() == 4) {
  471. if (!cmSystemTools::RenameFile(args[2].c_str(), args[3].c_str())) {
  472. std::string e = cmSystemTools::GetLastSystemError();
  473. std::cerr << "Error renaming from \"" << args[2] << "\" to \""
  474. << args[3] << "\": " << e << "\n";
  475. return 1;
  476. }
  477. return 0;
  478. }
  479. // Compare files
  480. if (args[1] == "compare_files" && args.size() == 4) {
  481. if (cmSystemTools::FilesDiffer(args[2], args[3])) {
  482. std::cerr << "Files \"" << args[2] << "\" to \"" << args[3]
  483. << "\" are different.\n";
  484. return 1;
  485. }
  486. return 0;
  487. }
  488. #if defined(_WIN32) && defined(CMAKE_BUILD_WITH_CMAKE)
  489. else if (args[1] == "__create_def") {
  490. if (args.size() < 4) {
  491. std::cerr
  492. << "__create_def Usage: -E __create_def outfile.def objlistfile\n";
  493. return 1;
  494. }
  495. FILE* fout = cmsys::SystemTools::Fopen(args[2].c_str(), "w+");
  496. if (!fout) {
  497. std::cerr << "could not open output .def file: " << args[2].c_str()
  498. << "\n";
  499. return 1;
  500. }
  501. cmsys::ifstream fin(args[3].c_str(), std::ios::in | std::ios::binary);
  502. if (!fin) {
  503. std::cerr << "could not open object list file: " << args[3].c_str()
  504. << "\n";
  505. return 1;
  506. }
  507. std::string file;
  508. bindexplib deffile;
  509. while (cmSystemTools::GetLineFromStream(fin, file)) {
  510. std::string const& ext = cmSystemTools::GetFilenameLastExtension(file);
  511. if (cmSystemTools::LowerCase(ext) == ".def") {
  512. if (!deffile.AddDefinitionFile(file.c_str())) {
  513. return 1;
  514. }
  515. } else {
  516. if (!deffile.AddObjectFile(file.c_str())) {
  517. return 1;
  518. }
  519. }
  520. }
  521. deffile.WriteFile(fout);
  522. fclose(fout);
  523. return 0;
  524. }
  525. #endif
  526. if (args[1] == "__run_co_compile") {
  527. return cmcmd::HandleCoCompileCommands(args);
  528. }
  529. // Echo string
  530. if (args[1] == "echo") {
  531. std::cout << cmJoin(cmMakeRange(args).advance(2), " ") << std::endl;
  532. return 0;
  533. }
  534. // Echo string no new line
  535. if (args[1] == "echo_append") {
  536. std::cout << cmJoin(cmMakeRange(args).advance(2), " ");
  537. return 0;
  538. }
  539. if (args[1] == "env") {
  540. std::vector<std::string>::const_iterator ai = args.begin() + 2;
  541. std::vector<std::string>::const_iterator ae = args.end();
  542. for (; ai != ae; ++ai) {
  543. std::string const& a = *ai;
  544. if (cmHasLiteralPrefix(a, "--unset=")) {
  545. // Unset environment variable.
  546. cmSystemTools::UnPutEnv(a.c_str() + 8);
  547. } else if (!a.empty() && a[0] == '-') {
  548. // Environment variable and command names cannot start in '-',
  549. // so this must be an unknown option.
  550. std::cerr << "cmake -E env: unknown option '" << a << "'"
  551. << std::endl;
  552. return 1;
  553. } else if (a.find('=') != std::string::npos) {
  554. // Set environment variable.
  555. cmSystemTools::PutEnv(a);
  556. } else {
  557. // This is the beginning of the command.
  558. break;
  559. }
  560. }
  561. if (ai == ae) {
  562. std::cerr << "cmake -E env: no command given" << std::endl;
  563. return 1;
  564. }
  565. // Execute command from remaining arguments.
  566. std::vector<std::string> cmd(ai, ae);
  567. int retval;
  568. if (cmSystemTools::RunSingleCommand(cmd, nullptr, nullptr, &retval,
  569. nullptr,
  570. cmSystemTools::OUTPUT_PASSTHROUGH)) {
  571. return retval;
  572. }
  573. return 1;
  574. }
  575. #if defined(CMAKE_BUILD_WITH_CMAKE)
  576. if (args[1] == "environment") {
  577. std::vector<std::string> env = cmSystemTools::GetEnvironmentVariables();
  578. std::vector<std::string>::iterator it;
  579. for (it = env.begin(); it != env.end(); ++it) {
  580. std::cout << *it << std::endl;
  581. }
  582. return 0;
  583. }
  584. #endif
  585. if (args[1] == "make_directory" && args.size() > 2) {
  586. // If error occurs we want to continue copying next files.
  587. bool return_value = false;
  588. for (std::string::size_type cc = 2; cc < args.size(); cc++) {
  589. if (!cmSystemTools::MakeDirectory(args[cc].c_str())) {
  590. std::cerr << "Error creating directory \"" << args[cc] << "\".\n";
  591. return_value = true;
  592. }
  593. }
  594. return return_value;
  595. }
  596. if (args[1] == "remove_directory" && args.size() == 3) {
  597. if (cmSystemTools::FileIsDirectory(args[2]) &&
  598. !cmSystemTools::RemoveADirectory(args[2])) {
  599. std::cerr << "Error removing directory \"" << args[2] << "\".\n";
  600. return 1;
  601. }
  602. return 0;
  603. }
  604. // Remove file
  605. if (args[1] == "remove" && args.size() > 2) {
  606. bool force = false;
  607. for (std::string::size_type cc = 2; cc < args.size(); cc++) {
  608. if (args[cc] == "\\-f" || args[cc] == "-f") {
  609. force = true;
  610. } else {
  611. // Complain if the file could not be removed, still exists,
  612. // and the -f option was not given.
  613. if (!cmSystemTools::RemoveFile(args[cc]) && !force &&
  614. cmSystemTools::FileExists(args[cc].c_str())) {
  615. return 1;
  616. }
  617. }
  618. }
  619. return 0;
  620. }
  621. // Touch file
  622. if (args[1] == "touch" && args.size() > 2) {
  623. for (std::string::size_type cc = 2; cc < args.size(); cc++) {
  624. if (!cmSystemTools::Touch(args[cc], true)) {
  625. return 1;
  626. }
  627. }
  628. return 0;
  629. }
  630. // Touch file
  631. if (args[1] == "touch_nocreate" && args.size() > 2) {
  632. for (std::string::size_type cc = 2; cc < args.size(); cc++) {
  633. // Complain if the file could not be removed, still exists,
  634. // and the -f option was not given.
  635. if (!cmSystemTools::Touch(args[cc], false)) {
  636. return 1;
  637. }
  638. }
  639. return 0;
  640. }
  641. // capabilities
  642. if (args[1] == "capabilities") {
  643. if (args.size() > 2) {
  644. std::cerr << "-E capabilities accepts no additional arguments\n";
  645. return 1;
  646. }
  647. cmake cm(cmake::RoleInternal);
  648. #if defined(CMAKE_BUILD_WITH_CMAKE)
  649. std::cout << cm.ReportCapabilities(true);
  650. #else
  651. std::cout << cm.ReportCapabilities(false);
  652. #endif
  653. return 0;
  654. }
  655. // Sleep command
  656. if (args[1] == "sleep" && args.size() > 2) {
  657. double total = 0;
  658. for (size_t i = 2; i < args.size(); ++i) {
  659. double num = 0.0;
  660. char unit;
  661. char extra;
  662. int n = sscanf(args[i].c_str(), "%lg%c%c", &num, &unit, &extra);
  663. if ((n == 1 || (n == 2 && unit == 's')) && num >= 0) {
  664. total += num;
  665. } else {
  666. std::cerr << "Unknown sleep time format \"" << args[i] << "\".\n";
  667. return 1;
  668. }
  669. }
  670. if (total > 0) {
  671. cmSystemTools::Delay(static_cast<unsigned int>(total * 1000));
  672. }
  673. return 0;
  674. }
  675. // Clock command
  676. if (args[1] == "time" && args.size() > 2) {
  677. std::vector<std::string> command(args.begin() + 2, args.end());
  678. clock_t clock_start, clock_finish;
  679. time_t time_start, time_finish;
  680. time(&time_start);
  681. clock_start = clock();
  682. int ret = 0;
  683. cmSystemTools::RunSingleCommand(command, nullptr, nullptr, &ret);
  684. clock_finish = clock();
  685. time(&time_finish);
  686. double clocks_per_sec = static_cast<double>(CLOCKS_PER_SEC);
  687. std::cout << "Elapsed time: "
  688. << static_cast<long>(time_finish - time_start) << " s. (time)"
  689. << ", "
  690. << static_cast<double>(clock_finish - clock_start) /
  691. clocks_per_sec
  692. << " s. (clock)"
  693. << "\n";
  694. return ret;
  695. }
  696. // Command to calculate the md5sum of a file
  697. if (args[1] == "md5sum" && args.size() >= 3) {
  698. return HashSumFile(args, cmCryptoHash::AlgoMD5);
  699. }
  700. // Command to calculate the sha1sum of a file
  701. if (args[1] == "sha1sum" && args.size() >= 3) {
  702. return HashSumFile(args, cmCryptoHash::AlgoSHA1);
  703. }
  704. if (args[1] == "sha224sum" && args.size() >= 3) {
  705. return HashSumFile(args, cmCryptoHash::AlgoSHA224);
  706. }
  707. if (args[1] == "sha256sum" && args.size() >= 3) {
  708. return HashSumFile(args, cmCryptoHash::AlgoSHA256);
  709. }
  710. if (args[1] == "sha384sum" && args.size() >= 3) {
  711. return HashSumFile(args, cmCryptoHash::AlgoSHA384);
  712. }
  713. if (args[1] == "sha512sum" && args.size() >= 3) {
  714. return HashSumFile(args, cmCryptoHash::AlgoSHA512);
  715. }
  716. // Command to change directory and run a program.
  717. if (args[1] == "chdir" && args.size() >= 4) {
  718. std::string const& directory = args[2];
  719. if (!cmSystemTools::FileExists(directory.c_str())) {
  720. cmSystemTools::Error("Directory does not exist for chdir command: ",
  721. args[2].c_str());
  722. return 1;
  723. }
  724. std::string command =
  725. cmWrap('"', cmMakeRange(args).advance(3), '"', " ");
  726. int retval = 0;
  727. int timeout = 0;
  728. if (cmSystemTools::RunSingleCommand(
  729. command.c_str(), nullptr, nullptr, &retval, directory.c_str(),
  730. cmSystemTools::OUTPUT_PASSTHROUGH, timeout)) {
  731. return retval;
  732. }
  733. return 1;
  734. }
  735. // Command to start progress for a build
  736. if (args[1] == "cmake_progress_start" && args.size() == 4) {
  737. // basically remove the directory
  738. std::string dirName = args[2];
  739. dirName += "/Progress";
  740. cmSystemTools::RemoveADirectory(dirName);
  741. // is the last argument a filename that exists?
  742. FILE* countFile = cmsys::SystemTools::Fopen(args[3], "r");
  743. int count;
  744. if (countFile) {
  745. if (1 != fscanf(countFile, "%i", &count)) {
  746. cmSystemTools::Message("Could not read from count file.");
  747. }
  748. fclose(countFile);
  749. } else {
  750. count = atoi(args[3].c_str());
  751. }
  752. if (count) {
  753. cmSystemTools::MakeDirectory(dirName.c_str());
  754. // write the count into the directory
  755. std::string fName = dirName;
  756. fName += "/count.txt";
  757. FILE* progFile = cmsys::SystemTools::Fopen(fName, "w");
  758. if (progFile) {
  759. fprintf(progFile, "%i\n", count);
  760. fclose(progFile);
  761. }
  762. }
  763. return 0;
  764. }
  765. // Command to report progress for a build
  766. if (args[1] == "cmake_progress_report" && args.size() >= 3) {
  767. // This has been superseded by cmake_echo_color --progress-*
  768. // options. We leave it here to avoid errors if somehow this
  769. // is invoked by an existing makefile without regenerating.
  770. return 0;
  771. }
  772. // Command to create a symbolic link. Fails on platforms not
  773. // supporting them.
  774. if (args[1] == "create_symlink" && args.size() == 4) {
  775. const char* destinationFileName = args[3].c_str();
  776. if ((cmSystemTools::FileExists(destinationFileName) ||
  777. cmSystemTools::FileIsSymlink(destinationFileName)) &&
  778. !cmSystemTools::RemoveFile(destinationFileName)) {
  779. std::string emsg = cmSystemTools::GetLastSystemError();
  780. std::cerr << "failed to create symbolic link '" << destinationFileName
  781. << "' because existing path cannot be removed: " << emsg
  782. << "\n";
  783. return 1;
  784. }
  785. if (!cmSystemTools::CreateSymlink(args[2], args[3])) {
  786. std::string emsg = cmSystemTools::GetLastSystemError();
  787. std::cerr << "failed to create symbolic link '" << destinationFileName
  788. << "': " << emsg << "\n";
  789. return 1;
  790. }
  791. return 0;
  792. }
  793. // Internal CMake shared library support.
  794. if (args[1] == "cmake_symlink_library" && args.size() == 5) {
  795. return cmcmd::SymlinkLibrary(args);
  796. }
  797. // Internal CMake versioned executable support.
  798. if (args[1] == "cmake_symlink_executable" && args.size() == 4) {
  799. return cmcmd::SymlinkExecutable(args);
  800. }
  801. // Internal CMake dependency scanning support.
  802. if (args[1] == "cmake_depends" && args.size() >= 6) {
  803. const bool verbose = isCMakeVerbose();
  804. // Create a cmake object instance to process dependencies.
  805. cmake cm(cmake::RoleScript); // All we need is the `set` command.
  806. std::string gen;
  807. std::string homeDir;
  808. std::string startDir;
  809. std::string homeOutDir;
  810. std::string startOutDir;
  811. std::string depInfo;
  812. bool color = false;
  813. if (args.size() >= 8) {
  814. // Full signature:
  815. //
  816. // -E cmake_depends <generator>
  817. // <home-src-dir> <start-src-dir>
  818. // <home-out-dir> <start-out-dir>
  819. // <dep-info> [--color=$(COLOR)]
  820. //
  821. // All paths are provided.
  822. gen = args[2];
  823. homeDir = args[3];
  824. startDir = args[4];
  825. homeOutDir = args[5];
  826. startOutDir = args[6];
  827. depInfo = args[7];
  828. if (args.size() >= 9 && args[8].length() >= 8 &&
  829. args[8].substr(0, 8) == "--color=") {
  830. // Enable or disable color based on the switch value.
  831. color = (args[8].size() == 8 ||
  832. cmSystemTools::IsOn(args[8].substr(8).c_str()));
  833. }
  834. } else {
  835. // Support older signature for existing makefiles:
  836. //
  837. // -E cmake_depends <generator>
  838. // <home-out-dir> <start-out-dir>
  839. // <dep-info>
  840. //
  841. // Just pretend the source directories are the same as the
  842. // binary directories so at least scanning will work.
  843. gen = args[2];
  844. homeDir = args[3];
  845. startDir = args[4];
  846. homeOutDir = args[3];
  847. startOutDir = args[3];
  848. depInfo = args[5];
  849. }
  850. // Create a local generator configured for the directory in
  851. // which dependencies will be scanned.
  852. homeDir = cmSystemTools::CollapseFullPath(homeDir);
  853. startDir = cmSystemTools::CollapseFullPath(startDir);
  854. homeOutDir = cmSystemTools::CollapseFullPath(homeOutDir);
  855. startOutDir = cmSystemTools::CollapseFullPath(startOutDir);
  856. cm.SetHomeDirectory(homeDir);
  857. cm.SetHomeOutputDirectory(homeOutDir);
  858. cm.GetCurrentSnapshot().SetDefaultDefinitions();
  859. if (cmGlobalGenerator* ggd = cm.CreateGlobalGenerator(gen)) {
  860. cm.SetGlobalGenerator(ggd);
  861. cmStateSnapshot snapshot = cm.GetCurrentSnapshot();
  862. snapshot.GetDirectory().SetCurrentBinary(startOutDir);
  863. snapshot.GetDirectory().SetCurrentSource(startDir);
  864. CM_AUTO_PTR<cmMakefile> mf(new cmMakefile(ggd, snapshot));
  865. CM_AUTO_PTR<cmLocalGenerator> lgd(ggd->CreateLocalGenerator(mf.get()));
  866. // Actually scan dependencies.
  867. return lgd->UpdateDependencies(depInfo.c_str(), verbose, color) ? 0
  868. : 2;
  869. }
  870. return 1;
  871. }
  872. // Internal CMake link script support.
  873. if (args[1] == "cmake_link_script" && args.size() >= 3) {
  874. return cmcmd::ExecuteLinkScript(args);
  875. }
  876. #ifdef CMAKE_BUILD_WITH_CMAKE
  877. // Internal CMake ninja dependency scanning support.
  878. if (args[1] == "cmake_ninja_depends") {
  879. return cmcmd_cmake_ninja_depends(args.begin() + 2, args.end());
  880. }
  881. // Internal CMake ninja dyndep support.
  882. if (args[1] == "cmake_ninja_dyndep") {
  883. return cmcmd_cmake_ninja_dyndep(args.begin() + 2, args.end());
  884. }
  885. #endif
  886. // Internal CMake unimplemented feature notification.
  887. if (args[1] == "cmake_unimplemented_variable") {
  888. std::cerr << "Feature not implemented for this platform.";
  889. if (args.size() == 3) {
  890. std::cerr << " Variable " << args[2] << " is not set.";
  891. }
  892. std::cerr << std::endl;
  893. return 1;
  894. }
  895. if (args[1] == "vs_link_exe") {
  896. return cmcmd::VisualStudioLink(args, 1);
  897. }
  898. if (args[1] == "vs_link_dll") {
  899. return cmcmd::VisualStudioLink(args, 2);
  900. }
  901. // Internal CMake color makefile support.
  902. if (args[1] == "cmake_echo_color") {
  903. return cmcmd::ExecuteEchoColor(args);
  904. }
  905. #ifdef CMAKE_BUILD_WITH_CMAKE
  906. if (args[1] == "cmake_autogen" && args.size() >= 4) {
  907. cmQtAutoGenerators autogen;
  908. std::string const& config = args[3];
  909. bool autogenSuccess = autogen.Run(args[2], config);
  910. return autogenSuccess ? 0 : 1;
  911. }
  912. #endif
  913. // Tar files
  914. if (args[1] == "tar" && args.size() > 3) {
  915. const char* knownFormats[] = { "7zip", "gnutar", "pax", "paxr", "zip" };
  916. std::string const& flags = args[2];
  917. std::string const& outFile = args[3];
  918. std::vector<std::string> files;
  919. std::string mtime;
  920. std::string format;
  921. bool doing_options = true;
  922. for (std::string::size_type cc = 4; cc < args.size(); cc++) {
  923. std::string const& arg = args[cc];
  924. if (doing_options && cmHasLiteralPrefix(arg, "--")) {
  925. if (arg == "--") {
  926. doing_options = false;
  927. } else if (cmHasLiteralPrefix(arg, "--mtime=")) {
  928. mtime = arg.substr(8);
  929. } else if (cmHasLiteralPrefix(arg, "--files-from=")) {
  930. std::string const& files_from = arg.substr(13);
  931. if (!cmTarFilesFrom(files_from, files)) {
  932. return 1;
  933. }
  934. } else if (cmHasLiteralPrefix(arg, "--format=")) {
  935. format = arg.substr(9);
  936. bool isKnown =
  937. std::find(cmArrayBegin(knownFormats), cmArrayEnd(knownFormats),
  938. format) != cmArrayEnd(knownFormats);
  939. if (!isKnown) {
  940. cmSystemTools::Error("Unknown -E tar --format= argument: ",
  941. format.c_str());
  942. return 1;
  943. }
  944. } else {
  945. cmSystemTools::Error("Unknown option to -E tar: ", arg.c_str());
  946. return 1;
  947. }
  948. } else {
  949. files.push_back(arg);
  950. }
  951. }
  952. cmSystemTools::cmTarCompression compress =
  953. cmSystemTools::TarCompressNone;
  954. bool verbose = false;
  955. int nCompress = 0;
  956. if (flags.find_first_of('j') != std::string::npos) {
  957. compress = cmSystemTools::TarCompressBZip2;
  958. ++nCompress;
  959. }
  960. if (flags.find_first_of('J') != std::string::npos) {
  961. compress = cmSystemTools::TarCompressXZ;
  962. ++nCompress;
  963. }
  964. if (flags.find_first_of('z') != std::string::npos) {
  965. compress = cmSystemTools::TarCompressGZip;
  966. ++nCompress;
  967. }
  968. if ((format == "7zip" || format == "zip") && nCompress > 0) {
  969. cmSystemTools::Error("Can not use compression flags with format: ",
  970. format.c_str());
  971. return 1;
  972. }
  973. if (nCompress > 1) {
  974. cmSystemTools::Error("Can only compress a tar file one way; "
  975. "at most one flag of z, j, or J may be used");
  976. return 1;
  977. }
  978. if (flags.find_first_of('v') != std::string::npos) {
  979. verbose = true;
  980. }
  981. if (flags.find_first_of('t') != std::string::npos) {
  982. if (!cmSystemTools::ListTar(outFile.c_str(), verbose)) {
  983. cmSystemTools::Error("Problem listing tar: ", outFile.c_str());
  984. return 1;
  985. }
  986. } else if (flags.find_first_of('c') != std::string::npos) {
  987. if (!cmSystemTools::CreateTar(outFile.c_str(), files, compress,
  988. verbose, mtime, format)) {
  989. cmSystemTools::Error("Problem creating tar: ", outFile.c_str());
  990. return 1;
  991. }
  992. } else if (flags.find_first_of('x') != std::string::npos) {
  993. if (!cmSystemTools::ExtractTar(outFile.c_str(), verbose)) {
  994. cmSystemTools::Error("Problem extracting tar: ", outFile.c_str());
  995. return 1;
  996. }
  997. #ifdef WIN32
  998. // OK, on windows 7 after we untar some files,
  999. // sometimes we can not rename the directory after
  1000. // the untar is done. This breaks the external project
  1001. // untar and rename code. So, by default we will wait
  1002. // 1/10th of a second after the untar. If CMAKE_UNTAR_DELAY
  1003. // is set in the env, its value will be used instead of 100.
  1004. int delay = 100;
  1005. std::string delayVar;
  1006. if (cmSystemTools::GetEnv("CMAKE_UNTAR_DELAY", delayVar)) {
  1007. delay = atoi(delayVar.c_str());
  1008. }
  1009. if (delay) {
  1010. cmSystemTools::Delay(delay);
  1011. }
  1012. #endif
  1013. }
  1014. return 0;
  1015. }
  1016. if (args[1] == "server") {
  1017. const std::string pipePrefix = "--pipe=";
  1018. bool supportExperimental = false;
  1019. bool isDebug = false;
  1020. std::string pipe;
  1021. for (size_t i = 2; i < args.size(); ++i) {
  1022. const std::string& a = args[i];
  1023. if (a == "--experimental") {
  1024. supportExperimental = true;
  1025. } else if (a == "--debug") {
  1026. pipe.clear();
  1027. isDebug = true;
  1028. } else if (a.substr(0, pipePrefix.size()) == pipePrefix) {
  1029. isDebug = false;
  1030. pipe = a.substr(pipePrefix.size());
  1031. if (pipe.empty()) {
  1032. cmSystemTools::Error("No pipe given after --pipe=");
  1033. return 2;
  1034. }
  1035. } else {
  1036. cmSystemTools::Error("Unknown argument for server mode");
  1037. return 1;
  1038. }
  1039. }
  1040. #if defined(CMAKE_BUILD_WITH_CMAKE)
  1041. cmConnection* conn;
  1042. if (isDebug) {
  1043. conn = new cmServerStdIoConnection;
  1044. } else {
  1045. conn = new cmServerPipeConnection(pipe);
  1046. }
  1047. cmServer server(conn, supportExperimental);
  1048. std::string errorMessage;
  1049. if (server.Serve(&errorMessage)) {
  1050. return 0;
  1051. }
  1052. cmSystemTools::Error(errorMessage.c_str());
  1053. #else
  1054. static_cast<void>(supportExperimental);
  1055. static_cast<void>(isDebug);
  1056. cmSystemTools::Error("CMake was not built with server mode enabled");
  1057. #endif
  1058. return 1;
  1059. }
  1060. #if defined(CMAKE_BUILD_WITH_CMAKE)
  1061. // Internal CMake Fortran module support.
  1062. if (args[1] == "cmake_copy_f90_mod" && args.size() >= 4) {
  1063. return cmDependsFortran::CopyModule(args) ? 0 : 1;
  1064. }
  1065. #endif
  1066. #if defined(_WIN32) && !defined(__CYGWIN__)
  1067. // Write registry value
  1068. if (args[1] == "write_regv" && args.size() > 3) {
  1069. return cmSystemTools::WriteRegistryValue(args[2].c_str(),
  1070. args[3].c_str())
  1071. ? 0
  1072. : 1;
  1073. }
  1074. // Delete registry value
  1075. if (args[1] == "delete_regv" && args.size() > 2) {
  1076. return cmSystemTools::DeleteRegistryValue(args[2].c_str()) ? 0 : 1;
  1077. }
  1078. // Remove file
  1079. if (args[1] == "comspec" && args.size() > 2) {
  1080. std::cerr << "Win9x helper \"cmake -E comspec\" no longer supported\n";
  1081. return 1;
  1082. }
  1083. if (args[1] == "env_vs8_wince" && args.size() == 3) {
  1084. return cmcmd::WindowsCEEnvironment("8.0", args[2]);
  1085. }
  1086. if (args[1] == "env_vs9_wince" && args.size() == 3) {
  1087. return cmcmd::WindowsCEEnvironment("9.0", args[2]);
  1088. }
  1089. #endif
  1090. }
  1091. ::CMakeCommandUsage(args[0].c_str());
  1092. return 1;
  1093. }
  1094. int cmcmd::HashSumFile(std::vector<std::string>& args, cmCryptoHash::Algo algo)
  1095. {
  1096. if (args.size() < 3) {
  1097. return -1;
  1098. }
  1099. int retval = 0;
  1100. for (std::string::size_type cc = 2; cc < args.size(); cc++) {
  1101. const char* filename = args[cc].c_str();
  1102. // Cannot compute sum of a directory
  1103. if (cmSystemTools::FileIsDirectory(filename)) {
  1104. std::cerr << "Error: " << filename << " is a directory" << std::endl;
  1105. retval++;
  1106. } else {
  1107. std::string value = cmSystemTools::ComputeFileHash(filename, algo);
  1108. if (value.empty()) {
  1109. // To mimic "md5sum/shasum" behavior in a shell:
  1110. std::cerr << filename << ": No such file or directory" << std::endl;
  1111. retval++;
  1112. } else {
  1113. std::cout << value << " " << filename << std::endl;
  1114. }
  1115. }
  1116. }
  1117. return retval;
  1118. }
  1119. int cmcmd::SymlinkLibrary(std::vector<std::string>& args)
  1120. {
  1121. int result = 0;
  1122. std::string const& realName = args[2];
  1123. std::string const& soName = args[3];
  1124. std::string const& name = args[4];
  1125. if (soName != realName) {
  1126. if (!cmcmd::SymlinkInternal(realName, soName)) {
  1127. cmSystemTools::ReportLastSystemError("cmake_symlink_library");
  1128. result = 1;
  1129. }
  1130. }
  1131. if (name != soName) {
  1132. if (!cmcmd::SymlinkInternal(soName, name)) {
  1133. cmSystemTools::ReportLastSystemError("cmake_symlink_library");
  1134. result = 1;
  1135. }
  1136. }
  1137. return result;
  1138. }
  1139. int cmcmd::SymlinkExecutable(std::vector<std::string>& args)
  1140. {
  1141. int result = 0;
  1142. std::string const& realName = args[2];
  1143. std::string const& name = args[3];
  1144. if (name != realName) {
  1145. if (!cmcmd::SymlinkInternal(realName, name)) {
  1146. cmSystemTools::ReportLastSystemError("cmake_symlink_executable");
  1147. result = 1;
  1148. }
  1149. }
  1150. return result;
  1151. }
  1152. bool cmcmd::SymlinkInternal(std::string const& file, std::string const& link)
  1153. {
  1154. if (cmSystemTools::FileExists(link.c_str()) ||
  1155. cmSystemTools::FileIsSymlink(link)) {
  1156. cmSystemTools::RemoveFile(link);
  1157. }
  1158. #if defined(_WIN32) && !defined(__CYGWIN__)
  1159. return cmSystemTools::CopyFileAlways(file.c_str(), link.c_str());
  1160. #else
  1161. std::string linktext = cmSystemTools::GetFilenameName(file);
  1162. return cmSystemTools::CreateSymlink(linktext, link);
  1163. #endif
  1164. }
  1165. static void cmcmdProgressReport(std::string const& dir, std::string const& num)
  1166. {
  1167. std::string dirName = dir;
  1168. dirName += "/Progress";
  1169. std::string fName;
  1170. FILE* progFile;
  1171. // read the count
  1172. fName = dirName;
  1173. fName += "/count.txt";
  1174. progFile = cmsys::SystemTools::Fopen(fName, "r");
  1175. int count = 0;
  1176. if (!progFile) {
  1177. return;
  1178. }
  1179. if (1 != fscanf(progFile, "%i", &count)) {
  1180. cmSystemTools::Message("Could not read from progress file.");
  1181. }
  1182. fclose(progFile);
  1183. const char* last = num.c_str();
  1184. for (const char* c = last;; ++c) {
  1185. if (*c == ',' || *c == '\0') {
  1186. if (c != last) {
  1187. fName = dirName;
  1188. fName += "/";
  1189. fName.append(last, c - last);
  1190. progFile = cmsys::SystemTools::Fopen(fName, "w");
  1191. if (progFile) {
  1192. fprintf(progFile, "empty");
  1193. fclose(progFile);
  1194. }
  1195. }
  1196. if (*c == '\0') {
  1197. break;
  1198. }
  1199. last = c + 1;
  1200. }
  1201. }
  1202. int fileNum =
  1203. static_cast<int>(cmsys::Directory::GetNumberOfFilesInDirectory(dirName));
  1204. if (count > 0) {
  1205. // print the progress
  1206. fprintf(stdout, "[%3i%%] ", ((fileNum - 3) * 100) / count);
  1207. }
  1208. }
  1209. int cmcmd::ExecuteEchoColor(std::vector<std::string>& args)
  1210. {
  1211. // The arguments are
  1212. // argv[0] == <cmake-executable>
  1213. // argv[1] == cmake_echo_color
  1214. bool enabled = true;
  1215. int color = cmsysTerminal_Color_Normal;
  1216. bool newline = true;
  1217. std::string progressDir;
  1218. for (unsigned int i = 2; i < args.size(); ++i) {
  1219. if (args[i].find("--switch=") == 0) {
  1220. // Enable or disable color based on the switch value.
  1221. std::string value = args[i].substr(9);
  1222. if (!value.empty()) {
  1223. enabled = cmSystemTools::IsOn(value.c_str());
  1224. }
  1225. } else if (cmHasLiteralPrefix(args[i], "--progress-dir=")) {
  1226. progressDir = args[i].substr(15);
  1227. } else if (cmHasLiteralPrefix(args[i], "--progress-num=")) {
  1228. if (!progressDir.empty()) {
  1229. std::string const& progressNum = args[i].substr(15);
  1230. cmcmdProgressReport(progressDir, progressNum);
  1231. }
  1232. } else if (args[i] == "--normal") {
  1233. color = cmsysTerminal_Color_Normal;
  1234. } else if (args[i] == "--black") {
  1235. color = cmsysTerminal_Color_ForegroundBlack;
  1236. } else if (args[i] == "--red") {
  1237. color = cmsysTerminal_Color_ForegroundRed;
  1238. } else if (args[i] == "--green") {
  1239. color = cmsysTerminal_Color_ForegroundGreen;
  1240. } else if (args[i] == "--yellow") {
  1241. color = cmsysTerminal_Color_ForegroundYellow;
  1242. } else if (args[i] == "--blue") {
  1243. color = cmsysTerminal_Color_ForegroundBlue;
  1244. } else if (args[i] == "--magenta") {
  1245. color = cmsysTerminal_Color_ForegroundMagenta;
  1246. } else if (args[i] == "--cyan") {
  1247. color = cmsysTerminal_Color_ForegroundCyan;
  1248. } else if (args[i] == "--white") {
  1249. color = cmsysTerminal_Color_ForegroundWhite;
  1250. } else if (args[i] == "--bold") {
  1251. color |= cmsysTerminal_Color_ForegroundBold;
  1252. } else if (args[i] == "--no-newline") {
  1253. newline = false;
  1254. } else if (args[i] == "--newline") {
  1255. newline = true;
  1256. } else {
  1257. // Color is enabled. Print with the current color.
  1258. cmSystemTools::MakefileColorEcho(color, args[i].c_str(), newline,
  1259. enabled);
  1260. }
  1261. }
  1262. return 0;
  1263. }
  1264. int cmcmd::ExecuteLinkScript(std::vector<std::string>& args)
  1265. {
  1266. // The arguments are
  1267. // argv[0] == <cmake-executable>
  1268. // argv[1] == cmake_link_script
  1269. // argv[2] == <link-script-name>
  1270. // argv[3] == --verbose=?
  1271. bool verbose = false;
  1272. if (args.size() >= 4) {
  1273. if (args[3].find("--verbose=") == 0) {
  1274. if (!cmSystemTools::IsOff(args[3].substr(10).c_str())) {
  1275. verbose = true;
  1276. }
  1277. }
  1278. }
  1279. // Allocate a process instance.
  1280. cmsysProcess* cp = cmsysProcess_New();
  1281. if (!cp) {
  1282. std::cerr << "Error allocating process instance in link script."
  1283. << std::endl;
  1284. return 1;
  1285. }
  1286. // Children should share stdout and stderr with this process.
  1287. cmsysProcess_SetPipeShared(cp, cmsysProcess_Pipe_STDOUT, 1);
  1288. cmsysProcess_SetPipeShared(cp, cmsysProcess_Pipe_STDERR, 1);
  1289. // Run the command lines verbatim.
  1290. cmsysProcess_SetOption(cp, cmsysProcess_Option_Verbatim, 1);
  1291. // Read command lines from the script.
  1292. cmsys::ifstream fin(args[2].c_str());
  1293. if (!fin) {
  1294. std::cerr << "Error opening link script \"" << args[2] << "\""
  1295. << std::endl;
  1296. return 1;
  1297. }
  1298. // Run one command at a time.
  1299. std::string command;
  1300. int result = 0;
  1301. while (result == 0 && cmSystemTools::GetLineFromStream(fin, command)) {
  1302. // Skip empty command lines.
  1303. if (command.find_first_not_of(" \t") == std::string::npos) {
  1304. continue;
  1305. }
  1306. // Setup this command line.
  1307. const char* cmd[2] = { command.c_str(), nullptr };
  1308. cmsysProcess_SetCommand(cp, cmd);
  1309. // Report the command if verbose output is enabled.
  1310. if (verbose) {
  1311. std::cout << command << std::endl;
  1312. }
  1313. // Run the command and wait for it to exit.
  1314. cmsysProcess_Execute(cp);
  1315. cmsysProcess_WaitForExit(cp, nullptr);
  1316. // Report failure if any.
  1317. switch (cmsysProcess_GetState(cp)) {
  1318. case cmsysProcess_State_Exited: {
  1319. int value = cmsysProcess_GetExitValue(cp);
  1320. if (value != 0) {
  1321. result = value;
  1322. }
  1323. } break;
  1324. case cmsysProcess_State_Exception:
  1325. std::cerr << "Error running link command: "
  1326. << cmsysProcess_GetExceptionString(cp) << std::endl;
  1327. result = 1;
  1328. break;
  1329. case cmsysProcess_State_Error:
  1330. std::cerr << "Error running link command: "
  1331. << cmsysProcess_GetErrorString(cp) << std::endl;
  1332. result = 2;
  1333. break;
  1334. default:
  1335. break;
  1336. };
  1337. }
  1338. // Free the process instance.
  1339. cmsysProcess_Delete(cp);
  1340. // Return the final resulting return value.
  1341. return result;
  1342. }
  1343. int cmcmd::WindowsCEEnvironment(const char* version, const std::string& name)
  1344. {
  1345. #if defined(CMAKE_BUILD_WITH_CMAKE) && defined(_WIN32) && !defined(__CYGWIN__)
  1346. cmVisualStudioWCEPlatformParser parser(name.c_str());
  1347. parser.ParseVersion(version);
  1348. if (parser.Found()) {
  1349. std::cout << "@echo off" << std::endl;
  1350. std::cout << "echo Environment Selection: " << name << std::endl;
  1351. std::cout << "set PATH=" << parser.GetPathDirectories() << std::endl;
  1352. std::cout << "set INCLUDE=" << parser.GetIncludeDirectories() << std::endl;
  1353. std::cout << "set LIB=" << parser.GetLibraryDirectories() << std::endl;
  1354. return 0;
  1355. }
  1356. #else
  1357. (void)version;
  1358. #endif
  1359. std::cerr << "Could not find " << name;
  1360. return -1;
  1361. }
  1362. class cmVSLink
  1363. {
  1364. int Type;
  1365. bool Verbose;
  1366. bool Incremental;
  1367. bool LinkGeneratesManifest;
  1368. std::vector<std::string> LinkCommand;
  1369. std::vector<std::string> UserManifests;
  1370. std::string LinkerManifestFile;
  1371. std::string ManifestFile;
  1372. std::string ManifestFileRC;
  1373. std::string ManifestFileRes;
  1374. std::string TargetFile;
  1375. public:
  1376. cmVSLink(int type, bool verbose)
  1377. : Type(type)
  1378. , Verbose(verbose)
  1379. , Incremental(false)
  1380. , LinkGeneratesManifest(true)
  1381. {
  1382. }
  1383. bool Parse(std::vector<std::string>::const_iterator argBeg,
  1384. std::vector<std::string>::const_iterator argEnd);
  1385. int Link();
  1386. private:
  1387. int LinkIncremental();
  1388. int LinkNonIncremental();
  1389. int RunMT(std::string const& out, bool notify);
  1390. };
  1391. // For visual studio 2005 and newer manifest files need to be embedded into
  1392. // exe and dll's. This code does that in such a way that incremental linking
  1393. // still works.
  1394. int cmcmd::VisualStudioLink(std::vector<std::string> const& args, int type)
  1395. {
  1396. if (args.size() < 2) {
  1397. return -1;
  1398. }
  1399. const bool verbose = cmSystemTools::HasEnv("VERBOSE");
  1400. std::vector<std::string> expandedArgs;
  1401. for (std::string const& i : args) {
  1402. // check for nmake temporary files
  1403. if (i[0] == '@' && i.find("@CMakeFiles") != 0) {
  1404. cmsys::ifstream fin(i.substr(1).c_str());
  1405. std::string line;
  1406. while (cmSystemTools::GetLineFromStream(fin, line)) {
  1407. cmSystemTools::ParseWindowsCommandLine(line.c_str(), expandedArgs);
  1408. }
  1409. } else {
  1410. expandedArgs.push_back(i);
  1411. }
  1412. }
  1413. cmVSLink vsLink(type, verbose);
  1414. if (!vsLink.Parse(expandedArgs.begin() + 2, expandedArgs.end())) {
  1415. return -1;
  1416. }
  1417. return vsLink.Link();
  1418. }
  1419. static bool RunCommand(const char* comment, std::vector<std::string>& command,
  1420. bool verbose, int* retCodeOut = nullptr)
  1421. {
  1422. if (verbose) {
  1423. std::cout << comment << ":\n";
  1424. std::cout << cmJoin(command, " ") << "\n";
  1425. }
  1426. std::string output;
  1427. int retCode = 0;
  1428. // use rc command to create .res file
  1429. bool res = cmSystemTools::RunSingleCommand(
  1430. command, &output, &output, &retCode, nullptr, cmSystemTools::OUTPUT_NONE);
  1431. // always print the output of the command, unless
  1432. // it is the dumb rc command banner, but if the command
  1433. // returned an error code then print the output anyway as
  1434. // the banner may be mixed with some other important information.
  1435. if (output.find("Resource Compiler Version") == std::string::npos || !res ||
  1436. retCode) {
  1437. std::cout << output;
  1438. }
  1439. if (!res) {
  1440. std::cout << comment << " failed to run." << std::endl;
  1441. return false;
  1442. }
  1443. // if retCodeOut is requested then always return true
  1444. // and set the retCodeOut to retCode
  1445. if (retCodeOut) {
  1446. *retCodeOut = retCode;
  1447. return true;
  1448. }
  1449. if (retCode != 0) {
  1450. std::cout << comment << " failed. with " << retCode << "\n";
  1451. }
  1452. return retCode == 0;
  1453. }
  1454. bool cmVSLink::Parse(std::vector<std::string>::const_iterator argBeg,
  1455. std::vector<std::string>::const_iterator argEnd)
  1456. {
  1457. // Parse our own arguments.
  1458. std::string intDir;
  1459. std::vector<std::string>::const_iterator arg = argBeg;
  1460. while (arg != argEnd && cmHasLiteralPrefix(*arg, "-")) {
  1461. if (*arg == "--") {
  1462. ++arg;
  1463. break;
  1464. }
  1465. if (*arg == "--manifests") {
  1466. for (++arg; arg != argEnd && !cmHasLiteralPrefix(*arg, "-"); ++arg) {
  1467. this->UserManifests.push_back(*arg);
  1468. }
  1469. } else if (cmHasLiteralPrefix(*arg, "--intdir=")) {
  1470. intDir = arg->substr(9);
  1471. ++arg;
  1472. } else {
  1473. std::cerr << "unknown argument '" << *arg << "'\n";
  1474. return false;
  1475. }
  1476. }
  1477. if (intDir.empty()) {
  1478. return false;
  1479. }
  1480. // The rest of the arguments form the link command.
  1481. if (arg == argEnd) {
  1482. return false;
  1483. }
  1484. this->LinkCommand.insert(this->LinkCommand.begin(), arg, argEnd);
  1485. // Parse the link command to extract information we need.
  1486. for (; arg != argEnd; ++arg) {
  1487. if (cmSystemTools::Strucmp(arg->c_str(), "/INCREMENTAL:YES") == 0) {
  1488. this->Incremental = true;
  1489. } else if (cmSystemTools::Strucmp(arg->c_str(), "/INCREMENTAL") == 0) {
  1490. this->Incremental = true;
  1491. } else if (cmSystemTools::Strucmp(arg->c_str(), "/MANIFEST:NO") == 0) {
  1492. this->LinkGeneratesManifest = false;
  1493. } else if (cmHasLiteralPrefix(*arg, "/Fe")) {
  1494. this->TargetFile = arg->substr(3);
  1495. } else if (cmHasLiteralPrefix(*arg, "/out:")) {
  1496. this->TargetFile = arg->substr(5);
  1497. }
  1498. }
  1499. if (this->TargetFile.empty()) {
  1500. return false;
  1501. }
  1502. this->ManifestFile = intDir + "/embed.manifest";
  1503. this->LinkerManifestFile = intDir + "/intermediate.manifest";
  1504. if (this->Incremental) {
  1505. // We will compile a resource containing the manifest and
  1506. // pass it to the link command.
  1507. this->ManifestFileRC = intDir + "/manifest.rc";
  1508. this->ManifestFileRes = intDir + "/manifest.res";
  1509. } else if (this->UserManifests.empty()) {
  1510. // Prior to support for user-specified manifests CMake placed the
  1511. // linker-generated manifest next to the binary (as if it were not to be
  1512. // embedded) when not linking incrementally. Preserve this behavior.
  1513. this->ManifestFile = this->TargetFile + ".manifest";
  1514. this->LinkerManifestFile = this->ManifestFile;
  1515. }
  1516. if (this->LinkGeneratesManifest) {
  1517. this->LinkCommand.push_back("/MANIFEST");
  1518. this->LinkCommand.push_back("/MANIFESTFILE:" + this->LinkerManifestFile);
  1519. }
  1520. return true;
  1521. }
  1522. int cmVSLink::Link()
  1523. {
  1524. if (this->Incremental &&
  1525. (this->LinkGeneratesManifest || !this->UserManifests.empty())) {
  1526. if (this->Verbose) {
  1527. std::cout << "Visual Studio Incremental Link with embedded manifests\n";
  1528. }
  1529. return LinkIncremental();
  1530. }
  1531. if (this->Verbose) {
  1532. if (!this->Incremental) {
  1533. std::cout << "Visual Studio Non-Incremental Link\n";
  1534. } else {
  1535. std::cout << "Visual Studio Incremental Link without manifests\n";
  1536. }
  1537. }
  1538. return LinkNonIncremental();
  1539. }
  1540. int cmVSLink::LinkIncremental()
  1541. {
  1542. // This follows the steps listed here:
  1543. // http://blogs.msdn.com/zakramer/archive/2006/05/22/603558.aspx
  1544. // 1. Compiler compiles the application and generates the *.obj files.
  1545. // 2. An empty manifest file is generated if this is a clean build and if
  1546. // not the previous one is reused.
  1547. // 3. The resource compiler (rc.exe) compiles the *.manifest file to a
  1548. // *.res file.
  1549. // 4. Linker generates the binary (EXE or DLL) with the /incremental
  1550. // switch and embeds the dummy manifest file. The linker also generates
  1551. // the real manifest file based on the binaries that your binary depends
  1552. // on.
  1553. // 5. The manifest tool (mt.exe) is then used to generate the final
  1554. // manifest.
  1555. // If the final manifest is changed, then 6 and 7 are run, if not
  1556. // they are skipped, and it is done.
  1557. // 6. The resource compiler is invoked one more time.
  1558. // 7. Finally, the Linker does another incremental link, but since the
  1559. // only thing that has changed is the *.res file that contains the
  1560. // manifest it is a short link.
  1561. // Create a resource file referencing the manifest.
  1562. std::string absManifestFile =
  1563. cmSystemTools::CollapseFullPath(this->ManifestFile);
  1564. if (this->Verbose) {
  1565. std::cout << "Create " << this->ManifestFileRC << "\n";
  1566. }
  1567. {
  1568. cmsys::ofstream fout(this->ManifestFileRC.c_str());
  1569. if (!fout) {
  1570. return -1;
  1571. }
  1572. fout << this->Type << " /* CREATEPROCESS_MANIFEST_RESOURCE_ID */ "
  1573. "24 /* RT_MANIFEST */ \""
  1574. << absManifestFile << "\"";
  1575. }
  1576. // If we have not previously generated a manifest file,
  1577. // generate an empty one so the resource compiler succeeds.
  1578. if (!cmSystemTools::FileExists(this->ManifestFile)) {
  1579. if (this->Verbose) {
  1580. std::cout << "Create empty: " << this->ManifestFile << "\n";
  1581. }
  1582. cmsys::ofstream foutTmp(this->ManifestFile.c_str());
  1583. }
  1584. // Compile the resource file.
  1585. std::vector<std::string> rcCommand;
  1586. rcCommand.push_back(cmSystemTools::FindProgram("rc.exe"));
  1587. rcCommand.push_back("/fo" + this->ManifestFileRes);
  1588. rcCommand.push_back(this->ManifestFileRC);
  1589. if (!RunCommand("RC Pass 1", rcCommand, this->Verbose)) {
  1590. return -1;
  1591. }
  1592. // Tell the linker to use our manifest compiled into a resource.
  1593. this->LinkCommand.push_back(this->ManifestFileRes);
  1594. // Run the link command (possibly generates intermediate manifest).
  1595. if (!RunCommand("LINK Pass 1", this->LinkCommand, this->Verbose)) {
  1596. return -1;
  1597. }
  1598. // Run the manifest tool to create the final manifest.
  1599. int mtRet = this->RunMT("/out:" + this->ManifestFile, true);
  1600. // If mt returns 1090650113 (or 187 on a posix host) then it updated the
  1601. // manifest file so we need to embed it again. Otherwise we are done.
  1602. if (mtRet != 1090650113 && mtRet != 187) {
  1603. return mtRet;
  1604. }
  1605. // Compile the resource file again.
  1606. if (!RunCommand("RC Pass 2", rcCommand, this->Verbose)) {
  1607. return -1;
  1608. }
  1609. // Link incrementally again to use the updated resource.
  1610. if (!RunCommand("FINAL LINK", this->LinkCommand, this->Verbose)) {
  1611. return -1;
  1612. }
  1613. return 0;
  1614. }
  1615. int cmVSLink::LinkNonIncremental()
  1616. {
  1617. // Run the link command (possibly generates intermediate manifest).
  1618. if (!RunCommand("LINK", this->LinkCommand, this->Verbose)) {
  1619. return -1;
  1620. }
  1621. // If we have no manifest files we are done.
  1622. if (!this->LinkGeneratesManifest && this->UserManifests.empty()) {
  1623. return 0;
  1624. }
  1625. // Run the manifest tool to embed the final manifest in the binary.
  1626. std::string mtOut =
  1627. "/outputresource:" + this->TargetFile + (this->Type == 1 ? ";#1" : ";#2");
  1628. return this->RunMT(mtOut, false);
  1629. }
  1630. int cmVSLink::RunMT(std::string const& out, bool notify)
  1631. {
  1632. std::vector<std::string> mtCommand;
  1633. mtCommand.push_back(cmSystemTools::FindProgram("mt.exe"));
  1634. mtCommand.push_back("/nologo");
  1635. mtCommand.push_back("/manifest");
  1636. if (this->LinkGeneratesManifest) {
  1637. mtCommand.push_back(this->LinkerManifestFile);
  1638. }
  1639. mtCommand.insert(mtCommand.end(), this->UserManifests.begin(),
  1640. this->UserManifests.end());
  1641. mtCommand.push_back(out);
  1642. if (notify) {
  1643. // Add an undocumented option that enables a special return
  1644. // code to notify us when the manifest is modified.
  1645. mtCommand.push_back("/notify_update");
  1646. }
  1647. int mtRet = 0;
  1648. if (!RunCommand("MT", mtCommand, this->Verbose, &mtRet)) {
  1649. return -1;
  1650. }
  1651. return mtRet;
  1652. }