cmcmd.cxx 43 KB

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