cmcmd.cxx 45 KB

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