cmGlobalVisualStudio6Generator.cxx 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  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(const char* 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. this->GenerateConfigurations(mf);
  28. this->cmGlobalGenerator::EnableLanguage(lang, mf);
  29. }
  30. void cmGlobalVisualStudio6Generator::GenerateConfigurations(cmMakefile* mf)
  31. {
  32. std::string fname= mf->GetDefinition("CMAKE_ROOT");
  33. const char* def= mf->GetDefinition( "MSPROJECT_TEMPLATE_DIRECTORY");
  34. if(def)
  35. {
  36. fname = def;
  37. }
  38. else
  39. {
  40. fname += "/Templates";
  41. }
  42. fname += "/CMakeVisualStudio6Configurations.cmake";
  43. if(!mf->ReadListFile(mf->GetCurrentListFile(), fname.c_str()))
  44. {
  45. cmSystemTools::Error("Cannot open ", fname.c_str(),
  46. ". Please copy this file from the main "
  47. "CMake/Templates directory and edit it for "
  48. "your build configurations.");
  49. }
  50. else if(!mf->GetDefinition("CMAKE_CONFIGURATION_TYPES"))
  51. {
  52. cmSystemTools::Error("CMAKE_CONFIGURATION_TYPES not set by ",
  53. fname.c_str(),
  54. ". Please copy this file from the main "
  55. "CMake/Templates directory and edit it for "
  56. "your build configurations.");
  57. }
  58. }
  59. int cmGlobalVisualStudio6Generator::TryCompile(const char *,
  60. const char *bindir,
  61. const char *projectName,
  62. const char *targetName,
  63. std::string *output)
  64. {
  65. // now build the test
  66. std::string makeCommand =
  67. m_CMakeInstance->GetCacheManager()->GetCacheValue("CMAKE_MAKE_PROGRAM");
  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 = makeCommand;
  72. makeCommand = cmSystemTools::FindProgram(makeCommand.c_str(), mp);
  73. if(makeCommand.size() == 0)
  74. {
  75. std::string e = "Generator cannot find Visual Studio 6 msdev program \"";
  76. e += originalCommand;
  77. e += "\" specified by CMAKE_MAKE_PROGRAM cache entry. ";
  78. e += "Please fix the setting.";
  79. cmSystemTools::Error(e.c_str());
  80. return 1;
  81. }
  82. makeCommand = cmSystemTools::ConvertToOutputPath(makeCommand.c_str());
  83. /**
  84. * Run an executable command and put the stdout in output.
  85. */
  86. std::string cwd = cmSystemTools::GetCurrentWorkingDirectory();
  87. cmSystemTools::ChangeDirectory(bindir);
  88. // if there are spaces in the makeCommand, assume a full path
  89. // and convert it to a path with no spaces in it as the
  90. // RunCommand does not like spaces
  91. #if defined(_WIN32) && !defined(__CYGWIN__)
  92. if(makeCommand.find(' ') != std::string::npos)
  93. {
  94. cmSystemTools::GetShortPath(makeCommand.c_str(), makeCommand);
  95. }
  96. #endif
  97. makeCommand += " ";
  98. makeCommand += projectName;
  99. makeCommand += ".dsw /MAKE \"";
  100. if (targetName)
  101. {
  102. makeCommand += targetName;
  103. }
  104. else
  105. {
  106. makeCommand += "ALL_BUILD";
  107. }
  108. makeCommand += " - Debug\"";
  109. int retVal;
  110. if (!cmSystemTools::RunCommand(makeCommand.c_str(), *output, retVal, 0, false))
  111. {
  112. std::string e = "Error executing make program \"";
  113. e += originalCommand;
  114. e += "\" specified by CMAKE_MAKE_PROGRAM cache entry. ";
  115. e += "The command string used was \"";
  116. e += makeCommand.c_str();
  117. e += "\".";
  118. cmSystemTools::Error(e.c_str());
  119. // return to the original directory
  120. cmSystemTools::ChangeDirectory(cwd.c_str());
  121. return 1;
  122. }
  123. cmSystemTools::ChangeDirectory(cwd.c_str());
  124. return retVal;
  125. }
  126. ///! Create a local generator appropriate to this Global Generator
  127. cmLocalGenerator *cmGlobalVisualStudio6Generator::CreateLocalGenerator()
  128. {
  129. cmLocalGenerator *lg = new cmLocalVisualStudio6Generator;
  130. lg->SetGlobalGenerator(this);
  131. return lg;
  132. }
  133. void cmGlobalVisualStudio6Generator::Generate()
  134. {
  135. // add a special target that depends on ALL projects for easy build
  136. // of Debug only
  137. m_LocalGenerators[0]->GetMakefile()->
  138. AddUtilityCommand("ALL_BUILD", "echo","\"Build all projects\"",false);
  139. // add the Run Tests command
  140. this->SetupTests();
  141. // first do the superclass method
  142. this->cmGlobalGenerator::Generate();
  143. // Now write out the DSW
  144. this->OutputDSWFile();
  145. }
  146. // output the DSW file
  147. void cmGlobalVisualStudio6Generator::OutputDSWFile()
  148. {
  149. // create the dsw file name
  150. std::string fname;
  151. fname = m_CMakeInstance->GetStartOutputDirectory();
  152. fname += "/";
  153. if(strlen(m_LocalGenerators[0]->GetMakefile()->GetProjectName()))
  154. {
  155. fname += m_LocalGenerators[0]->GetMakefile()->GetProjectName();
  156. }
  157. else
  158. {
  159. fname += "Project";
  160. }
  161. fname += ".dsw";
  162. std::ofstream fout(fname.c_str());
  163. if(!fout)
  164. {
  165. cmSystemTools::Error("Error can not open DSW file for write: "
  166. ,fname.c_str());
  167. return;
  168. }
  169. this->WriteDSWFile(fout);
  170. }
  171. inline std::string removeQuotes(const std::string& s)
  172. {
  173. if(s[0] == '\"' && s[s.size()-1] == '\"')
  174. {
  175. return s.substr(1, s.size()-2);
  176. }
  177. return s;
  178. }
  179. void cmGlobalVisualStudio6Generator::SetupTests()
  180. {
  181. std::string ctest =
  182. m_LocalGenerators[0]->GetMakefile()->GetDefinition("CMAKE_COMMAND");
  183. ctest = removeQuotes(ctest);
  184. ctest = cmSystemTools::GetFilenamePath(ctest.c_str());
  185. ctest += "/";
  186. ctest += "ctest";
  187. ctest += cmSystemTools::GetExecutableExtension();
  188. if(!cmSystemTools::FileExists(ctest.c_str()))
  189. {
  190. ctest =
  191. m_LocalGenerators[0]->GetMakefile()->GetDefinition("CMAKE_COMMAND");
  192. ctest = cmSystemTools::GetFilenamePath(ctest.c_str());
  193. ctest += "/Debug/";
  194. ctest += "ctest";
  195. ctest += cmSystemTools::GetExecutableExtension();
  196. }
  197. if(!cmSystemTools::FileExists(ctest.c_str()))
  198. {
  199. ctest =
  200. m_LocalGenerators[0]->GetMakefile()->GetDefinition("CMAKE_COMMAND");
  201. ctest = cmSystemTools::GetFilenamePath(ctest.c_str());
  202. ctest += "/Release/";
  203. ctest += "ctest";
  204. ctest += cmSystemTools::GetExecutableExtension();
  205. }
  206. // if we found ctest
  207. if (cmSystemTools::FileExists(ctest.c_str()))
  208. {
  209. // Create a full path filename for output Testfile
  210. std::string fname;
  211. fname = m_CMakeInstance->GetStartOutputDirectory();
  212. fname += "/";
  213. fname += "DartTestfile.txt";
  214. // If the file doesn't exist, then ENABLE_TESTING hasn't been run
  215. if (cmSystemTools::FileExists(fname.c_str()))
  216. {
  217. m_LocalGenerators[0]->GetMakefile()->
  218. AddUtilityCommand("RUN_TESTS", ctest.c_str(), "-D $(IntDir)",false);
  219. }
  220. }
  221. }
  222. // Write a DSW file to the stream
  223. void cmGlobalVisualStudio6Generator::WriteDSWFile(std::ostream& fout)
  224. {
  225. // Write out the header for a DSW file
  226. this->WriteDSWHeader(fout);
  227. // Get the home directory with the trailing slash
  228. std::string homedir = m_CMakeInstance->GetHomeDirectory();
  229. homedir += "/";
  230. unsigned int i;
  231. for(i = 0; i < m_LocalGenerators.size(); ++i)
  232. {
  233. cmMakefile* mf = m_LocalGenerators[i]->GetMakefile();
  234. // Get the source directory from the makefile
  235. std::string dir = mf->GetStartDirectory();
  236. // remove the home directory and / from the source directory
  237. // this gives a relative path
  238. cmSystemTools::ReplaceString(dir, homedir.c_str(), "");
  239. // Get the list of create dsp files names from the LocalGenerator, more
  240. // than one dsp could have been created per input CMakeLists.txt file
  241. // for each target
  242. std::vector<std::string> dspnames =
  243. static_cast<cmLocalVisualStudio6Generator *>(m_LocalGenerators[i])
  244. ->GetCreatedProjectNames();
  245. cmTargets &tgts = m_LocalGenerators[i]->GetMakefile()->GetTargets();
  246. cmTargets::iterator l = tgts.begin();
  247. for(std::vector<std::string>::iterator si = dspnames.begin();
  248. l != tgts.end(); ++l)
  249. {
  250. // special handling for the current makefile
  251. if(mf == m_LocalGenerators[0]->GetMakefile())
  252. {
  253. dir = "."; // no subdirectory for project generated
  254. // if this is the special ALL_BUILD utility, then
  255. // make it depend on every other non UTILITY project.
  256. // This is done by adding the names to the GetUtilities
  257. // vector on the makefile
  258. if(l->first == "ALL_BUILD")
  259. {
  260. unsigned int j;
  261. for(j = 0; j < m_LocalGenerators.size(); ++j)
  262. {
  263. const cmTargets &atgts =
  264. m_LocalGenerators[j]->GetMakefile()->GetTargets();
  265. for(cmTargets::const_iterator al = atgts.begin();
  266. al != atgts.end(); ++al)
  267. {
  268. if (al->second.IsInAll())
  269. {
  270. if (al->second.GetType() == cmTarget::UTILITY)
  271. {
  272. l->second.AddUtility(al->first.c_str());
  273. }
  274. else
  275. {
  276. l->second.AddLinkLibrary(al->first, cmTarget::GENERAL);
  277. }
  278. }
  279. }
  280. }
  281. }
  282. }
  283. // Write the project into the DSW file
  284. if (strncmp(l->first.c_str(), "INCLUDE_EXTERNAL_MSPROJECT", 26) == 0)
  285. {
  286. cmCustomCommand cc = l->second.GetCustomCommands()[0];
  287. // dodgy use of the cmCustomCommand's members to store the
  288. // arguments from the INCLUDE_EXTERNAL_MSPROJECT command
  289. std::vector<std::string> stuff = cc.GetDepends();
  290. std::vector<std::string> depends = cc.GetOutputs();
  291. this->WriteExternalProject(fout, stuff[0].c_str(), stuff[1].c_str(), depends);
  292. ++si;
  293. }
  294. else
  295. {
  296. if ((l->second.GetType() != cmTarget::INSTALL_FILES)
  297. && (l->second.GetType() != cmTarget::INSTALL_PROGRAMS))
  298. {
  299. this->WriteProject(fout, si->c_str(), dir.c_str(),l->second);
  300. ++si;
  301. }
  302. }
  303. }
  304. }
  305. // Write the footer for the DSW file
  306. this->WriteDSWFooter(fout);
  307. }
  308. // Write a dsp file into the DSW file,
  309. // Note, that dependencies from executables to
  310. // the libraries it uses are also done here
  311. void cmGlobalVisualStudio6Generator::WriteProject(std::ostream& fout,
  312. const char* dspname,
  313. const char* dir,
  314. const cmTarget& target)
  315. {
  316. fout << "#########################################################"
  317. "######################\n\n";
  318. fout << "Project: \"" << dspname << "\"="
  319. << dir << "\\" << dspname << ".dsp - Package Owner=<4>\n\n";
  320. fout << "Package=<5>\n{{{\n}}}\n\n";
  321. fout << "Package=<4>\n";
  322. fout << "{{{\n";
  323. // insert Begin Project Dependency Project_Dep_Name project stuff here
  324. if (target.GetType() != cmTarget::STATIC_LIBRARY)
  325. {
  326. cmTarget::LinkLibraries::const_iterator j, jend;
  327. j = target.GetLinkLibraries().begin();
  328. jend = target.GetLinkLibraries().end();
  329. for(;j!= jend; ++j)
  330. {
  331. if(j->first != dspname)
  332. {
  333. // is the library part of this DSW ? If so add dependency
  334. std::string libPath = j->first + "_CMAKE_PATH";
  335. const char* cacheValue
  336. = m_CMakeInstance->GetCacheDefinition(libPath.c_str());
  337. if(cacheValue)
  338. {
  339. fout << "Begin Project Dependency\n";
  340. fout << "Project_Dep_Name " << j->first << "\n";
  341. fout << "End Project Dependency\n";
  342. }
  343. }
  344. }
  345. }
  346. std::set<cmStdString>::const_iterator i, end;
  347. // write utility dependencies.
  348. i = target.GetUtilities().begin();
  349. end = target.GetUtilities().end();
  350. for(;i!= end; ++i)
  351. {
  352. if(*i != dspname)
  353. {
  354. fout << "Begin Project Dependency\n";
  355. fout << "Project_Dep_Name " << *i << "\n";
  356. fout << "End Project Dependency\n";
  357. }
  358. }
  359. fout << "}}}\n\n";
  360. }
  361. // Write a dsp file into the DSW file,
  362. // Note, that dependencies from executables to
  363. // the libraries it uses are also done here
  364. void cmGlobalVisualStudio6Generator::WriteExternalProject(std::ostream& fout,
  365. const char* name,
  366. const char* location,
  367. const std::vector<std::string>& dependencies)
  368. {
  369. fout << "#########################################################"
  370. "######################\n\n";
  371. fout << "Project: \"" << name << "\"="
  372. << location << " - Package Owner=<4>\n\n";
  373. fout << "Package=<5>\n{{{\n}}}\n\n";
  374. fout << "Package=<4>\n";
  375. fout << "{{{\n";
  376. std::vector<std::string>::const_iterator i, end;
  377. // write dependencies.
  378. i = dependencies.begin();
  379. end = dependencies.end();
  380. for(;i!= end; ++i)
  381. {
  382. fout << "Begin Project Dependency\n";
  383. fout << "Project_Dep_Name " << *i << "\n";
  384. fout << "End Project Dependency\n";
  385. }
  386. fout << "}}}\n\n";
  387. }
  388. // Standard end of dsw file
  389. void cmGlobalVisualStudio6Generator::WriteDSWFooter(std::ostream& fout)
  390. {
  391. fout << "######################################################"
  392. "#########################\n\n";
  393. fout << "Global:\n\n";
  394. fout << "Package=<5>\n{{{\n}}}\n\n";
  395. fout << "Package=<3>\n{{{\n}}}\n\n";
  396. fout << "#####################################################"
  397. "##########################\n\n";
  398. }
  399. // ouput standard header for dsw file
  400. void cmGlobalVisualStudio6Generator::WriteDSWHeader(std::ostream& fout)
  401. {
  402. fout << "Microsoft Developer Studio Workspace File, Format Version 6.00\n";
  403. fout << "# WARNING: DO NOT EDIT OR DELETE THIS WORKSPACE FILE!\n\n";
  404. }