cmGlobalVisualStudio6Generator.cxx 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  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. bool skip = false;
  199. // if it is a global target or the check build system target
  200. // or the all_build target
  201. // then only use the one that is for the root
  202. if(target->GetType() == cmTarget::GLOBAL_TARGET
  203. || !strcmp(target->GetName(), this->GetAllTargetName()))
  204. {
  205. if(target->GetMakefile() != root->GetMakefile())
  206. {
  207. skip = true;
  208. }
  209. }
  210. // if not skipping the project then write it into the
  211. // solution
  212. if(!skip)
  213. {
  214. std::string dspname = GetVS6TargetName(target->GetName());
  215. std::string dir = target->GetMakefile()->GetStartOutputDirectory();
  216. dir = root->Convert(dir.c_str(), cmLocalGenerator::START_OUTPUT);
  217. this->WriteProject(fout, dspname.c_str(), dir.c_str(), *target);
  218. }
  219. }
  220. }
  221. // Write the footer for the DSW file
  222. this->WriteDSWFooter(fout);
  223. }
  224. void cmGlobalVisualStudio6Generator
  225. ::OutputDSWFile(cmLocalGenerator* root,
  226. std::vector<cmLocalGenerator*>& generators)
  227. {
  228. if(generators.size() == 0)
  229. {
  230. return;
  231. }
  232. std::string fname = root->GetMakefile()->GetStartOutputDirectory();
  233. fname += "/";
  234. fname += root->GetMakefile()->GetProjectName();
  235. fname += ".dsw";
  236. std::ofstream fout(fname.c_str());
  237. if(!fout)
  238. {
  239. cmSystemTools::Error("Error can not open DSW file for write: ",
  240. fname.c_str());
  241. cmSystemTools::ReportLastSystemError("");
  242. return;
  243. }
  244. this->WriteDSWFile(fout, root, generators);
  245. }
  246. // output the DSW file
  247. void cmGlobalVisualStudio6Generator::OutputDSWFile()
  248. {
  249. std::map<cmStdString, std::vector<cmLocalGenerator*> >::iterator it;
  250. for(it = this->ProjectMap.begin(); it!= this->ProjectMap.end(); ++it)
  251. {
  252. this->OutputDSWFile(it->second[0], it->second);
  253. }
  254. }
  255. // Write a dsp file into the DSW file,
  256. // Note, that dependencies from executables to
  257. // the libraries it uses are also done here
  258. void cmGlobalVisualStudio6Generator::WriteProject(std::ostream& fout,
  259. const char* dspname,
  260. const char* dir,
  261. cmTarget& target)
  262. {
  263. fout << "#########################################################"
  264. "######################\n\n";
  265. fout << "Project: \"" << dspname << "\"="
  266. << dir << "\\" << dspname << ".dsp - Package Owner=<4>\n\n";
  267. fout << "Package=<5>\n{{{\n}}}\n\n";
  268. fout << "Package=<4>\n";
  269. fout << "{{{\n";
  270. // insert Begin Project Dependency Project_Dep_Name project stuff here
  271. if (target.GetType() != cmTarget::STATIC_LIBRARY)
  272. {
  273. cmTarget::LinkLibraryVectorType::const_iterator j, jend;
  274. j = target.GetLinkLibraries().begin();
  275. jend = target.GetLinkLibraries().end();
  276. for(;j!= jend; ++j)
  277. {
  278. if(j->first != dspname)
  279. {
  280. // is the library part of this DSW ? If so add dependency
  281. if(this->FindTarget(0, j->first.c_str()))
  282. {
  283. fout << "Begin Project Dependency\n";
  284. fout << "Project_Dep_Name "
  285. << GetVS6TargetName(j->first.c_str()) << "\n";
  286. fout << "End Project Dependency\n";
  287. }
  288. }
  289. }
  290. }
  291. std::set<cmStdString>::const_iterator i, end;
  292. // write utility dependencies.
  293. i = target.GetUtilities().begin();
  294. end = target.GetUtilities().end();
  295. for(;i!= end; ++i)
  296. {
  297. if(*i != dspname)
  298. {
  299. std::string depName = this->GetUtilityForTarget(target, i->c_str());
  300. fout << "Begin Project Dependency\n";
  301. fout << "Project_Dep_Name " << GetVS6TargetName(depName) << "\n";
  302. fout << "End Project Dependency\n";
  303. }
  304. }
  305. fout << "}}}\n\n";
  306. }
  307. // Write a dsp file into the DSW file,
  308. // Note, that dependencies from executables to
  309. // the libraries it uses are also done here
  310. void cmGlobalVisualStudio6Generator::WriteExternalProject(std::ostream& fout,
  311. const char* name,
  312. const char* location,
  313. const std::set<cmStdString>& dependencies)
  314. {
  315. fout << "#########################################################"
  316. "######################\n\n";
  317. fout << "Project: \"" << name << "\"="
  318. << location << " - Package Owner=<4>\n\n";
  319. fout << "Package=<5>\n{{{\n}}}\n\n";
  320. fout << "Package=<4>\n";
  321. fout << "{{{\n";
  322. std::set<cmStdString>::const_iterator i, end;
  323. // write dependencies.
  324. i = dependencies.begin();
  325. end = dependencies.end();
  326. for(;i!= end; ++i)
  327. {
  328. fout << "Begin Project Dependency\n";
  329. fout << "Project_Dep_Name " << GetVS6TargetName(*i) << "\n";
  330. fout << "End Project Dependency\n";
  331. }
  332. fout << "}}}\n\n";
  333. }
  334. // Standard end of dsw file
  335. void cmGlobalVisualStudio6Generator::WriteDSWFooter(std::ostream& fout)
  336. {
  337. fout << "######################################################"
  338. "#########################\n\n";
  339. fout << "Global:\n\n";
  340. fout << "Package=<5>\n{{{\n}}}\n\n";
  341. fout << "Package=<3>\n{{{\n}}}\n\n";
  342. fout << "#####################################################"
  343. "##########################\n\n";
  344. }
  345. // ouput standard header for dsw file
  346. void cmGlobalVisualStudio6Generator::WriteDSWHeader(std::ostream& fout)
  347. {
  348. fout << "Microsoft Developer Studio Workspace File, Format Version 6.00\n";
  349. fout << "# WARNING: DO NOT EDIT OR DELETE THIS WORKSPACE FILE!\n\n";
  350. }
  351. //----------------------------------------------------------------------------
  352. void cmGlobalVisualStudio6Generator
  353. ::GetDocumentation(cmDocumentationEntry& entry) const
  354. {
  355. entry.Name = this->GetName();
  356. entry.Brief = "Generates Visual Studio 6 project files.";
  357. entry.Full = "";
  358. }
  359. //----------------------------------------------------------------------------
  360. void
  361. cmGlobalVisualStudio6Generator
  362. ::AppendDirectoryForConfig(const char* prefix,
  363. const char* config,
  364. const char* suffix,
  365. std::string& dir)
  366. {
  367. if(config)
  368. {
  369. dir += prefix;
  370. dir += config;
  371. dir += suffix;
  372. }
  373. }