cmGlobalVisualStudio6Generator.cxx 16 KB

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