cmGlobalVisualStudio7Generator.cxx 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595
  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 "cmGlobalVisualStudio7Generator.h"
  14. #include "cmLocalVisualStudio7Generator.h"
  15. #include "cmMakefile.h"
  16. #include "cmake.h"
  17. #include "windows.h"
  18. cmGlobalVisualStudio7Generator::cmGlobalVisualStudio7Generator()
  19. {
  20. m_FindMakeProgramFile = "CMakeVS7FindMake.cmake";
  21. }
  22. void cmGlobalVisualStudio7Generator::EnableLanguage(const char* lang,
  23. cmMakefile *mf)
  24. {
  25. mf->AddDefinition("CMAKE_CFG_INTDIR","$(IntDir)");
  26. mf->AddDefinition("CMAKE_GENERATOR_CC", "cl");
  27. mf->AddDefinition("CMAKE_GENERATOR_CXX", "cl");
  28. // Create list of configurations requested by user's cache, if any.
  29. this->GenerateConfigurations(mf);
  30. this->cmGlobalGenerator::EnableLanguage(lang, mf);
  31. }
  32. int cmGlobalVisualStudio7Generator::TryCompile(const char *,
  33. const char *bindir,
  34. const char *projectName,
  35. const char *targetName,
  36. std::string *output)
  37. {
  38. // now build the test
  39. std::string makeCommand =
  40. m_CMakeInstance->GetCacheManager()->GetCacheValue("CMAKE_MAKE_PROGRAM");
  41. if(makeCommand.size() == 0)
  42. {
  43. cmSystemTools::Error(
  44. "Generator cannot find the appropriate make command.");
  45. return 1;
  46. }
  47. makeCommand = cmSystemTools::ConvertToOutputPath(makeCommand.c_str());
  48. std::string lowerCaseCommand = makeCommand;
  49. cmSystemTools::LowerCase(lowerCaseCommand);
  50. /**
  51. * Run an executable command and put the stdout in output.
  52. */
  53. std::string cwd = cmSystemTools::GetCurrentWorkingDirectory();
  54. cmSystemTools::ChangeDirectory(bindir);
  55. // if there are spaces in the makeCommand, assume a full path
  56. // and convert it to a path with no spaces in it as the
  57. // RunSingleCommand does not like spaces
  58. #if defined(_WIN32) && !defined(__CYGWIN__)
  59. if(makeCommand.find(' ') != std::string::npos)
  60. {
  61. cmSystemTools::GetShortPath(makeCommand.c_str(), makeCommand);
  62. }
  63. #endif
  64. makeCommand += " ";
  65. makeCommand += projectName;
  66. makeCommand += ".sln /build Debug /project ";
  67. if (targetName)
  68. {
  69. makeCommand += targetName;
  70. }
  71. else
  72. {
  73. makeCommand += "ALL_BUILD";
  74. }
  75. int retVal;
  76. int timeout = cmGlobalGenerator::s_TryCompileTimeout;
  77. if (!cmSystemTools::RunSingleCommand(makeCommand.c_str(), output, &retVal,
  78. 0, false, timeout))
  79. {
  80. cmSystemTools::Error("Generator: execution of devenv failed.");
  81. // return to the original directory
  82. cmSystemTools::ChangeDirectory(cwd.c_str());
  83. return 1;
  84. }
  85. cmSystemTools::ChangeDirectory(cwd.c_str());
  86. return retVal;
  87. }
  88. ///! Create a local generator appropriate to this Global Generator
  89. cmLocalGenerator *cmGlobalVisualStudio7Generator::CreateLocalGenerator()
  90. {
  91. cmLocalGenerator *lg = new cmLocalVisualStudio7Generator;
  92. lg->SetGlobalGenerator(this);
  93. return lg;
  94. }
  95. void cmGlobalVisualStudio7Generator::SetupTests()
  96. {
  97. std::string ctest =
  98. m_LocalGenerators[0]->GetMakefile()->GetDefinition("CMAKE_COMMAND");
  99. ctest = cmSystemTools::GetFilenamePath(ctest.c_str());
  100. ctest += "/";
  101. ctest += "ctest";
  102. ctest += cmSystemTools::GetExecutableExtension();
  103. if(!cmSystemTools::FileExists(ctest.c_str()))
  104. {
  105. ctest =
  106. m_LocalGenerators[0]->GetMakefile()->GetDefinition("CMAKE_COMMAND");
  107. ctest = cmSystemTools::GetFilenamePath(ctest.c_str());
  108. ctest += "/Debug/";
  109. ctest += "ctest";
  110. ctest += cmSystemTools::GetExecutableExtension();
  111. }
  112. if(!cmSystemTools::FileExists(ctest.c_str()))
  113. {
  114. ctest =
  115. m_LocalGenerators[0]->GetMakefile()->GetDefinition("CMAKE_COMMAND");
  116. ctest = cmSystemTools::GetFilenamePath(ctest.c_str());
  117. ctest += "/Release/";
  118. ctest += "ctest";
  119. ctest += cmSystemTools::GetExecutableExtension();
  120. }
  121. // if we found ctest
  122. if (cmSystemTools::FileExists(ctest.c_str()))
  123. {
  124. // Create a full path filename for output Testfile
  125. std::string fname;
  126. fname = m_CMakeInstance->GetStartOutputDirectory();
  127. fname += "/";
  128. fname += "DartTestfile.txt";
  129. // If the file doesn't exist, then ENABLE_TESTING hasn't been run
  130. if (cmSystemTools::FileExists(fname.c_str()))
  131. {
  132. std::vector<std::string> srcs;
  133. m_LocalGenerators[0]->GetMakefile()->
  134. AddUtilityCommand("RUN_TESTS", ctest.c_str(), "-D $(IntDir)",false, srcs);
  135. }
  136. }
  137. }
  138. void cmGlobalVisualStudio7Generator::GenerateConfigurations(cmMakefile* mf)
  139. {
  140. // process the configurations
  141. const char* ct
  142. = m_CMakeInstance->GetCacheDefinition("CMAKE_CONFIGURATION_TYPES");
  143. if ( ct )
  144. {
  145. std::string configTypes = ct;
  146. std::string::size_type start = 0;
  147. std::string::size_type endpos = 0;
  148. while(endpos != std::string::npos)
  149. {
  150. endpos = configTypes.find_first_of(" ;", start);
  151. std::string config;
  152. std::string::size_type len;
  153. if(endpos != std::string::npos)
  154. {
  155. len = endpos - start;
  156. }
  157. else
  158. {
  159. len = configTypes.size() - start;
  160. }
  161. config = configTypes.substr(start, len);
  162. if(config == "Debug" || config == "Release" ||
  163. config == "MinSizeRel" || config == "RelWithDebInfo")
  164. {
  165. // only add unique configurations
  166. if(std::find(m_Configurations.begin(),
  167. m_Configurations.end(), config) == m_Configurations.end())
  168. {
  169. m_Configurations.push_back(config);
  170. }
  171. }
  172. else
  173. {
  174. cmSystemTools::Error(
  175. "Invalid configuration type in CMAKE_CONFIGURATION_TYPES: ",
  176. config.c_str(),
  177. " (Valid types are Debug,Release,MinSizeRel,RelWithDebInfo)");
  178. }
  179. start = endpos+1;
  180. }
  181. }
  182. if(m_Configurations.size() == 0)
  183. {
  184. m_Configurations.push_back("Debug");
  185. m_Configurations.push_back("Release");
  186. }
  187. // Reset the entry to have a semi-colon separated list.
  188. std::string configs = m_Configurations[0];
  189. for(unsigned int i=1; i < m_Configurations.size(); ++i)
  190. {
  191. configs += ";";
  192. configs += m_Configurations[i];
  193. }
  194. mf->AddCacheDefinition(
  195. "CMAKE_CONFIGURATION_TYPES",
  196. configs.c_str(),
  197. "Semicolon separated list of supported configuration types, "
  198. "only supports Debug, Release, MinSizeRel, and RelWithDebInfo, "
  199. "anything else will be ignored.",
  200. cmCacheManager::STRING);
  201. }
  202. void cmGlobalVisualStudio7Generator::Generate()
  203. {
  204. // add a special target that depends on ALL projects for easy build
  205. // of Debug only
  206. std::vector<std::string> srcs;
  207. m_LocalGenerators[0]->GetMakefile()->
  208. AddUtilityCommand("ALL_BUILD", "echo","\"Build all projects\"",false, srcs);
  209. // add the Run Tests command
  210. this->SetupTests();
  211. // first do the superclass method
  212. this->cmGlobalGenerator::Generate();
  213. // Now write out the DSW
  214. this->OutputSLNFile();
  215. }
  216. // output the SLN file
  217. void cmGlobalVisualStudio7Generator::OutputSLNFile()
  218. {
  219. // if this is an out of source build, create the output directory
  220. if(strcmp(m_CMakeInstance->GetStartOutputDirectory(),
  221. m_CMakeInstance->GetHomeDirectory()) != 0)
  222. {
  223. if(!cmSystemTools::MakeDirectory(m_CMakeInstance->GetStartOutputDirectory()))
  224. {
  225. cmSystemTools::Error("Error creating output directory for SLN file",
  226. m_CMakeInstance->GetStartOutputDirectory());
  227. }
  228. }
  229. // create the dsw file name
  230. std::string fname;
  231. fname = m_CMakeInstance->GetStartOutputDirectory();
  232. fname += "/";
  233. if(strlen(m_LocalGenerators[0]->GetMakefile()->GetProjectName()) == 0)
  234. {
  235. m_LocalGenerators[0]->GetMakefile()->SetProjectName("Project");
  236. }
  237. fname += m_LocalGenerators[0]->GetMakefile()->GetProjectName();
  238. fname += ".sln";
  239. std::ofstream fout(fname.c_str());
  240. if(!fout)
  241. {
  242. cmSystemTools::Error("Error can not open SLN file for write: "
  243. ,fname.c_str());
  244. return;
  245. }
  246. this->WriteSLNFile(fout);
  247. }
  248. // Write a SLN file to the stream
  249. void cmGlobalVisualStudio7Generator::WriteSLNFile(std::ostream& fout)
  250. {
  251. // Write out the header for a SLN file
  252. this->WriteSLNHeader(fout);
  253. // Get the home directory with the trailing slash
  254. std::string homedir = m_CMakeInstance->GetHomeDirectory();
  255. homedir += "/";
  256. // For each cmMakefile, create a VCProj for it, and
  257. // add it to this SLN file
  258. unsigned int i;
  259. for(i = 0; i < m_LocalGenerators.size(); ++i)
  260. {
  261. cmMakefile* mf = m_LocalGenerators[i]->GetMakefile();
  262. // Get the source directory from the makefile
  263. std::string dir = mf->GetStartDirectory();
  264. // remove the home directory and / from the source directory
  265. // this gives a relative path
  266. cmSystemTools::ReplaceString(dir, homedir.c_str(), "");
  267. // Get the list of create dsp files names from the cmVCProjWriter, more
  268. // than one dsp could have been created per input CMakeLists.txt file
  269. // for each target
  270. std::vector<std::string> dspnames =
  271. static_cast<cmLocalVisualStudio7Generator *>(m_LocalGenerators[i])
  272. ->GetCreatedProjectNames();
  273. cmTargets &tgts = m_LocalGenerators[i]->GetMakefile()->GetTargets();
  274. cmTargets::iterator l = tgts.begin();
  275. for(std::vector<std::string>::iterator si = dspnames.begin();
  276. l != tgts.end(); ++l)
  277. {
  278. // special handling for the current makefile
  279. if(mf == m_LocalGenerators[0]->GetMakefile())
  280. {
  281. dir = "."; // no subdirectory for project generated
  282. // if this is the special ALL_BUILD utility, then
  283. // make it depend on every other non UTILITY project.
  284. // This is done by adding the names to the GetUtilities
  285. // vector on the makefile
  286. if(l->first == "ALL_BUILD")
  287. {
  288. unsigned int j;
  289. for(j = 0; j < m_LocalGenerators.size(); ++j)
  290. {
  291. const cmTargets &atgts =
  292. m_LocalGenerators[j]->GetMakefile()->GetTargets();
  293. for(cmTargets::const_iterator al = atgts.begin();
  294. al != atgts.end(); ++al)
  295. {
  296. if (al->second.IsInAll())
  297. {
  298. if (al->second.GetType() == cmTarget::UTILITY)
  299. {
  300. l->second.AddUtility(al->first.c_str());
  301. }
  302. else
  303. {
  304. l->second.AddLinkLibrary(al->first,cmTarget::GENERAL);
  305. }
  306. }
  307. }
  308. }
  309. }
  310. }
  311. // Write the project into the SLN file
  312. if (strncmp(l->first.c_str(), "INCLUDE_EXTERNAL_MSPROJECT", 26) == 0)
  313. {
  314. cmCustomCommand cc = l->second.GetPreLinkCommands()[0];
  315. // dodgy use of the cmCustomCommand's members to store the
  316. // arguments from the INCLUDE_EXTERNAL_MSPROJECT command
  317. std::vector<std::string> stuff = cc.GetDepends();
  318. std::vector<std::string> depends;
  319. depends.push_back(cc.GetOutput());
  320. this->WriteExternalProject(fout, stuff[0].c_str(),
  321. stuff[1].c_str(), depends);
  322. ++si;
  323. }
  324. else
  325. {
  326. if ((l->second.GetType() != cmTarget::INSTALL_FILES)
  327. && (l->second.GetType() != cmTarget::INSTALL_PROGRAMS))
  328. {
  329. this->WriteProject(fout, si->c_str(), dir.c_str(),l->second);
  330. ++si;
  331. }
  332. }
  333. }
  334. }
  335. fout << "Global\n"
  336. << "\tGlobalSection(SolutionConfiguration) = preSolution\n";
  337. int c = 0;
  338. for(std::vector<std::string>::iterator i = m_Configurations.begin();
  339. i != m_Configurations.end(); ++i)
  340. {
  341. fout << "\t\tConfigName." << c << " = " << *i << "\n";
  342. c++;
  343. }
  344. fout << "\tEndGlobalSection\n"
  345. << "\tGlobalSection(ProjectDependencies) = postSolution\n";
  346. // loop over again and compute the depends
  347. for(i = 0; i < m_LocalGenerators.size(); ++i)
  348. {
  349. cmMakefile* mf = m_LocalGenerators[i]->GetMakefile();
  350. cmLocalVisualStudio7Generator* pg =
  351. static_cast<cmLocalVisualStudio7Generator*>(m_LocalGenerators[i]);
  352. // Get the list of create dsp files names from the cmVCProjWriter, more
  353. // than one dsp could have been created per input CMakeLists.txt file
  354. // for each target
  355. std::vector<std::string> dspnames =
  356. pg->GetCreatedProjectNames();
  357. cmTargets &tgts = pg->GetMakefile()->GetTargets();
  358. cmTargets::iterator l = tgts.begin();
  359. std::string dir = mf->GetStartDirectory();
  360. for(std::vector<std::string>::iterator si = dspnames.begin();
  361. l != tgts.end(); ++l)
  362. {
  363. if ((l->second.GetType() != cmTarget::INSTALL_FILES)
  364. && (l->second.GetType() != cmTarget::INSTALL_PROGRAMS))
  365. {
  366. this->WriteProjectDepends(fout, si->c_str(), dir.c_str(),l->second);
  367. ++si;
  368. }
  369. }
  370. }
  371. fout << "\tEndGlobalSection\n";
  372. fout << "\tGlobalSection(ProjectConfiguration) = postSolution\n";
  373. // loop over again and compute the depends
  374. for(i = 0; i < m_LocalGenerators.size(); ++i)
  375. {
  376. cmMakefile* mf = m_LocalGenerators[i]->GetMakefile();
  377. cmLocalVisualStudio7Generator* pg =
  378. static_cast<cmLocalVisualStudio7Generator*>(m_LocalGenerators[i]);
  379. // Get the list of create dsp files names from the cmVCProjWriter, more
  380. // than one dsp could have been created per input CMakeLists.txt file
  381. // for each target
  382. std::vector<std::string> dspnames =
  383. pg->GetCreatedProjectNames();
  384. cmTargets &tgts = pg->GetMakefile()->GetTargets();
  385. cmTargets::iterator l = tgts.begin();
  386. std::string dir = mf->GetStartDirectory();
  387. for(std::vector<std::string>::iterator si = dspnames.begin();
  388. l != tgts.end(); ++l)
  389. {
  390. if ((l->second.GetType() != cmTarget::INSTALL_FILES)
  391. && (l->second.GetType() != cmTarget::INSTALL_PROGRAMS))
  392. {
  393. this->WriteProjectConfigurations(fout, si->c_str(), l->second.IsInAll());
  394. ++si;
  395. }
  396. }
  397. }
  398. fout << "\tEndGlobalSection\n";
  399. // Write the footer for the SLN file
  400. this->WriteSLNFooter(fout);
  401. }
  402. // Write a dsp file into the SLN file,
  403. // Note, that dependencies from executables to
  404. // the libraries it uses are also done here
  405. void cmGlobalVisualStudio7Generator::WriteProject(std::ostream& fout,
  406. const char* dspname,
  407. const char* dir,
  408. const cmTarget&)
  409. {
  410. std::string d = cmSystemTools::ConvertToOutputPath(dir);
  411. fout << "Project(\"{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}\") = \""
  412. << dspname << "\", \""
  413. << d << "\\" << dspname << ".vcproj\", \"{"
  414. << this->CreateGUID(dspname) << "}\"\nEndProject\n";
  415. }
  416. // Write a dsp file into the SLN file,
  417. // Note, that dependencies from executables to
  418. // the libraries it uses are also done here
  419. void cmGlobalVisualStudio7Generator::WriteProjectDepends(std::ostream& fout,
  420. const char* dspname,
  421. const char* ,
  422. const cmTarget& target
  423. )
  424. {
  425. int depcount = 0;
  426. // insert Begin Project Dependency Project_Dep_Name project stuff here
  427. if (target.GetType() != cmTarget::STATIC_LIBRARY)
  428. {
  429. cmTarget::LinkLibraries::const_iterator j, jend;
  430. j = target.GetLinkLibraries().begin();
  431. jend = target.GetLinkLibraries().end();
  432. for(;j!= jend; ++j)
  433. {
  434. if(j->first != dspname)
  435. {
  436. // is the library part of this SLN ? If so add dependency
  437. std::string libPath = j->first + "_CMAKE_PATH";
  438. const char* cacheValue
  439. = m_CMakeInstance->GetCacheDefinition(libPath.c_str());
  440. if(cacheValue)
  441. {
  442. fout << "\t\t{" << this->CreateGUID(dspname) << "}." << depcount << " = {"
  443. << this->CreateGUID(j->first.c_str()) << "}\n";
  444. depcount++;
  445. }
  446. }
  447. }
  448. }
  449. std::set<cmStdString>::const_iterator i, end;
  450. // write utility dependencies.
  451. i = target.GetUtilities().begin();
  452. end = target.GetUtilities().end();
  453. for(;i!= end; ++i)
  454. {
  455. if(*i != dspname)
  456. {
  457. fout << "\t\t{" << this->CreateGUID(dspname) << "}." << depcount << " = {"
  458. << this->CreateGUID(i->c_str()) << "}\n";
  459. depcount++;
  460. }
  461. }
  462. }
  463. // Write a dsp file into the SLN file,
  464. // Note, that dependencies from executables to
  465. // the libraries it uses are also done here
  466. void
  467. cmGlobalVisualStudio7Generator::WriteProjectConfigurations(std::ostream& fout,
  468. const char* name,
  469. bool in_all_build)
  470. {
  471. std::string guid = this->CreateGUID(name);
  472. for(std::vector<std::string>::iterator i = m_Configurations.begin();
  473. i != m_Configurations.end(); ++i)
  474. {
  475. fout << "\t\t{" << guid << "}." << *i << ".ActiveCfg = " << *i << "|Win32\n";
  476. if (in_all_build)
  477. {
  478. fout << "\t\t{" << guid << "}." << *i << ".Build.0 = " << *i << "|Win32\n";
  479. }
  480. }
  481. }
  482. // Write a dsp file into the SLN file,
  483. // Note, that dependencies from executables to
  484. // the libraries it uses are also done here
  485. void cmGlobalVisualStudio7Generator::WriteExternalProject(std::ostream& ,
  486. const char* ,
  487. const char* ,
  488. const std::vector<std::string>& )
  489. {
  490. cmSystemTools::Error("WriteExternalProject not implemented for Visual Studio 7");
  491. }
  492. // Standard end of dsw file
  493. void cmGlobalVisualStudio7Generator::WriteSLNFooter(std::ostream& fout)
  494. {
  495. fout << "\tGlobalSection(ExtensibilityGlobals) = postSolution\n"
  496. << "\tEndGlobalSection\n"
  497. << "\tGlobalSection(ExtensibilityAddIns) = postSolution\n"
  498. << "\tEndGlobalSection\n"
  499. << "EndGlobal\n";
  500. }
  501. // ouput standard header for dsw file
  502. void cmGlobalVisualStudio7Generator::WriteSLNHeader(std::ostream& fout)
  503. {
  504. fout << "Microsoft Visual Studio Solution File, Format Version 7.00\n";
  505. }
  506. std::string cmGlobalVisualStudio7Generator::CreateGUID(const char* name)
  507. {
  508. std::map<cmStdString, cmStdString>::iterator i = m_GUIDMap.find(name);
  509. if(i != m_GUIDMap.end())
  510. {
  511. return i->second;
  512. }
  513. std::string ret;
  514. UUID uid;
  515. unsigned char *uidstr;
  516. UuidCreate(&uid);
  517. UuidToString(&uid,&uidstr);
  518. ret = reinterpret_cast<char*>(uidstr);
  519. RpcStringFree(&uidstr);
  520. ret = cmSystemTools::UpperCase(ret);
  521. m_GUIDMap[name] = ret;
  522. return ret;
  523. }
  524. void cmGlobalVisualStudio7Generator::LocalGenerate()
  525. {
  526. this->cmGlobalGenerator::LocalGenerate();
  527. }
  528. std::vector<std::string> *cmGlobalVisualStudio7Generator::GetConfigurations()
  529. {
  530. return &m_Configurations;
  531. };
  532. //----------------------------------------------------------------------------
  533. void cmGlobalVisualStudio7Generator::GetDocumentation(cmDocumentationEntry& entry) const
  534. {
  535. entry.name = this->GetName();
  536. entry.brief = "Generates Visual Studio .NET 2002 project files.";
  537. entry.full = "";
  538. }