cmGlobalVisualStudio6Generator.cxx 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534
  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. {
  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. // add a special target that depends on ALL projects for easy build
  150. // of one configuration only.
  151. const char* no_output = 0;
  152. std::vector<std::string> no_depends;
  153. const char* no_working_dir = 0;
  154. std::map<cmStdString, std::vector<cmLocalGenerator*> >::iterator it;
  155. for(it = this->ProjectMap.begin(); it!= this->ProjectMap.end(); ++it)
  156. {
  157. std::vector<cmLocalGenerator*>& gen = it->second;
  158. // add the ALL_BUILD to the first local generator of each project
  159. if(gen.size())
  160. {
  161. gen[0]->GetMakefile()->AddUtilityCommand("ALL_BUILD", false,
  162. no_output, no_depends,
  163. no_working_dir,
  164. "echo", "Build all projects");
  165. }
  166. }
  167. // add the Run Tests command
  168. this->SetupTests();
  169. // first do the superclass method
  170. this->cmGlobalGenerator::Generate();
  171. // Now write out the DSW
  172. this->OutputDSWFile();
  173. }
  174. // Write a DSW file to the stream
  175. void cmGlobalVisualStudio6Generator
  176. ::WriteDSWFile(std::ostream& fout,cmLocalGenerator* root,
  177. std::vector<cmLocalGenerator*>& generators)
  178. {
  179. // Write out the header for a DSW file
  180. this->WriteDSWHeader(fout);
  181. // Get the home directory with the trailing slash
  182. std::string homedir = root->GetMakefile()->GetStartOutputDirectory();
  183. homedir += "/";
  184. unsigned int i;
  185. bool doneAllBuild = false;
  186. bool doneRunTests = false;
  187. bool doneInstall = false;
  188. bool doneEditCache = false;
  189. bool doneRebuildCache = false;
  190. bool donePackage = false;
  191. for(i = 0; i < generators.size(); ++i)
  192. {
  193. if(this->IsExcluded(root, generators[i]))
  194. {
  195. continue;
  196. }
  197. cmMakefile* mf = generators[i]->GetMakefile();
  198. // Get the source directory from the makefile
  199. std::string dir = mf->GetStartOutputDirectory();
  200. // remove the home directory and / from the source directory
  201. // this gives a relative path
  202. cmSystemTools::ReplaceString(dir, homedir.c_str(), "");
  203. // Get the list of create dsp files names from the LocalGenerator, more
  204. // than one dsp could have been created per input CMakeLists.txt file
  205. // for each target
  206. std::vector<std::string> dspnames =
  207. static_cast<cmLocalVisualStudio6Generator *>(generators[i])
  208. ->GetCreatedProjectNames();
  209. cmTargets &tgts = generators[i]->GetMakefile()->GetTargets();
  210. cmTargets::iterator l = tgts.begin();
  211. for(std::vector<std::string>::iterator si = dspnames.begin();
  212. l != tgts.end(); ++l)
  213. {
  214. // special handling for the current makefile
  215. if(mf == generators[0]->GetMakefile())
  216. {
  217. dir = "."; // no subdirectory for project generated
  218. // if this is the special ALL_BUILD utility, then
  219. // make it depend on every other non UTILITY project.
  220. // This is done by adding the names to the GetUtilities
  221. // vector on the makefile
  222. if(l->first == "ALL_BUILD" && !doneAllBuild)
  223. {
  224. unsigned int j;
  225. for(j = 0; j < generators.size(); ++j)
  226. {
  227. cmTargets &atgts = generators[j]->GetMakefile()->GetTargets();
  228. for(cmTargets::iterator al = atgts.begin();
  229. al != atgts.end(); ++al)
  230. {
  231. if (al->second.IsInAll())
  232. {
  233. if (al->second.GetType() == cmTarget::UTILITY ||
  234. al->second.GetType() == cmTarget::GLOBAL_TARGET)
  235. {
  236. l->second.AddUtility(al->first.c_str());
  237. }
  238. else
  239. {
  240. l->second.AddLinkLibrary(al->first, cmTarget::GENERAL);
  241. }
  242. }
  243. }
  244. }
  245. }
  246. }
  247. // Write the project into the DSW file
  248. if (strncmp(l->first.c_str(), "INCLUDE_EXTERNAL_MSPROJECT", 26) == 0)
  249. {
  250. cmCustomCommand cc = l->second.GetPostBuildCommands()[0];
  251. const cmCustomCommandLines& cmds = cc.GetCommandLines();
  252. std::string project = cmds[0][0];
  253. std::string location = cmds[0][1];
  254. this->WriteExternalProject(fout, project.c_str(),
  255. location.c_str(), cc.GetDepends());
  256. }
  257. else
  258. {
  259. if ((l->second.GetType() != cmTarget::INSTALL_FILES)
  260. && (l->second.GetType() != cmTarget::INSTALL_PROGRAMS))
  261. {
  262. bool skip = false;
  263. // skip ALL_BUILD and RUN_TESTS if they have already been added
  264. if(l->first == "ALL_BUILD" )
  265. {
  266. if(doneAllBuild)
  267. {
  268. skip = true;
  269. }
  270. else
  271. {
  272. doneAllBuild = true;
  273. }
  274. }
  275. if(l->first == "INSTALL")
  276. {
  277. if(doneInstall)
  278. {
  279. skip = true;
  280. }
  281. else
  282. {
  283. doneInstall = true;
  284. }
  285. }
  286. if(l->first == "RUN_TESTS")
  287. {
  288. if(doneRunTests)
  289. {
  290. skip = true;
  291. }
  292. else
  293. {
  294. doneRunTests = true;
  295. }
  296. }
  297. if(l->first == "EDIT_CACHE")
  298. {
  299. if(doneEditCache)
  300. {
  301. skip = true;
  302. }
  303. else
  304. {
  305. doneEditCache = true;
  306. }
  307. }
  308. if(l->first == "REBUILD_CACHE")
  309. {
  310. if(doneRebuildCache)
  311. {
  312. skip = true;
  313. }
  314. else
  315. {
  316. doneRebuildCache = true;
  317. }
  318. }
  319. if(l->first == "PACKAGE")
  320. {
  321. if(donePackage)
  322. {
  323. skip = true;
  324. }
  325. else
  326. {
  327. donePackage = true;
  328. }
  329. }
  330. if(!skip)
  331. {
  332. this->WriteProject(fout, si->c_str(), dir.c_str(),l->second);
  333. }
  334. ++si;
  335. }
  336. }
  337. }
  338. }
  339. // Write the footer for the DSW file
  340. this->WriteDSWFooter(fout);
  341. }
  342. void cmGlobalVisualStudio6Generator
  343. ::OutputDSWFile(cmLocalGenerator* root,
  344. std::vector<cmLocalGenerator*>& generators)
  345. {
  346. if(generators.size() == 0)
  347. {
  348. return;
  349. }
  350. std::string fname = root->GetMakefile()->GetStartOutputDirectory();
  351. fname += "/";
  352. fname += root->GetMakefile()->GetProjectName();
  353. fname += ".dsw";
  354. std::ofstream fout(fname.c_str());
  355. if(!fout)
  356. {
  357. cmSystemTools::Error("Error can not open DSW file for write: ",
  358. fname.c_str());
  359. cmSystemTools::ReportLastSystemError("");
  360. return;
  361. }
  362. this->WriteDSWFile(fout, root, generators);
  363. }
  364. // output the DSW file
  365. void cmGlobalVisualStudio6Generator::OutputDSWFile()
  366. {
  367. std::map<cmStdString, std::vector<cmLocalGenerator*> >::iterator it;
  368. for(it = this->ProjectMap.begin(); it!= this->ProjectMap.end(); ++it)
  369. {
  370. this->OutputDSWFile(it->second[0], it->second);
  371. }
  372. }
  373. // Write a dsp file into the DSW file,
  374. // Note, that dependencies from executables to
  375. // the libraries it uses are also done here
  376. void cmGlobalVisualStudio6Generator::WriteProject(std::ostream& fout,
  377. const char* dspname,
  378. const char* dir,
  379. cmTarget& target)
  380. {
  381. fout << "#########################################################"
  382. "######################\n\n";
  383. fout << "Project: \"" << dspname << "\"="
  384. << dir << "\\" << dspname << ".dsp - Package Owner=<4>\n\n";
  385. fout << "Package=<5>\n{{{\n}}}\n\n";
  386. fout << "Package=<4>\n";
  387. fout << "{{{\n";
  388. // insert Begin Project Dependency Project_Dep_Name project stuff here
  389. if (target.GetType() != cmTarget::STATIC_LIBRARY)
  390. {
  391. cmTarget::LinkLibraryVectorType::const_iterator j, jend;
  392. j = target.GetLinkLibraries().begin();
  393. jend = target.GetLinkLibraries().end();
  394. for(;j!= jend; ++j)
  395. {
  396. if(j->first != dspname)
  397. {
  398. // is the library part of this DSW ? If so add dependency
  399. if(this->FindTarget(0, j->first.c_str()))
  400. {
  401. fout << "Begin Project Dependency\n";
  402. fout << "Project_Dep_Name " << j->first.c_str() << "\n";
  403. fout << "End Project Dependency\n";
  404. }
  405. }
  406. }
  407. }
  408. std::set<cmStdString>::const_iterator i, end;
  409. // write utility dependencies.
  410. i = target.GetUtilities().begin();
  411. end = target.GetUtilities().end();
  412. for(;i!= end; ++i)
  413. {
  414. if(*i != dspname)
  415. {
  416. std::string depName = *i;
  417. if(strncmp(depName.c_str(), "INCLUDE_EXTERNAL_MSPROJECT", 26) == 0)
  418. {
  419. depName.erase(depName.begin(), depName.begin() + 27);
  420. }
  421. fout << "Begin Project Dependency\n";
  422. fout << "Project_Dep_Name " << depName << "\n";
  423. fout << "End Project Dependency\n";
  424. }
  425. }
  426. fout << "}}}\n\n";
  427. }
  428. // Write a dsp file into the DSW file,
  429. // Note, that dependencies from executables to
  430. // the libraries it uses are also done here
  431. void cmGlobalVisualStudio6Generator::WriteExternalProject(std::ostream& fout,
  432. const char* name,
  433. const char* location,
  434. const std::vector<std::string>& dependencies)
  435. {
  436. fout << "#########################################################"
  437. "######################\n\n";
  438. fout << "Project: \"" << name << "\"="
  439. << location << " - Package Owner=<4>\n\n";
  440. fout << "Package=<5>\n{{{\n}}}\n\n";
  441. fout << "Package=<4>\n";
  442. fout << "{{{\n";
  443. std::vector<std::string>::const_iterator i, end;
  444. // write dependencies.
  445. i = dependencies.begin();
  446. end = dependencies.end();
  447. for(;i!= end; ++i)
  448. {
  449. fout << "Begin Project Dependency\n";
  450. fout << "Project_Dep_Name " << *i << "\n";
  451. fout << "End Project Dependency\n";
  452. }
  453. fout << "}}}\n\n";
  454. }
  455. // Standard end of dsw file
  456. void cmGlobalVisualStudio6Generator::WriteDSWFooter(std::ostream& fout)
  457. {
  458. fout << "######################################################"
  459. "#########################\n\n";
  460. fout << "Global:\n\n";
  461. fout << "Package=<5>\n{{{\n}}}\n\n";
  462. fout << "Package=<3>\n{{{\n}}}\n\n";
  463. fout << "#####################################################"
  464. "##########################\n\n";
  465. }
  466. // ouput standard header for dsw file
  467. void cmGlobalVisualStudio6Generator::WriteDSWHeader(std::ostream& fout)
  468. {
  469. fout << "Microsoft Developer Studio Workspace File, Format Version 6.00\n";
  470. fout << "# WARNING: DO NOT EDIT OR DELETE THIS WORKSPACE FILE!\n\n";
  471. }
  472. //----------------------------------------------------------------------------
  473. void cmGlobalVisualStudio6Generator
  474. ::GetDocumentation(cmDocumentationEntry& entry) const
  475. {
  476. entry.name = this->GetName();
  477. entry.brief = "Generates Visual Studio 6 project files.";
  478. entry.full = "";
  479. }
  480. //----------------------------------------------------------------------------
  481. void
  482. cmGlobalVisualStudio6Generator
  483. ::AppendDirectoryForConfig(const char* prefix,
  484. const char* config,
  485. const char* suffix,
  486. std::string& dir)
  487. {
  488. if(config)
  489. {
  490. dir += prefix;
  491. dir += config;
  492. dir += suffix;
  493. }
  494. }