cmcmd.cxx 45 KB

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