cmcmd.cxx 45 KB

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