cmGlobalVisualStudio6Generator.cxx 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  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->ReadDependentFile(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. if (!this->CMakeInstance->GetIsInTryCompile())
  172. {
  173. const char* cmakeWarnVS6 =
  174. this->CMakeInstance->GetState()->GetCacheEntryValue("CMAKE_WARN_VS6");
  175. if (!cmakeWarnVS6 || !cmSystemTools::IsOff(cmakeWarnVS6))
  176. {
  177. this->CMakeInstance->IssueMessage(
  178. cmake::WARNING,
  179. "The \"Visual Studio 6\" generator is deprecated "
  180. "and will be removed in a future version of CMake."
  181. "\n"
  182. "Add CMAKE_WARN_VS6=OFF to the cache to disable this warning."
  183. );
  184. }
  185. }
  186. }
  187. // Write a DSW file to the stream
  188. void cmGlobalVisualStudio6Generator
  189. ::WriteDSWFile(std::ostream& fout,cmLocalGenerator* root,
  190. std::vector<cmLocalGenerator*>& generators)
  191. {
  192. // Write out the header for a DSW file
  193. this->WriteDSWHeader(fout);
  194. // Collect all targets under this root generator and the transitive
  195. // closure of their dependencies.
  196. TargetDependSet projectTargets;
  197. TargetDependSet originalTargets;
  198. this->GetTargetSets(projectTargets, originalTargets, root, generators);
  199. OrderedTargetDependSet orderedProjectTargets(projectTargets);
  200. for(OrderedTargetDependSet::const_iterator
  201. tt = orderedProjectTargets.begin();
  202. tt != orderedProjectTargets.end(); ++tt)
  203. {
  204. cmTarget const* target = *tt;
  205. if(target->GetType() == cmTarget::INTERFACE_LIBRARY)
  206. {
  207. continue;
  208. }
  209. // Write the project into the DSW file
  210. const char* expath = target->GetProperty("EXTERNAL_MSPROJECT");
  211. if(expath)
  212. {
  213. std::string project = target->GetName();
  214. std::string location = expath;
  215. this->WriteExternalProject(fout, project.c_str(),
  216. location.c_str(), target->GetUtilities());
  217. }
  218. else
  219. {
  220. std::string dspname = GetVS6TargetName(target->GetName());
  221. std::string dir = target->GetMakefile()->GetCurrentBinaryDirectory();
  222. dir = root->Convert(dir.c_str(), cmLocalGenerator::START_OUTPUT);
  223. this->WriteProject(fout, dspname.c_str(), dir.c_str(), *target);
  224. }
  225. }
  226. // Write the footer for the DSW file
  227. this->WriteDSWFooter(fout);
  228. }
  229. void cmGlobalVisualStudio6Generator
  230. ::OutputDSWFile(cmLocalGenerator* root,
  231. std::vector<cmLocalGenerator*>& generators)
  232. {
  233. if(generators.size() == 0)
  234. {
  235. return;
  236. }
  237. std::string fname = root->GetMakefile()->GetCurrentBinaryDirectory();
  238. fname += "/";
  239. fname += root->GetMakefile()->GetProjectName();
  240. fname += ".dsw";
  241. cmsys::ofstream fout(fname.c_str());
  242. if(!fout)
  243. {
  244. cmSystemTools::Error("Error can not open DSW file for write: ",
  245. fname.c_str());
  246. cmSystemTools::ReportLastSystemError("");
  247. return;
  248. }
  249. this->WriteDSWFile(fout, root, generators);
  250. }
  251. // output the DSW file
  252. void cmGlobalVisualStudio6Generator::OutputDSWFile()
  253. {
  254. std::map<std::string, std::vector<cmLocalGenerator*> >::iterator it;
  255. for(it = this->ProjectMap.begin(); it!= this->ProjectMap.end(); ++it)
  256. {
  257. this->OutputDSWFile(it->second[0], it->second);
  258. }
  259. }
  260. // Write a dsp file into the DSW file,
  261. // Note, that dependencies from executables to
  262. // the libraries it uses are also done here
  263. void cmGlobalVisualStudio6Generator::WriteProject(std::ostream& fout,
  264. const std::string& dspname,
  265. const char* dir,
  266. cmTarget const& target)
  267. {
  268. fout << "#########################################################"
  269. "######################\n\n";
  270. fout << "Project: \"" << dspname << "\"="
  271. << dir << "\\" << dspname << ".dsp - Package Owner=<4>\n\n";
  272. fout << "Package=<5>\n{{{\n}}}\n\n";
  273. fout << "Package=<4>\n";
  274. fout << "{{{\n";
  275. VSDependSet const& depends = this->VSTargetDepends[&target];
  276. for(VSDependSet::const_iterator di = depends.begin();
  277. di != depends.end(); ++di)
  278. {
  279. const char* name = di->c_str();
  280. fout << "Begin Project Dependency\n";
  281. fout << "Project_Dep_Name " << GetVS6TargetName(name) << "\n";
  282. fout << "End Project Dependency\n";
  283. }
  284. fout << "}}}\n\n";
  285. UtilityDependsMap::iterator ui = this->UtilityDepends.find(&target);
  286. if(ui != this->UtilityDepends.end())
  287. {
  288. const char* uname = ui->second.c_str();
  289. fout << "Project: \"" << uname << "\"="
  290. << dir << "\\" << uname << ".dsp - Package Owner=<4>\n\n";
  291. fout <<
  292. "Package=<5>\n{{{\n}}}\n\n"
  293. "Package=<4>\n"
  294. "{{{\n"
  295. "Begin Project Dependency\n"
  296. "Project_Dep_Name " << dspname << "\n"
  297. "End Project Dependency\n"
  298. "}}}\n\n";
  299. ;
  300. }
  301. }
  302. // Write a dsp file into the DSW file,
  303. // Note, that dependencies from executables to
  304. // the libraries it uses are also done here
  305. void cmGlobalVisualStudio6Generator::WriteExternalProject(std::ostream& fout,
  306. const std::string& name,
  307. const char* location,
  308. const std::set<std::string>& dependencies)
  309. {
  310. fout << "#########################################################"
  311. "######################\n\n";
  312. fout << "Project: \"" << name << "\"="
  313. << location << " - Package Owner=<4>\n\n";
  314. fout << "Package=<5>\n{{{\n}}}\n\n";
  315. fout << "Package=<4>\n";
  316. fout << "{{{\n";
  317. std::set<std::string>::const_iterator i, end;
  318. // write dependencies.
  319. i = dependencies.begin();
  320. end = dependencies.end();
  321. for(;i!= end; ++i)
  322. {
  323. fout << "Begin Project Dependency\n";
  324. fout << "Project_Dep_Name " << GetVS6TargetName(*i) << "\n";
  325. fout << "End Project Dependency\n";
  326. }
  327. fout << "}}}\n\n";
  328. }
  329. // Standard end of dsw file
  330. void cmGlobalVisualStudio6Generator::WriteDSWFooter(std::ostream& fout)
  331. {
  332. fout << "######################################################"
  333. "#########################\n\n";
  334. fout << "Global:\n\n";
  335. fout << "Package=<5>\n{{{\n}}}\n\n";
  336. fout << "Package=<3>\n{{{\n}}}\n\n";
  337. fout << "#####################################################"
  338. "##########################\n\n";
  339. }
  340. // ouput standard header for dsw file
  341. void cmGlobalVisualStudio6Generator::WriteDSWHeader(std::ostream& fout)
  342. {
  343. fout << "Microsoft Developer Studio Workspace File, Format Version 6.00\n";
  344. fout << "# WARNING: DO NOT EDIT OR DELETE THIS WORKSPACE FILE!\n\n";
  345. }
  346. //----------------------------------------------------------------------------
  347. std::string
  348. cmGlobalVisualStudio6Generator::WriteUtilityDepend(cmTarget const* target)
  349. {
  350. std::string pname = target->GetName();
  351. pname += "_UTILITY";
  352. pname = GetVS6TargetName(pname.c_str());
  353. std::string fname = target->GetMakefile()->GetCurrentBinaryDirectory();
  354. fname += "/";
  355. fname += pname;
  356. fname += ".dsp";
  357. cmGeneratedFileStream fout(fname.c_str());
  358. fout.SetCopyIfDifferent(true);
  359. fout <<
  360. "# Microsoft Developer Studio Project File - Name=\""
  361. << pname << "\" - Package Owner=<4>\n"
  362. "# Microsoft Developer Studio Generated Build File, Format Version 6.00\n"
  363. "# ** DO NOT EDIT **\n"
  364. "\n"
  365. "# TARGTYPE \"Win32 (x86) Generic Project\" 0x010a\n"
  366. "\n"
  367. "CFG=" << pname << " - Win32 Debug\n"
  368. "!MESSAGE \"" << pname << " - Win32 Debug\""
  369. " (based on \"Win32 (x86) Generic Project\")\n"
  370. "!MESSAGE \"" << pname << " - Win32 Release\" "
  371. "(based on \"Win32 (x86) Generic Project\")\n"
  372. "!MESSAGE \"" << pname << " - Win32 MinSizeRel\" "
  373. "(based on \"Win32 (x86) Generic Project\")\n"
  374. "!MESSAGE \"" << pname << " - Win32 RelWithDebInfo\" "
  375. "(based on \"Win32 (x86) Generic Project\")\n"
  376. "\n"
  377. "# Begin Project\n"
  378. "# Begin Target\n"
  379. "# Name \"" << pname << " - Win32 Debug\"\n"
  380. "# Name \"" << pname << " - Win32 Release\"\n"
  381. "# Name \"" << pname << " - Win32 MinSizeRel\"\n"
  382. "# Name \"" << pname << " - Win32 RelWithDebInfo\"\n"
  383. "# End Target\n"
  384. "# End Project\n"
  385. ;
  386. return pname;
  387. }
  388. //----------------------------------------------------------------------------
  389. void cmGlobalVisualStudio6Generator
  390. ::GetDocumentation(cmDocumentationEntry& entry)
  391. {
  392. entry.Name = cmGlobalVisualStudio6Generator::GetActualName();
  393. entry.Brief = "Deprecated. Generates Visual Studio 6 project files.";
  394. }
  395. //----------------------------------------------------------------------------
  396. void
  397. cmGlobalVisualStudio6Generator
  398. ::AppendDirectoryForConfig(const std::string& prefix,
  399. const std::string& config,
  400. const std::string& suffix,
  401. std::string& dir)
  402. {
  403. if(!config.empty())
  404. {
  405. dir += prefix;
  406. dir += config;
  407. dir += suffix;
  408. }
  409. }