cmGlobalVisualStudio6Generator.cxx 14 KB

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