cmGlobalVisualStudio6Generator.cxx 14 KB

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