cmGlobalVisualStudio6Generator.cxx 13 KB

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