cmcmd.cxx 49 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687
  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(cmMakeRange(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(cmMakeRange(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(cmMakeRange(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 =
  538. cmWrap('"', cmMakeRange(args).advance(3), '"', " ");
  539. int retval = 0;
  540. int timeout = 0;
  541. if ( cmSystemTools::RunSingleCommand(command.c_str(), 0, 0, &retval,
  542. directory.c_str(), cmSystemTools::OUTPUT_NORMAL, timeout) )
  543. {
  544. return retval;
  545. }
  546. return 1;
  547. }
  548. // Command to start progress for a build
  549. else if (args[1] == "cmake_progress_start" && args.size() == 4)
  550. {
  551. // basically remove the directory
  552. std::string dirName = args[2];
  553. dirName += "/Progress";
  554. cmSystemTools::RemoveADirectory(dirName);
  555. // is the last argument a filename that exists?
  556. FILE *countFile = cmsys::SystemTools::Fopen(args[3],"r");
  557. int count;
  558. if (countFile)
  559. {
  560. if (1!=fscanf(countFile,"%i",&count))
  561. {
  562. cmSystemTools::Message("Could not read from count file.");
  563. }
  564. fclose(countFile);
  565. }
  566. else
  567. {
  568. count = atoi(args[3].c_str());
  569. }
  570. if (count)
  571. {
  572. cmSystemTools::MakeDirectory(dirName.c_str());
  573. // write the count into the directory
  574. std::string fName = dirName;
  575. fName += "/count.txt";
  576. FILE *progFile = cmsys::SystemTools::Fopen(fName,"w");
  577. if (progFile)
  578. {
  579. fprintf(progFile,"%i\n",count);
  580. fclose(progFile);
  581. }
  582. }
  583. return 0;
  584. }
  585. // Command to report progress for a build
  586. else if (args[1] == "cmake_progress_report" && args.size() >= 3)
  587. {
  588. // This has been superseded by cmake_echo_color --progress-*
  589. // options. We leave it here to avoid errors if somehow this
  590. // is invoked by an existing makefile without regenerating.
  591. return 0;
  592. }
  593. // Command to create a symbolic link. Fails on platforms not
  594. // supporting them.
  595. else if (args[1] == "create_symlink" && args.size() == 4)
  596. {
  597. const char* destinationFileName = args[3].c_str();
  598. if((cmSystemTools::FileExists(destinationFileName) ||
  599. cmSystemTools::FileIsSymlink(destinationFileName)) &&
  600. !cmSystemTools::RemoveFile(destinationFileName))
  601. {
  602. std::string emsg = cmSystemTools::GetLastSystemError();
  603. std::cerr <<
  604. "failed to create symbolic link '" << destinationFileName <<
  605. "' because existing path cannot be removed: " << emsg << "\n";
  606. return 1;
  607. }
  608. if(!cmSystemTools::CreateSymlink(args[2], args[3]))
  609. {
  610. std::string emsg = cmSystemTools::GetLastSystemError();
  611. std::cerr <<
  612. "failed to create symbolic link '" << destinationFileName <<
  613. "': " << emsg << "\n";
  614. return 1;
  615. }
  616. return 0;
  617. }
  618. // Internal CMake shared library support.
  619. else if (args[1] == "cmake_symlink_library" && args.size() == 5)
  620. {
  621. return cmcmd::SymlinkLibrary(args);
  622. }
  623. // Internal CMake versioned executable support.
  624. else if (args[1] == "cmake_symlink_executable" && args.size() == 4)
  625. {
  626. return cmcmd::SymlinkExecutable(args);
  627. }
  628. #if defined(CMAKE_HAVE_VS_GENERATORS)
  629. // Internal CMake support for calling Visual Studio macros.
  630. else if (args[1] == "cmake_call_visual_studio_macro" && args.size() >= 4)
  631. {
  632. // args[2] = full path to .sln file or "ALL"
  633. // args[3] = name of Visual Studio macro to call
  634. // args[4..args.size()-1] = [optional] args for Visual Studio macro
  635. std::string macroArgs;
  636. if (args.size() > 4)
  637. {
  638. macroArgs = args[4];
  639. for (size_t i = 5; i < args.size(); ++i)
  640. {
  641. macroArgs += " ";
  642. macroArgs += args[i];
  643. }
  644. }
  645. return cmCallVisualStudioMacro::CallMacro(args[2], args[3],
  646. macroArgs, true);
  647. }
  648. #endif
  649. // Internal CMake dependency scanning support.
  650. else if (args[1] == "cmake_depends" && args.size() >= 6)
  651. {
  652. // Use the make system's VERBOSE environment variable to enable
  653. // verbose output. This can be skipped by also setting CMAKE_NO_VERBOSE
  654. // (which is set by the Eclipse and KDevelop generators).
  655. bool verbose = ((cmSystemTools::GetEnv("VERBOSE") != 0)
  656. && (cmSystemTools::GetEnv("CMAKE_NO_VERBOSE") == 0));
  657. // Create a cmake object instance to process dependencies.
  658. cmake cm;
  659. std::string gen;
  660. std::string homeDir;
  661. std::string startDir;
  662. std::string homeOutDir;
  663. std::string startOutDir;
  664. std::string depInfo;
  665. bool color = false;
  666. if(args.size() >= 8)
  667. {
  668. // Full signature:
  669. //
  670. // -E cmake_depends <generator>
  671. // <home-src-dir> <start-src-dir>
  672. // <home-out-dir> <start-out-dir>
  673. // <dep-info> [--color=$(COLOR)]
  674. //
  675. // All paths are provided.
  676. gen = args[2];
  677. homeDir = args[3];
  678. startDir = args[4];
  679. homeOutDir = args[5];
  680. startOutDir = args[6];
  681. depInfo = args[7];
  682. if(args.size() >= 9 &&
  683. args[8].length() >= 8 &&
  684. args[8].substr(0, 8) == "--color=")
  685. {
  686. // Enable or disable color based on the switch value.
  687. color = (args[8].size() == 8 ||
  688. cmSystemTools::IsOn(args[8].substr(8).c_str()));
  689. }
  690. }
  691. else
  692. {
  693. // Support older signature for existing makefiles:
  694. //
  695. // -E cmake_depends <generator>
  696. // <home-out-dir> <start-out-dir>
  697. // <dep-info>
  698. //
  699. // Just pretend the source directories are the same as the
  700. // binary directories so at least scanning will work.
  701. gen = args[2];
  702. homeDir = args[3];
  703. startDir = args[4];
  704. homeOutDir = args[3];
  705. startOutDir = args[3];
  706. depInfo = args[5];
  707. }
  708. // Create a local generator configured for the directory in
  709. // which dependencies will be scanned.
  710. homeDir = cmSystemTools::CollapseFullPath(homeDir);
  711. startDir = cmSystemTools::CollapseFullPath(startDir);
  712. homeOutDir = cmSystemTools::CollapseFullPath(homeOutDir);
  713. startOutDir = cmSystemTools::CollapseFullPath(startOutDir);
  714. cm.SetHomeDirectory(homeDir);
  715. cm.SetHomeOutputDirectory(homeOutDir);
  716. if(cmGlobalGenerator* ggd = cm.CreateGlobalGenerator(gen))
  717. {
  718. cm.SetGlobalGenerator(ggd);
  719. cmState::Snapshot snapshot = cm.GetCurrentSnapshot();
  720. cmsys::auto_ptr<cmLocalGenerator> lgd(
  721. ggd->MakeLocalGenerator(snapshot));
  722. lgd->GetMakefile()->SetCurrentSourceDirectory(startDir);
  723. lgd->GetMakefile()->SetCurrentBinaryDirectory(startOutDir);
  724. // Actually scan dependencies.
  725. return lgd->UpdateDependencies(depInfo.c_str(),
  726. verbose, color)? 0 : 2;
  727. }
  728. return 1;
  729. }
  730. // Internal CMake link script support.
  731. else if (args[1] == "cmake_link_script" && args.size() >= 3)
  732. {
  733. return cmcmd::ExecuteLinkScript(args);
  734. }
  735. // Internal CMake unimplemented feature notification.
  736. else if (args[1] == "cmake_unimplemented_variable")
  737. {
  738. std::cerr << "Feature not implemented for this platform.";
  739. if(args.size() == 3)
  740. {
  741. std::cerr << " Variable " << args[2] << " is not set.";
  742. }
  743. std::cerr << std::endl;
  744. return 1;
  745. }
  746. else if (args[1] == "vs_link_exe")
  747. {
  748. return cmcmd::VisualStudioLink(args, 1);
  749. }
  750. else if (args[1] == "vs_link_dll")
  751. {
  752. return cmcmd::VisualStudioLink(args, 2);
  753. }
  754. // Internal CMake color makefile support.
  755. else if (args[1] == "cmake_echo_color")
  756. {
  757. return cmcmd::ExecuteEchoColor(args);
  758. }
  759. #ifdef CMAKE_BUILD_WITH_CMAKE
  760. else if (args[1] == "cmake_autogen" && args.size() >= 4)
  761. {
  762. cmQtAutoGenerators autogen;
  763. std::string const& config = args[3];
  764. bool autogenSuccess = autogen.Run(args[2], config);
  765. return autogenSuccess ? 0 : 1;
  766. }
  767. #endif
  768. // Tar files
  769. else if (args[1] == "tar" && args.size() > 3)
  770. {
  771. const char* knownFormats[] =
  772. {
  773. "7zip",
  774. "gnutar",
  775. "pax",
  776. "paxr",
  777. "zip"
  778. };
  779. std::string flags = args[2];
  780. std::string outFile = args[3];
  781. std::vector<std::string> files;
  782. std::string mtime;
  783. std::string format;
  784. bool doing_options = true;
  785. for (std::string::size_type cc = 4; cc < args.size(); cc ++)
  786. {
  787. std::string const& arg = args[cc];
  788. if (doing_options && cmHasLiteralPrefix(arg, "--"))
  789. {
  790. if (arg == "--")
  791. {
  792. doing_options = false;
  793. }
  794. else if (cmHasLiteralPrefix(arg, "--mtime="))
  795. {
  796. mtime = arg.substr(8);
  797. }
  798. else if (cmHasLiteralPrefix(arg, "--files-from="))
  799. {
  800. std::string const& files_from = arg.substr(13);
  801. if (!cmTarFilesFrom(files_from, files))
  802. {
  803. return 1;
  804. }
  805. }
  806. else if (cmHasLiteralPrefix(arg, "--format="))
  807. {
  808. format = arg.substr(9);
  809. bool isKnown = std::find(cmArrayBegin(knownFormats),
  810. cmArrayEnd(knownFormats), format) != cmArrayEnd(knownFormats);
  811. if(!isKnown)
  812. {
  813. cmSystemTools::Error("Unknown -E tar --format= argument: ",
  814. format.c_str());
  815. return 1;
  816. }
  817. }
  818. else
  819. {
  820. cmSystemTools::Error("Unknown option to -E tar: ", arg.c_str());
  821. return 1;
  822. }
  823. }
  824. else
  825. {
  826. files.push_back(arg);
  827. }
  828. }
  829. cmSystemTools::cmTarCompression compress =
  830. cmSystemTools::TarCompressNone;
  831. bool verbose = false;
  832. int nCompress = 0;
  833. if ( flags.find_first_of('j') != flags.npos )
  834. {
  835. compress = cmSystemTools::TarCompressBZip2;
  836. ++nCompress;
  837. }
  838. if ( flags.find_first_of('J') != flags.npos )
  839. {
  840. compress = cmSystemTools::TarCompressXZ;
  841. ++nCompress;
  842. }
  843. if ( flags.find_first_of('z') != flags.npos )
  844. {
  845. compress = cmSystemTools::TarCompressGZip;
  846. ++nCompress;
  847. }
  848. if ( (format == "7zip" || format == "zip") && nCompress > 0 )
  849. {
  850. cmSystemTools::Error("Can not use compression flags with format: ",
  851. format.c_str());
  852. return 1;
  853. }
  854. else if ( nCompress > 1 )
  855. {
  856. cmSystemTools::Error("Can only compress a tar file one way; "
  857. "at most one flag of z, j, or J may be used");
  858. return 1;
  859. }
  860. if ( flags.find_first_of('v') != flags.npos )
  861. {
  862. verbose = true;
  863. }
  864. if ( flags.find_first_of('t') != flags.npos )
  865. {
  866. if ( !cmSystemTools::ListTar(outFile.c_str(), verbose) )
  867. {
  868. cmSystemTools::Error("Problem listing tar: ", outFile.c_str());
  869. return 1;
  870. }
  871. }
  872. else if ( flags.find_first_of('c') != flags.npos )
  873. {
  874. if ( !cmSystemTools::CreateTar(
  875. outFile.c_str(), files, compress, verbose, mtime, format) )
  876. {
  877. cmSystemTools::Error("Problem creating tar: ", outFile.c_str());
  878. return 1;
  879. }
  880. }
  881. else if ( flags.find_first_of('x') != flags.npos )
  882. {
  883. if ( !cmSystemTools::ExtractTar(
  884. outFile.c_str(), verbose) )
  885. {
  886. cmSystemTools::Error("Problem extracting tar: ", outFile.c_str());
  887. return 1;
  888. }
  889. #ifdef WIN32
  890. // OK, on windows 7 after we untar some files,
  891. // sometimes we can not rename the directory after
  892. // the untar is done. This breaks the external project
  893. // untar and rename code. So, by default we will wait
  894. // 1/10th of a second after the untar. If CMAKE_UNTAR_DELAY
  895. // is set in the env, its value will be used instead of 100.
  896. int delay = 100;
  897. const char* delayVar = cmSystemTools::GetEnv("CMAKE_UNTAR_DELAY");
  898. if(delayVar)
  899. {
  900. delay = atoi(delayVar);
  901. }
  902. if(delay)
  903. {
  904. cmSystemTools::Delay(delay);
  905. }
  906. #endif
  907. }
  908. return 0;
  909. }
  910. #if defined(CMAKE_BUILD_WITH_CMAKE)
  911. // Internal CMake Fortran module support.
  912. else if (args[1] == "cmake_copy_f90_mod" && args.size() >= 4)
  913. {
  914. return cmDependsFortran::CopyModule(args)? 0 : 1;
  915. }
  916. #endif
  917. #if defined(_WIN32) && !defined(__CYGWIN__)
  918. // Write registry value
  919. else if (args[1] == "write_regv" && args.size() > 3)
  920. {
  921. return cmSystemTools::WriteRegistryValue(args[2].c_str(),
  922. args[3].c_str()) ? 0 : 1;
  923. }
  924. // Delete registry value
  925. else if (args[1] == "delete_regv" && args.size() > 2)
  926. {
  927. return cmSystemTools::DeleteRegistryValue(args[2].c_str()) ? 0 : 1;
  928. }
  929. // Remove file
  930. else if (args[1] == "comspec" && args.size() > 2)
  931. {
  932. std::cerr << "Win9x helper \"cmake -E comspec\" no longer supported\n";
  933. return 1;
  934. }
  935. else if (args[1] == "env_vs8_wince" && args.size() == 3)
  936. {
  937. return cmcmd::WindowsCEEnvironment("8.0", args[2]);
  938. }
  939. else if (args[1] == "env_vs9_wince" && args.size() == 3)
  940. {
  941. return cmcmd::WindowsCEEnvironment("9.0", args[2]);
  942. }
  943. #endif
  944. }
  945. ::CMakeCommandUsage(args[0].c_str());
  946. return 1;
  947. }
  948. //----------------------------------------------------------------------------
  949. int cmcmd::SymlinkLibrary(std::vector<std::string>& args)
  950. {
  951. int result = 0;
  952. std::string realName = args[2];
  953. std::string soName = args[3];
  954. std::string name = args[4];
  955. if(soName != realName)
  956. {
  957. if(!cmcmd::SymlinkInternal(realName, soName))
  958. {
  959. cmSystemTools::ReportLastSystemError("cmake_symlink_library");
  960. result = 1;
  961. }
  962. }
  963. if(name != soName)
  964. {
  965. if(!cmcmd::SymlinkInternal(soName, name))
  966. {
  967. cmSystemTools::ReportLastSystemError("cmake_symlink_library");
  968. result = 1;
  969. }
  970. }
  971. return result;
  972. }
  973. //----------------------------------------------------------------------------
  974. int cmcmd::SymlinkExecutable(std::vector<std::string>& args)
  975. {
  976. int result = 0;
  977. std::string realName = args[2];
  978. std::string name = args[3];
  979. if(name != realName)
  980. {
  981. if(!cmcmd::SymlinkInternal(realName, name))
  982. {
  983. cmSystemTools::ReportLastSystemError("cmake_symlink_executable");
  984. result = 1;
  985. }
  986. }
  987. return result;
  988. }
  989. //----------------------------------------------------------------------------
  990. bool cmcmd::SymlinkInternal(std::string const& file, std::string const& link)
  991. {
  992. if(cmSystemTools::FileExists(link.c_str()) ||
  993. cmSystemTools::FileIsSymlink(link))
  994. {
  995. cmSystemTools::RemoveFile(link);
  996. }
  997. #if defined(_WIN32) && !defined(__CYGWIN__)
  998. return cmSystemTools::CopyFileAlways(file.c_str(), link.c_str());
  999. #else
  1000. std::string linktext = cmSystemTools::GetFilenameName(file);
  1001. return cmSystemTools::CreateSymlink(linktext, link);
  1002. #endif
  1003. }
  1004. //----------------------------------------------------------------------------
  1005. static void cmcmdProgressReport(std::string const& dir,
  1006. std::string const& num)
  1007. {
  1008. std::string dirName = dir;
  1009. dirName += "/Progress";
  1010. std::string fName;
  1011. FILE *progFile;
  1012. // read the count
  1013. fName = dirName;
  1014. fName += "/count.txt";
  1015. progFile = cmsys::SystemTools::Fopen(fName,"r");
  1016. int count = 0;
  1017. if (!progFile)
  1018. {
  1019. return;
  1020. }
  1021. else
  1022. {
  1023. if (1!=fscanf(progFile,"%i",&count))
  1024. {
  1025. cmSystemTools::Message("Could not read from progress file.");
  1026. }
  1027. fclose(progFile);
  1028. }
  1029. const char* last = num.c_str();
  1030. for(const char* c = last;; ++c)
  1031. {
  1032. if (*c == ',' || *c == '\0')
  1033. {
  1034. if (c != last)
  1035. {
  1036. fName = dirName;
  1037. fName += "/";
  1038. fName.append(last, c-last);
  1039. progFile = cmsys::SystemTools::Fopen(fName,"w");
  1040. if (progFile)
  1041. {
  1042. fprintf(progFile,"empty");
  1043. fclose(progFile);
  1044. }
  1045. }
  1046. if(*c == '\0')
  1047. {
  1048. break;
  1049. }
  1050. last = c + 1;
  1051. }
  1052. }
  1053. int fileNum = static_cast<int>
  1054. (cmsys::Directory::GetNumberOfFilesInDirectory(dirName));
  1055. if (count > 0)
  1056. {
  1057. // print the progress
  1058. fprintf(stdout,"[%3i%%] ",((fileNum-3)*100)/count);
  1059. }
  1060. }
  1061. //----------------------------------------------------------------------------
  1062. int cmcmd::ExecuteEchoColor(std::vector<std::string>& args)
  1063. {
  1064. // The arguments are
  1065. // argv[0] == <cmake-executable>
  1066. // argv[1] == cmake_echo_color
  1067. bool enabled = true;
  1068. int color = cmsysTerminal_Color_Normal;
  1069. bool newline = true;
  1070. std::string progressDir;
  1071. for(unsigned int i=2; i < args.size(); ++i)
  1072. {
  1073. if(args[i].find("--switch=") == 0)
  1074. {
  1075. // Enable or disable color based on the switch value.
  1076. std::string value = args[i].substr(9);
  1077. if(!value.empty())
  1078. {
  1079. if(cmSystemTools::IsOn(value.c_str()))
  1080. {
  1081. enabled = true;
  1082. }
  1083. else
  1084. {
  1085. enabled = false;
  1086. }
  1087. }
  1088. }
  1089. else if(cmHasLiteralPrefix(args[i], "--progress-dir="))
  1090. {
  1091. progressDir = args[i].substr(15);
  1092. }
  1093. else if(cmHasLiteralPrefix(args[i], "--progress-num="))
  1094. {
  1095. if (!progressDir.empty())
  1096. {
  1097. std::string const& progressNum = args[i].substr(15);
  1098. cmcmdProgressReport(progressDir, progressNum);
  1099. }
  1100. }
  1101. else if(args[i] == "--normal")
  1102. {
  1103. color = cmsysTerminal_Color_Normal;
  1104. }
  1105. else if(args[i] == "--black")
  1106. {
  1107. color = cmsysTerminal_Color_ForegroundBlack;
  1108. }
  1109. else if(args[i] == "--red")
  1110. {
  1111. color = cmsysTerminal_Color_ForegroundRed;
  1112. }
  1113. else if(args[i] == "--green")
  1114. {
  1115. color = cmsysTerminal_Color_ForegroundGreen;
  1116. }
  1117. else if(args[i] == "--yellow")
  1118. {
  1119. color = cmsysTerminal_Color_ForegroundYellow;
  1120. }
  1121. else if(args[i] == "--blue")
  1122. {
  1123. color = cmsysTerminal_Color_ForegroundBlue;
  1124. }
  1125. else if(args[i] == "--magenta")
  1126. {
  1127. color = cmsysTerminal_Color_ForegroundMagenta;
  1128. }
  1129. else if(args[i] == "--cyan")
  1130. {
  1131. color = cmsysTerminal_Color_ForegroundCyan;
  1132. }
  1133. else if(args[i] == "--white")
  1134. {
  1135. color = cmsysTerminal_Color_ForegroundWhite;
  1136. }
  1137. else if(args[i] == "--bold")
  1138. {
  1139. color |= cmsysTerminal_Color_ForegroundBold;
  1140. }
  1141. else if(args[i] == "--no-newline")
  1142. {
  1143. newline = false;
  1144. }
  1145. else if(args[i] == "--newline")
  1146. {
  1147. newline = true;
  1148. }
  1149. else
  1150. {
  1151. // Color is enabled. Print with the current color.
  1152. cmSystemTools::MakefileColorEcho(color, args[i].c_str(),
  1153. newline, enabled);
  1154. }
  1155. }
  1156. return 0;
  1157. }
  1158. //----------------------------------------------------------------------------
  1159. int cmcmd::ExecuteLinkScript(std::vector<std::string>& args)
  1160. {
  1161. // The arguments are
  1162. // argv[0] == <cmake-executable>
  1163. // argv[1] == cmake_link_script
  1164. // argv[2] == <link-script-name>
  1165. // argv[3] == --verbose=?
  1166. bool verbose = false;
  1167. if(args.size() >= 4)
  1168. {
  1169. if(args[3].find("--verbose=") == 0)
  1170. {
  1171. if(!cmSystemTools::IsOff(args[3].substr(10).c_str()))
  1172. {
  1173. verbose = true;
  1174. }
  1175. }
  1176. }
  1177. // Allocate a process instance.
  1178. cmsysProcess* cp = cmsysProcess_New();
  1179. if(!cp)
  1180. {
  1181. std::cerr << "Error allocating process instance in link script."
  1182. << std::endl;
  1183. return 1;
  1184. }
  1185. // Children should share stdout and stderr with this process.
  1186. cmsysProcess_SetPipeShared(cp, cmsysProcess_Pipe_STDOUT, 1);
  1187. cmsysProcess_SetPipeShared(cp, cmsysProcess_Pipe_STDERR, 1);
  1188. // Run the command lines verbatim.
  1189. cmsysProcess_SetOption(cp, cmsysProcess_Option_Verbatim, 1);
  1190. // Read command lines from the script.
  1191. cmsys::ifstream fin(args[2].c_str());
  1192. if(!fin)
  1193. {
  1194. std::cerr << "Error opening link script \""
  1195. << args[2] << "\"" << std::endl;
  1196. return 1;
  1197. }
  1198. // Run one command at a time.
  1199. std::string command;
  1200. int result = 0;
  1201. while(result == 0 && cmSystemTools::GetLineFromStream(fin, command))
  1202. {
  1203. // Skip empty command lines.
  1204. if(command.find_first_not_of(" \t") == command.npos)
  1205. {
  1206. continue;
  1207. }
  1208. // Setup this command line.
  1209. const char* cmd[2] = {command.c_str(), 0};
  1210. cmsysProcess_SetCommand(cp, cmd);
  1211. // Report the command if verbose output is enabled.
  1212. if(verbose)
  1213. {
  1214. std::cout << command << std::endl;
  1215. }
  1216. // Run the command and wait for it to exit.
  1217. cmsysProcess_Execute(cp);
  1218. cmsysProcess_WaitForExit(cp, 0);
  1219. // Report failure if any.
  1220. switch(cmsysProcess_GetState(cp))
  1221. {
  1222. case cmsysProcess_State_Exited:
  1223. {
  1224. int value = cmsysProcess_GetExitValue(cp);
  1225. if(value != 0)
  1226. {
  1227. result = value;
  1228. }
  1229. }
  1230. break;
  1231. case cmsysProcess_State_Exception:
  1232. std::cerr << "Error running link command: "
  1233. << cmsysProcess_GetExceptionString(cp) << std::endl;
  1234. result = 1;
  1235. break;
  1236. case cmsysProcess_State_Error:
  1237. std::cerr << "Error running link command: "
  1238. << cmsysProcess_GetErrorString(cp) << std::endl;
  1239. result = 2;
  1240. break;
  1241. default:
  1242. break;
  1243. };
  1244. }
  1245. // Free the process instance.
  1246. cmsysProcess_Delete(cp);
  1247. // Return the final resulting return value.
  1248. return result;
  1249. }
  1250. //----------------------------------------------------------------------------
  1251. int cmcmd::WindowsCEEnvironment(const char* version, const std::string& name)
  1252. {
  1253. #if defined(CMAKE_HAVE_VS_GENERATORS)
  1254. cmVisualStudioWCEPlatformParser parser(name.c_str());
  1255. parser.ParseVersion(version);
  1256. if (parser.Found())
  1257. {
  1258. std::cout << "@echo off" << std::endl;
  1259. std::cout << "echo Environment Selection: " << name << std::endl;
  1260. std::cout << "set PATH=" << parser.GetPathDirectories() << std::endl;
  1261. std::cout << "set INCLUDE=" << parser.GetIncludeDirectories() <<std::endl;
  1262. std::cout << "set LIB=" << parser.GetLibraryDirectories() <<std::endl;
  1263. return 0;
  1264. }
  1265. #else
  1266. (void)version;
  1267. #endif
  1268. std::cerr << "Could not find " << name;
  1269. return -1;
  1270. }
  1271. // For visual studio 2005 and newer manifest files need to be embedded into
  1272. // exe and dll's. This code does that in such a way that incremental linking
  1273. // still works.
  1274. int cmcmd::VisualStudioLink(std::vector<std::string>& args, int type)
  1275. {
  1276. if(args.size() < 2)
  1277. {
  1278. return -1;
  1279. }
  1280. bool verbose = false;
  1281. if(cmSystemTools::GetEnv("VERBOSE"))
  1282. {
  1283. verbose = true;
  1284. }
  1285. std::vector<std::string> expandedArgs;
  1286. for(std::vector<std::string>::iterator i = args.begin();
  1287. i != args.end(); ++i)
  1288. {
  1289. // check for nmake temporary files
  1290. if((*i)[0] == '@' && i->find("@CMakeFiles") != 0 )
  1291. {
  1292. cmsys::ifstream fin(i->substr(1).c_str());
  1293. std::string line;
  1294. while(cmSystemTools::GetLineFromStream(fin,
  1295. line))
  1296. {
  1297. cmSystemTools::ParseWindowsCommandLine(line.c_str(), expandedArgs);
  1298. }
  1299. }
  1300. else
  1301. {
  1302. expandedArgs.push_back(*i);
  1303. }
  1304. }
  1305. bool hasIncremental = false;
  1306. bool hasManifest = true;
  1307. for(std::vector<std::string>::iterator i = expandedArgs.begin();
  1308. i != expandedArgs.end(); ++i)
  1309. {
  1310. if(cmSystemTools::Strucmp(i->c_str(), "/INCREMENTAL:YES") == 0)
  1311. {
  1312. hasIncremental = true;
  1313. }
  1314. if(cmSystemTools::Strucmp(i->c_str(), "/INCREMENTAL") == 0)
  1315. {
  1316. hasIncremental = true;
  1317. }
  1318. if(cmSystemTools::Strucmp(i->c_str(), "/MANIFEST:NO") == 0)
  1319. {
  1320. hasManifest = false;
  1321. }
  1322. }
  1323. if(hasIncremental && hasManifest)
  1324. {
  1325. if(verbose)
  1326. {
  1327. std::cout << "Visual Studio Incremental Link with embedded manifests\n";
  1328. }
  1329. return cmcmd::VisualStudioLinkIncremental(expandedArgs, type, verbose);
  1330. }
  1331. if(verbose)
  1332. {
  1333. if(!hasIncremental)
  1334. {
  1335. std::cout << "Visual Studio Non-Incremental Link\n";
  1336. }
  1337. else
  1338. {
  1339. std::cout << "Visual Studio Incremental Link without manifests\n";
  1340. }
  1341. }
  1342. return cmcmd::VisualStudioLinkNonIncremental(expandedArgs,
  1343. type, hasManifest, verbose);
  1344. }
  1345. int cmcmd::ParseVisualStudioLinkCommand(std::vector<std::string>& args,
  1346. std::vector<std::string>& command,
  1347. std::string& targetName)
  1348. {
  1349. std::vector<std::string>::iterator i = args.begin();
  1350. i++; // skip -E
  1351. i++; // skip vs_link_dll or vs_link_exe
  1352. command.push_back(*i);
  1353. i++; // move past link command
  1354. for(; i != args.end(); ++i)
  1355. {
  1356. command.push_back(*i);
  1357. if(i->find("/Fe") == 0)
  1358. {
  1359. targetName = i->substr(3);
  1360. }
  1361. if(i->find("/out:") == 0)
  1362. {
  1363. targetName = i->substr(5);
  1364. }
  1365. }
  1366. if(targetName.empty() || command.empty())
  1367. {
  1368. return -1;
  1369. }
  1370. return 0;
  1371. }
  1372. bool cmcmd::RunCommand(const char* comment,
  1373. std::vector<std::string>& command,
  1374. bool verbose,
  1375. int* retCodeOut)
  1376. {
  1377. if(verbose)
  1378. {
  1379. std::cout << comment << ":\n";
  1380. std::cout << cmJoin(command, " ") << "\n";
  1381. }
  1382. std::string output;
  1383. int retCode =0;
  1384. // use rc command to create .res file
  1385. bool res = cmSystemTools::RunSingleCommand(command,
  1386. &output, &output,
  1387. &retCode, 0,
  1388. cmSystemTools::OUTPUT_NONE);
  1389. // always print the output of the command, unless
  1390. // it is the dumb rc command banner, but if the command
  1391. // returned an error code then print the output anyway as
  1392. // the banner may be mixed with some other important information.
  1393. if(output.find("Resource Compiler Version") == output.npos
  1394. || !res || retCode)
  1395. {
  1396. std::cout << output;
  1397. }
  1398. if (!res)
  1399. {
  1400. std::cout << comment << " failed to run." << std::endl;
  1401. return false;
  1402. }
  1403. // if retCodeOut is requested then always return true
  1404. // and set the retCodeOut to retCode
  1405. if(retCodeOut)
  1406. {
  1407. *retCodeOut = retCode;
  1408. return true;
  1409. }
  1410. if(retCode != 0)
  1411. {
  1412. std::cout << comment << " failed. with " << retCode << "\n";
  1413. }
  1414. return retCode == 0;
  1415. }
  1416. int cmcmd::VisualStudioLinkIncremental(std::vector<std::string>& args,
  1417. int type, bool verbose)
  1418. {
  1419. // This follows the steps listed here:
  1420. // http://blogs.msdn.com/zakramer/archive/2006/05/22/603558.aspx
  1421. // 1. Compiler compiles the application and generates the *.obj files.
  1422. // 2. An empty manifest file is generated if this is a clean build and if
  1423. // not the previous one is reused.
  1424. // 3. The resource compiler (rc.exe) compiles the *.manifest file to a
  1425. // *.res file.
  1426. // 4. Linker generates the binary (EXE or DLL) with the /incremental
  1427. // switch and embeds the dummy manifest file. The linker also generates
  1428. // the real manifest file based on the binaries that your binary depends
  1429. // on.
  1430. // 5. The manifest tool (mt.exe) is then used to generate the final
  1431. // manifest.
  1432. // If the final manifest is changed, then 6 and 7 are run, if not
  1433. // they are skipped, and it is done.
  1434. // 6. The resource compiler is invoked one more time.
  1435. // 7. Finally, the Linker does another incremental link, but since the
  1436. // only thing that has changed is the *.res file that contains the
  1437. // manifest it is a short link.
  1438. std::vector<std::string> linkCommand;
  1439. std::string targetName;
  1440. if(cmcmd::ParseVisualStudioLinkCommand(args, linkCommand, targetName) == -1)
  1441. {
  1442. return -1;
  1443. }
  1444. std::string manifestArg = "/MANIFESTFILE:";
  1445. std::vector<std::string> rcCommand;
  1446. rcCommand.push_back(cmSystemTools::FindProgram("rc.exe"));
  1447. std::vector<std::string> mtCommand;
  1448. mtCommand.push_back(cmSystemTools::FindProgram("mt.exe"));
  1449. std::string tempManifest;
  1450. tempManifest = targetName;
  1451. tempManifest += ".intermediate.manifest";
  1452. std::string resourceInputFile = targetName;
  1453. resourceInputFile += ".resource.txt";
  1454. if(verbose)
  1455. {
  1456. std::cout << "Create " << resourceInputFile << "\n";
  1457. }
  1458. // Create input file for rc command
  1459. cmsys::ofstream fout(resourceInputFile.c_str());
  1460. if(!fout)
  1461. {
  1462. return -1;
  1463. }
  1464. std::string manifestFile = targetName;
  1465. manifestFile += ".embed.manifest";
  1466. std::string fullPath= cmSystemTools::CollapseFullPath(manifestFile);
  1467. fout << type << " /* CREATEPROCESS_MANIFEST_RESOURCE_ID "
  1468. "*/ 24 /* RT_MANIFEST */ " << "\"" << fullPath << "\"";
  1469. fout.close();
  1470. manifestArg += tempManifest;
  1471. // add the manifest arg to the linkCommand
  1472. linkCommand.push_back("/MANIFEST");
  1473. linkCommand.push_back(manifestArg);
  1474. // if manifestFile is not yet created, create an
  1475. // empty one
  1476. if(!cmSystemTools::FileExists(manifestFile.c_str()))
  1477. {
  1478. if(verbose)
  1479. {
  1480. std::cout << "Create empty: " << manifestFile << "\n";
  1481. }
  1482. cmsys::ofstream foutTmp(manifestFile.c_str());
  1483. }
  1484. std::string resourceFile = manifestFile;
  1485. resourceFile += ".res";
  1486. // add the resource file to the end of the link command
  1487. linkCommand.push_back(resourceFile);
  1488. std::string outputOpt = "/fo";
  1489. outputOpt += resourceFile;
  1490. rcCommand.push_back(outputOpt);
  1491. rcCommand.push_back(resourceInputFile);
  1492. // Run rc command to create resource
  1493. if(!cmcmd::RunCommand("RC Pass 1", rcCommand, verbose))
  1494. {
  1495. return -1;
  1496. }
  1497. // Now run the link command to link and create manifest
  1498. if(!cmcmd::RunCommand("LINK Pass 1", linkCommand, verbose))
  1499. {
  1500. return -1;
  1501. }
  1502. // create mt command
  1503. std::string outArg("/out:");
  1504. outArg+= manifestFile;
  1505. mtCommand.push_back("/nologo");
  1506. mtCommand.push_back(outArg);
  1507. mtCommand.push_back("/notify_update");
  1508. mtCommand.push_back("/manifest");
  1509. mtCommand.push_back(tempManifest);
  1510. // now run mt.exe to create the final manifest file
  1511. int mtRet =0;
  1512. if(!cmcmd::RunCommand("MT", mtCommand, verbose, &mtRet))
  1513. {
  1514. return -1;
  1515. }
  1516. // if mt returns 0, then the manifest was not changed and
  1517. // we do not need to do another link step
  1518. if(mtRet == 0)
  1519. {
  1520. return 0;
  1521. }
  1522. // check for magic mt return value if mt returns the magic number
  1523. // 1090650113 then it means that it updated the manifest file and we need
  1524. // to do the final link. If mt has any value other than 0 or 1090650113
  1525. // then there was some problem with the command itself and there was an
  1526. // error so return the error code back out of cmake so make can report it.
  1527. // (when hosted on a posix system the value is 187)
  1528. if(mtRet != 1090650113 && mtRet != 187)
  1529. {
  1530. return mtRet;
  1531. }
  1532. // update the resource file with the new manifest from the mt command.
  1533. if(!cmcmd::RunCommand("RC Pass 2", rcCommand, verbose))
  1534. {
  1535. return -1;
  1536. }
  1537. // Run the final incremental link that will put the new manifest resource
  1538. // into the file incrementally.
  1539. if(!cmcmd::RunCommand("FINAL LINK", linkCommand, verbose))
  1540. {
  1541. return -1;
  1542. }
  1543. return 0;
  1544. }
  1545. int cmcmd::VisualStudioLinkNonIncremental(std::vector<std::string>& args,
  1546. int type,
  1547. bool hasManifest,
  1548. bool verbose)
  1549. {
  1550. std::vector<std::string> linkCommand;
  1551. std::string targetName;
  1552. if(cmcmd::ParseVisualStudioLinkCommand(args, linkCommand, targetName) == -1)
  1553. {
  1554. return -1;
  1555. }
  1556. // Run the link command as given
  1557. if (hasManifest)
  1558. {
  1559. linkCommand.push_back("/MANIFEST");
  1560. }
  1561. if(!cmcmd::RunCommand("LINK", linkCommand, verbose))
  1562. {
  1563. return -1;
  1564. }
  1565. if(!hasManifest)
  1566. {
  1567. return 0;
  1568. }
  1569. std::vector<std::string> mtCommand;
  1570. mtCommand.push_back(cmSystemTools::FindProgram("mt.exe"));
  1571. mtCommand.push_back("/nologo");
  1572. mtCommand.push_back("/manifest");
  1573. std::string manifestFile = targetName;
  1574. manifestFile += ".manifest";
  1575. mtCommand.push_back(manifestFile);
  1576. std::string outresource = "/outputresource:";
  1577. outresource += targetName;
  1578. outresource += ";#";
  1579. if(type == 1)
  1580. {
  1581. outresource += "1";
  1582. }
  1583. else if(type == 2)
  1584. {
  1585. outresource += "2";
  1586. }
  1587. mtCommand.push_back(outresource);
  1588. // Now use the mt tool to embed the manifest into the exe or dll
  1589. if(!cmcmd::RunCommand("MT", mtCommand, verbose))
  1590. {
  1591. return -1;
  1592. }
  1593. return 0;
  1594. }