cmcmd.cxx 49 KB

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