cmGlobalVisualStudio6Generator.cxx 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  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 "cmGlobalVisualStudio6Generator.h"
  11. #include "cmLocalVisualStudio6Generator.h"
  12. #include "cmMakefile.h"
  13. #include "cmake.h"
  14. #include "cmGeneratedFileStream.h"
  15. // Utility function to make a valid VS6 *.dsp filename out
  16. // of a CMake target name:
  17. //
  18. std::string GetVS6TargetName(const std::string& targetName)
  19. {
  20. std::string name(targetName);
  21. // Eliminate hyphens. VS6 cannot handle hyphens in *.dsp filenames...
  22. // Replace them with underscores.
  23. //
  24. cmSystemTools::ReplaceString(name, "-", "_");
  25. return name;
  26. }
  27. cmGlobalVisualStudio6Generator::cmGlobalVisualStudio6Generator()
  28. {
  29. this->MSDevCommandInitialized = false;
  30. }
  31. void cmGlobalVisualStudio6Generator
  32. ::EnableLanguage(std::vector<std::string>const& lang,
  33. cmMakefile *mf,
  34. bool optional)
  35. {
  36. cmGlobalVisualStudioGenerator::AddPlatformDefinitions(mf);
  37. mf->AddDefinition("CMAKE_GENERATOR_RC", "rc");
  38. mf->AddDefinition("CMAKE_GENERATOR_NO_COMPILER_ENV", "1");
  39. this->GenerateConfigurations(mf);
  40. this->cmGlobalGenerator::EnableLanguage(lang, mf, optional);
  41. }
  42. void cmGlobalVisualStudio6Generator::GenerateConfigurations(cmMakefile* mf)
  43. {
  44. std::string fname= mf->GetRequiredDefinition("CMAKE_ROOT");
  45. const char* def= mf->GetDefinition( "MSPROJECT_TEMPLATE_DIRECTORY");
  46. if(def)
  47. {
  48. fname = def;
  49. }
  50. else
  51. {
  52. fname += "/Templates";
  53. }
  54. fname += "/CMakeVisualStudio6Configurations.cmake";
  55. if(!mf->ReadListFile(mf->GetCurrentListFile(), fname.c_str()))
  56. {
  57. cmSystemTools::Error("Cannot open ", fname.c_str(),
  58. ". Please copy this file from the main "
  59. "CMake/Templates directory and edit it for "
  60. "your build configurations.");
  61. }
  62. else if(!mf->GetDefinition("CMAKE_CONFIGURATION_TYPES"))
  63. {
  64. cmSystemTools::Error("CMAKE_CONFIGURATION_TYPES not set by ",
  65. fname.c_str(),
  66. ". Please copy this file from the main "
  67. "CMake/Templates directory and edit it for "
  68. "your build configurations.");
  69. }
  70. }
  71. //----------------------------------------------------------------------------
  72. void cmGlobalVisualStudio6Generator::FindMakeProgram(cmMakefile* mf)
  73. {
  74. this->cmGlobalVisualStudioGenerator::FindMakeProgram(mf);
  75. mf->AddDefinition("CMAKE_VS_MSDEV_COMMAND",
  76. this->GetMSDevCommand().c_str());
  77. }
  78. //----------------------------------------------------------------------------
  79. std::string const& cmGlobalVisualStudio6Generator::GetMSDevCommand()
  80. {
  81. if(!this->MSDevCommandInitialized)
  82. {
  83. this->MSDevCommandInitialized = true;
  84. this->MSDevCommand = this->FindMSDevCommand();
  85. }
  86. return this->MSDevCommand;
  87. }
  88. //----------------------------------------------------------------------------
  89. std::string cmGlobalVisualStudio6Generator::FindMSDevCommand()
  90. {
  91. std::string vscmd;
  92. std::string vskey = this->GetRegistryBase() + "\\Setup;VsCommonDir";
  93. if(cmSystemTools::ReadRegistryValue(vskey.c_str(), vscmd,
  94. cmSystemTools::KeyWOW64_32))
  95. {
  96. cmSystemTools::ConvertToUnixSlashes(vscmd);
  97. vscmd += "/MSDev98/Bin/";
  98. }
  99. vscmd += "msdev.exe";
  100. return vscmd;
  101. }
  102. //----------------------------------------------------------------------------
  103. void
  104. cmGlobalVisualStudio6Generator::GenerateBuildCommand(
  105. std::vector<std::string>& makeCommand,
  106. const char* makeProgram,
  107. const char* projectName,
  108. const char* /*projectDir*/,
  109. const char* targetName,
  110. const char* config,
  111. bool /*fast*/,
  112. std::vector<std::string> const& makeOptions
  113. )
  114. {
  115. // now build the test
  116. makeCommand.push_back(
  117. this->SelectMakeProgram(makeProgram, this->GetMSDevCommand())
  118. );
  119. makeCommand.push_back(std::string(projectName)+".dsw");
  120. makeCommand.push_back("/MAKE");
  121. std::string targetArg;
  122. bool clean = false;
  123. if ( targetName && strcmp(targetName, "clean") == 0 )
  124. {
  125. clean = true;
  126. targetName = "ALL_BUILD";
  127. }
  128. if (targetName && strlen(targetName))
  129. {
  130. targetArg += targetName;
  131. }
  132. else
  133. {
  134. targetArg += "ALL_BUILD";
  135. }
  136. targetArg += " - ";
  137. if(config && strlen(config))
  138. {
  139. targetArg += config;
  140. }
  141. else
  142. {
  143. targetArg += "Debug";
  144. }
  145. makeCommand.push_back(targetArg);
  146. if(clean)
  147. {
  148. makeCommand.push_back("/CLEAN");
  149. }
  150. else
  151. {
  152. makeCommand.push_back("/BUILD");
  153. }
  154. makeCommand.insert(makeCommand.end(),
  155. makeOptions.begin(), makeOptions.end());
  156. }
  157. ///! Create a local generator appropriate to this Global Generator
  158. cmLocalGenerator *cmGlobalVisualStudio6Generator::CreateLocalGenerator()
  159. {
  160. cmLocalGenerator *lg = new cmLocalVisualStudio6Generator;
  161. lg->SetGlobalGenerator(this);
  162. return lg;
  163. }
  164. void cmGlobalVisualStudio6Generator::Generate()
  165. {
  166. // first do the superclass method
  167. this->cmGlobalVisualStudioGenerator::Generate();
  168. // Now write out the DSW
  169. this->OutputDSWFile();
  170. }
  171. // Write a DSW file to the stream
  172. void cmGlobalVisualStudio6Generator
  173. ::WriteDSWFile(std::ostream& fout,cmLocalGenerator* root,
  174. std::vector<cmLocalGenerator*>& generators)
  175. {
  176. // Write out the header for a DSW file
  177. this->WriteDSWHeader(fout);
  178. // Collect all targets under this root generator and the transitive
  179. // closure of their dependencies.
  180. TargetDependSet projectTargets;
  181. TargetDependSet originalTargets;
  182. this->GetTargetSets(projectTargets, originalTargets, root, generators);
  183. OrderedTargetDependSet orderedProjectTargets(projectTargets);
  184. for(OrderedTargetDependSet::const_iterator
  185. tt = orderedProjectTargets.begin();
  186. tt != orderedProjectTargets.end(); ++tt)
  187. {
  188. cmTarget const* target = *tt;
  189. if(target->GetType() == cmTarget::INTERFACE_LIBRARY)
  190. {
  191. continue;
  192. }
  193. // Write the project into the DSW file
  194. const char* expath = target->GetProperty("EXTERNAL_MSPROJECT");
  195. if(expath)
  196. {
  197. std::string project = target->GetName();
  198. std::string location = expath;
  199. this->WriteExternalProject(fout, project.c_str(),
  200. location.c_str(), target->GetUtilities());
  201. }
  202. else
  203. {
  204. std::string dspname = GetVS6TargetName(target->GetName());
  205. std::string dir = target->GetMakefile()->GetStartOutputDirectory();
  206. dir = root->Convert(dir.c_str(), cmLocalGenerator::START_OUTPUT);
  207. this->WriteProject(fout, dspname.c_str(), dir.c_str(), *target);
  208. }
  209. }
  210. // Write the footer for the DSW file
  211. this->WriteDSWFooter(fout);
  212. }
  213. void cmGlobalVisualStudio6Generator
  214. ::OutputDSWFile(cmLocalGenerator* root,
  215. std::vector<cmLocalGenerator*>& generators)
  216. {
  217. if(generators.size() == 0)
  218. {
  219. return;
  220. }
  221. std::string fname = root->GetMakefile()->GetStartOutputDirectory();
  222. fname += "/";
  223. fname += root->GetMakefile()->GetProjectName();
  224. fname += ".dsw";
  225. std::ofstream fout(fname.c_str());
  226. if(!fout)
  227. {
  228. cmSystemTools::Error("Error can not open DSW file for write: ",
  229. fname.c_str());
  230. cmSystemTools::ReportLastSystemError("");
  231. return;
  232. }
  233. this->WriteDSWFile(fout, root, generators);
  234. }
  235. // output the DSW file
  236. void cmGlobalVisualStudio6Generator::OutputDSWFile()
  237. {
  238. std::map<cmStdString, std::vector<cmLocalGenerator*> >::iterator it;
  239. for(it = this->ProjectMap.begin(); it!= this->ProjectMap.end(); ++it)
  240. {
  241. this->OutputDSWFile(it->second[0], it->second);
  242. }
  243. }
  244. // Write a dsp file into the DSW file,
  245. // Note, that dependencies from executables to
  246. // the libraries it uses are also done here
  247. void cmGlobalVisualStudio6Generator::WriteProject(std::ostream& fout,
  248. const char* dspname,
  249. const char* dir,
  250. cmTarget const& target)
  251. {
  252. fout << "#########################################################"
  253. "######################\n\n";
  254. fout << "Project: \"" << dspname << "\"="
  255. << dir << "\\" << dspname << ".dsp - Package Owner=<4>\n\n";
  256. fout << "Package=<5>\n{{{\n}}}\n\n";
  257. fout << "Package=<4>\n";
  258. fout << "{{{\n";
  259. VSDependSet const& depends = this->VSTargetDepends[&target];
  260. for(VSDependSet::const_iterator di = depends.begin();
  261. di != depends.end(); ++di)
  262. {
  263. const char* name = di->c_str();
  264. fout << "Begin Project Dependency\n";
  265. fout << "Project_Dep_Name " << GetVS6TargetName(name) << "\n";
  266. fout << "End Project Dependency\n";
  267. }
  268. fout << "}}}\n\n";
  269. UtilityDependsMap::iterator ui = this->UtilityDepends.find(&target);
  270. if(ui != this->UtilityDepends.end())
  271. {
  272. const char* uname = ui->second.c_str();
  273. fout << "Project: \"" << uname << "\"="
  274. << dir << "\\" << uname << ".dsp - Package Owner=<4>\n\n";
  275. fout <<
  276. "Package=<5>\n{{{\n}}}\n\n"
  277. "Package=<4>\n"
  278. "{{{\n"
  279. "Begin Project Dependency\n"
  280. "Project_Dep_Name " << dspname << "\n"
  281. "End Project Dependency\n"
  282. "}}}\n\n";
  283. ;
  284. }
  285. }
  286. // Write a dsp file into the DSW file,
  287. // Note, that dependencies from executables to
  288. // the libraries it uses are also done here
  289. void cmGlobalVisualStudio6Generator::WriteExternalProject(std::ostream& fout,
  290. const char* name,
  291. const char* location,
  292. const std::set<cmStdString>& dependencies)
  293. {
  294. fout << "#########################################################"
  295. "######################\n\n";
  296. fout << "Project: \"" << name << "\"="
  297. << location << " - Package Owner=<4>\n\n";
  298. fout << "Package=<5>\n{{{\n}}}\n\n";
  299. fout << "Package=<4>\n";
  300. fout << "{{{\n";
  301. std::set<cmStdString>::const_iterator i, end;
  302. // write dependencies.
  303. i = dependencies.begin();
  304. end = dependencies.end();
  305. for(;i!= end; ++i)
  306. {
  307. fout << "Begin Project Dependency\n";
  308. fout << "Project_Dep_Name " << GetVS6TargetName(*i) << "\n";
  309. fout << "End Project Dependency\n";
  310. }
  311. fout << "}}}\n\n";
  312. }
  313. // Standard end of dsw file
  314. void cmGlobalVisualStudio6Generator::WriteDSWFooter(std::ostream& fout)
  315. {
  316. fout << "######################################################"
  317. "#########################\n\n";
  318. fout << "Global:\n\n";
  319. fout << "Package=<5>\n{{{\n}}}\n\n";
  320. fout << "Package=<3>\n{{{\n}}}\n\n";
  321. fout << "#####################################################"
  322. "##########################\n\n";
  323. }
  324. // ouput standard header for dsw file
  325. void cmGlobalVisualStudio6Generator::WriteDSWHeader(std::ostream& fout)
  326. {
  327. fout << "Microsoft Developer Studio Workspace File, Format Version 6.00\n";
  328. fout << "# WARNING: DO NOT EDIT OR DELETE THIS WORKSPACE FILE!\n\n";
  329. }
  330. //----------------------------------------------------------------------------
  331. std::string
  332. cmGlobalVisualStudio6Generator::WriteUtilityDepend(cmTarget const* target)
  333. {
  334. std::string pname = target->GetName();
  335. pname += "_UTILITY";
  336. pname = GetVS6TargetName(pname.c_str());
  337. std::string fname = target->GetMakefile()->GetStartOutputDirectory();
  338. fname += "/";
  339. fname += pname;
  340. fname += ".dsp";
  341. cmGeneratedFileStream fout(fname.c_str());
  342. fout.SetCopyIfDifferent(true);
  343. fout <<
  344. "# Microsoft Developer Studio Project File - Name=\""
  345. << pname << "\" - Package Owner=<4>\n"
  346. "# Microsoft Developer Studio Generated Build File, Format Version 6.00\n"
  347. "# ** DO NOT EDIT **\n"
  348. "\n"
  349. "# TARGTYPE \"Win32 (x86) Generic Project\" 0x010a\n"
  350. "\n"
  351. "CFG=" << pname << " - Win32 Debug\n"
  352. "!MESSAGE \"" << pname << " - Win32 Debug\""
  353. " (based on \"Win32 (x86) Generic Project\")\n"
  354. "!MESSAGE \"" << pname << " - Win32 Release\" "
  355. "(based on \"Win32 (x86) Generic Project\")\n"
  356. "!MESSAGE \"" << pname << " - Win32 MinSizeRel\" "
  357. "(based on \"Win32 (x86) Generic Project\")\n"
  358. "!MESSAGE \"" << pname << " - Win32 RelWithDebInfo\" "
  359. "(based on \"Win32 (x86) Generic Project\")\n"
  360. "\n"
  361. "# Begin Project\n"
  362. "# Begin Target\n"
  363. "# Name \"" << pname << " - Win32 Debug\"\n"
  364. "# Name \"" << pname << " - Win32 Release\"\n"
  365. "# Name \"" << pname << " - Win32 MinSizeRel\"\n"
  366. "# Name \"" << pname << " - Win32 RelWithDebInfo\"\n"
  367. "# End Target\n"
  368. "# End Project\n"
  369. ;
  370. return pname;
  371. }
  372. //----------------------------------------------------------------------------
  373. void cmGlobalVisualStudio6Generator
  374. ::GetDocumentation(cmDocumentationEntry& entry)
  375. {
  376. entry.Name = cmGlobalVisualStudio6Generator::GetActualName();
  377. entry.Brief = "Generates Visual Studio 6 project files.";
  378. }
  379. //----------------------------------------------------------------------------
  380. void
  381. cmGlobalVisualStudio6Generator
  382. ::AppendDirectoryForConfig(const char* prefix,
  383. const char* config,
  384. const char* suffix,
  385. std::string& dir)
  386. {
  387. if(config)
  388. {
  389. dir += prefix;
  390. dir += config;
  391. dir += suffix;
  392. }
  393. }