cmGlobalVisualStudio6Generator.cxx 16 KB

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