cmGlobalVisualStudio6Generator.cxx 12 KB

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