cmGlobalVisualStudio6Generator.cxx 14 KB

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