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()
  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 *
  160. cmGlobalVisualStudio6Generator::CreateLocalGenerator(cmLocalGenerator* parent)
  161. {
  162. cmLocalGenerator *lg = new cmLocalVisualStudio6Generator(parent);
  163. lg->SetGlobalGenerator(this);
  164. return lg;
  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;
  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.size() == 0)
  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. }