cmGlobalVisualStudio6Generator.cxx 15 KB

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