cmcmd.cxx 48 KB

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