cmGlobalVisualStudio6Generator.cxx 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518
  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. cmGlobalVisualStudio6Generator::cmGlobalVisualStudio6Generator()
  15. {
  16. this->FindMakeProgramFile = "CMakeVS6FindMake.cmake";
  17. }
  18. void cmGlobalVisualStudio6Generator
  19. ::EnableLanguage(std::vector<std::string>const& lang,
  20. cmMakefile *mf,
  21. bool optional)
  22. {
  23. mf->AddDefinition("CMAKE_GENERATOR_CC", "cl");
  24. mf->AddDefinition("CMAKE_GENERATOR_CXX", "cl");
  25. mf->AddDefinition("CMAKE_GENERATOR_RC", "rc");
  26. mf->AddDefinition("CMAKE_GENERATOR_NO_COMPILER_ENV", "1");
  27. mf->AddDefinition("CMAKE_GENERATOR_Fortran", "ifort");
  28. mf->AddDefinition("MSVC60", "1");
  29. this->GenerateConfigurations(mf);
  30. this->cmGlobalGenerator::EnableLanguage(lang, mf, optional);
  31. }
  32. void cmGlobalVisualStudio6Generator::GenerateConfigurations(cmMakefile* mf)
  33. {
  34. std::string fname= mf->GetRequiredDefinition("CMAKE_ROOT");
  35. const char* def= mf->GetDefinition( "MSPROJECT_TEMPLATE_DIRECTORY");
  36. if(def)
  37. {
  38. fname = def;
  39. }
  40. else
  41. {
  42. fname += "/Templates";
  43. }
  44. fname += "/CMakeVisualStudio6Configurations.cmake";
  45. if(!mf->ReadListFile(mf->GetCurrentListFile(), fname.c_str()))
  46. {
  47. cmSystemTools::Error("Cannot open ", fname.c_str(),
  48. ". Please copy this file from the main "
  49. "CMake/Templates directory and edit it for "
  50. "your build configurations.");
  51. }
  52. else if(!mf->GetDefinition("CMAKE_CONFIGURATION_TYPES"))
  53. {
  54. cmSystemTools::Error("CMAKE_CONFIGURATION_TYPES not set by ",
  55. fname.c_str(),
  56. ". Please copy this file from the main "
  57. "CMake/Templates directory and edit it for "
  58. "your build configurations.");
  59. }
  60. }
  61. std::string cmGlobalVisualStudio6Generator
  62. ::GenerateBuildCommand(const char* makeProgram,
  63. const char *projectName,
  64. const char* additionalOptions,
  65. const char *targetName,
  66. const char* config,
  67. bool ignoreErrors,
  68. bool)
  69. {
  70. // Ingoring errors is not implemented in visual studio 6
  71. (void) ignoreErrors;
  72. // now build the test
  73. std::vector<std::string> mp;
  74. mp.push_back("[HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\VisualStudio"
  75. "\\6.0\\Setup;VsCommonDir]/MSDev98/Bin");
  76. cmSystemTools::ExpandRegistryValues(mp[0]);
  77. std::string originalCommand = makeProgram;
  78. std::string makeCommand =
  79. cmSystemTools::FindProgram(makeProgram, mp);
  80. if(makeCommand.size() == 0)
  81. {
  82. std::string e = "Generator cannot find Visual Studio 6 msdev program \"";
  83. e += originalCommand;
  84. e += "\" specified by CMAKE_MAKE_PROGRAM cache entry. ";
  85. e += "Please fix the setting.";
  86. cmSystemTools::Error(e.c_str());
  87. return "";
  88. }
  89. makeCommand = cmSystemTools::ConvertToOutputPath(makeCommand.c_str());
  90. // if there are spaces in the makeCommand, assume a full path
  91. // and convert it to a path with no spaces in it as the
  92. // RunSingleCommand does not like spaces
  93. #if defined(_WIN32) && !defined(__CYGWIN__)
  94. if(makeCommand.find(' ') != std::string::npos)
  95. {
  96. cmSystemTools::GetShortPath(makeCommand.c_str(), makeCommand);
  97. }
  98. #endif
  99. makeCommand += " ";
  100. makeCommand += projectName;
  101. makeCommand += ".dsw /MAKE \"";
  102. bool clean = false;
  103. if ( targetName && strcmp(targetName, "clean") == 0 )
  104. {
  105. clean = true;
  106. targetName = "ALL_BUILD";
  107. }
  108. if (targetName && strlen(targetName))
  109. {
  110. makeCommand += targetName;
  111. }
  112. else
  113. {
  114. makeCommand += "ALL_BUILD";
  115. }
  116. makeCommand += " - ";
  117. if(config && strlen(config))
  118. {
  119. makeCommand += config;
  120. }
  121. else
  122. {
  123. makeCommand += "Debug";
  124. }
  125. if(clean)
  126. {
  127. makeCommand += "\" /CLEAN";
  128. }
  129. else
  130. {
  131. makeCommand += "\" /BUILD";
  132. }
  133. if ( additionalOptions )
  134. {
  135. makeCommand += " ";
  136. makeCommand += additionalOptions;
  137. }
  138. return makeCommand;
  139. }
  140. ///! Create a local generator appropriate to this Global Generator
  141. cmLocalGenerator *cmGlobalVisualStudio6Generator::CreateLocalGenerator()
  142. {
  143. cmLocalGenerator *lg = new cmLocalVisualStudio6Generator;
  144. lg->SetGlobalGenerator(this);
  145. return lg;
  146. }
  147. void cmGlobalVisualStudio6Generator::Generate()
  148. {
  149. // first do the superclass method
  150. this->cmGlobalVisualStudioGenerator::Generate();
  151. // Now write out the DSW
  152. this->OutputDSWFile();
  153. }
  154. // Write a DSW file to the stream
  155. void cmGlobalVisualStudio6Generator
  156. ::WriteDSWFile(std::ostream& fout,cmLocalGenerator* root,
  157. std::vector<cmLocalGenerator*>& generators)
  158. {
  159. // Write out the header for a DSW file
  160. this->WriteDSWHeader(fout);
  161. // Get the home directory with the trailing slash
  162. std::string homedir = root->GetMakefile()->GetStartOutputDirectory();
  163. homedir += "/";
  164. unsigned int i;
  165. bool doneAllBuild = false;
  166. bool doneRunTests = false;
  167. bool doneInstall = false;
  168. bool doneEditCache = false;
  169. bool doneRebuildCache = false;
  170. bool donePackage = false;
  171. for(i = 0; i < generators.size(); ++i)
  172. {
  173. if(this->IsExcluded(root, generators[i]))
  174. {
  175. continue;
  176. }
  177. cmMakefile* mf = generators[i]->GetMakefile();
  178. // Get the source directory from the makefile
  179. std::string dir = mf->GetStartOutputDirectory();
  180. // remove the home directory and / from the source directory
  181. // this gives a relative path
  182. cmSystemTools::ReplaceString(dir, homedir.c_str(), "");
  183. // Get the list of create dsp files names from the LocalGenerator, more
  184. // than one dsp could have been created per input CMakeLists.txt file
  185. // for each target
  186. std::vector<std::string> dspnames =
  187. static_cast<cmLocalVisualStudio6Generator *>(generators[i])
  188. ->GetCreatedProjectNames();
  189. cmTargets &tgts = generators[i]->GetMakefile()->GetTargets();
  190. std::vector<std::string>::iterator si = dspnames.begin();
  191. for(cmTargets::iterator l = tgts.begin(); l != tgts.end(); ++l)
  192. {
  193. // special handling for the current makefile
  194. if(mf == generators[0]->GetMakefile())
  195. {
  196. dir = "."; // no subdirectory for project generated
  197. // if this is the special ALL_BUILD utility, then
  198. // make it depend on every other non UTILITY project.
  199. // This is done by adding the names to the GetUtilities
  200. // vector on the makefile
  201. if(l->first == "ALL_BUILD" && !doneAllBuild)
  202. {
  203. unsigned int j;
  204. for(j = 0; j < generators.size(); ++j)
  205. {
  206. cmTargets &atgts = generators[j]->GetMakefile()->GetTargets();
  207. for(cmTargets::iterator al = atgts.begin();
  208. al != atgts.end(); ++al)
  209. {
  210. if (!al->second.GetPropertyAsBool("EXCLUDE_FROM_ALL"))
  211. {
  212. if (al->second.GetType() == cmTarget::UTILITY ||
  213. al->second.GetType() == cmTarget::GLOBAL_TARGET)
  214. {
  215. l->second.AddUtility(al->first.c_str());
  216. }
  217. else
  218. {
  219. l->second.AddLinkLibrary(al->first, cmTarget::GENERAL);
  220. }
  221. }
  222. }
  223. }
  224. }
  225. }
  226. // Write the project into the DSW file
  227. cmTarget* target = &l->second;
  228. const char* expath = target->GetProperty("EXTERNAL_MSPROJECT");
  229. if(expath)
  230. {
  231. std::string project = target->GetName();
  232. std::string location = expath;
  233. this->WriteExternalProject(fout, project.c_str(),
  234. location.c_str(), target->GetUtilities());
  235. }
  236. else
  237. {
  238. bool skip = false;
  239. // skip ALL_BUILD and RUN_TESTS if they have already been added
  240. if(l->first == "ALL_BUILD" )
  241. {
  242. if(doneAllBuild)
  243. {
  244. skip = true;
  245. }
  246. else
  247. {
  248. doneAllBuild = true;
  249. }
  250. }
  251. if(l->first == "INSTALL")
  252. {
  253. if(doneInstall)
  254. {
  255. skip = true;
  256. }
  257. else
  258. {
  259. doneInstall = true;
  260. }
  261. }
  262. if(l->first == "RUN_TESTS")
  263. {
  264. if(doneRunTests)
  265. {
  266. skip = true;
  267. }
  268. else
  269. {
  270. doneRunTests = true;
  271. }
  272. }
  273. if(l->first == "EDIT_CACHE")
  274. {
  275. if(doneEditCache)
  276. {
  277. skip = true;
  278. }
  279. else
  280. {
  281. doneEditCache = true;
  282. }
  283. }
  284. if(l->first == "REBUILD_CACHE")
  285. {
  286. if(doneRebuildCache)
  287. {
  288. skip = true;
  289. }
  290. else
  291. {
  292. doneRebuildCache = true;
  293. }
  294. }
  295. if(l->first == "PACKAGE")
  296. {
  297. if(donePackage)
  298. {
  299. skip = true;
  300. }
  301. else
  302. {
  303. donePackage = true;
  304. }
  305. }
  306. if(!skip)
  307. {
  308. this->WriteProject(fout, si->c_str(), dir.c_str(),l->second);
  309. }
  310. ++si;
  311. }
  312. }
  313. }
  314. // Write the footer for the DSW file
  315. this->WriteDSWFooter(fout);
  316. }
  317. void cmGlobalVisualStudio6Generator
  318. ::OutputDSWFile(cmLocalGenerator* root,
  319. std::vector<cmLocalGenerator*>& generators)
  320. {
  321. if(generators.size() == 0)
  322. {
  323. return;
  324. }
  325. std::string fname = root->GetMakefile()->GetStartOutputDirectory();
  326. fname += "/";
  327. fname += root->GetMakefile()->GetProjectName();
  328. fname += ".dsw";
  329. std::ofstream fout(fname.c_str());
  330. if(!fout)
  331. {
  332. cmSystemTools::Error("Error can not open DSW file for write: ",
  333. fname.c_str());
  334. cmSystemTools::ReportLastSystemError("");
  335. return;
  336. }
  337. this->WriteDSWFile(fout, root, generators);
  338. }
  339. // output the DSW file
  340. void cmGlobalVisualStudio6Generator::OutputDSWFile()
  341. {
  342. std::map<cmStdString, std::vector<cmLocalGenerator*> >::iterator it;
  343. for(it = this->ProjectMap.begin(); it!= this->ProjectMap.end(); ++it)
  344. {
  345. this->OutputDSWFile(it->second[0], it->second);
  346. }
  347. }
  348. // Utility function to make a valid VS6 *.dsp filename out
  349. // of a CMake target name:
  350. //
  351. std::string GetVS6TargetName(const std::string& targetName)
  352. {
  353. std::string name(targetName);
  354. // Eliminate hyphens. VS6 cannot handle hyphens in *.dsp filenames...
  355. // Replace them with underscores.
  356. //
  357. cmSystemTools::ReplaceString(name, "-", "_");
  358. return name;
  359. }
  360. // Write a dsp file into the DSW file,
  361. // Note, that dependencies from executables to
  362. // the libraries it uses are also done here
  363. void cmGlobalVisualStudio6Generator::WriteProject(std::ostream& fout,
  364. const char* dspname,
  365. const char* dir,
  366. cmTarget& target)
  367. {
  368. fout << "#########################################################"
  369. "######################\n\n";
  370. fout << "Project: \"" << dspname << "\"="
  371. << dir << "\\" << dspname << ".dsp - Package Owner=<4>\n\n";
  372. fout << "Package=<5>\n{{{\n}}}\n\n";
  373. fout << "Package=<4>\n";
  374. fout << "{{{\n";
  375. // insert Begin Project Dependency Project_Dep_Name project stuff here
  376. if (target.GetType() != cmTarget::STATIC_LIBRARY)
  377. {
  378. cmTarget::LinkLibraryVectorType::const_iterator j, jend;
  379. j = target.GetLinkLibraries().begin();
  380. jend = target.GetLinkLibraries().end();
  381. for(;j!= jend; ++j)
  382. {
  383. if(j->first != dspname)
  384. {
  385. // is the library part of this DSW ? If so add dependency
  386. if(this->FindTarget(0, j->first.c_str()))
  387. {
  388. fout << "Begin Project Dependency\n";
  389. fout << "Project_Dep_Name "
  390. << GetVS6TargetName(j->first.c_str()) << "\n";
  391. fout << "End Project Dependency\n";
  392. }
  393. }
  394. }
  395. }
  396. std::set<cmStdString>::const_iterator i, end;
  397. // write utility dependencies.
  398. i = target.GetUtilities().begin();
  399. end = target.GetUtilities().end();
  400. for(;i!= end; ++i)
  401. {
  402. if(*i != dspname)
  403. {
  404. std::string depName = this->GetUtilityForTarget(target, i->c_str());
  405. fout << "Begin Project Dependency\n";
  406. fout << "Project_Dep_Name " << GetVS6TargetName(depName) << "\n";
  407. fout << "End Project Dependency\n";
  408. }
  409. }
  410. fout << "}}}\n\n";
  411. }
  412. // Write a dsp file into the DSW file,
  413. // Note, that dependencies from executables to
  414. // the libraries it uses are also done here
  415. void cmGlobalVisualStudio6Generator::WriteExternalProject(std::ostream& fout,
  416. const char* name,
  417. const char* location,
  418. const std::set<cmStdString>& dependencies)
  419. {
  420. fout << "#########################################################"
  421. "######################\n\n";
  422. fout << "Project: \"" << name << "\"="
  423. << location << " - Package Owner=<4>\n\n";
  424. fout << "Package=<5>\n{{{\n}}}\n\n";
  425. fout << "Package=<4>\n";
  426. fout << "{{{\n";
  427. std::set<cmStdString>::const_iterator i, end;
  428. // write dependencies.
  429. i = dependencies.begin();
  430. end = dependencies.end();
  431. for(;i!= end; ++i)
  432. {
  433. fout << "Begin Project Dependency\n";
  434. fout << "Project_Dep_Name " << GetVS6TargetName(*i) << "\n";
  435. fout << "End Project Dependency\n";
  436. }
  437. fout << "}}}\n\n";
  438. }
  439. // Standard end of dsw file
  440. void cmGlobalVisualStudio6Generator::WriteDSWFooter(std::ostream& fout)
  441. {
  442. fout << "######################################################"
  443. "#########################\n\n";
  444. fout << "Global:\n\n";
  445. fout << "Package=<5>\n{{{\n}}}\n\n";
  446. fout << "Package=<3>\n{{{\n}}}\n\n";
  447. fout << "#####################################################"
  448. "##########################\n\n";
  449. }
  450. // ouput standard header for dsw file
  451. void cmGlobalVisualStudio6Generator::WriteDSWHeader(std::ostream& fout)
  452. {
  453. fout << "Microsoft Developer Studio Workspace File, Format Version 6.00\n";
  454. fout << "# WARNING: DO NOT EDIT OR DELETE THIS WORKSPACE FILE!\n\n";
  455. }
  456. //----------------------------------------------------------------------------
  457. void cmGlobalVisualStudio6Generator
  458. ::GetDocumentation(cmDocumentationEntry& entry) const
  459. {
  460. entry.Name = this->GetName();
  461. entry.Brief = "Generates Visual Studio 6 project files.";
  462. entry.Full = "";
  463. }
  464. //----------------------------------------------------------------------------
  465. void
  466. cmGlobalVisualStudio6Generator
  467. ::AppendDirectoryForConfig(const char* prefix,
  468. const char* config,
  469. const char* suffix,
  470. std::string& dir)
  471. {
  472. if(config)
  473. {
  474. dir += prefix;
  475. dir += config;
  476. dir += suffix;
  477. }
  478. }