cmGlobalVisualStudio6Generator.cxx 13 KB

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