cmcmd.cxx 40 KB

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