cmGlobalVisualStudio6Generator.cxx 18 KB

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