cmGlobalVisualStudio6Generator.cxx 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  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. for(OrderedTargetDependSet::const_iterator
  183. tt = orderedProjectTargets.begin();
  184. tt != orderedProjectTargets.end(); ++tt)
  185. {
  186. cmTarget* target = *tt;
  187. cmMakefile* mf = target->GetMakefile();
  188. // Write the project into the DSW file
  189. const char* expath = target->GetProperty("EXTERNAL_MSPROJECT");
  190. if(expath)
  191. {
  192. std::string project = target->GetName();
  193. std::string location = expath;
  194. this->WriteExternalProject(fout, project.c_str(),
  195. location.c_str(), target->GetUtilities());
  196. }
  197. else
  198. {
  199. std::string dspname = GetVS6TargetName(target->GetName());
  200. std::string dir = target->GetMakefile()->GetStartOutputDirectory();
  201. dir = root->Convert(dir.c_str(), cmLocalGenerator::START_OUTPUT);
  202. this->WriteProject(fout, dspname.c_str(), dir.c_str(), *target);
  203. }
  204. }
  205. // Write the footer for the DSW file
  206. this->WriteDSWFooter(fout);
  207. }
  208. void cmGlobalVisualStudio6Generator
  209. ::OutputDSWFile(cmLocalGenerator* root,
  210. std::vector<cmLocalGenerator*>& generators)
  211. {
  212. if(generators.size() == 0)
  213. {
  214. return;
  215. }
  216. std::string fname = root->GetMakefile()->GetStartOutputDirectory();
  217. fname += "/";
  218. fname += root->GetMakefile()->GetProjectName();
  219. fname += ".dsw";
  220. std::ofstream fout(fname.c_str());
  221. if(!fout)
  222. {
  223. cmSystemTools::Error("Error can not open DSW file for write: ",
  224. fname.c_str());
  225. cmSystemTools::ReportLastSystemError("");
  226. return;
  227. }
  228. this->WriteDSWFile(fout, root, generators);
  229. }
  230. // output the DSW file
  231. void cmGlobalVisualStudio6Generator::OutputDSWFile()
  232. {
  233. std::map<cmStdString, std::vector<cmLocalGenerator*> >::iterator it;
  234. for(it = this->ProjectMap.begin(); it!= this->ProjectMap.end(); ++it)
  235. {
  236. this->OutputDSWFile(it->second[0], it->second);
  237. }
  238. }
  239. // Write a dsp file into the DSW file,
  240. // Note, that dependencies from executables to
  241. // the libraries it uses are also done here
  242. void cmGlobalVisualStudio6Generator::WriteProject(std::ostream& fout,
  243. const char* dspname,
  244. const char* dir,
  245. cmTarget& target)
  246. {
  247. fout << "#########################################################"
  248. "######################\n\n";
  249. fout << "Project: \"" << dspname << "\"="
  250. << dir << "\\" << dspname << ".dsp - Package Owner=<4>\n\n";
  251. fout << "Package=<5>\n{{{\n}}}\n\n";
  252. fout << "Package=<4>\n";
  253. fout << "{{{\n";
  254. VSDependSet const& depends = this->VSTargetDepends[&target];
  255. for(VSDependSet::const_iterator di = depends.begin();
  256. di != depends.end(); ++di)
  257. {
  258. const char* name = di->c_str();
  259. fout << "Begin Project Dependency\n";
  260. fout << "Project_Dep_Name " << GetVS6TargetName(name) << "\n";
  261. fout << "End Project Dependency\n";
  262. }
  263. fout << "}}}\n\n";
  264. UtilityDependsMap::iterator ui = this->UtilityDepends.find(&target);
  265. if(ui != this->UtilityDepends.end())
  266. {
  267. const char* uname = ui->second.c_str();
  268. fout << "Project: \"" << uname << "\"="
  269. << dir << "\\" << uname << ".dsp - Package Owner=<4>\n\n";
  270. fout <<
  271. "Package=<5>\n{{{\n}}}\n\n"
  272. "Package=<4>\n"
  273. "{{{\n"
  274. "Begin Project Dependency\n"
  275. "Project_Dep_Name " << dspname << "\n"
  276. "End Project Dependency\n"
  277. "}}}\n\n";
  278. ;
  279. }
  280. }
  281. // Write a dsp file into the DSW file,
  282. // Note, that dependencies from executables to
  283. // the libraries it uses are also done here
  284. void cmGlobalVisualStudio6Generator::WriteExternalProject(std::ostream& fout,
  285. const char* name,
  286. const char* location,
  287. const std::set<cmStdString>& dependencies)
  288. {
  289. fout << "#########################################################"
  290. "######################\n\n";
  291. fout << "Project: \"" << name << "\"="
  292. << location << " - Package Owner=<4>\n\n";
  293. fout << "Package=<5>\n{{{\n}}}\n\n";
  294. fout << "Package=<4>\n";
  295. fout << "{{{\n";
  296. std::set<cmStdString>::const_iterator i, end;
  297. // write dependencies.
  298. i = dependencies.begin();
  299. end = dependencies.end();
  300. for(;i!= end; ++i)
  301. {
  302. fout << "Begin Project Dependency\n";
  303. fout << "Project_Dep_Name " << GetVS6TargetName(*i) << "\n";
  304. fout << "End Project Dependency\n";
  305. }
  306. fout << "}}}\n\n";
  307. }
  308. // Standard end of dsw file
  309. void cmGlobalVisualStudio6Generator::WriteDSWFooter(std::ostream& fout)
  310. {
  311. fout << "######################################################"
  312. "#########################\n\n";
  313. fout << "Global:\n\n";
  314. fout << "Package=<5>\n{{{\n}}}\n\n";
  315. fout << "Package=<3>\n{{{\n}}}\n\n";
  316. fout << "#####################################################"
  317. "##########################\n\n";
  318. }
  319. // ouput standard header for dsw file
  320. void cmGlobalVisualStudio6Generator::WriteDSWHeader(std::ostream& fout)
  321. {
  322. fout << "Microsoft Developer Studio Workspace File, Format Version 6.00\n";
  323. fout << "# WARNING: DO NOT EDIT OR DELETE THIS WORKSPACE FILE!\n\n";
  324. }
  325. //----------------------------------------------------------------------------
  326. std::string
  327. cmGlobalVisualStudio6Generator::WriteUtilityDepend(cmTarget* target)
  328. {
  329. std::string pname = target->GetName();
  330. pname += "_UTILITY";
  331. pname = GetVS6TargetName(pname.c_str());
  332. std::string fname = target->GetMakefile()->GetStartOutputDirectory();
  333. fname += "/";
  334. fname += pname;
  335. fname += ".dsp";
  336. cmGeneratedFileStream fout(fname.c_str());
  337. fout.SetCopyIfDifferent(true);
  338. fout <<
  339. "# Microsoft Developer Studio Project File - Name=\""
  340. << pname << "\" - Package Owner=<4>\n"
  341. "# Microsoft Developer Studio Generated Build File, Format Version 6.00\n"
  342. "# ** DO NOT EDIT **\n"
  343. "\n"
  344. "# TARGTYPE \"Win32 (x86) Generic Project\" 0x010a\n"
  345. "\n"
  346. "CFG=" << pname << " - Win32 Debug\n"
  347. "!MESSAGE \"" << pname << " - Win32 Debug\""
  348. " (based on \"Win32 (x86) Generic Project\")\n"
  349. "!MESSAGE \"" << pname << " - Win32 Release\" "
  350. "(based on \"Win32 (x86) Generic Project\")\n"
  351. "!MESSAGE \"" << pname << " - Win32 MinSizeRel\" "
  352. "(based on \"Win32 (x86) Generic Project\")\n"
  353. "!MESSAGE \"" << pname << " - Win32 RelWithDebInfo\" "
  354. "(based on \"Win32 (x86) Generic Project\")\n"
  355. "\n"
  356. "# Begin Project\n"
  357. "# Begin Target\n"
  358. "# Name \"" << pname << " - Win32 Debug\"\n"
  359. "# Name \"" << pname << " - Win32 Release\"\n"
  360. "# Name \"" << pname << " - Win32 MinSizeRel\"\n"
  361. "# Name \"" << pname << " - Win32 RelWithDebInfo\"\n"
  362. "# End Target\n"
  363. "# End Project\n"
  364. ;
  365. return pname;
  366. }
  367. //----------------------------------------------------------------------------
  368. void cmGlobalVisualStudio6Generator
  369. ::GetDocumentation(cmDocumentationEntry& entry) const
  370. {
  371. entry.Name = this->GetName();
  372. entry.Brief = "Generates Visual Studio 6 project files.";
  373. entry.Full = "";
  374. }
  375. //----------------------------------------------------------------------------
  376. void
  377. cmGlobalVisualStudio6Generator
  378. ::AppendDirectoryForConfig(const char* prefix,
  379. const char* config,
  380. const char* suffix,
  381. std::string& dir)
  382. {
  383. if(config)
  384. {
  385. dir += prefix;
  386. dir += config;
  387. dir += suffix;
  388. }
  389. }