cmGlobalVisualStudio6Generator.cxx 13 KB

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