cmGlobalVisualStudio6Generator.cxx 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570
  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(std::vector<std::string>const& 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. mf->AddDefinition("CMAKE_GENERATOR_RC", "rc");
  28. mf->AddDefinition("CMAKE_GENERATOR_NO_COMPILER_ENV", "1");
  29. mf->AddDefinition("CMAKE_GENERATOR_Fortran", "ifort");
  30. this->GenerateConfigurations(mf);
  31. this->cmGlobalGenerator::EnableLanguage(lang, mf);
  32. }
  33. void cmGlobalVisualStudio6Generator::GenerateConfigurations(cmMakefile* mf)
  34. {
  35. std::string fname= mf->GetRequiredDefinition("CMAKE_ROOT");
  36. const char* def= mf->GetDefinition( "MSPROJECT_TEMPLATE_DIRECTORY");
  37. if(def)
  38. {
  39. fname = def;
  40. }
  41. else
  42. {
  43. fname += "/Templates";
  44. }
  45. fname += "/CMakeVisualStudio6Configurations.cmake";
  46. if(!mf->ReadListFile(mf->GetCurrentListFile(), fname.c_str()))
  47. {
  48. cmSystemTools::Error("Cannot open ", fname.c_str(),
  49. ". Please copy this file from the main "
  50. "CMake/Templates directory and edit it for "
  51. "your build configurations.");
  52. }
  53. else if(!mf->GetDefinition("CMAKE_CONFIGURATION_TYPES"))
  54. {
  55. cmSystemTools::Error("CMAKE_CONFIGURATION_TYPES not set by ",
  56. fname.c_str(),
  57. ". Please copy this file from the main "
  58. "CMake/Templates directory and edit it for "
  59. "your build configurations.");
  60. }
  61. }
  62. int cmGlobalVisualStudio6Generator::TryCompile(const char *,
  63. const char *bindir,
  64. const char *projectName,
  65. const char *targetName,
  66. std::string *output,
  67. cmMakefile* mf)
  68. {
  69. // now build the test
  70. std::string makeCommand =
  71. m_CMakeInstance->GetCacheManager()->GetCacheValue("CMAKE_MAKE_PROGRAM");
  72. std::vector<std::string> mp;
  73. mp.push_back("[HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\VisualStudio\\6.0\\Setup;VsCommonDir]/MSDev98/Bin");
  74. cmSystemTools::ExpandRegistryValues(mp[0]);
  75. std::string originalCommand = makeCommand;
  76. makeCommand = cmSystemTools::FindProgram(makeCommand.c_str(), mp);
  77. if(makeCommand.size() == 0)
  78. {
  79. std::string e = "Generator cannot find Visual Studio 6 msdev program \"";
  80. e += originalCommand;
  81. e += "\" specified by CMAKE_MAKE_PROGRAM cache entry. ";
  82. e += "Please fix the setting.";
  83. cmSystemTools::Error(e.c_str());
  84. return 1;
  85. }
  86. makeCommand = cmSystemTools::ConvertToOutputPath(makeCommand.c_str());
  87. /**
  88. * Run an executable command and put the stdout in output.
  89. */
  90. std::string cwd = cmSystemTools::GetCurrentWorkingDirectory();
  91. cmSystemTools::ChangeDirectory(bindir);
  92. // if there are spaces in the makeCommand, assume a full path
  93. // and convert it to a path with no spaces in it as the
  94. // RunSingleCommand does not like spaces
  95. #if defined(_WIN32) && !defined(__CYGWIN__)
  96. if(makeCommand.find(' ') != std::string::npos)
  97. {
  98. cmSystemTools::GetShortPath(makeCommand.c_str(), makeCommand);
  99. }
  100. #endif
  101. makeCommand += " ";
  102. makeCommand += projectName;
  103. makeCommand += ".dsw /MAKE \"";
  104. if (targetName)
  105. {
  106. makeCommand += targetName;
  107. }
  108. else
  109. {
  110. makeCommand += "ALL_BUILD";
  111. }
  112. makeCommand += " - ";
  113. if(const char* config = mf->GetDefinition("CMAKE_TRY_COMPILE_CONFIGURATION"))
  114. {
  115. makeCommand += config;
  116. }
  117. else
  118. {
  119. makeCommand += "Debug";
  120. }
  121. makeCommand += "\"";
  122. int retVal;
  123. int timeout = cmGlobalGenerator::s_TryCompileTimeout;
  124. if (!cmSystemTools::RunSingleCommand(makeCommand.c_str(), output,
  125. &retVal, 0, false, timeout))
  126. {
  127. std::string e = "Error executing make program \"";
  128. e += originalCommand;
  129. e += "\" specified by CMAKE_MAKE_PROGRAM cache entry. ";
  130. e += "The command string used was \"";
  131. e += makeCommand.c_str();
  132. e += "\".";
  133. cmSystemTools::Error(e.c_str());
  134. // return to the original directory
  135. cmSystemTools::ChangeDirectory(cwd.c_str());
  136. return 1;
  137. }
  138. cmSystemTools::ChangeDirectory(cwd.c_str());
  139. return retVal;
  140. }
  141. ///! Create a local generator appropriate to this Global Generator
  142. cmLocalGenerator *cmGlobalVisualStudio6Generator::CreateLocalGenerator()
  143. {
  144. cmLocalGenerator *lg = new cmLocalVisualStudio6Generator;
  145. lg->SetGlobalGenerator(this);
  146. return lg;
  147. }
  148. void cmGlobalVisualStudio6Generator::Generate()
  149. {
  150. // collect sub-projects
  151. this->CollectSubprojects();
  152. // add a special target that depends on ALL projects for easy build
  153. // of one configuration only.
  154. std::vector<std::string> srcs;
  155. std::map<cmStdString, std::vector<cmLocalGenerator*> >::iterator it;
  156. for(it = m_SubProjectMap.begin(); it!= m_SubProjectMap.end(); ++it)
  157. {
  158. std::vector<cmLocalGenerator*>& gen = it->second;
  159. // add the ALL_BUILD to the first local generator of each project
  160. if(gen.size())
  161. {
  162. gen[0]->GetMakefile()->
  163. AddUtilityCommand("ALL_BUILD", "echo","\"Build all projects\"",false,srcs);
  164. std::string cmake_command =
  165. m_LocalGenerators[0]->GetMakefile()->GetRequiredDefinition("CMAKE_COMMAND");
  166. gen[0]->GetMakefile()->
  167. AddUtilityCommand("INSTALL", cmake_command.c_str(),
  168. "-DBUILD_TYPE=$(IntDir) -P cmake_install.cmake",false,srcs);
  169. }
  170. }
  171. // add the Run Tests command
  172. this->SetupTests();
  173. // first do the superclass method
  174. this->cmGlobalGenerator::Generate();
  175. // Now write out the DSW
  176. this->OutputDSWFile();
  177. }
  178. // populate the m_SubProjectMap
  179. void cmGlobalVisualStudio6Generator::CollectSubprojects()
  180. {
  181. unsigned int i;
  182. for(i = 0; i < m_LocalGenerators.size(); ++i)
  183. {
  184. std::string name = m_LocalGenerators[i]->GetMakefile()->GetProjectName();
  185. m_SubProjectMap[name].push_back(m_LocalGenerators[i]);
  186. std::vector<std::string> const& pprojects
  187. = m_LocalGenerators[i]->GetMakefile()->GetParentProjects();
  188. for(unsigned int k =0; k < pprojects.size(); ++k)
  189. {
  190. m_SubProjectMap[pprojects[k]].push_back(m_LocalGenerators[i]);
  191. }
  192. }
  193. }
  194. // Write a DSW file to the stream
  195. void cmGlobalVisualStudio6Generator::WriteDSWFile(std::ostream& fout,
  196. cmLocalGenerator* root,
  197. std::vector<cmLocalGenerator*>& generators)
  198. {
  199. // Write out the header for a DSW file
  200. this->WriteDSWHeader(fout);
  201. // Get the home directory with the trailing slash
  202. std::string homedir = root->GetMakefile()->GetCurrentDirectory();
  203. homedir += "/";
  204. unsigned int i;
  205. bool doneAllBuild = false;
  206. bool doneRunTests = false;
  207. bool doneInstall = false;
  208. for(i = 0; i < generators.size(); ++i)
  209. {
  210. if(this->IsExcluded(root, generators[i]))
  211. {
  212. continue;
  213. }
  214. cmMakefile* mf = generators[i]->GetMakefile();
  215. // Get the source directory from the makefile
  216. std::string dir = mf->GetStartDirectory();
  217. // remove the home directory and / from the source directory
  218. // this gives a relative path
  219. cmSystemTools::ReplaceString(dir, homedir.c_str(), "");
  220. // Get the list of create dsp files names from the LocalGenerator, more
  221. // than one dsp could have been created per input CMakeLists.txt file
  222. // for each target
  223. std::vector<std::string> dspnames =
  224. static_cast<cmLocalVisualStudio6Generator *>(generators[i])
  225. ->GetCreatedProjectNames();
  226. cmTargets &tgts = generators[i]->GetMakefile()->GetTargets();
  227. cmTargets::iterator l = tgts.begin();
  228. for(std::vector<std::string>::iterator si = dspnames.begin();
  229. l != tgts.end(); ++l)
  230. {
  231. // special handling for the current makefile
  232. if(mf == generators[0]->GetMakefile())
  233. {
  234. dir = "."; // no subdirectory for project generated
  235. // if this is the special ALL_BUILD utility, then
  236. // make it depend on every other non UTILITY project.
  237. // This is done by adding the names to the GetUtilities
  238. // vector on the makefile
  239. if(l->first == "ALL_BUILD" && !doneAllBuild)
  240. {
  241. unsigned int j;
  242. for(j = 0; j < generators.size(); ++j)
  243. {
  244. const cmTargets &atgts =
  245. generators[j]->GetMakefile()->GetTargets();
  246. for(cmTargets::const_iterator al = atgts.begin();
  247. al != atgts.end(); ++al)
  248. {
  249. if (al->second.IsInAll())
  250. {
  251. if (al->second.GetType() == cmTarget::UTILITY)
  252. {
  253. l->second.AddUtility(al->first.c_str());
  254. }
  255. else
  256. {
  257. l->second.AddLinkLibrary(al->first, cmTarget::GENERAL);
  258. }
  259. }
  260. }
  261. }
  262. }
  263. }
  264. // Write the project into the DSW file
  265. if (strncmp(l->first.c_str(), "INCLUDE_EXTERNAL_MSPROJECT", 26) == 0)
  266. {
  267. cmCustomCommand cc = l->second.GetPostBuildCommands()[0];
  268. std::string project = cc.GetCommand();
  269. std::string location = cc.GetArguments();
  270. this->WriteExternalProject(fout, project.c_str(), location.c_str(), cc.GetDepends());
  271. }
  272. else
  273. {
  274. if ((l->second.GetType() != cmTarget::INSTALL_FILES)
  275. && (l->second.GetType() != cmTarget::INSTALL_PROGRAMS))
  276. {
  277. bool skip = false;
  278. // skip ALL_BUILD and RUN_TESTS if they have already been added
  279. if(l->first == "ALL_BUILD" )
  280. {
  281. if(doneAllBuild)
  282. {
  283. skip = true;
  284. }
  285. else
  286. {
  287. doneAllBuild = true;
  288. }
  289. }
  290. if(l->first == "INSTALL")
  291. {
  292. if(doneInstall)
  293. {
  294. skip = true;
  295. }
  296. else
  297. {
  298. doneInstall = true;
  299. }
  300. }
  301. if(l->first == "RUN_TESTS")
  302. {
  303. if(doneRunTests)
  304. {
  305. skip = true;
  306. }
  307. else
  308. {
  309. doneRunTests = true;
  310. }
  311. }
  312. if(!skip)
  313. {
  314. this->WriteProject(fout, si->c_str(), dir.c_str(),l->second);
  315. }
  316. ++si;
  317. }
  318. }
  319. }
  320. }
  321. // Write the footer for the DSW file
  322. this->WriteDSWFooter(fout);
  323. }
  324. void cmGlobalVisualStudio6Generator::OutputDSWFile(cmLocalGenerator* root,
  325. std::vector<cmLocalGenerator*>& generators)
  326. {
  327. if(generators.size() == 0)
  328. {
  329. return;
  330. }
  331. std::string fname = root->GetMakefile()->GetStartOutputDirectory();
  332. fname += "/";
  333. fname += root->GetMakefile()->GetProjectName();
  334. fname += ".dsw";
  335. std::ofstream fout(fname.c_str());
  336. if(!fout)
  337. {
  338. cmSystemTools::Error("Error can not open DSW file for write: ",
  339. fname.c_str());
  340. cmSystemTools::ReportLastSystemError("");
  341. return;
  342. }
  343. this->WriteDSWFile(fout, root, generators);
  344. }
  345. // output the DSW file
  346. void cmGlobalVisualStudio6Generator::OutputDSWFile()
  347. {
  348. std::map<cmStdString, std::vector<cmLocalGenerator*> >::iterator it;
  349. for(it = m_SubProjectMap.begin(); it!= m_SubProjectMap.end(); ++it)
  350. {
  351. this->OutputDSWFile(it->second[0], it->second);
  352. }
  353. }
  354. inline std::string removeQuotes(const std::string& s)
  355. {
  356. if(s[0] == '\"' && s[s.size()-1] == '\"')
  357. {
  358. return s.substr(1, s.size()-2);
  359. }
  360. return s;
  361. }
  362. void cmGlobalVisualStudio6Generator::SetupTests()
  363. {
  364. std::string ctest =
  365. m_LocalGenerators[0]->GetMakefile()->GetRequiredDefinition("CMAKE_COMMAND");
  366. ctest = removeQuotes(ctest);
  367. ctest = cmSystemTools::GetFilenamePath(ctest.c_str());
  368. ctest += "/";
  369. ctest += "ctest";
  370. ctest += cmSystemTools::GetExecutableExtension();
  371. if(!cmSystemTools::FileExists(ctest.c_str()))
  372. {
  373. ctest =
  374. m_LocalGenerators[0]->GetMakefile()->GetRequiredDefinition("CMAKE_COMMAND");
  375. ctest = cmSystemTools::GetFilenamePath(ctest.c_str());
  376. ctest += "/Debug/";
  377. ctest += "ctest";
  378. ctest += cmSystemTools::GetExecutableExtension();
  379. }
  380. if(!cmSystemTools::FileExists(ctest.c_str()))
  381. {
  382. ctest =
  383. m_LocalGenerators[0]->GetMakefile()->GetRequiredDefinition("CMAKE_COMMAND");
  384. ctest = cmSystemTools::GetFilenamePath(ctest.c_str());
  385. ctest += "/Release/";
  386. ctest += "ctest";
  387. ctest += cmSystemTools::GetExecutableExtension();
  388. }
  389. // if we found ctest
  390. if (cmSystemTools::FileExists(ctest.c_str()))
  391. {
  392. // Create a full path filename for output Testfile
  393. std::string fname;
  394. fname = m_CMakeInstance->GetStartOutputDirectory();
  395. fname += "/";
  396. fname += "DartTestfile.txt";
  397. // If the file doesn't exist, then ENABLE_TESTING hasn't been run
  398. if (cmSystemTools::FileExists(fname.c_str()))
  399. {
  400. std::vector<std::string> srcs;
  401. std::map<cmStdString, std::vector<cmLocalGenerator*> >::iterator it;
  402. for(it = m_SubProjectMap.begin(); it!= m_SubProjectMap.end(); ++it)
  403. {
  404. std::vector<cmLocalGenerator*>& gen = it->second;
  405. // add the ALL_BUILD to the first local generator of each project
  406. if(gen.size())
  407. {
  408. gen[0]->GetMakefile()->
  409. AddUtilityCommand("RUN_TESTS", ctest.c_str(), "-C $(IntDir)",false,srcs);
  410. }
  411. }
  412. }
  413. }
  414. }
  415. // Write a dsp file into the DSW file,
  416. // Note, that dependencies from executables to
  417. // the libraries it uses are also done here
  418. void cmGlobalVisualStudio6Generator::WriteProject(std::ostream& fout,
  419. const char* dspname,
  420. const char* dir,
  421. const cmTarget& target)
  422. {
  423. fout << "#########################################################"
  424. "######################\n\n";
  425. fout << "Project: \"" << dspname << "\"="
  426. << dir << "\\" << dspname << ".dsp - Package Owner=<4>\n\n";
  427. fout << "Package=<5>\n{{{\n}}}\n\n";
  428. fout << "Package=<4>\n";
  429. fout << "{{{\n";
  430. // insert Begin Project Dependency Project_Dep_Name project stuff here
  431. if (target.GetType() != cmTarget::STATIC_LIBRARY)
  432. {
  433. cmTarget::LinkLibraries::const_iterator j, jend;
  434. j = target.GetLinkLibraries().begin();
  435. jend = target.GetLinkLibraries().end();
  436. for(;j!= jend; ++j)
  437. {
  438. if(j->first != dspname)
  439. {
  440. // is the library part of this DSW ? If so add dependency
  441. std::string libPath = j->first + "_CMAKE_PATH";
  442. const char* cacheValue
  443. = m_CMakeInstance->GetCacheDefinition(libPath.c_str());
  444. if(cacheValue && *cacheValue)
  445. {
  446. fout << "Begin Project Dependency\n";
  447. fout << "Project_Dep_Name " << j->first.c_str() << "\n";
  448. fout << "End Project Dependency\n";
  449. }
  450. }
  451. }
  452. }
  453. std::set<cmStdString>::const_iterator i, end;
  454. // write utility dependencies.
  455. i = target.GetUtilities().begin();
  456. end = target.GetUtilities().end();
  457. for(;i!= end; ++i)
  458. {
  459. if(*i != dspname)
  460. {
  461. std::string depName = *i;
  462. if(strncmp(depName.c_str(), "INCLUDE_EXTERNAL_MSPROJECT", 26) == 0)
  463. {
  464. depName.erase(depName.begin(), depName.begin() + 27);
  465. }
  466. fout << "Begin Project Dependency\n";
  467. fout << "Project_Dep_Name " << depName << "\n";
  468. fout << "End Project Dependency\n";
  469. }
  470. }
  471. fout << "}}}\n\n";
  472. }
  473. // Write a dsp file into the DSW file,
  474. // Note, that dependencies from executables to
  475. // the libraries it uses are also done here
  476. void cmGlobalVisualStudio6Generator::WriteExternalProject(std::ostream& fout,
  477. const char* name,
  478. const char* location,
  479. const std::vector<std::string>& dependencies)
  480. {
  481. fout << "#########################################################"
  482. "######################\n\n";
  483. fout << "Project: \"" << name << "\"="
  484. << location << " - Package Owner=<4>\n\n";
  485. fout << "Package=<5>\n{{{\n}}}\n\n";
  486. fout << "Package=<4>\n";
  487. fout << "{{{\n";
  488. std::vector<std::string>::const_iterator i, end;
  489. // write dependencies.
  490. i = dependencies.begin();
  491. end = dependencies.end();
  492. for(;i!= end; ++i)
  493. {
  494. fout << "Begin Project Dependency\n";
  495. fout << "Project_Dep_Name " << *i << "\n";
  496. fout << "End Project Dependency\n";
  497. }
  498. fout << "}}}\n\n";
  499. }
  500. // Standard end of dsw file
  501. void cmGlobalVisualStudio6Generator::WriteDSWFooter(std::ostream& fout)
  502. {
  503. fout << "######################################################"
  504. "#########################\n\n";
  505. fout << "Global:\n\n";
  506. fout << "Package=<5>\n{{{\n}}}\n\n";
  507. fout << "Package=<3>\n{{{\n}}}\n\n";
  508. fout << "#####################################################"
  509. "##########################\n\n";
  510. }
  511. // ouput standard header for dsw file
  512. void cmGlobalVisualStudio6Generator::WriteDSWHeader(std::ostream& fout)
  513. {
  514. fout << "Microsoft Developer Studio Workspace File, Format Version 6.00\n";
  515. fout << "# WARNING: DO NOT EDIT OR DELETE THIS WORKSPACE FILE!\n\n";
  516. }
  517. //----------------------------------------------------------------------------
  518. void cmGlobalVisualStudio6Generator::GetDocumentation(cmDocumentationEntry& entry) const
  519. {
  520. entry.name = this->GetName();
  521. entry.brief = "Generates Visual Studio 6 project files.";
  522. entry.full = "";
  523. }