cmGlobalVisualStudio6Generator.cxx 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540
  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. // RunSingleCommand 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 += " - ";
  109. if ( m_LocalGenerators[0]->GetMakefile()->GetDefinition("CMAKE_TRY_COMPILE_CONFIGURATION") )
  110. {
  111. makeCommand +=
  112. m_LocalGenerators[0]->GetMakefile()->GetDefinition("CMAKE_TRY_COMPILE_CONFIGURATION");
  113. }
  114. else
  115. {
  116. makeCommand += "Debug";
  117. }
  118. makeCommand += "\"";
  119. int retVal;
  120. int timeout = cmGlobalGenerator::s_TryCompileTimeout;
  121. if (!cmSystemTools::RunSingleCommand(makeCommand.c_str(), output,
  122. &retVal, 0, false, timeout))
  123. {
  124. std::string e = "Error executing make program \"";
  125. e += originalCommand;
  126. e += "\" specified by CMAKE_MAKE_PROGRAM cache entry. ";
  127. e += "The command string used was \"";
  128. e += makeCommand.c_str();
  129. e += "\".";
  130. cmSystemTools::Error(e.c_str());
  131. // return to the original directory
  132. cmSystemTools::ChangeDirectory(cwd.c_str());
  133. return 1;
  134. }
  135. cmSystemTools::ChangeDirectory(cwd.c_str());
  136. return retVal;
  137. }
  138. ///! Create a local generator appropriate to this Global Generator
  139. cmLocalGenerator *cmGlobalVisualStudio6Generator::CreateLocalGenerator()
  140. {
  141. cmLocalGenerator *lg = new cmLocalVisualStudio6Generator;
  142. lg->SetGlobalGenerator(this);
  143. return lg;
  144. }
  145. void cmGlobalVisualStudio6Generator::Generate()
  146. {
  147. // collect sub-projects
  148. this->CollectSubprojects();
  149. // add a special target that depends on ALL projects for easy build
  150. // of one configuration only.
  151. std::vector<std::string> srcs;
  152. std::map<cmStdString, std::vector<cmLocalGenerator*> >::iterator it;
  153. for(it = m_SubProjectMap.begin(); it!= m_SubProjectMap.end(); ++it)
  154. {
  155. std::vector<cmLocalGenerator*>& gen = it->second;
  156. // add the ALL_BUILD to the first local generator of each project
  157. if(gen.size())
  158. {
  159. gen[0]->GetMakefile()->
  160. AddUtilityCommand("ALL_BUILD", "echo","\"Build all projects\"",false,srcs);
  161. }
  162. }
  163. // add the Run Tests command
  164. this->SetupTests();
  165. // first do the superclass method
  166. this->cmGlobalGenerator::Generate();
  167. // Now write out the DSW
  168. this->OutputDSWFile();
  169. }
  170. // populate the m_SubProjectMap
  171. void cmGlobalVisualStudio6Generator::CollectSubprojects()
  172. {
  173. unsigned int i;
  174. for(i = 0; i < m_LocalGenerators.size(); ++i)
  175. {
  176. std::string name = m_LocalGenerators[i]->GetMakefile()->GetProjectName();
  177. m_SubProjectMap[name].push_back(m_LocalGenerators[i]);
  178. std::vector<std::string> const& pprojects
  179. = m_LocalGenerators[i]->GetMakefile()->GetParentProjects();
  180. for(unsigned int k =0; k < pprojects.size(); ++k)
  181. {
  182. m_SubProjectMap[pprojects[k]].push_back(m_LocalGenerators[i]);
  183. }
  184. }
  185. }
  186. // Write a DSW file to the stream
  187. void cmGlobalVisualStudio6Generator::WriteDSWFile(std::ostream& fout,
  188. std::vector<cmLocalGenerator*>& generators)
  189. {
  190. // Write out the header for a DSW file
  191. this->WriteDSWHeader(fout);
  192. // Get the home directory with the trailing slash
  193. std::string homedir = m_CMakeInstance->GetHomeDirectory();
  194. homedir += "/";
  195. unsigned int i;
  196. bool doneAllBuild = false;
  197. bool doneRunTests = false;
  198. for(i = 0; i < generators.size(); ++i)
  199. {
  200. cmMakefile* mf = generators[i]->GetMakefile();
  201. // Get the source directory from the makefile
  202. std::string dir = mf->GetStartDirectory();
  203. // remove the home directory and / from the source directory
  204. // this gives a relative path
  205. cmSystemTools::ReplaceString(dir, homedir.c_str(), "");
  206. // Get the list of create dsp files names from the LocalGenerator, more
  207. // than one dsp could have been created per input CMakeLists.txt file
  208. // for each target
  209. std::vector<std::string> dspnames =
  210. static_cast<cmLocalVisualStudio6Generator *>(generators[i])
  211. ->GetCreatedProjectNames();
  212. cmTargets &tgts = generators[i]->GetMakefile()->GetTargets();
  213. cmTargets::iterator l = tgts.begin();
  214. for(std::vector<std::string>::iterator si = dspnames.begin();
  215. l != tgts.end(); ++l)
  216. {
  217. // special handling for the current makefile
  218. if(mf == generators[0]->GetMakefile())
  219. {
  220. dir = "."; // no subdirectory for project generated
  221. // if this is the special ALL_BUILD utility, then
  222. // make it depend on every other non UTILITY project.
  223. // This is done by adding the names to the GetUtilities
  224. // vector on the makefile
  225. if(l->first == "ALL_BUILD" && !doneAllBuild)
  226. {
  227. unsigned int j;
  228. for(j = 0; j < generators.size(); ++j)
  229. {
  230. const cmTargets &atgts =
  231. generators[j]->GetMakefile()->GetTargets();
  232. for(cmTargets::const_iterator al = atgts.begin();
  233. al != atgts.end(); ++al)
  234. {
  235. if (al->second.IsInAll())
  236. {
  237. if (al->second.GetType() == cmTarget::UTILITY)
  238. {
  239. l->second.AddUtility(al->first.c_str());
  240. }
  241. else
  242. {
  243. l->second.AddLinkLibrary(al->first, cmTarget::GENERAL);
  244. }
  245. }
  246. }
  247. }
  248. }
  249. }
  250. // Write the project into the DSW file
  251. if (strncmp(l->first.c_str(), "INCLUDE_EXTERNAL_MSPROJECT", 26) == 0)
  252. {
  253. cmCustomCommand cc = l->second.GetPostBuildCommands()[0];
  254. // dodgy use of the cmCustomCommand's members to store the
  255. // arguments from the INCLUDE_EXTERNAL_MSPROJECT command
  256. std::vector<std::string> stuff = cc.GetDepends();
  257. std::vector<std::string> depends;
  258. depends.push_back(cc.GetOutput());
  259. this->WriteExternalProject(fout, stuff[0].c_str(), stuff[1].c_str(), depends);
  260. ++si;
  261. }
  262. else
  263. {
  264. if ((l->second.GetType() != cmTarget::INSTALL_FILES)
  265. && (l->second.GetType() != cmTarget::INSTALL_PROGRAMS))
  266. {
  267. bool skip = false;
  268. // skip ALL_BUILD and RUN_TESTS if they have already been added
  269. if(l->first == "ALL_BUILD" )
  270. {
  271. if(doneAllBuild)
  272. {
  273. skip = true;
  274. }
  275. else
  276. {
  277. doneAllBuild = true;
  278. }
  279. }
  280. if(l->first == "RUN_TESTS")
  281. {
  282. if(doneRunTests)
  283. {
  284. skip = true;
  285. }
  286. else
  287. {
  288. doneRunTests = true;
  289. }
  290. }
  291. if(!skip)
  292. {
  293. this->WriteProject(fout, si->c_str(), dir.c_str(),l->second);
  294. }
  295. ++si;
  296. }
  297. }
  298. }
  299. }
  300. // Write the footer for the DSW file
  301. this->WriteDSWFooter(fout);
  302. }
  303. void cmGlobalVisualStudio6Generator::OutputDSWFile(std::vector<cmLocalGenerator*>& generators)
  304. {
  305. if(generators.size() == 0)
  306. {
  307. return;
  308. }
  309. std::string fname = generators[0]->GetMakefile()->GetStartOutputDirectory();
  310. fname += "/";
  311. fname += generators[0]->GetMakefile()->GetProjectName();
  312. fname += ".dsw";
  313. std::ofstream fout(fname.c_str());
  314. if(!fout)
  315. {
  316. cmSystemTools::Error("Error can not open DSW file for write: ",
  317. fname.c_str());
  318. return;
  319. }
  320. this->WriteDSWFile(fout, generators);
  321. }
  322. // output the DSW file
  323. void cmGlobalVisualStudio6Generator::OutputDSWFile()
  324. {
  325. std::map<cmStdString, std::vector<cmLocalGenerator*> >::iterator it;
  326. for(it = m_SubProjectMap.begin(); it!= m_SubProjectMap.end(); ++it)
  327. {
  328. this->OutputDSWFile(it->second);
  329. }
  330. }
  331. inline std::string removeQuotes(const std::string& s)
  332. {
  333. if(s[0] == '\"' && s[s.size()-1] == '\"')
  334. {
  335. return s.substr(1, s.size()-2);
  336. }
  337. return s;
  338. }
  339. void cmGlobalVisualStudio6Generator::SetupTests()
  340. {
  341. std::string ctest =
  342. m_LocalGenerators[0]->GetMakefile()->GetDefinition("CMAKE_COMMAND");
  343. ctest = removeQuotes(ctest);
  344. ctest = cmSystemTools::GetFilenamePath(ctest.c_str());
  345. ctest += "/";
  346. ctest += "ctest";
  347. ctest += cmSystemTools::GetExecutableExtension();
  348. if(!cmSystemTools::FileExists(ctest.c_str()))
  349. {
  350. ctest =
  351. m_LocalGenerators[0]->GetMakefile()->GetDefinition("CMAKE_COMMAND");
  352. ctest = cmSystemTools::GetFilenamePath(ctest.c_str());
  353. ctest += "/Debug/";
  354. ctest += "ctest";
  355. ctest += cmSystemTools::GetExecutableExtension();
  356. }
  357. if(!cmSystemTools::FileExists(ctest.c_str()))
  358. {
  359. ctest =
  360. m_LocalGenerators[0]->GetMakefile()->GetDefinition("CMAKE_COMMAND");
  361. ctest = cmSystemTools::GetFilenamePath(ctest.c_str());
  362. ctest += "/Release/";
  363. ctest += "ctest";
  364. ctest += cmSystemTools::GetExecutableExtension();
  365. }
  366. // if we found ctest
  367. if (cmSystemTools::FileExists(ctest.c_str()))
  368. {
  369. // Create a full path filename for output Testfile
  370. std::string fname;
  371. fname = m_CMakeInstance->GetStartOutputDirectory();
  372. fname += "/";
  373. fname += "DartTestfile.txt";
  374. // If the file doesn't exist, then ENABLE_TESTING hasn't been run
  375. if (cmSystemTools::FileExists(fname.c_str()))
  376. {
  377. std::vector<std::string> srcs;
  378. std::map<cmStdString, std::vector<cmLocalGenerator*> >::iterator it;
  379. for(it = m_SubProjectMap.begin(); it!= m_SubProjectMap.end(); ++it)
  380. {
  381. std::vector<cmLocalGenerator*>& gen = it->second;
  382. // add the ALL_BUILD to the first local generator of each project
  383. if(gen.size())
  384. {
  385. gen[0]->GetMakefile()->
  386. AddUtilityCommand("RUN_TESTS", ctest.c_str(), "-D $(IntDir)",false,srcs);
  387. }
  388. }
  389. }
  390. }
  391. }
  392. // Write a dsp file into the DSW file,
  393. // Note, that dependencies from executables to
  394. // the libraries it uses are also done here
  395. void cmGlobalVisualStudio6Generator::WriteProject(std::ostream& fout,
  396. const char* dspname,
  397. const char* dir,
  398. const cmTarget& target)
  399. {
  400. fout << "#########################################################"
  401. "######################\n\n";
  402. fout << "Project: \"" << dspname << "\"="
  403. << dir << "\\" << dspname << ".dsp - Package Owner=<4>\n\n";
  404. fout << "Package=<5>\n{{{\n}}}\n\n";
  405. fout << "Package=<4>\n";
  406. fout << "{{{\n";
  407. // insert Begin Project Dependency Project_Dep_Name project stuff here
  408. if (target.GetType() != cmTarget::STATIC_LIBRARY)
  409. {
  410. cmTarget::LinkLibraries::const_iterator j, jend;
  411. j = target.GetLinkLibraries().begin();
  412. jend = target.GetLinkLibraries().end();
  413. for(;j!= jend; ++j)
  414. {
  415. if(j->first != dspname)
  416. {
  417. // is the library part of this DSW ? If so add dependency
  418. std::string libPath = j->first + "_CMAKE_PATH";
  419. const char* cacheValue
  420. = m_CMakeInstance->GetCacheDefinition(libPath.c_str());
  421. if(cacheValue && *cacheValue)
  422. {
  423. fout << "Begin Project Dependency\n";
  424. fout << "Project_Dep_Name " << j->first << "\n";
  425. fout << "End Project Dependency\n";
  426. }
  427. }
  428. }
  429. }
  430. std::set<cmStdString>::const_iterator i, end;
  431. // write utility dependencies.
  432. i = target.GetUtilities().begin();
  433. end = target.GetUtilities().end();
  434. for(;i!= end; ++i)
  435. {
  436. if(*i != dspname)
  437. {
  438. fout << "Begin Project Dependency\n";
  439. fout << "Project_Dep_Name " << *i << "\n";
  440. fout << "End Project Dependency\n";
  441. }
  442. }
  443. fout << "}}}\n\n";
  444. }
  445. // Write a dsp file into the DSW file,
  446. // Note, that dependencies from executables to
  447. // the libraries it uses are also done here
  448. void cmGlobalVisualStudio6Generator::WriteExternalProject(std::ostream& fout,
  449. const char* name,
  450. const char* location,
  451. const std::vector<std::string>& dependencies)
  452. {
  453. fout << "#########################################################"
  454. "######################\n\n";
  455. fout << "Project: \"" << name << "\"="
  456. << location << " - Package Owner=<4>\n\n";
  457. fout << "Package=<5>\n{{{\n}}}\n\n";
  458. fout << "Package=<4>\n";
  459. fout << "{{{\n";
  460. std::vector<std::string>::const_iterator i, end;
  461. // write dependencies.
  462. i = dependencies.begin();
  463. end = dependencies.end();
  464. for(;i!= end; ++i)
  465. {
  466. fout << "Begin Project Dependency\n";
  467. fout << "Project_Dep_Name " << *i << "\n";
  468. fout << "End Project Dependency\n";
  469. }
  470. fout << "}}}\n\n";
  471. }
  472. // Standard end of dsw file
  473. void cmGlobalVisualStudio6Generator::WriteDSWFooter(std::ostream& fout)
  474. {
  475. fout << "######################################################"
  476. "#########################\n\n";
  477. fout << "Global:\n\n";
  478. fout << "Package=<5>\n{{{\n}}}\n\n";
  479. fout << "Package=<3>\n{{{\n}}}\n\n";
  480. fout << "#####################################################"
  481. "##########################\n\n";
  482. }
  483. // ouput standard header for dsw file
  484. void cmGlobalVisualStudio6Generator::WriteDSWHeader(std::ostream& fout)
  485. {
  486. fout << "Microsoft Developer Studio Workspace File, Format Version 6.00\n";
  487. fout << "# WARNING: DO NOT EDIT OR DELETE THIS WORKSPACE FILE!\n\n";
  488. }
  489. //----------------------------------------------------------------------------
  490. void cmGlobalVisualStudio6Generator::GetDocumentation(cmDocumentationEntry& entry) const
  491. {
  492. entry.name = this->GetName();
  493. entry.brief = "Generates Visual Studio 6 project files.";
  494. entry.full = "";
  495. }