cmcmd.cxx 40 KB

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