cmGlobalVisualStudio6Generator.cxx 15 KB

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