cmGlobalVisualStudio7Generator.cxx 19 KB

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