cmGlobalVisualStudio6Generator.cxx 16 KB

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