cmGlobalVisualStudio7Generator.cxx 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040
  1. /*============================================================================
  2. CMake - Cross Platform Makefile Generator
  3. Copyright 2000-2009 Kitware, Inc., Insight Software Consortium
  4. Distributed under the OSI-approved BSD License (the "License");
  5. see accompanying file Copyright.txt for details.
  6. This software is distributed WITHOUT ANY WARRANTY; without even the
  7. implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  8. See the License for more information.
  9. ============================================================================*/
  10. #include "windows.h" // this must be first to define GetCurrentDirectory
  11. #include <assert.h>
  12. #include "cmGlobalVisualStudio7Generator.h"
  13. #include "cmGeneratedFileStream.h"
  14. #include "cmLocalVisualStudio7Generator.h"
  15. #include "cmMakefile.h"
  16. #include "cmake.h"
  17. #include <cmsys/Encoding.hxx>
  18. cmGlobalVisualStudio7Generator::cmGlobalVisualStudio7Generator(
  19. const std::string& platformName)
  20. {
  21. this->IntelProjectVersion = 0;
  22. this->DevEnvCommandInitialized = false;
  23. if (platformName.empty())
  24. {
  25. this->PlatformName = "Win32";
  26. }
  27. else
  28. {
  29. this->PlatformName = platformName;
  30. }
  31. }
  32. cmGlobalVisualStudio7Generator::~cmGlobalVisualStudio7Generator()
  33. {
  34. free(this->IntelProjectVersion);
  35. }
  36. // Package GUID of Intel Visual Fortran plugin to VS IDE
  37. #define CM_INTEL_PLUGIN_GUID "{B68A201D-CB9B-47AF-A52F-7EEC72E217E4}"
  38. const char* cmGlobalVisualStudio7Generator::GetIntelProjectVersion()
  39. {
  40. if(!this->IntelProjectVersion)
  41. {
  42. // Compute the version of the Intel plugin to the VS IDE.
  43. // If the key does not exist then use a default guess.
  44. std::string intelVersion;
  45. std::string vskey = this->GetRegistryBase();
  46. vskey += "\\Packages\\" CM_INTEL_PLUGIN_GUID ";ProductVersion";
  47. cmSystemTools::ReadRegistryValue(vskey.c_str(), intelVersion,
  48. cmSystemTools::KeyWOW64_32);
  49. unsigned int intelVersionNumber = ~0u;
  50. sscanf(intelVersion.c_str(), "%u", &intelVersionNumber);
  51. if(intelVersionNumber >= 11)
  52. {
  53. // Default to latest known project file version.
  54. intelVersion = "11.0";
  55. }
  56. else if(intelVersionNumber == 10)
  57. {
  58. // Version 10.x actually uses 9.10 in project files!
  59. intelVersion = "9.10";
  60. }
  61. else
  62. {
  63. // Version <= 9: use ProductVersion from registry.
  64. }
  65. this->IntelProjectVersion = strdup(intelVersion.c_str());
  66. }
  67. return this->IntelProjectVersion;
  68. }
  69. void cmGlobalVisualStudio7Generator
  70. ::EnableLanguage(std::vector<std::string>const & lang,
  71. cmMakefile *mf, bool optional)
  72. {
  73. mf->AddDefinition("CMAKE_GENERATOR_RC", "rc");
  74. mf->AddDefinition("CMAKE_GENERATOR_NO_COMPILER_ENV", "1");
  75. this->AddPlatformDefinitions(mf);
  76. if(!mf->GetDefinition("CMAKE_CONFIGURATION_TYPES"))
  77. {
  78. mf->AddCacheDefinition(
  79. "CMAKE_CONFIGURATION_TYPES",
  80. "Debug;Release;MinSizeRel;RelWithDebInfo",
  81. "Semicolon separated list of supported configuration types, "
  82. "only supports Debug, Release, MinSizeRel, and RelWithDebInfo, "
  83. "anything else will be ignored.",
  84. cmCacheManager::STRING);
  85. }
  86. // Create list of configurations requested by user's cache, if any.
  87. this->cmGlobalGenerator::EnableLanguage(lang, mf, optional);
  88. this->GenerateConfigurations(mf);
  89. // if this environment variable is set, then copy it to
  90. // a static cache entry. It will be used by
  91. // cmLocalGenerator::ConstructScript, to add an extra PATH
  92. // to all custom commands. This is because the VS IDE
  93. // does not use the environment it is run in, and this allows
  94. // for running commands and using dll's that the IDE environment
  95. // does not know about.
  96. const char* extraPath = cmSystemTools::GetEnv("CMAKE_MSVCIDE_RUN_PATH");
  97. if(extraPath)
  98. {
  99. mf->AddCacheDefinition
  100. ("CMAKE_MSVCIDE_RUN_PATH", extraPath,
  101. "Saved environment variable CMAKE_MSVCIDE_RUN_PATH",
  102. cmCacheManager::STATIC);
  103. }
  104. }
  105. //----------------------------------------------------------------------------
  106. void cmGlobalVisualStudio7Generator::FindMakeProgram(cmMakefile* mf)
  107. {
  108. this->cmGlobalVisualStudioGenerator::FindMakeProgram(mf);
  109. mf->AddDefinition("CMAKE_VS_DEVENV_COMMAND",
  110. this->GetDevEnvCommand().c_str());
  111. }
  112. //----------------------------------------------------------------------------
  113. std::string const& cmGlobalVisualStudio7Generator::GetDevEnvCommand()
  114. {
  115. if(!this->DevEnvCommandInitialized)
  116. {
  117. this->DevEnvCommandInitialized = true;
  118. this->DevEnvCommand = this->FindDevEnvCommand();
  119. }
  120. return this->DevEnvCommand;
  121. }
  122. //----------------------------------------------------------------------------
  123. std::string cmGlobalVisualStudio7Generator::FindDevEnvCommand()
  124. {
  125. std::string vscmd;
  126. std::string vskey = this->GetRegistryBase() + ";InstallDir";
  127. if(cmSystemTools::ReadRegistryValue(vskey.c_str(), vscmd,
  128. cmSystemTools::KeyWOW64_32))
  129. {
  130. cmSystemTools::ConvertToUnixSlashes(vscmd);
  131. vscmd += "/";
  132. }
  133. vscmd += "devenv.com";
  134. return vscmd;
  135. }
  136. //----------------------------------------------------------------------------
  137. const char* cmGlobalVisualStudio7Generator::ExternalProjectType(
  138. const char* location)
  139. {
  140. std::string extension = cmSystemTools::GetFilenameLastExtension(location);
  141. if (extension == ".vbproj")
  142. {
  143. return "F184B08F-C81C-45F6-A57F-5ABD9991F28F";
  144. }
  145. else if (extension == ".csproj")
  146. {
  147. return "FAE04EC0-301F-11D3-BF4B-00C04F79EFBC";
  148. }
  149. else if (extension == ".fsproj")
  150. {
  151. return "F2A71F9B-5D33-465A-A702-920D77279786";
  152. }
  153. else if (extension == ".vdproj")
  154. {
  155. return "54435603-DBB4-11D2-8724-00A0C9A8B90C";
  156. }
  157. else if (extension == ".dbproj")
  158. {
  159. return "C8D11400-126E-41CD-887F-60BD40844F9E";
  160. }
  161. else if (extension == ".wixproj")
  162. {
  163. return "930C7802-8A8C-48F9-8165-68863BCCD9DD";
  164. }
  165. else if (extension == ".pyproj")
  166. {
  167. return "888888A0-9F3D-457C-B088-3A5042F75D52";
  168. }
  169. return "8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942";
  170. }
  171. //----------------------------------------------------------------------------
  172. void cmGlobalVisualStudio7Generator::GenerateBuildCommand(
  173. std::vector<std::string>& makeCommand,
  174. const std::string& makeProgram,
  175. const std::string& projectName,
  176. const std::string& /*projectDir*/,
  177. const std::string& targetName,
  178. const std::string& config,
  179. bool /*fast*/,
  180. std::vector<std::string> const& makeOptions)
  181. {
  182. // Select the caller- or user-preferred make program, else devenv.
  183. std::string makeProgramSelected =
  184. this->SelectMakeProgram(makeProgram, this->GetDevEnvCommand());
  185. // Ignore the above preference if it is msbuild.
  186. // Assume any other value is either a devenv or
  187. // command-line compatible with devenv.
  188. std::string makeProgramLower = makeProgramSelected;
  189. cmSystemTools::LowerCase(makeProgramLower);
  190. if(makeProgramLower.find("msbuild") != std::string::npos)
  191. {
  192. makeProgramSelected = this->GetDevEnvCommand();
  193. }
  194. makeCommand.push_back(makeProgramSelected);
  195. makeCommand.push_back(std::string(projectName) + ".sln");
  196. std::string realTarget = targetName;
  197. bool clean = false;
  198. if ( realTarget == "clean" )
  199. {
  200. clean = true;
  201. realTarget = "ALL_BUILD";
  202. }
  203. if(clean)
  204. {
  205. makeCommand.push_back("/clean");
  206. }
  207. else
  208. {
  209. makeCommand.push_back("/build");
  210. }
  211. if(!config.empty())
  212. {
  213. makeCommand.push_back(config);
  214. }
  215. else
  216. {
  217. makeCommand.push_back("Debug");
  218. }
  219. makeCommand.push_back("/project");
  220. if (!realTarget.empty())
  221. {
  222. makeCommand.push_back(realTarget);
  223. }
  224. else
  225. {
  226. makeCommand.push_back("ALL_BUILD");
  227. }
  228. makeCommand.insert(makeCommand.end(),
  229. makeOptions.begin(), makeOptions.end());
  230. }
  231. ///! Create a local generator appropriate to this Global Generator
  232. cmLocalGenerator *cmGlobalVisualStudio7Generator::CreateLocalGenerator()
  233. {
  234. cmLocalVisualStudio7Generator *lg =
  235. new cmLocalVisualStudio7Generator(cmLocalVisualStudioGenerator::VS7);
  236. lg->SetExtraFlagTable(this->GetExtraFlagTableVS7());
  237. lg->SetGlobalGenerator(this);
  238. return lg;
  239. }
  240. //----------------------------------------------------------------------------
  241. void cmGlobalVisualStudio7Generator::AddPlatformDefinitions(cmMakefile* mf)
  242. {
  243. cmGlobalVisualStudioGenerator::AddPlatformDefinitions(mf);
  244. mf->AddDefinition("CMAKE_VS_PLATFORM_NAME", this->GetPlatformName().c_str());
  245. mf->AddDefinition("CMAKE_VS_INTEL_Fortran_PROJECT_VERSION",
  246. this->GetIntelProjectVersion());
  247. }
  248. void cmGlobalVisualStudio7Generator::GenerateConfigurations(cmMakefile* mf)
  249. {
  250. // process the configurations
  251. const char* ct
  252. = this->CMakeInstance->GetCacheDefinition("CMAKE_CONFIGURATION_TYPES");
  253. if ( ct )
  254. {
  255. std::vector<std::string> argsOut;
  256. cmSystemTools::ExpandListArgument(ct, argsOut);
  257. for(std::vector<std::string>::iterator i = argsOut.begin();
  258. i != argsOut.end(); ++i)
  259. {
  260. if(std::find(this->Configurations.begin(),
  261. this->Configurations.end(),
  262. *i) == this->Configurations.end())
  263. {
  264. this->Configurations.push_back(*i);
  265. }
  266. }
  267. }
  268. // default to at least Debug and Release
  269. if(this->Configurations.size() == 0)
  270. {
  271. this->Configurations.push_back("Debug");
  272. this->Configurations.push_back("Release");
  273. }
  274. // Reset the entry to have a semi-colon separated list.
  275. std::string configs = this->Configurations[0];
  276. for(unsigned int i=1; i < this->Configurations.size(); ++i)
  277. {
  278. configs += ";";
  279. configs += this->Configurations[i];
  280. }
  281. mf->AddCacheDefinition(
  282. "CMAKE_CONFIGURATION_TYPES",
  283. configs.c_str(),
  284. "Semicolon separated list of supported configuration types, "
  285. "only supports Debug, Release, MinSizeRel, and RelWithDebInfo, "
  286. "anything else will be ignored.",
  287. cmCacheManager::STRING);
  288. }
  289. void cmGlobalVisualStudio7Generator::Generate()
  290. {
  291. // first do the superclass method
  292. this->cmGlobalVisualStudioGenerator::Generate();
  293. // Now write out the DSW
  294. this->OutputSLNFile();
  295. // If any solution or project files changed during the generation,
  296. // tell Visual Studio to reload them...
  297. if(!cmSystemTools::GetErrorOccuredFlag())
  298. {
  299. this->CallVisualStudioMacro(MacroReload);
  300. }
  301. }
  302. void cmGlobalVisualStudio7Generator
  303. ::OutputSLNFile(cmLocalGenerator* root,
  304. std::vector<cmLocalGenerator*>& generators)
  305. {
  306. if(generators.size() == 0)
  307. {
  308. return;
  309. }
  310. this->CurrentProject = root->GetMakefile()->GetProjectName();
  311. std::string fname = root->GetMakefile()->GetStartOutputDirectory();
  312. fname += "/";
  313. fname += root->GetMakefile()->GetProjectName();
  314. fname += ".sln";
  315. cmGeneratedFileStream fout(fname.c_str());
  316. fout.SetCopyIfDifferent(true);
  317. if(!fout)
  318. {
  319. return;
  320. }
  321. this->WriteSLNFile(fout, root, generators);
  322. if (fout.Close())
  323. {
  324. this->FileReplacedDuringGenerate(fname);
  325. }
  326. }
  327. // output the SLN file
  328. void cmGlobalVisualStudio7Generator::OutputSLNFile()
  329. {
  330. std::map<std::string, std::vector<cmLocalGenerator*> >::iterator it;
  331. for(it = this->ProjectMap.begin(); it!= this->ProjectMap.end(); ++it)
  332. {
  333. this->OutputSLNFile(it->second[0], it->second);
  334. }
  335. }
  336. void cmGlobalVisualStudio7Generator::WriteTargetConfigurations(
  337. std::ostream& fout,
  338. cmLocalGenerator* root,
  339. OrderedTargetDependSet const& projectTargets)
  340. {
  341. // loop over again and write out configurations for each target
  342. // in the solution
  343. for(OrderedTargetDependSet::const_iterator tt =
  344. projectTargets.begin(); tt != projectTargets.end(); ++tt)
  345. {
  346. cmTarget const* target = *tt;
  347. if(target->GetType() == cmTarget::INTERFACE_LIBRARY)
  348. {
  349. continue;
  350. }
  351. const char* expath = target->GetProperty("EXTERNAL_MSPROJECT");
  352. if(expath)
  353. {
  354. std::set<std::string> allConfigurations(this->Configurations.begin(),
  355. this->Configurations.end());
  356. const char* mapping = target->GetProperty("VS_PLATFORM_MAPPING");
  357. this->WriteProjectConfigurations(
  358. fout, target->GetName().c_str(), target->GetType(),
  359. allConfigurations, mapping ? mapping : "");
  360. }
  361. else
  362. {
  363. const std::set<std::string>& configsPartOfDefaultBuild =
  364. this->IsPartOfDefaultBuild(root->GetMakefile()->GetProjectName(),
  365. target);
  366. const char *vcprojName =
  367. target->GetProperty("GENERATOR_FILE_NAME");
  368. if (vcprojName)
  369. {
  370. this->WriteProjectConfigurations(fout, vcprojName, target->GetType(),
  371. configsPartOfDefaultBuild);
  372. }
  373. }
  374. }
  375. }
  376. void cmGlobalVisualStudio7Generator::WriteTargetsToSolution(
  377. std::ostream& fout,
  378. cmLocalGenerator* root,
  379. OrderedTargetDependSet const& projectTargets)
  380. {
  381. VisualStudioFolders.clear();
  382. for(OrderedTargetDependSet::const_iterator tt =
  383. projectTargets.begin(); tt != projectTargets.end(); ++tt)
  384. {
  385. cmTarget const* target = *tt;
  386. if(target->GetType() == cmTarget::INTERFACE_LIBRARY)
  387. {
  388. continue;
  389. }
  390. bool written = false;
  391. // handle external vc project files
  392. const char* expath = target->GetProperty("EXTERNAL_MSPROJECT");
  393. if(expath)
  394. {
  395. std::string project = target->GetName();
  396. std::string location = expath;
  397. this->WriteExternalProject(fout,
  398. project.c_str(),
  399. location.c_str(),
  400. target->GetProperty("VS_PROJECT_TYPE"),
  401. target->GetUtilities());
  402. written = true;
  403. }
  404. else
  405. {
  406. const char *vcprojName =
  407. target->GetProperty("GENERATOR_FILE_NAME");
  408. if(vcprojName)
  409. {
  410. cmMakefile* tmf = target->GetMakefile();
  411. std::string dir = tmf->GetStartOutputDirectory();
  412. dir = root->Convert(dir.c_str(),
  413. cmLocalGenerator::START_OUTPUT);
  414. if(dir == ".")
  415. {
  416. dir = ""; // msbuild cannot handle ".\" prefix
  417. }
  418. this->WriteProject(fout, vcprojName, dir.c_str(),
  419. *target);
  420. written = true;
  421. }
  422. }
  423. // Create "solution folder" information from FOLDER target property
  424. //
  425. if (written && this->UseFolderProperty())
  426. {
  427. const char *targetFolder = target->GetProperty("FOLDER");
  428. if (targetFolder)
  429. {
  430. std::vector<cmsys::String> tokens =
  431. cmSystemTools::SplitString(targetFolder, '/', false);
  432. std::string cumulativePath = "";
  433. for(std::vector<cmsys::String>::iterator iter = tokens.begin();
  434. iter != tokens.end(); ++iter)
  435. {
  436. if(!iter->size())
  437. {
  438. continue;
  439. }
  440. if (cumulativePath.empty())
  441. {
  442. cumulativePath = "CMAKE_FOLDER_GUID_" + *iter;
  443. }
  444. else
  445. {
  446. VisualStudioFolders[cumulativePath].insert(
  447. cumulativePath + "/" + *iter);
  448. cumulativePath = cumulativePath + "/" + *iter;
  449. }
  450. this->CreateGUID(cumulativePath.c_str());
  451. }
  452. if (!cumulativePath.empty())
  453. {
  454. VisualStudioFolders[cumulativePath].insert(target->GetName());
  455. }
  456. }
  457. }
  458. }
  459. }
  460. void cmGlobalVisualStudio7Generator::WriteTargetDepends(
  461. std::ostream& fout,
  462. OrderedTargetDependSet const& projectTargets
  463. )
  464. {
  465. for(OrderedTargetDependSet::const_iterator tt =
  466. projectTargets.begin(); tt != projectTargets.end(); ++tt)
  467. {
  468. cmTarget const* target = *tt;
  469. if(target->GetType() == cmTarget::INTERFACE_LIBRARY)
  470. {
  471. continue;
  472. }
  473. cmMakefile* mf = target->GetMakefile();
  474. const char *vcprojName =
  475. target->GetProperty("GENERATOR_FILE_NAME");
  476. if (vcprojName)
  477. {
  478. std::string dir = mf->GetStartDirectory();
  479. this->WriteProjectDepends(fout, vcprojName,
  480. dir.c_str(), *target);
  481. }
  482. }
  483. }
  484. //----------------------------------------------------------------------------
  485. // Write a SLN file to the stream
  486. void cmGlobalVisualStudio7Generator
  487. ::WriteSLNFile(std::ostream& fout,
  488. cmLocalGenerator* root,
  489. std::vector<cmLocalGenerator*>& generators)
  490. {
  491. // Write out the header for a SLN file
  492. this->WriteSLNHeader(fout);
  493. // Collect all targets under this root generator and the transitive
  494. // closure of their dependencies.
  495. TargetDependSet projectTargets;
  496. TargetDependSet originalTargets;
  497. this->GetTargetSets(projectTargets, originalTargets, root, generators);
  498. OrderedTargetDependSet orderedProjectTargets(projectTargets);
  499. this->WriteTargetsToSolution(fout, root, orderedProjectTargets);
  500. bool useFolderProperty = this->UseFolderProperty();
  501. if (useFolderProperty)
  502. {
  503. this->WriteFolders(fout);
  504. }
  505. // Write out the configurations information for the solution
  506. fout << "Global\n"
  507. << "\tGlobalSection(SolutionConfiguration) = preSolution\n";
  508. int c = 0;
  509. for(std::vector<std::string>::iterator i = this->Configurations.begin();
  510. i != this->Configurations.end(); ++i)
  511. {
  512. fout << "\t\tConfigName." << c << " = " << *i << "\n";
  513. c++;
  514. }
  515. fout << "\tEndGlobalSection\n";
  516. // Write out project(target) depends
  517. fout << "\tGlobalSection(ProjectDependencies) = postSolution\n";
  518. this->WriteTargetDepends(fout, orderedProjectTargets);
  519. fout << "\tEndGlobalSection\n";
  520. if (useFolderProperty)
  521. {
  522. // Write out project folders
  523. fout << "\tGlobalSection(NestedProjects) = preSolution\n";
  524. this->WriteFoldersContent(fout);
  525. fout << "\tEndGlobalSection\n";
  526. }
  527. // Write out the configurations for all the targets in the project
  528. fout << "\tGlobalSection(ProjectConfiguration) = postSolution\n";
  529. this->WriteTargetConfigurations(fout, root, orderedProjectTargets);
  530. fout << "\tEndGlobalSection\n";
  531. // Write out global sections
  532. this->WriteSLNGlobalSections(fout, root);
  533. // Write the footer for the SLN file
  534. this->WriteSLNFooter(fout);
  535. }
  536. //----------------------------------------------------------------------------
  537. void cmGlobalVisualStudio7Generator::WriteFolders(std::ostream& fout)
  538. {
  539. const char *prefix = "CMAKE_FOLDER_GUID_";
  540. const std::string::size_type skip_prefix = strlen(prefix);
  541. std::string guidProjectTypeFolder = "2150E333-8FDC-42A3-9474-1A3956D46DE8";
  542. for(std::map<std::string,std::set<std::string> >::iterator iter =
  543. VisualStudioFolders.begin(); iter != VisualStudioFolders.end(); ++iter)
  544. {
  545. std::string fullName = iter->first;
  546. std::string guid = this->GetGUID(fullName.c_str());
  547. cmSystemTools::ReplaceString(fullName, "/", "\\");
  548. if (cmSystemTools::StringStartsWith(fullName.c_str(), prefix))
  549. {
  550. fullName = fullName.substr(skip_prefix);
  551. }
  552. std::string nameOnly = cmSystemTools::GetFilenameName(fullName);
  553. fout << "Project(\"{" <<
  554. guidProjectTypeFolder << "}\") = \"" <<
  555. nameOnly << "\", \"" <<
  556. fullName << "\", \"{" <<
  557. guid <<
  558. "}\"\nEndProject\n";
  559. }
  560. }
  561. //----------------------------------------------------------------------------
  562. void cmGlobalVisualStudio7Generator::WriteFoldersContent(std::ostream& fout)
  563. {
  564. for(std::map<std::string,std::set<std::string> >::iterator iter =
  565. VisualStudioFolders.begin(); iter != VisualStudioFolders.end(); ++iter)
  566. {
  567. std::string key(iter->first);
  568. std::string guidParent(this->GetGUID(key.c_str()));
  569. for(std::set<std::string>::iterator it = iter->second.begin();
  570. it != iter->second.end(); ++it)
  571. {
  572. std::string value(*it);
  573. std::string guid(this->GetGUID(value.c_str()));
  574. fout << "\t\t{" << guid << "} = {" << guidParent << "}\n";
  575. }
  576. }
  577. }
  578. //----------------------------------------------------------------------------
  579. std::string
  580. cmGlobalVisualStudio7Generator::ConvertToSolutionPath(const char* path)
  581. {
  582. // Convert to backslashes. Do not use ConvertToOutputPath because
  583. // we will add quoting ourselves, and we know these projects always
  584. // use windows slashes.
  585. std::string d = path;
  586. std::string::size_type pos = 0;
  587. while((pos = d.find('/', pos)) != d.npos)
  588. {
  589. d[pos++] = '\\';
  590. }
  591. return d;
  592. }
  593. // Write a dsp file into the SLN file,
  594. // Note, that dependencies from executables to
  595. // the libraries it uses are also done here
  596. void cmGlobalVisualStudio7Generator::WriteProject(std::ostream& fout,
  597. const std::string& dspname,
  598. const char* dir, cmTarget const& target)
  599. {
  600. // check to see if this is a fortran build
  601. const char* ext = ".vcproj";
  602. const char* project =
  603. "Project(\"{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}\") = \"";
  604. if(this->TargetIsFortranOnly(target))
  605. {
  606. ext = ".vfproj";
  607. project = "Project(\"{6989167D-11E4-40FE-8C1A-2192A86A7E90}\") = \"";
  608. }
  609. fout << project
  610. << dspname << "\", \""
  611. << this->ConvertToSolutionPath(dir) << (dir[0]? "\\":"")
  612. << dspname << ext << "\", \"{"
  613. << this->GetGUID(dspname) << "}\"\nEndProject\n";
  614. UtilityDependsMap::iterator ui = this->UtilityDepends.find(&target);
  615. if(ui != this->UtilityDepends.end())
  616. {
  617. const char* uname = ui->second.c_str();
  618. fout << "Project(\"{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}\") = \""
  619. << uname << "\", \""
  620. << this->ConvertToSolutionPath(dir) << (dir[0]? "\\":"")
  621. << uname << ".vcproj" << "\", \"{"
  622. << this->GetGUID(uname) << "}\"\n"
  623. << "EndProject\n";
  624. }
  625. }
  626. // Write a dsp file into the SLN file,
  627. // Note, that dependencies from executables to
  628. // the libraries it uses are also done here
  629. void
  630. cmGlobalVisualStudio7Generator
  631. ::WriteProjectDepends(std::ostream& fout,
  632. const std::string& dspname,
  633. const char*, cmTarget const& target)
  634. {
  635. int depcount = 0;
  636. std::string dspguid = this->GetGUID(dspname);
  637. VSDependSet const& depends = this->VSTargetDepends[&target];
  638. for(VSDependSet::const_iterator di = depends.begin();
  639. di != depends.end(); ++di)
  640. {
  641. const char* name = di->c_str();
  642. std::string guid = this->GetGUID(name);
  643. if(guid.size() == 0)
  644. {
  645. std::string m = "Target: ";
  646. m += target.GetName();
  647. m += " depends on unknown target: ";
  648. m += name;
  649. cmSystemTools::Error(m.c_str());
  650. }
  651. fout << "\t\t{" << dspguid << "}." << depcount << " = {" << guid << "}\n";
  652. depcount++;
  653. }
  654. UtilityDependsMap::iterator ui = this->UtilityDepends.find(&target);
  655. if(ui != this->UtilityDepends.end())
  656. {
  657. const char* uname = ui->second.c_str();
  658. fout << "\t\t{" << this->GetGUID(uname) << "}.0 = {" << dspguid << "}\n";
  659. }
  660. }
  661. // Write a dsp file into the SLN file, Note, that dependencies from
  662. // executables to the libraries it uses are also done here
  663. void cmGlobalVisualStudio7Generator
  664. ::WriteProjectConfigurations(
  665. std::ostream& fout, const std::string& name, cmTarget::TargetType,
  666. const std::set<std::string>& configsPartOfDefaultBuild,
  667. const std::string& platformMapping)
  668. {
  669. const std::string& platformName =
  670. !platformMapping.empty() ? platformMapping : this->GetPlatformName();
  671. std::string guid = this->GetGUID(name);
  672. for(std::vector<std::string>::iterator i = this->Configurations.begin();
  673. i != this->Configurations.end(); ++i)
  674. {
  675. fout << "\t\t{" << guid << "}." << *i
  676. << ".ActiveCfg = " << *i << "|" << platformName << "\n";
  677. std::set<std::string>::const_iterator
  678. ci = configsPartOfDefaultBuild.find(*i);
  679. if(!(ci == configsPartOfDefaultBuild.end()))
  680. {
  681. fout << "\t\t{" << guid << "}." << *i
  682. << ".Build.0 = " << *i << "|" << platformName << "\n";
  683. }
  684. }
  685. }
  686. // Write a dsp file into the SLN file,
  687. // Note, that dependencies from executables to
  688. // the libraries it uses are also done here
  689. void cmGlobalVisualStudio7Generator::WriteExternalProject(std::ostream& fout,
  690. const std::string& name,
  691. const char* location,
  692. const char* typeGuid,
  693. const std::set<std::string>&)
  694. {
  695. std::string d = cmSystemTools::ConvertToOutputPath(location);
  696. fout << "Project("
  697. << "\"{"
  698. << (typeGuid ? typeGuid : this->ExternalProjectType(location))
  699. << "}\") = \""
  700. << name << "\", \""
  701. << this->ConvertToSolutionPath(location) << "\", \"{"
  702. << this->GetGUID(name)
  703. << "}\"\n";
  704. fout << "EndProject\n";
  705. }
  706. void cmGlobalVisualStudio7Generator
  707. ::WriteSLNGlobalSections(std::ostream& fout,
  708. cmLocalGenerator* root)
  709. {
  710. bool extensibilityGlobalsOverridden = false;
  711. bool extensibilityAddInsOverridden = false;
  712. const cmPropertyMap& props = root->GetMakefile()->GetProperties();
  713. for(cmPropertyMap::const_iterator itProp = props.begin();
  714. itProp != props.end(); ++itProp)
  715. {
  716. if(itProp->first.find("VS_GLOBAL_SECTION_") == 0)
  717. {
  718. std::string sectionType;
  719. std::string name = itProp->first.substr(18);
  720. if(name.find("PRE_") == 0)
  721. {
  722. name = name.substr(4);
  723. sectionType = "preSolution";
  724. }
  725. else if(name.find("POST_") == 0)
  726. {
  727. name = name.substr(5);
  728. sectionType = "postSolution";
  729. }
  730. else
  731. continue;
  732. if(!name.empty())
  733. {
  734. if(name == "ExtensibilityGlobals" && sectionType == "postSolution")
  735. extensibilityGlobalsOverridden = true;
  736. else if(name == "ExtensibilityAddIns" && sectionType == "postSolution")
  737. extensibilityAddInsOverridden = true;
  738. fout << "\tGlobalSection(" << name << ") = " << sectionType << "\n";
  739. std::vector<std::string> keyValuePairs;
  740. cmSystemTools::ExpandListArgument(itProp->second.GetValue(),
  741. keyValuePairs);
  742. for(std::vector<std::string>::const_iterator itPair =
  743. keyValuePairs.begin(); itPair != keyValuePairs.end(); ++itPair)
  744. {
  745. const std::string::size_type posEqual = itPair->find('=');
  746. if(posEqual != std::string::npos)
  747. {
  748. const std::string key =
  749. cmSystemTools::TrimWhitespace(itPair->substr(0, posEqual));
  750. const std::string value =
  751. cmSystemTools::TrimWhitespace(itPair->substr(posEqual + 1));
  752. fout << "\t\t" << key << " = " << value << "\n";
  753. }
  754. }
  755. fout << "\tEndGlobalSection\n";
  756. }
  757. }
  758. }
  759. if(!extensibilityGlobalsOverridden)
  760. fout << "\tGlobalSection(ExtensibilityGlobals) = postSolution\n"
  761. << "\tEndGlobalSection\n";
  762. if(!extensibilityAddInsOverridden)
  763. fout << "\tGlobalSection(ExtensibilityAddIns) = postSolution\n"
  764. << "\tEndGlobalSection\n";
  765. }
  766. // Standard end of dsw file
  767. void cmGlobalVisualStudio7Generator::WriteSLNFooter(std::ostream& fout)
  768. {
  769. fout << "EndGlobal\n";
  770. }
  771. // ouput standard header for dsw file
  772. void cmGlobalVisualStudio7Generator::WriteSLNHeader(std::ostream& fout)
  773. {
  774. fout << "Microsoft Visual Studio Solution File, Format Version 7.00\n";
  775. }
  776. //----------------------------------------------------------------------------
  777. std::string
  778. cmGlobalVisualStudio7Generator::WriteUtilityDepend(cmTarget const* target)
  779. {
  780. std::string pname = target->GetName();
  781. pname += "_UTILITY";
  782. std::string fname = target->GetMakefile()->GetStartOutputDirectory();
  783. fname += "/";
  784. fname += pname;
  785. fname += ".vcproj";
  786. cmGeneratedFileStream fout(fname.c_str());
  787. fout.SetCopyIfDifferent(true);
  788. this->CreateGUID(pname.c_str());
  789. std::string guid = this->GetGUID(pname.c_str());
  790. fout <<
  791. "<?xml version=\"1.0\" encoding = \"Windows-1252\"?>\n"
  792. "<VisualStudioProject\n"
  793. "\tProjectType=\"Visual C++\"\n"
  794. "\tVersion=\"" << this->GetIDEVersion() << "0\"\n"
  795. "\tName=\"" << pname << "\"\n"
  796. "\tProjectGUID=\"{" << guid << "}\"\n"
  797. "\tKeyword=\"Win32Proj\">\n"
  798. "\t<Platforms><Platform Name=\"Win32\"/></Platforms>\n"
  799. "\t<Configurations>\n"
  800. ;
  801. for(std::vector<std::string>::iterator i = this->Configurations.begin();
  802. i != this->Configurations.end(); ++i)
  803. {
  804. fout <<
  805. "\t\t<Configuration\n"
  806. "\t\t\tName=\"" << *i << "|Win32\"\n"
  807. "\t\t\tOutputDirectory=\"" << *i << "\"\n"
  808. "\t\t\tIntermediateDirectory=\"" << pname << ".dir\\" << *i << "\"\n"
  809. "\t\t\tConfigurationType=\"10\"\n"
  810. "\t\t\tUseOfMFC=\"0\"\n"
  811. "\t\t\tATLMinimizesCRunTimeLibraryUsage=\"FALSE\"\n"
  812. "\t\t\tCharacterSet=\"2\">\n"
  813. "\t\t</Configuration>\n"
  814. ;
  815. }
  816. fout <<
  817. "\t</Configurations>\n"
  818. "\t<Files></Files>\n"
  819. "\t<Globals></Globals>\n"
  820. "</VisualStudioProject>\n"
  821. ;
  822. if(fout.Close())
  823. {
  824. this->FileReplacedDuringGenerate(fname);
  825. }
  826. return pname;
  827. }
  828. std::string cmGlobalVisualStudio7Generator::GetGUID(const std::string& name)
  829. {
  830. std::string guidStoreName = name;
  831. guidStoreName += "_GUID_CMAKE";
  832. const char* storedGUID =
  833. this->CMakeInstance->GetCacheDefinition(guidStoreName.c_str());
  834. if(storedGUID)
  835. {
  836. return std::string(storedGUID);
  837. }
  838. cmSystemTools::Error("Unknown Target referenced : ",
  839. name.c_str());
  840. return "";
  841. }
  842. void cmGlobalVisualStudio7Generator::CreateGUID(const std::string& name)
  843. {
  844. std::string guidStoreName = name;
  845. guidStoreName += "_GUID_CMAKE";
  846. if(this->CMakeInstance->GetCacheDefinition(guidStoreName.c_str()))
  847. {
  848. return;
  849. }
  850. std::string ret;
  851. UUID uid;
  852. unsigned short *uidstr;
  853. UuidCreate(&uid);
  854. UuidToStringW(&uid,&uidstr);
  855. ret = cmsys::Encoding::ToNarrow(reinterpret_cast<wchar_t*>(uidstr));
  856. RpcStringFreeW(&uidstr);
  857. ret = cmSystemTools::UpperCase(ret);
  858. this->CMakeInstance->AddCacheEntry(guidStoreName.c_str(),
  859. ret.c_str(), "Stored GUID",
  860. cmCacheManager::INTERNAL);
  861. }
  862. std::vector<std::string> *cmGlobalVisualStudio7Generator::GetConfigurations()
  863. {
  864. return &this->Configurations;
  865. };
  866. //----------------------------------------------------------------------------
  867. void cmGlobalVisualStudio7Generator
  868. ::GetDocumentation(cmDocumentationEntry& entry)
  869. {
  870. entry.Name = cmGlobalVisualStudio7Generator::GetActualName();
  871. entry.Brief = "Generates Visual Studio .NET 2002 project files.";
  872. }
  873. //----------------------------------------------------------------------------
  874. void
  875. cmGlobalVisualStudio7Generator
  876. ::AppendDirectoryForConfig(const std::string& prefix,
  877. const std::string& config,
  878. const std::string& suffix,
  879. std::string& dir)
  880. {
  881. if(!config.empty())
  882. {
  883. dir += prefix;
  884. dir += config;
  885. dir += suffix;
  886. }
  887. }
  888. std::set<std::string>
  889. cmGlobalVisualStudio7Generator::IsPartOfDefaultBuild(
  890. const std::string& project,
  891. cmTarget const* target)
  892. {
  893. std::set<std::string> activeConfigs;
  894. // if it is a utilitiy target then only make it part of the
  895. // default build if another target depends on it
  896. int type = target->GetType();
  897. if (type == cmTarget::GLOBAL_TARGET)
  898. {
  899. return activeConfigs;
  900. }
  901. if(type == cmTarget::UTILITY && !this->IsDependedOn(project, target))
  902. {
  903. return activeConfigs;
  904. }
  905. // inspect EXCLUDE_FROM_DEFAULT_BUILD[_<CONFIG>] properties
  906. for(std::vector<std::string>::iterator i = this->Configurations.begin();
  907. i != this->Configurations.end(); ++i)
  908. {
  909. const char* propertyValue =
  910. target->GetFeature("EXCLUDE_FROM_DEFAULT_BUILD", i->c_str());
  911. if(cmSystemTools::IsOff(propertyValue))
  912. {
  913. activeConfigs.insert(*i);
  914. }
  915. }
  916. return activeConfigs;
  917. }
  918. //----------------------------------------------------------------------------
  919. static cmVS7FlagTable cmVS7ExtraFlagTable[] =
  920. {
  921. // Precompiled header and related options. Note that the
  922. // UsePrecompiledHeader entries are marked as "Continue" so that the
  923. // corresponding PrecompiledHeaderThrough entry can be found.
  924. {"UsePrecompiledHeader", "YX", "Automatically Generate", "2",
  925. cmVS7FlagTable::UserValueIgnored | cmVS7FlagTable::Continue},
  926. {"PrecompiledHeaderThrough", "YX", "Precompiled Header Name", "",
  927. cmVS7FlagTable::UserValueRequired},
  928. {"UsePrecompiledHeader", "Yu", "Use Precompiled Header", "3",
  929. cmVS7FlagTable::UserValueIgnored | cmVS7FlagTable::Continue},
  930. {"PrecompiledHeaderThrough", "Yu", "Precompiled Header Name", "",
  931. cmVS7FlagTable::UserValueRequired},
  932. {"WholeProgramOptimization", "LTCG", "WholeProgramOptimization", "TRUE", 0},
  933. // Exception handling mode. If no entries match, it will be FALSE.
  934. {"ExceptionHandling", "GX", "enable c++ exceptions", "TRUE", 0},
  935. {"ExceptionHandling", "EHsc", "enable c++ exceptions", "TRUE", 0},
  936. // The EHa option does not have an IDE setting. Let it go to false,
  937. // and have EHa passed on the command line by leaving out the table
  938. // entry.
  939. {0,0,0,0,0}
  940. };
  941. cmIDEFlagTable const* cmGlobalVisualStudio7Generator::GetExtraFlagTableVS7()
  942. {
  943. return cmVS7ExtraFlagTable;
  944. }