cmGlobalVisualStudio7Generator.cxx 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068
  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(projectTargets, target);
  365. const char *vcprojName =
  366. target->GetProperty("GENERATOR_FILE_NAME");
  367. if (vcprojName)
  368. {
  369. this->WriteProjectConfigurations(fout, vcprojName, target->GetType(),
  370. configsPartOfDefaultBuild);
  371. }
  372. }
  373. }
  374. }
  375. void cmGlobalVisualStudio7Generator::WriteTargetsToSolution(
  376. std::ostream& fout,
  377. cmLocalGenerator* root,
  378. OrderedTargetDependSet const& projectTargets)
  379. {
  380. VisualStudioFolders.clear();
  381. for(OrderedTargetDependSet::const_iterator tt =
  382. projectTargets.begin(); tt != projectTargets.end(); ++tt)
  383. {
  384. cmTarget const* target = *tt;
  385. if(target->GetType() == cmTarget::INTERFACE_LIBRARY)
  386. {
  387. continue;
  388. }
  389. bool written = false;
  390. // handle external vc project files
  391. const char* expath = target->GetProperty("EXTERNAL_MSPROJECT");
  392. if(expath)
  393. {
  394. std::string project = target->GetName();
  395. std::string location = expath;
  396. this->WriteExternalProject(fout,
  397. project.c_str(),
  398. location.c_str(),
  399. target->GetProperty("VS_PROJECT_TYPE"),
  400. target->GetUtilities());
  401. written = true;
  402. }
  403. else
  404. {
  405. const char *vcprojName =
  406. target->GetProperty("GENERATOR_FILE_NAME");
  407. if(vcprojName)
  408. {
  409. cmMakefile* tmf = target->GetMakefile();
  410. std::string dir = tmf->GetStartOutputDirectory();
  411. dir = root->Convert(dir.c_str(),
  412. cmLocalGenerator::START_OUTPUT);
  413. if(dir == ".")
  414. {
  415. dir = ""; // msbuild cannot handle ".\" prefix
  416. }
  417. this->WriteProject(fout, vcprojName, dir.c_str(),
  418. *target);
  419. written = true;
  420. }
  421. }
  422. // Create "solution folder" information from FOLDER target property
  423. //
  424. if (written && this->UseFolderProperty())
  425. {
  426. const char *targetFolder = target->GetProperty("FOLDER");
  427. if (targetFolder)
  428. {
  429. std::vector<cmsys::String> tokens =
  430. cmSystemTools::SplitString(targetFolder, '/', false);
  431. std::string cumulativePath = "";
  432. for(std::vector<cmsys::String>::iterator iter = tokens.begin();
  433. iter != tokens.end(); ++iter)
  434. {
  435. if(!iter->size())
  436. {
  437. continue;
  438. }
  439. if (cumulativePath.empty())
  440. {
  441. cumulativePath = "CMAKE_FOLDER_GUID_" + *iter;
  442. }
  443. else
  444. {
  445. VisualStudioFolders[cumulativePath].insert(
  446. cumulativePath + "/" + *iter);
  447. cumulativePath = cumulativePath + "/" + *iter;
  448. }
  449. this->CreateGUID(cumulativePath.c_str());
  450. }
  451. if (!cumulativePath.empty())
  452. {
  453. VisualStudioFolders[cumulativePath].insert(target->GetName());
  454. }
  455. }
  456. }
  457. }
  458. }
  459. void cmGlobalVisualStudio7Generator::WriteTargetDepends(
  460. std::ostream& fout,
  461. OrderedTargetDependSet const& projectTargets
  462. )
  463. {
  464. for(OrderedTargetDependSet::const_iterator tt =
  465. projectTargets.begin(); tt != projectTargets.end(); ++tt)
  466. {
  467. cmTarget const* target = *tt;
  468. if(target->GetType() == cmTarget::INTERFACE_LIBRARY)
  469. {
  470. continue;
  471. }
  472. cmMakefile* mf = target->GetMakefile();
  473. const char *vcprojName =
  474. target->GetProperty("GENERATOR_FILE_NAME");
  475. if (vcprojName)
  476. {
  477. std::string dir = mf->GetStartDirectory();
  478. this->WriteProjectDepends(fout, vcprojName,
  479. dir.c_str(), *target);
  480. }
  481. }
  482. }
  483. //----------------------------------------------------------------------------
  484. // Write a SLN file to the stream
  485. void cmGlobalVisualStudio7Generator
  486. ::WriteSLNFile(std::ostream& fout,
  487. cmLocalGenerator* root,
  488. std::vector<cmLocalGenerator*>& generators)
  489. {
  490. // Write out the header for a SLN file
  491. this->WriteSLNHeader(fout);
  492. // Collect all targets under this root generator and the transitive
  493. // closure of their dependencies.
  494. TargetDependSet projectTargets;
  495. TargetDependSet originalTargets;
  496. this->GetTargetSets(projectTargets, originalTargets, root, generators);
  497. OrderedTargetDependSet orderedProjectTargets(projectTargets);
  498. this->WriteTargetsToSolution(fout, root, orderedProjectTargets);
  499. bool useFolderProperty = this->UseFolderProperty();
  500. if (useFolderProperty)
  501. {
  502. this->WriteFolders(fout);
  503. }
  504. // Write out the configurations information for the solution
  505. fout << "Global\n"
  506. << "\tGlobalSection(SolutionConfiguration) = preSolution\n";
  507. int c = 0;
  508. for(std::vector<std::string>::iterator i = this->Configurations.begin();
  509. i != this->Configurations.end(); ++i)
  510. {
  511. fout << "\t\tConfigName." << c << " = " << *i << "\n";
  512. c++;
  513. }
  514. fout << "\tEndGlobalSection\n";
  515. // Write out project(target) depends
  516. fout << "\tGlobalSection(ProjectDependencies) = postSolution\n";
  517. this->WriteTargetDepends(fout, orderedProjectTargets);
  518. fout << "\tEndGlobalSection\n";
  519. if (useFolderProperty)
  520. {
  521. // Write out project folders
  522. fout << "\tGlobalSection(NestedProjects) = preSolution\n";
  523. this->WriteFoldersContent(fout);
  524. fout << "\tEndGlobalSection\n";
  525. }
  526. // Write out the configurations for all the targets in the project
  527. fout << "\tGlobalSection(ProjectConfiguration) = postSolution\n";
  528. this->WriteTargetConfigurations(fout, root, orderedProjectTargets);
  529. fout << "\tEndGlobalSection\n";
  530. // Write out global sections
  531. this->WriteSLNGlobalSections(fout, root);
  532. // Write the footer for the SLN file
  533. this->WriteSLNFooter(fout);
  534. }
  535. //----------------------------------------------------------------------------
  536. void cmGlobalVisualStudio7Generator::WriteFolders(std::ostream& fout)
  537. {
  538. const char *prefix = "CMAKE_FOLDER_GUID_";
  539. const std::string::size_type skip_prefix = strlen(prefix);
  540. std::string guidProjectTypeFolder = "2150E333-8FDC-42A3-9474-1A3956D46DE8";
  541. for(std::map<std::string,std::set<std::string> >::iterator iter =
  542. VisualStudioFolders.begin(); iter != VisualStudioFolders.end(); ++iter)
  543. {
  544. std::string fullName = iter->first;
  545. std::string guid = this->GetGUID(fullName.c_str());
  546. cmSystemTools::ReplaceString(fullName, "/", "\\");
  547. if (cmSystemTools::StringStartsWith(fullName.c_str(), prefix))
  548. {
  549. fullName = fullName.substr(skip_prefix);
  550. }
  551. std::string nameOnly = cmSystemTools::GetFilenameName(fullName);
  552. fout << "Project(\"{" <<
  553. guidProjectTypeFolder << "}\") = \"" <<
  554. nameOnly << "\", \"" <<
  555. fullName << "\", \"{" <<
  556. guid <<
  557. "}\"\nEndProject\n";
  558. }
  559. }
  560. //----------------------------------------------------------------------------
  561. void cmGlobalVisualStudio7Generator::WriteFoldersContent(std::ostream& fout)
  562. {
  563. for(std::map<std::string,std::set<std::string> >::iterator iter =
  564. VisualStudioFolders.begin(); iter != VisualStudioFolders.end(); ++iter)
  565. {
  566. std::string key(iter->first);
  567. std::string guidParent(this->GetGUID(key.c_str()));
  568. for(std::set<std::string>::iterator it = iter->second.begin();
  569. it != iter->second.end(); ++it)
  570. {
  571. std::string value(*it);
  572. std::string guid(this->GetGUID(value.c_str()));
  573. fout << "\t\t{" << guid << "} = {" << guidParent << "}\n";
  574. }
  575. }
  576. }
  577. //----------------------------------------------------------------------------
  578. std::string
  579. cmGlobalVisualStudio7Generator::ConvertToSolutionPath(const char* path)
  580. {
  581. // Convert to backslashes. Do not use ConvertToOutputPath because
  582. // we will add quoting ourselves, and we know these projects always
  583. // use windows slashes.
  584. std::string d = path;
  585. std::string::size_type pos = 0;
  586. while((pos = d.find('/', pos)) != d.npos)
  587. {
  588. d[pos++] = '\\';
  589. }
  590. return d;
  591. }
  592. // Write a dsp file into the SLN file,
  593. // Note, that dependencies from executables to
  594. // the libraries it uses are also done here
  595. void cmGlobalVisualStudio7Generator::WriteProject(std::ostream& fout,
  596. const std::string& dspname,
  597. const char* dir, cmTarget const& target)
  598. {
  599. // check to see if this is a fortran build
  600. const char* ext = ".vcproj";
  601. const char* project =
  602. "Project(\"{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}\") = \"";
  603. if(this->TargetIsFortranOnly(target))
  604. {
  605. ext = ".vfproj";
  606. project = "Project(\"{6989167D-11E4-40FE-8C1A-2192A86A7E90}\") = \"";
  607. }
  608. fout << project
  609. << dspname << "\", \""
  610. << this->ConvertToSolutionPath(dir) << (dir[0]? "\\":"")
  611. << dspname << ext << "\", \"{"
  612. << this->GetGUID(dspname) << "}\"\nEndProject\n";
  613. UtilityDependsMap::iterator ui = this->UtilityDepends.find(&target);
  614. if(ui != this->UtilityDepends.end())
  615. {
  616. const char* uname = ui->second.c_str();
  617. fout << "Project(\"{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}\") = \""
  618. << uname << "\", \""
  619. << this->ConvertToSolutionPath(dir) << (dir[0]? "\\":"")
  620. << uname << ".vcproj" << "\", \"{"
  621. << this->GetGUID(uname) << "}\"\n"
  622. << "EndProject\n";
  623. }
  624. }
  625. // Write a dsp file into the SLN file,
  626. // Note, that dependencies from executables to
  627. // the libraries it uses are also done here
  628. void
  629. cmGlobalVisualStudio7Generator
  630. ::WriteProjectDepends(std::ostream& fout,
  631. const std::string& dspname,
  632. const char*, cmTarget const& target)
  633. {
  634. int depcount = 0;
  635. std::string dspguid = this->GetGUID(dspname);
  636. VSDependSet const& depends = this->VSTargetDepends[&target];
  637. for(VSDependSet::const_iterator di = depends.begin();
  638. di != depends.end(); ++di)
  639. {
  640. const char* name = di->c_str();
  641. std::string guid = this->GetGUID(name);
  642. if(guid.size() == 0)
  643. {
  644. std::string m = "Target: ";
  645. m += target.GetName();
  646. m += " depends on unknown target: ";
  647. m += name;
  648. cmSystemTools::Error(m.c_str());
  649. }
  650. fout << "\t\t{" << dspguid << "}." << depcount << " = {" << guid << "}\n";
  651. depcount++;
  652. }
  653. UtilityDependsMap::iterator ui = this->UtilityDepends.find(&target);
  654. if(ui != this->UtilityDepends.end())
  655. {
  656. const char* uname = ui->second.c_str();
  657. fout << "\t\t{" << this->GetGUID(uname) << "}.0 = {" << dspguid << "}\n";
  658. }
  659. }
  660. // Write a dsp file into the SLN file, Note, that dependencies from
  661. // executables to the libraries it uses are also done here
  662. void cmGlobalVisualStudio7Generator
  663. ::WriteProjectConfigurations(
  664. std::ostream& fout, const std::string& name, cmTarget::TargetType,
  665. const std::set<std::string>& configsPartOfDefaultBuild,
  666. const std::string& platformMapping)
  667. {
  668. const std::string& platformName =
  669. !platformMapping.empty() ? platformMapping : this->GetPlatformName();
  670. std::string guid = this->GetGUID(name);
  671. for(std::vector<std::string>::iterator i = this->Configurations.begin();
  672. i != this->Configurations.end(); ++i)
  673. {
  674. fout << "\t\t{" << guid << "}." << *i
  675. << ".ActiveCfg = " << *i << "|" << platformName << "\n";
  676. std::set<std::string>::const_iterator
  677. ci = configsPartOfDefaultBuild.find(*i);
  678. if(!(ci == configsPartOfDefaultBuild.end()))
  679. {
  680. fout << "\t\t{" << guid << "}." << *i
  681. << ".Build.0 = " << *i << "|" << platformName << "\n";
  682. }
  683. }
  684. }
  685. // Write a dsp file into the SLN file,
  686. // Note, that dependencies from executables to
  687. // the libraries it uses are also done here
  688. void cmGlobalVisualStudio7Generator::WriteExternalProject(std::ostream& fout,
  689. const std::string& name,
  690. const char* location,
  691. const char* typeGuid,
  692. const std::set<std::string>&)
  693. {
  694. std::string d = cmSystemTools::ConvertToOutputPath(location);
  695. fout << "Project("
  696. << "\"{"
  697. << (typeGuid ? typeGuid : this->ExternalProjectType(location))
  698. << "}\") = \""
  699. << name << "\", \""
  700. << this->ConvertToSolutionPath(location) << "\", \"{"
  701. << this->GetGUID(name)
  702. << "}\"\n";
  703. fout << "EndProject\n";
  704. }
  705. void cmGlobalVisualStudio7Generator
  706. ::WriteSLNGlobalSections(std::ostream& fout,
  707. cmLocalGenerator* root)
  708. {
  709. bool extensibilityGlobalsOverridden = false;
  710. bool extensibilityAddInsOverridden = false;
  711. const cmPropertyMap& props = root->GetMakefile()->GetProperties();
  712. for(cmPropertyMap::const_iterator itProp = props.begin();
  713. itProp != props.end(); ++itProp)
  714. {
  715. if(itProp->first.find("VS_GLOBAL_SECTION_") == 0)
  716. {
  717. std::string sectionType;
  718. std::string name = itProp->first.substr(18);
  719. if(name.find("PRE_") == 0)
  720. {
  721. name = name.substr(4);
  722. sectionType = "preSolution";
  723. }
  724. else if(name.find("POST_") == 0)
  725. {
  726. name = name.substr(5);
  727. sectionType = "postSolution";
  728. }
  729. else
  730. continue;
  731. if(!name.empty())
  732. {
  733. if(name == "ExtensibilityGlobals" && sectionType == "postSolution")
  734. extensibilityGlobalsOverridden = true;
  735. else if(name == "ExtensibilityAddIns" && sectionType == "postSolution")
  736. extensibilityAddInsOverridden = true;
  737. fout << "\tGlobalSection(" << name << ") = " << sectionType << "\n";
  738. std::vector<std::string> keyValuePairs;
  739. cmSystemTools::ExpandListArgument(itProp->second.GetValue(),
  740. keyValuePairs);
  741. for(std::vector<std::string>::const_iterator itPair =
  742. keyValuePairs.begin(); itPair != keyValuePairs.end(); ++itPair)
  743. {
  744. const std::string::size_type posEqual = itPair->find('=');
  745. if(posEqual != std::string::npos)
  746. {
  747. const std::string key =
  748. cmSystemTools::TrimWhitespace(itPair->substr(0, posEqual));
  749. const std::string value =
  750. cmSystemTools::TrimWhitespace(itPair->substr(posEqual + 1));
  751. fout << "\t\t" << key << " = " << value << "\n";
  752. }
  753. }
  754. fout << "\tEndGlobalSection\n";
  755. }
  756. }
  757. }
  758. if(!extensibilityGlobalsOverridden)
  759. fout << "\tGlobalSection(ExtensibilityGlobals) = postSolution\n"
  760. << "\tEndGlobalSection\n";
  761. if(!extensibilityAddInsOverridden)
  762. fout << "\tGlobalSection(ExtensibilityAddIns) = postSolution\n"
  763. << "\tEndGlobalSection\n";
  764. }
  765. // Standard end of dsw file
  766. void cmGlobalVisualStudio7Generator::WriteSLNFooter(std::ostream& fout)
  767. {
  768. fout << "EndGlobal\n";
  769. }
  770. // ouput standard header for dsw file
  771. void cmGlobalVisualStudio7Generator::WriteSLNHeader(std::ostream& fout)
  772. {
  773. fout << "Microsoft Visual Studio Solution File, Format Version 7.00\n";
  774. }
  775. //----------------------------------------------------------------------------
  776. std::string
  777. cmGlobalVisualStudio7Generator::WriteUtilityDepend(cmTarget const* target)
  778. {
  779. std::string pname = target->GetName();
  780. pname += "_UTILITY";
  781. std::string fname = target->GetMakefile()->GetStartOutputDirectory();
  782. fname += "/";
  783. fname += pname;
  784. fname += ".vcproj";
  785. cmGeneratedFileStream fout(fname.c_str());
  786. fout.SetCopyIfDifferent(true);
  787. this->CreateGUID(pname.c_str());
  788. std::string guid = this->GetGUID(pname.c_str());
  789. fout <<
  790. "<?xml version=\"1.0\" encoding = \""
  791. << this->Encoding() << "\"?>\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. OrderedTargetDependSet const& projectTargets, cmTarget const* target)
  891. {
  892. std::set<std::string> activeConfigs;
  893. // if it is a utilitiy target then only make it part of the
  894. // default build if another target depends on it
  895. int type = target->GetType();
  896. if (type == cmTarget::GLOBAL_TARGET)
  897. {
  898. return activeConfigs;
  899. }
  900. if(type == cmTarget::UTILITY && !this->IsDependedOn(projectTargets, target))
  901. {
  902. return activeConfigs;
  903. }
  904. // inspect EXCLUDE_FROM_DEFAULT_BUILD[_<CONFIG>] properties
  905. for(std::vector<std::string>::iterator i = this->Configurations.begin();
  906. i != this->Configurations.end(); ++i)
  907. {
  908. const char* propertyValue =
  909. target->GetFeature("EXCLUDE_FROM_DEFAULT_BUILD", i->c_str());
  910. if(cmSystemTools::IsOff(propertyValue))
  911. {
  912. activeConfigs.insert(*i);
  913. }
  914. }
  915. return activeConfigs;
  916. }
  917. bool
  918. cmGlobalVisualStudio7Generator
  919. ::IsDependedOn(OrderedTargetDependSet const& projectTargets,
  920. cmTarget const* targetIn)
  921. {
  922. for (OrderedTargetDependSet::const_iterator l = projectTargets.begin();
  923. l != projectTargets.end(); ++l)
  924. {
  925. cmTarget const& target = **l;
  926. TargetDependSet const& tgtdeps = this->GetTargetDirectDepends(target);
  927. if(tgtdeps.count(targetIn))
  928. {
  929. return true;
  930. }
  931. }
  932. return false;
  933. }
  934. //----------------------------------------------------------------------------
  935. static cmVS7FlagTable cmVS7ExtraFlagTable[] =
  936. {
  937. // Precompiled header and related options. Note that the
  938. // UsePrecompiledHeader entries are marked as "Continue" so that the
  939. // corresponding PrecompiledHeaderThrough entry can be found.
  940. {"UsePrecompiledHeader", "YX", "Automatically Generate", "2",
  941. cmVS7FlagTable::UserValueIgnored | cmVS7FlagTable::Continue},
  942. {"PrecompiledHeaderThrough", "YX", "Precompiled Header Name", "",
  943. cmVS7FlagTable::UserValueRequired},
  944. {"UsePrecompiledHeader", "Yu", "Use Precompiled Header", "3",
  945. cmVS7FlagTable::UserValueIgnored | cmVS7FlagTable::Continue},
  946. {"PrecompiledHeaderThrough", "Yu", "Precompiled Header Name", "",
  947. cmVS7FlagTable::UserValueRequired},
  948. {"WholeProgramOptimization", "LTCG", "WholeProgramOptimization", "true", 0},
  949. // Exception handling mode. If no entries match, it will be FALSE.
  950. {"ExceptionHandling", "GX", "enable c++ exceptions", "true", 0},
  951. {"ExceptionHandling", "EHsc", "enable c++ exceptions", "true", 0},
  952. // The EHa option does not have an IDE setting. Let it go to false,
  953. // and have EHa passed on the command line by leaving out the table
  954. // entry.
  955. {0,0,0,0,0}
  956. };
  957. cmIDEFlagTable const* cmGlobalVisualStudio7Generator::GetExtraFlagTableVS7()
  958. {
  959. return cmVS7ExtraFlagTable;
  960. }
  961. std::string cmGlobalVisualStudio7Generator::Encoding()
  962. {
  963. std::ostringstream encoding;
  964. #ifdef CMAKE_ENCODING_UTF8
  965. encoding << "UTF-8";
  966. #else
  967. encoding << "Windows-1252";
  968. #endif
  969. return encoding.str();
  970. }