cmcmd.cxx 45 KB

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