cmGlobalVisualStudio6Generator.cxx 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  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. #include <cmsys/FStream.hxx>
  16. // Utility function to make a valid VS6 *.dsp filename out
  17. // of a CMake target name:
  18. //
  19. std::string GetVS6TargetName(const std::string& targetName)
  20. {
  21. std::string name(targetName);
  22. // Eliminate hyphens. VS6 cannot handle hyphens in *.dsp filenames...
  23. // Replace them with underscores.
  24. //
  25. cmSystemTools::ReplaceString(name, "-", "_");
  26. return name;
  27. }
  28. cmGlobalVisualStudio6Generator::cmGlobalVisualStudio6Generator()
  29. {
  30. this->MSDevCommandInitialized = false;
  31. }
  32. void cmGlobalVisualStudio6Generator
  33. ::EnableLanguage(std::vector<std::string>const& lang,
  34. cmMakefile *mf,
  35. bool optional)
  36. {
  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 std::string& makeProgram,
  107. const std::string& projectName,
  108. const std::string& /*projectDir*/,
  109. const std::string& targetName,
  110. const std::string& config,
  111. bool /*fast*/, bool /*verbose*/,
  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. std::string realTarget = targetName;
  124. if ( realTarget == "clean" )
  125. {
  126. clean = true;
  127. realTarget = "ALL_BUILD";
  128. }
  129. if (!realTarget.empty())
  130. {
  131. targetArg += realTarget;
  132. }
  133. else
  134. {
  135. targetArg += "ALL_BUILD";
  136. }
  137. targetArg += " - ";
  138. if(!config.empty())
  139. {
  140. targetArg += config;
  141. }
  142. else
  143. {
  144. targetArg += "Debug";
  145. }
  146. makeCommand.push_back(targetArg);
  147. if(clean)
  148. {
  149. makeCommand.push_back("/CLEAN");
  150. }
  151. else
  152. {
  153. makeCommand.push_back("/BUILD");
  154. }
  155. makeCommand.insert(makeCommand.end(),
  156. makeOptions.begin(), makeOptions.end());
  157. }
  158. ///! Create a local generator appropriate to this Global Generator
  159. cmLocalGenerator *cmGlobalVisualStudio6Generator::CreateLocalGenerator()
  160. {
  161. cmLocalGenerator *lg = new cmLocalVisualStudio6Generator;
  162. lg->SetGlobalGenerator(this);
  163. return lg;
  164. }
  165. void cmGlobalVisualStudio6Generator::Generate()
  166. {
  167. // first do the superclass method
  168. this->cmGlobalVisualStudioGenerator::Generate();
  169. // Now write out the DSW
  170. this->OutputDSWFile();
  171. }
  172. // Write a DSW file to the stream
  173. void cmGlobalVisualStudio6Generator
  174. ::WriteDSWFile(std::ostream& fout,cmLocalGenerator* root,
  175. std::vector<cmLocalGenerator*>& generators)
  176. {
  177. // Write out the header for a DSW file
  178. this->WriteDSWHeader(fout);
  179. // Collect all targets under this root generator and the transitive
  180. // closure of their dependencies.
  181. TargetDependSet projectTargets;
  182. TargetDependSet originalTargets;
  183. this->GetTargetSets(projectTargets, originalTargets, root, generators);
  184. OrderedTargetDependSet orderedProjectTargets(projectTargets);
  185. for(OrderedTargetDependSet::const_iterator
  186. tt = orderedProjectTargets.begin();
  187. tt != orderedProjectTargets.end(); ++tt)
  188. {
  189. cmTarget const* target = *tt;
  190. if(target->GetType() == cmTarget::INTERFACE_LIBRARY)
  191. {
  192. continue;
  193. }
  194. // Write the project into the DSW file
  195. const char* expath = target->GetProperty("EXTERNAL_MSPROJECT");
  196. if(expath)
  197. {
  198. std::string project = target->GetName();
  199. std::string location = expath;
  200. this->WriteExternalProject(fout, project.c_str(),
  201. location.c_str(), target->GetUtilities());
  202. }
  203. else
  204. {
  205. std::string dspname = GetVS6TargetName(target->GetName());
  206. std::string dir = target->GetMakefile()->GetStartOutputDirectory();
  207. dir = root->Convert(dir.c_str(), cmLocalGenerator::START_OUTPUT);
  208. this->WriteProject(fout, dspname.c_str(), dir.c_str(), *target);
  209. }
  210. }
  211. // Write the footer for the DSW file
  212. this->WriteDSWFooter(fout);
  213. }
  214. void cmGlobalVisualStudio6Generator
  215. ::OutputDSWFile(cmLocalGenerator* root,
  216. std::vector<cmLocalGenerator*>& generators)
  217. {
  218. if(generators.size() == 0)
  219. {
  220. return;
  221. }
  222. std::string fname = root->GetMakefile()->GetStartOutputDirectory();
  223. fname += "/";
  224. fname += root->GetMakefile()->GetProjectName();
  225. fname += ".dsw";
  226. cmsys::ofstream fout(fname.c_str());
  227. if(!fout)
  228. {
  229. cmSystemTools::Error("Error can not open DSW file for write: ",
  230. fname.c_str());
  231. cmSystemTools::ReportLastSystemError("");
  232. return;
  233. }
  234. this->WriteDSWFile(fout, root, generators);
  235. }
  236. // output the DSW file
  237. void cmGlobalVisualStudio6Generator::OutputDSWFile()
  238. {
  239. std::map<std::string, std::vector<cmLocalGenerator*> >::iterator it;
  240. for(it = this->ProjectMap.begin(); it!= this->ProjectMap.end(); ++it)
  241. {
  242. this->OutputDSWFile(it->second[0], it->second);
  243. }
  244. }
  245. // Write a dsp file into the DSW file,
  246. // Note, that dependencies from executables to
  247. // the libraries it uses are also done here
  248. void cmGlobalVisualStudio6Generator::WriteProject(std::ostream& fout,
  249. const std::string& dspname,
  250. const char* dir,
  251. cmTarget const& target)
  252. {
  253. fout << "#########################################################"
  254. "######################\n\n";
  255. fout << "Project: \"" << dspname << "\"="
  256. << dir << "\\" << dspname << ".dsp - Package Owner=<4>\n\n";
  257. fout << "Package=<5>\n{{{\n}}}\n\n";
  258. fout << "Package=<4>\n";
  259. fout << "{{{\n";
  260. VSDependSet const& depends = this->VSTargetDepends[&target];
  261. for(VSDependSet::const_iterator di = depends.begin();
  262. di != depends.end(); ++di)
  263. {
  264. const char* name = di->c_str();
  265. fout << "Begin Project Dependency\n";
  266. fout << "Project_Dep_Name " << GetVS6TargetName(name) << "\n";
  267. fout << "End Project Dependency\n";
  268. }
  269. fout << "}}}\n\n";
  270. UtilityDependsMap::iterator ui = this->UtilityDepends.find(&target);
  271. if(ui != this->UtilityDepends.end())
  272. {
  273. const char* uname = ui->second.c_str();
  274. fout << "Project: \"" << uname << "\"="
  275. << dir << "\\" << uname << ".dsp - Package Owner=<4>\n\n";
  276. fout <<
  277. "Package=<5>\n{{{\n}}}\n\n"
  278. "Package=<4>\n"
  279. "{{{\n"
  280. "Begin Project Dependency\n"
  281. "Project_Dep_Name " << dspname << "\n"
  282. "End Project Dependency\n"
  283. "}}}\n\n";
  284. ;
  285. }
  286. }
  287. // Write a dsp file into the DSW file,
  288. // Note, that dependencies from executables to
  289. // the libraries it uses are also done here
  290. void cmGlobalVisualStudio6Generator::WriteExternalProject(std::ostream& fout,
  291. const std::string& name,
  292. const char* location,
  293. const std::set<std::string>& dependencies)
  294. {
  295. fout << "#########################################################"
  296. "######################\n\n";
  297. fout << "Project: \"" << name << "\"="
  298. << location << " - Package Owner=<4>\n\n";
  299. fout << "Package=<5>\n{{{\n}}}\n\n";
  300. fout << "Package=<4>\n";
  301. fout << "{{{\n";
  302. std::set<std::string>::const_iterator i, end;
  303. // write dependencies.
  304. i = dependencies.begin();
  305. end = dependencies.end();
  306. for(;i!= end; ++i)
  307. {
  308. fout << "Begin Project Dependency\n";
  309. fout << "Project_Dep_Name " << GetVS6TargetName(*i) << "\n";
  310. fout << "End Project Dependency\n";
  311. }
  312. fout << "}}}\n\n";
  313. }
  314. // Standard end of dsw file
  315. void cmGlobalVisualStudio6Generator::WriteDSWFooter(std::ostream& fout)
  316. {
  317. fout << "######################################################"
  318. "#########################\n\n";
  319. fout << "Global:\n\n";
  320. fout << "Package=<5>\n{{{\n}}}\n\n";
  321. fout << "Package=<3>\n{{{\n}}}\n\n";
  322. fout << "#####################################################"
  323. "##########################\n\n";
  324. }
  325. // ouput standard header for dsw file
  326. void cmGlobalVisualStudio6Generator::WriteDSWHeader(std::ostream& fout)
  327. {
  328. fout << "Microsoft Developer Studio Workspace File, Format Version 6.00\n";
  329. fout << "# WARNING: DO NOT EDIT OR DELETE THIS WORKSPACE FILE!\n\n";
  330. }
  331. //----------------------------------------------------------------------------
  332. std::string
  333. cmGlobalVisualStudio6Generator::WriteUtilityDepend(cmTarget const* target)
  334. {
  335. std::string pname = target->GetName();
  336. pname += "_UTILITY";
  337. pname = GetVS6TargetName(pname.c_str());
  338. std::string fname = target->GetMakefile()->GetStartOutputDirectory();
  339. fname += "/";
  340. fname += pname;
  341. fname += ".dsp";
  342. cmGeneratedFileStream fout(fname.c_str());
  343. fout.SetCopyIfDifferent(true);
  344. fout <<
  345. "# Microsoft Developer Studio Project File - Name=\""
  346. << pname << "\" - Package Owner=<4>\n"
  347. "# Microsoft Developer Studio Generated Build File, Format Version 6.00\n"
  348. "# ** DO NOT EDIT **\n"
  349. "\n"
  350. "# TARGTYPE \"Win32 (x86) Generic Project\" 0x010a\n"
  351. "\n"
  352. "CFG=" << pname << " - Win32 Debug\n"
  353. "!MESSAGE \"" << pname << " - Win32 Debug\""
  354. " (based on \"Win32 (x86) Generic Project\")\n"
  355. "!MESSAGE \"" << pname << " - Win32 Release\" "
  356. "(based on \"Win32 (x86) Generic Project\")\n"
  357. "!MESSAGE \"" << pname << " - Win32 MinSizeRel\" "
  358. "(based on \"Win32 (x86) Generic Project\")\n"
  359. "!MESSAGE \"" << pname << " - Win32 RelWithDebInfo\" "
  360. "(based on \"Win32 (x86) Generic Project\")\n"
  361. "\n"
  362. "# Begin Project\n"
  363. "# Begin Target\n"
  364. "# Name \"" << pname << " - Win32 Debug\"\n"
  365. "# Name \"" << pname << " - Win32 Release\"\n"
  366. "# Name \"" << pname << " - Win32 MinSizeRel\"\n"
  367. "# Name \"" << pname << " - Win32 RelWithDebInfo\"\n"
  368. "# End Target\n"
  369. "# End Project\n"
  370. ;
  371. return pname;
  372. }
  373. //----------------------------------------------------------------------------
  374. void cmGlobalVisualStudio6Generator
  375. ::GetDocumentation(cmDocumentationEntry& entry)
  376. {
  377. entry.Name = cmGlobalVisualStudio6Generator::GetActualName();
  378. entry.Brief = "Generates Visual Studio 6 project files.";
  379. }
  380. //----------------------------------------------------------------------------
  381. void
  382. cmGlobalVisualStudio6Generator
  383. ::AppendDirectoryForConfig(const std::string& prefix,
  384. const std::string& config,
  385. const std::string& suffix,
  386. std::string& dir)
  387. {
  388. if(!config.empty())
  389. {
  390. dir += prefix;
  391. dir += config;
  392. dir += suffix;
  393. }
  394. }