cmGlobalVisualStudio6Generator.cxx 13 KB

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