cmGlobalVisualStudio6Generator.cxx 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  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. // Utility function to make a valid VS6 *.dsp filename out
  16. // of a CMake target name:
  17. //
  18. std::string GetVS6TargetName(const std::string& targetName)
  19. {
  20. std::string name(targetName);
  21. // Eliminate hyphens. VS6 cannot handle hyphens in *.dsp filenames...
  22. // Replace them with underscores.
  23. //
  24. cmSystemTools::ReplaceString(name, "-", "_");
  25. return name;
  26. }
  27. cmGlobalVisualStudio6Generator::cmGlobalVisualStudio6Generator()
  28. {
  29. this->FindMakeProgramFile = "CMakeVS6FindMake.cmake";
  30. }
  31. void cmGlobalVisualStudio6Generator
  32. ::EnableLanguage(std::vector<std::string>const& lang,
  33. cmMakefile *mf,
  34. bool optional)
  35. {
  36. mf->AddDefinition("CMAKE_GENERATOR_CC", "cl");
  37. mf->AddDefinition("CMAKE_GENERATOR_CXX", "cl");
  38. mf->AddDefinition("CMAKE_GENERATOR_RC", "rc");
  39. mf->AddDefinition("CMAKE_GENERATOR_NO_COMPILER_ENV", "1");
  40. mf->AddDefinition("CMAKE_GENERATOR_Fortran", "ifort");
  41. mf->AddDefinition("MSVC_C_ARCHITECTURE_ID", "X86");
  42. mf->AddDefinition("MSVC_CXX_ARCHITECTURE_ID", "X86");
  43. mf->AddDefinition("MSVC60", "1");
  44. this->GenerateConfigurations(mf);
  45. this->cmGlobalGenerator::EnableLanguage(lang, mf, optional);
  46. }
  47. void cmGlobalVisualStudio6Generator::GenerateConfigurations(cmMakefile* mf)
  48. {
  49. std::string fname= mf->GetRequiredDefinition("CMAKE_ROOT");
  50. const char* def= mf->GetDefinition( "MSPROJECT_TEMPLATE_DIRECTORY");
  51. if(def)
  52. {
  53. fname = def;
  54. }
  55. else
  56. {
  57. fname += "/Templates";
  58. }
  59. fname += "/CMakeVisualStudio6Configurations.cmake";
  60. if(!mf->ReadListFile(mf->GetCurrentListFile(), fname.c_str()))
  61. {
  62. cmSystemTools::Error("Cannot open ", fname.c_str(),
  63. ". Please copy this file from the main "
  64. "CMake/Templates directory and edit it for "
  65. "your build configurations.");
  66. }
  67. else if(!mf->GetDefinition("CMAKE_CONFIGURATION_TYPES"))
  68. {
  69. cmSystemTools::Error("CMAKE_CONFIGURATION_TYPES not set by ",
  70. fname.c_str(),
  71. ". Please copy this file from the main "
  72. "CMake/Templates directory and edit it for "
  73. "your build configurations.");
  74. }
  75. }
  76. std::string cmGlobalVisualStudio6Generator
  77. ::GenerateBuildCommand(const char* makeProgram,
  78. const char *projectName,
  79. const char* additionalOptions,
  80. const char *targetName,
  81. const char* config,
  82. bool ignoreErrors,
  83. bool)
  84. {
  85. // Ingoring errors is not implemented in visual studio 6
  86. (void) ignoreErrors;
  87. // now build the test
  88. std::vector<std::string> mp;
  89. mp.push_back("[HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\VisualStudio"
  90. "\\6.0\\Setup;VsCommonDir]/MSDev98/Bin");
  91. cmSystemTools::ExpandRegistryValues(mp[0]);
  92. std::string originalCommand = makeProgram;
  93. std::string makeCommand =
  94. cmSystemTools::FindProgram(makeProgram, mp);
  95. if(makeCommand.size() == 0)
  96. {
  97. std::string e = "Generator cannot find Visual Studio 6 msdev program \"";
  98. e += originalCommand;
  99. e += "\" specified by CMAKE_MAKE_PROGRAM cache entry. ";
  100. e += "Please fix the setting.";
  101. cmSystemTools::Error(e.c_str());
  102. return "";
  103. }
  104. makeCommand = cmSystemTools::ConvertToOutputPath(makeCommand.c_str());
  105. // if there are spaces in the makeCommand, assume a full path
  106. // and convert it to a path with no spaces in it as the
  107. // RunSingleCommand does not like spaces
  108. #if defined(_WIN32) && !defined(__CYGWIN__)
  109. if(makeCommand.find(' ') != std::string::npos)
  110. {
  111. cmSystemTools::GetShortPath(makeCommand.c_str(), makeCommand);
  112. }
  113. #endif
  114. makeCommand += " ";
  115. makeCommand += projectName;
  116. makeCommand += ".dsw /MAKE \"";
  117. bool clean = false;
  118. if ( targetName && strcmp(targetName, "clean") == 0 )
  119. {
  120. clean = true;
  121. targetName = "ALL_BUILD";
  122. }
  123. if (targetName && strlen(targetName))
  124. {
  125. makeCommand += targetName;
  126. }
  127. else
  128. {
  129. makeCommand += "ALL_BUILD";
  130. }
  131. makeCommand += " - ";
  132. if(config && strlen(config))
  133. {
  134. makeCommand += config;
  135. }
  136. else
  137. {
  138. makeCommand += "Debug";
  139. }
  140. if(clean)
  141. {
  142. makeCommand += "\" /CLEAN";
  143. }
  144. else
  145. {
  146. makeCommand += "\" /BUILD";
  147. }
  148. if ( additionalOptions )
  149. {
  150. makeCommand += " ";
  151. makeCommand += additionalOptions;
  152. }
  153. return makeCommand;
  154. }
  155. ///! Create a local generator appropriate to this Global Generator
  156. cmLocalGenerator *cmGlobalVisualStudio6Generator::CreateLocalGenerator()
  157. {
  158. cmLocalGenerator *lg = new cmLocalVisualStudio6Generator;
  159. lg->SetGlobalGenerator(this);
  160. return lg;
  161. }
  162. void cmGlobalVisualStudio6Generator::Generate()
  163. {
  164. // first do the superclass method
  165. this->cmGlobalVisualStudioGenerator::Generate();
  166. // Now write out the DSW
  167. this->OutputDSWFile();
  168. }
  169. // Write a DSW file to the stream
  170. void cmGlobalVisualStudio6Generator
  171. ::WriteDSWFile(std::ostream& fout,cmLocalGenerator* root,
  172. std::vector<cmLocalGenerator*>& generators)
  173. {
  174. // Write out the header for a DSW file
  175. this->WriteDSWHeader(fout);
  176. // Collect all targets under this root generator and the transitive
  177. // closure of their dependencies.
  178. TargetDependSet projectTargets;
  179. TargetDependSet originalTargets;
  180. this->GetTargetSets(projectTargets, originalTargets, root, generators);
  181. OrderedTargetDependSet orderedProjectTargets(projectTargets);
  182. std::string rootdir = root->GetMakefile()->GetStartOutputDirectory();
  183. rootdir += "/";
  184. for(OrderedTargetDependSet::const_iterator
  185. tt = orderedProjectTargets.begin();
  186. tt != orderedProjectTargets.end(); ++tt)
  187. {
  188. cmTarget* target = *tt;
  189. cmMakefile* mf = target->GetMakefile();
  190. // Write the project into the DSW file
  191. const char* expath = target->GetProperty("EXTERNAL_MSPROJECT");
  192. if(expath)
  193. {
  194. std::string project = target->GetName();
  195. std::string location = expath;
  196. this->WriteExternalProject(fout, project.c_str(),
  197. location.c_str(), target->GetUtilities());
  198. }
  199. else
  200. {
  201. std::string dspname = GetVS6TargetName(target->GetName());
  202. std::string dir = target->GetMakefile()->GetStartOutputDirectory();
  203. dir = root->Convert(dir.c_str(), cmLocalGenerator::START_OUTPUT);
  204. this->WriteProject(fout, dspname.c_str(), dir.c_str(), *target);
  205. }
  206. }
  207. // Write the footer for the DSW file
  208. this->WriteDSWFooter(fout);
  209. }
  210. void cmGlobalVisualStudio6Generator
  211. ::OutputDSWFile(cmLocalGenerator* root,
  212. std::vector<cmLocalGenerator*>& generators)
  213. {
  214. if(generators.size() == 0)
  215. {
  216. return;
  217. }
  218. std::string fname = root->GetMakefile()->GetStartOutputDirectory();
  219. fname += "/";
  220. fname += root->GetMakefile()->GetProjectName();
  221. fname += ".dsw";
  222. std::ofstream fout(fname.c_str());
  223. if(!fout)
  224. {
  225. cmSystemTools::Error("Error can not open DSW file for write: ",
  226. fname.c_str());
  227. cmSystemTools::ReportLastSystemError("");
  228. return;
  229. }
  230. this->WriteDSWFile(fout, root, generators);
  231. }
  232. // output the DSW file
  233. void cmGlobalVisualStudio6Generator::OutputDSWFile()
  234. {
  235. std::map<cmStdString, std::vector<cmLocalGenerator*> >::iterator it;
  236. for(it = this->ProjectMap.begin(); it!= this->ProjectMap.end(); ++it)
  237. {
  238. this->OutputDSWFile(it->second[0], it->second);
  239. }
  240. }
  241. // Write a dsp file into the DSW file,
  242. // Note, that dependencies from executables to
  243. // the libraries it uses are also done here
  244. void cmGlobalVisualStudio6Generator::WriteProject(std::ostream& fout,
  245. const char* dspname,
  246. const char* dir,
  247. cmTarget& target)
  248. {
  249. fout << "#########################################################"
  250. "######################\n\n";
  251. fout << "Project: \"" << dspname << "\"="
  252. << dir << "\\" << dspname << ".dsp - Package Owner=<4>\n\n";
  253. fout << "Package=<5>\n{{{\n}}}\n\n";
  254. fout << "Package=<4>\n";
  255. fout << "{{{\n";
  256. VSDependSet const& depends = this->VSTargetDepends[&target];
  257. for(VSDependSet::const_iterator di = depends.begin();
  258. di != depends.end(); ++di)
  259. {
  260. const char* name = di->c_str();
  261. fout << "Begin Project Dependency\n";
  262. fout << "Project_Dep_Name " << GetVS6TargetName(name) << "\n";
  263. fout << "End Project Dependency\n";
  264. }
  265. fout << "}}}\n\n";
  266. UtilityDependsMap::iterator ui = this->UtilityDepends.find(&target);
  267. if(ui != this->UtilityDepends.end())
  268. {
  269. const char* uname = ui->second.c_str();
  270. fout << "Project: \"" << uname << "\"="
  271. << dir << "\\" << uname << ".dsp - Package Owner=<4>\n\n";
  272. fout <<
  273. "Package=<5>\n{{{\n}}}\n\n"
  274. "Package=<4>\n"
  275. "{{{\n"
  276. "Begin Project Dependency\n"
  277. "Project_Dep_Name " << dspname << "\n"
  278. "End Project Dependency\n"
  279. "}}}\n\n";
  280. ;
  281. }
  282. }
  283. // Write a dsp file into the DSW file,
  284. // Note, that dependencies from executables to
  285. // the libraries it uses are also done here
  286. void cmGlobalVisualStudio6Generator::WriteExternalProject(std::ostream& fout,
  287. const char* name,
  288. const char* location,
  289. const std::set<cmStdString>& dependencies)
  290. {
  291. fout << "#########################################################"
  292. "######################\n\n";
  293. fout << "Project: \"" << name << "\"="
  294. << location << " - Package Owner=<4>\n\n";
  295. fout << "Package=<5>\n{{{\n}}}\n\n";
  296. fout << "Package=<4>\n";
  297. fout << "{{{\n";
  298. std::set<cmStdString>::const_iterator i, end;
  299. // write dependencies.
  300. i = dependencies.begin();
  301. end = dependencies.end();
  302. for(;i!= end; ++i)
  303. {
  304. fout << "Begin Project Dependency\n";
  305. fout << "Project_Dep_Name " << GetVS6TargetName(*i) << "\n";
  306. fout << "End Project Dependency\n";
  307. }
  308. fout << "}}}\n\n";
  309. }
  310. // Standard end of dsw file
  311. void cmGlobalVisualStudio6Generator::WriteDSWFooter(std::ostream& fout)
  312. {
  313. fout << "######################################################"
  314. "#########################\n\n";
  315. fout << "Global:\n\n";
  316. fout << "Package=<5>\n{{{\n}}}\n\n";
  317. fout << "Package=<3>\n{{{\n}}}\n\n";
  318. fout << "#####################################################"
  319. "##########################\n\n";
  320. }
  321. // ouput standard header for dsw file
  322. void cmGlobalVisualStudio6Generator::WriteDSWHeader(std::ostream& fout)
  323. {
  324. fout << "Microsoft Developer Studio Workspace File, Format Version 6.00\n";
  325. fout << "# WARNING: DO NOT EDIT OR DELETE THIS WORKSPACE FILE!\n\n";
  326. }
  327. //----------------------------------------------------------------------------
  328. std::string
  329. cmGlobalVisualStudio6Generator::WriteUtilityDepend(cmTarget* target)
  330. {
  331. std::string pname = target->GetName();
  332. pname += "_UTILITY";
  333. pname = GetVS6TargetName(pname.c_str());
  334. std::string fname = target->GetMakefile()->GetStartOutputDirectory();
  335. fname += "/";
  336. fname += pname;
  337. fname += ".dsp";
  338. cmGeneratedFileStream fout(fname.c_str());
  339. fout.SetCopyIfDifferent(true);
  340. fout <<
  341. "# Microsoft Developer Studio Project File - Name=\""
  342. << pname << "\" - Package Owner=<4>\n"
  343. "# Microsoft Developer Studio Generated Build File, Format Version 6.00\n"
  344. "# ** DO NOT EDIT **\n"
  345. "\n"
  346. "# TARGTYPE \"Win32 (x86) Generic Project\" 0x010a\n"
  347. "\n"
  348. "CFG=" << pname << " - Win32 Debug\n"
  349. "!MESSAGE \"" << pname << " - Win32 Debug\""
  350. " (based on \"Win32 (x86) Generic Project\")\n"
  351. "!MESSAGE \"" << pname << " - Win32 Release\" "
  352. "(based on \"Win32 (x86) Generic Project\")\n"
  353. "!MESSAGE \"" << pname << " - Win32 MinSizeRel\" "
  354. "(based on \"Win32 (x86) Generic Project\")\n"
  355. "!MESSAGE \"" << pname << " - Win32 RelWithDebInfo\" "
  356. "(based on \"Win32 (x86) Generic Project\")\n"
  357. "\n"
  358. "# Begin Project\n"
  359. "# Begin Target\n"
  360. "# Name \"" << pname << " - Win32 Debug\"\n"
  361. "# Name \"" << pname << " - Win32 Release\"\n"
  362. "# Name \"" << pname << " - Win32 MinSizeRel\"\n"
  363. "# Name \"" << pname << " - Win32 RelWithDebInfo\"\n"
  364. "# End Target\n"
  365. "# End Project\n"
  366. ;
  367. return pname;
  368. }
  369. //----------------------------------------------------------------------------
  370. void cmGlobalVisualStudio6Generator
  371. ::GetDocumentation(cmDocumentationEntry& entry) const
  372. {
  373. entry.Name = this->GetName();
  374. entry.Brief = "Generates Visual Studio 6 project files.";
  375. entry.Full = "";
  376. }
  377. //----------------------------------------------------------------------------
  378. void
  379. cmGlobalVisualStudio6Generator
  380. ::AppendDirectoryForConfig(const char* prefix,
  381. const char* config,
  382. const char* suffix,
  383. std::string& dir)
  384. {
  385. if(config)
  386. {
  387. dir += prefix;
  388. dir += config;
  389. dir += suffix;
  390. }
  391. }