cmGlobalVisualStudio6Generator.cxx 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535
  1. /*=========================================================================
  2. Program: CMake - Cross-Platform Makefile Generator
  3. Module: $RCSfile$
  4. Language: C++
  5. Date: $Date$
  6. Version: $Revision$
  7. Copyright (c) 2002 Kitware, Inc., Insight Consortium. All rights reserved.
  8. See Copyright.txt or http://www.cmake.org/HTML/Copyright.html for details.
  9. This software is distributed WITHOUT ANY WARRANTY; without even
  10. the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  11. PURPOSE. See the above copyright notices for more information.
  12. =========================================================================*/
  13. #include "cmGlobalVisualStudio6Generator.h"
  14. #include "cmLocalVisualStudio6Generator.h"
  15. #include "cmMakefile.h"
  16. #include "cmake.h"
  17. cmGlobalVisualStudio6Generator::cmGlobalVisualStudio6Generator()
  18. {
  19. this->FindMakeProgramFile = "CMakeVS6FindMake.cmake";
  20. }
  21. void cmGlobalVisualStudio6Generator
  22. ::EnableLanguage(std::vector<std::string>const& lang, cmMakefile *mf)
  23. {
  24. mf->AddDefinition("CMAKE_GENERATOR_CC", "cl");
  25. mf->AddDefinition("CMAKE_GENERATOR_CXX", "cl");
  26. mf->AddDefinition("CMAKE_GENERATOR_RC", "rc");
  27. mf->AddDefinition("CMAKE_GENERATOR_NO_COMPILER_ENV", "1");
  28. mf->AddDefinition("CMAKE_GENERATOR_Fortran", "ifort");
  29. mf->AddDefinition("MSVC60", "1");
  30. this->GenerateConfigurations(mf);
  31. this->cmGlobalGenerator::EnableLanguage(lang, mf);
  32. }
  33. void cmGlobalVisualStudio6Generator::GenerateConfigurations(cmMakefile* mf)
  34. {
  35. std::string fname= mf->GetRequiredDefinition("CMAKE_ROOT");
  36. const char* def= mf->GetDefinition( "MSPROJECT_TEMPLATE_DIRECTORY");
  37. if(def)
  38. {
  39. fname = def;
  40. }
  41. else
  42. {
  43. fname += "/Templates";
  44. }
  45. fname += "/CMakeVisualStudio6Configurations.cmake";
  46. if(!mf->ReadListFile(mf->GetCurrentListFile(), fname.c_str()))
  47. {
  48. cmSystemTools::Error("Cannot open ", fname.c_str(),
  49. ". Please copy this file from the main "
  50. "CMake/Templates directory and edit it for "
  51. "your build configurations.");
  52. }
  53. else if(!mf->GetDefinition("CMAKE_CONFIGURATION_TYPES"))
  54. {
  55. cmSystemTools::Error("CMAKE_CONFIGURATION_TYPES not set by ",
  56. fname.c_str(),
  57. ". Please copy this file from the main "
  58. "CMake/Templates directory and edit it for "
  59. "your build configurations.");
  60. }
  61. }
  62. std::string cmGlobalVisualStudio6Generator
  63. ::GenerateBuildCommand(const char* makeProgram,
  64. const char *projectName,
  65. const char* additionalOptions,
  66. const char *targetName,
  67. const char* config,
  68. bool ignoreErrors,
  69. bool)
  70. {
  71. // Ingoring errors is not implemented in visual studio 6
  72. (void) ignoreErrors;
  73. // now build the test
  74. std::vector<std::string> mp;
  75. mp.push_back("[HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\VisualStudio"
  76. "\\6.0\\Setup;VsCommonDir]/MSDev98/Bin");
  77. cmSystemTools::ExpandRegistryValues(mp[0]);
  78. std::string originalCommand = makeProgram;
  79. std::string makeCommand =
  80. cmSystemTools::FindProgram(makeProgram, mp);
  81. if(makeCommand.size() == 0)
  82. {
  83. std::string e = "Generator cannot find Visual Studio 6 msdev program \"";
  84. e += originalCommand;
  85. e += "\" specified by CMAKE_MAKE_PROGRAM cache entry. ";
  86. e += "Please fix the setting.";
  87. cmSystemTools::Error(e.c_str());
  88. return "";
  89. }
  90. makeCommand = cmSystemTools::ConvertToOutputPath(makeCommand.c_str());
  91. // if there are spaces in the makeCommand, assume a full path
  92. // and convert it to a path with no spaces in it as the
  93. // RunSingleCommand does not like spaces
  94. #if defined(_WIN32) && !defined(__CYGWIN__)
  95. if(makeCommand.find(' ') != std::string::npos)
  96. {
  97. cmSystemTools::GetShortPath(makeCommand.c_str(), makeCommand);
  98. }
  99. #endif
  100. makeCommand += " ";
  101. makeCommand += projectName;
  102. makeCommand += ".dsw /MAKE \"";
  103. bool clean = false;
  104. if ( targetName && strcmp(targetName, "clean") == 0 )
  105. {
  106. clean = true;
  107. targetName = "ALL_BUILD";
  108. }
  109. if (targetName && strlen(targetName))
  110. {
  111. makeCommand += targetName;
  112. }
  113. else
  114. {
  115. makeCommand += "ALL_BUILD";
  116. }
  117. makeCommand += " - ";
  118. if(config && strlen(config))
  119. {
  120. makeCommand += config;
  121. }
  122. else
  123. {
  124. makeCommand += "Debug";
  125. }
  126. if(clean)
  127. {
  128. makeCommand += "\" /CLEAN";
  129. }
  130. else
  131. {
  132. makeCommand += "\" /BUILD";
  133. }
  134. if ( additionalOptions )
  135. {
  136. makeCommand += " ";
  137. makeCommand += additionalOptions;
  138. }
  139. return makeCommand;
  140. }
  141. ///! Create a local generator appropriate to this Global Generator
  142. cmLocalGenerator *cmGlobalVisualStudio6Generator::CreateLocalGenerator()
  143. {
  144. cmLocalGenerator *lg = new cmLocalVisualStudio6Generator;
  145. lg->SetGlobalGenerator(this);
  146. return lg;
  147. }
  148. void cmGlobalVisualStudio6Generator::Generate()
  149. {
  150. // add a special target that depends on ALL projects for easy build
  151. // of one configuration only.
  152. const char* no_output = 0;
  153. std::vector<std::string> no_depends;
  154. const char* no_working_dir = 0;
  155. std::map<cmStdString, std::vector<cmLocalGenerator*> >::iterator it;
  156. for(it = this->ProjectMap.begin(); it!= this->ProjectMap.end(); ++it)
  157. {
  158. std::vector<cmLocalGenerator*>& gen = it->second;
  159. // add the ALL_BUILD to the first local generator of each project
  160. if(gen.size())
  161. {
  162. gen[0]->GetMakefile()->AddUtilityCommand("ALL_BUILD", false,
  163. no_output, no_depends,
  164. no_working_dir,
  165. "echo", "Build all projects");
  166. }
  167. }
  168. // add the Run Tests command
  169. this->SetupTests();
  170. // first do the superclass method
  171. this->cmGlobalGenerator::Generate();
  172. // Now write out the DSW
  173. this->OutputDSWFile();
  174. }
  175. // Write a DSW file to the stream
  176. void cmGlobalVisualStudio6Generator
  177. ::WriteDSWFile(std::ostream& fout,cmLocalGenerator* root,
  178. std::vector<cmLocalGenerator*>& generators)
  179. {
  180. // Write out the header for a DSW file
  181. this->WriteDSWHeader(fout);
  182. // Get the home directory with the trailing slash
  183. std::string homedir = root->GetMakefile()->GetStartOutputDirectory();
  184. homedir += "/";
  185. unsigned int i;
  186. bool doneAllBuild = false;
  187. bool doneRunTests = false;
  188. bool doneInstall = false;
  189. bool doneEditCache = false;
  190. bool doneRebuildCache = false;
  191. bool donePackage = false;
  192. for(i = 0; i < generators.size(); ++i)
  193. {
  194. if(this->IsExcluded(root, generators[i]))
  195. {
  196. continue;
  197. }
  198. cmMakefile* mf = generators[i]->GetMakefile();
  199. // Get the source directory from the makefile
  200. std::string dir = mf->GetStartOutputDirectory();
  201. // remove the home directory and / from the source directory
  202. // this gives a relative path
  203. cmSystemTools::ReplaceString(dir, homedir.c_str(), "");
  204. // Get the list of create dsp files names from the LocalGenerator, more
  205. // than one dsp could have been created per input CMakeLists.txt file
  206. // for each target
  207. std::vector<std::string> dspnames =
  208. static_cast<cmLocalVisualStudio6Generator *>(generators[i])
  209. ->GetCreatedProjectNames();
  210. cmTargets &tgts = generators[i]->GetMakefile()->GetTargets();
  211. cmTargets::iterator l = tgts.begin();
  212. for(std::vector<std::string>::iterator si = dspnames.begin();
  213. l != tgts.end(); ++l)
  214. {
  215. // special handling for the current makefile
  216. if(mf == generators[0]->GetMakefile())
  217. {
  218. dir = "."; // no subdirectory for project generated
  219. // if this is the special ALL_BUILD utility, then
  220. // make it depend on every other non UTILITY project.
  221. // This is done by adding the names to the GetUtilities
  222. // vector on the makefile
  223. if(l->first == "ALL_BUILD" && !doneAllBuild)
  224. {
  225. unsigned int j;
  226. for(j = 0; j < generators.size(); ++j)
  227. {
  228. cmTargets &atgts = generators[j]->GetMakefile()->GetTargets();
  229. for(cmTargets::iterator al = atgts.begin();
  230. al != atgts.end(); ++al)
  231. {
  232. if (al->second.IsInAll())
  233. {
  234. if (al->second.GetType() == cmTarget::UTILITY ||
  235. al->second.GetType() == cmTarget::GLOBAL_TARGET)
  236. {
  237. l->second.AddUtility(al->first.c_str());
  238. }
  239. else
  240. {
  241. l->second.AddLinkLibrary(al->first, cmTarget::GENERAL);
  242. }
  243. }
  244. }
  245. }
  246. }
  247. }
  248. // Write the project into the DSW file
  249. if (strncmp(l->first.c_str(), "INCLUDE_EXTERNAL_MSPROJECT", 26) == 0)
  250. {
  251. cmCustomCommand cc = l->second.GetPostBuildCommands()[0];
  252. const cmCustomCommandLines& cmds = cc.GetCommandLines();
  253. std::string project = cmds[0][0];
  254. std::string location = cmds[0][1];
  255. this->WriteExternalProject(fout, project.c_str(),
  256. location.c_str(), cc.GetDepends());
  257. }
  258. else
  259. {
  260. if ((l->second.GetType() != cmTarget::INSTALL_FILES)
  261. && (l->second.GetType() != cmTarget::INSTALL_PROGRAMS))
  262. {
  263. bool skip = false;
  264. // skip ALL_BUILD and RUN_TESTS if they have already been added
  265. if(l->first == "ALL_BUILD" )
  266. {
  267. if(doneAllBuild)
  268. {
  269. skip = true;
  270. }
  271. else
  272. {
  273. doneAllBuild = true;
  274. }
  275. }
  276. if(l->first == "INSTALL")
  277. {
  278. if(doneInstall)
  279. {
  280. skip = true;
  281. }
  282. else
  283. {
  284. doneInstall = true;
  285. }
  286. }
  287. if(l->first == "RUN_TESTS")
  288. {
  289. if(doneRunTests)
  290. {
  291. skip = true;
  292. }
  293. else
  294. {
  295. doneRunTests = true;
  296. }
  297. }
  298. if(l->first == "EDIT_CACHE")
  299. {
  300. if(doneEditCache)
  301. {
  302. skip = true;
  303. }
  304. else
  305. {
  306. doneEditCache = true;
  307. }
  308. }
  309. if(l->first == "REBUILD_CACHE")
  310. {
  311. if(doneRebuildCache)
  312. {
  313. skip = true;
  314. }
  315. else
  316. {
  317. doneRebuildCache = true;
  318. }
  319. }
  320. if(l->first == "PACKAGE")
  321. {
  322. if(donePackage)
  323. {
  324. skip = true;
  325. }
  326. else
  327. {
  328. donePackage = true;
  329. }
  330. }
  331. if(!skip)
  332. {
  333. this->WriteProject(fout, si->c_str(), dir.c_str(),l->second);
  334. }
  335. ++si;
  336. }
  337. }
  338. }
  339. }
  340. // Write the footer for the DSW file
  341. this->WriteDSWFooter(fout);
  342. }
  343. void cmGlobalVisualStudio6Generator
  344. ::OutputDSWFile(cmLocalGenerator* root,
  345. std::vector<cmLocalGenerator*>& generators)
  346. {
  347. if(generators.size() == 0)
  348. {
  349. return;
  350. }
  351. std::string fname = root->GetMakefile()->GetStartOutputDirectory();
  352. fname += "/";
  353. fname += root->GetMakefile()->GetProjectName();
  354. fname += ".dsw";
  355. std::ofstream fout(fname.c_str());
  356. if(!fout)
  357. {
  358. cmSystemTools::Error("Error can not open DSW file for write: ",
  359. fname.c_str());
  360. cmSystemTools::ReportLastSystemError("");
  361. return;
  362. }
  363. this->WriteDSWFile(fout, root, generators);
  364. }
  365. // output the DSW file
  366. void cmGlobalVisualStudio6Generator::OutputDSWFile()
  367. {
  368. std::map<cmStdString, std::vector<cmLocalGenerator*> >::iterator it;
  369. for(it = this->ProjectMap.begin(); it!= this->ProjectMap.end(); ++it)
  370. {
  371. this->OutputDSWFile(it->second[0], it->second);
  372. }
  373. }
  374. // Write a dsp file into the DSW file,
  375. // Note, that dependencies from executables to
  376. // the libraries it uses are also done here
  377. void cmGlobalVisualStudio6Generator::WriteProject(std::ostream& fout,
  378. const char* dspname,
  379. const char* dir,
  380. cmTarget& target)
  381. {
  382. fout << "#########################################################"
  383. "######################\n\n";
  384. fout << "Project: \"" << dspname << "\"="
  385. << dir << "\\" << dspname << ".dsp - Package Owner=<4>\n\n";
  386. fout << "Package=<5>\n{{{\n}}}\n\n";
  387. fout << "Package=<4>\n";
  388. fout << "{{{\n";
  389. // insert Begin Project Dependency Project_Dep_Name project stuff here
  390. if (target.GetType() != cmTarget::STATIC_LIBRARY)
  391. {
  392. cmTarget::LinkLibraryVectorType::const_iterator j, jend;
  393. j = target.GetLinkLibraries().begin();
  394. jend = target.GetLinkLibraries().end();
  395. for(;j!= jend; ++j)
  396. {
  397. if(j->first != dspname)
  398. {
  399. // is the library part of this DSW ? If so add dependency
  400. if(this->FindTarget(0, j->first.c_str()))
  401. {
  402. fout << "Begin Project Dependency\n";
  403. fout << "Project_Dep_Name " << j->first.c_str() << "\n";
  404. fout << "End Project Dependency\n";
  405. }
  406. }
  407. }
  408. }
  409. std::set<cmStdString>::const_iterator i, end;
  410. // write utility dependencies.
  411. i = target.GetUtilities().begin();
  412. end = target.GetUtilities().end();
  413. for(;i!= end; ++i)
  414. {
  415. if(*i != dspname)
  416. {
  417. std::string depName = *i;
  418. if(strncmp(depName.c_str(), "INCLUDE_EXTERNAL_MSPROJECT", 26) == 0)
  419. {
  420. depName.erase(depName.begin(), depName.begin() + 27);
  421. }
  422. fout << "Begin Project Dependency\n";
  423. fout << "Project_Dep_Name " << depName << "\n";
  424. fout << "End Project Dependency\n";
  425. }
  426. }
  427. fout << "}}}\n\n";
  428. }
  429. // Write a dsp file into the DSW file,
  430. // Note, that dependencies from executables to
  431. // the libraries it uses are also done here
  432. void cmGlobalVisualStudio6Generator::WriteExternalProject(std::ostream& fout,
  433. const char* name,
  434. const char* location,
  435. const std::vector<std::string>& dependencies)
  436. {
  437. fout << "#########################################################"
  438. "######################\n\n";
  439. fout << "Project: \"" << name << "\"="
  440. << location << " - Package Owner=<4>\n\n";
  441. fout << "Package=<5>\n{{{\n}}}\n\n";
  442. fout << "Package=<4>\n";
  443. fout << "{{{\n";
  444. std::vector<std::string>::const_iterator i, end;
  445. // write dependencies.
  446. i = dependencies.begin();
  447. end = dependencies.end();
  448. for(;i!= end; ++i)
  449. {
  450. fout << "Begin Project Dependency\n";
  451. fout << "Project_Dep_Name " << *i << "\n";
  452. fout << "End Project Dependency\n";
  453. }
  454. fout << "}}}\n\n";
  455. }
  456. // Standard end of dsw file
  457. void cmGlobalVisualStudio6Generator::WriteDSWFooter(std::ostream& fout)
  458. {
  459. fout << "######################################################"
  460. "#########################\n\n";
  461. fout << "Global:\n\n";
  462. fout << "Package=<5>\n{{{\n}}}\n\n";
  463. fout << "Package=<3>\n{{{\n}}}\n\n";
  464. fout << "#####################################################"
  465. "##########################\n\n";
  466. }
  467. // ouput standard header for dsw file
  468. void cmGlobalVisualStudio6Generator::WriteDSWHeader(std::ostream& fout)
  469. {
  470. fout << "Microsoft Developer Studio Workspace File, Format Version 6.00\n";
  471. fout << "# WARNING: DO NOT EDIT OR DELETE THIS WORKSPACE FILE!\n\n";
  472. }
  473. //----------------------------------------------------------------------------
  474. void cmGlobalVisualStudio6Generator
  475. ::GetDocumentation(cmDocumentationEntry& entry) const
  476. {
  477. entry.name = this->GetName();
  478. entry.brief = "Generates Visual Studio 6 project files.";
  479. entry.full = "";
  480. }
  481. //----------------------------------------------------------------------------
  482. void
  483. cmGlobalVisualStudio6Generator
  484. ::AppendDirectoryForConfig(const char* prefix,
  485. const char* config,
  486. const char* suffix,
  487. std::string& dir)
  488. {
  489. if(config)
  490. {
  491. dir += prefix;
  492. dir += config;
  493. dir += suffix;
  494. }
  495. }