cmGlobalVisualStudio7Generator.cxx 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092
  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. this->MasmEnabled = false;
  24. if (platformName.empty())
  25. {
  26. this->DefaultPlatformName = "Win32";
  27. }
  28. else
  29. {
  30. this->DefaultPlatformName = platformName;
  31. }
  32. }
  33. cmGlobalVisualStudio7Generator::~cmGlobalVisualStudio7Generator()
  34. {
  35. free(this->IntelProjectVersion);
  36. }
  37. // Package GUID of Intel Visual Fortran plugin to VS IDE
  38. #define CM_INTEL_PLUGIN_GUID "{B68A201D-CB9B-47AF-A52F-7EEC72E217E4}"
  39. const char* cmGlobalVisualStudio7Generator::GetIntelProjectVersion()
  40. {
  41. if(!this->IntelProjectVersion)
  42. {
  43. // Compute the version of the Intel plugin to the VS IDE.
  44. // If the key does not exist then use a default guess.
  45. std::string intelVersion;
  46. std::string vskey = this->GetRegistryBase();
  47. vskey += "\\Packages\\" CM_INTEL_PLUGIN_GUID ";ProductVersion";
  48. cmSystemTools::ReadRegistryValue(vskey.c_str(), intelVersion,
  49. cmSystemTools::KeyWOW64_32);
  50. unsigned int intelVersionNumber = ~0u;
  51. sscanf(intelVersion.c_str(), "%u", &intelVersionNumber);
  52. if(intelVersionNumber >= 11)
  53. {
  54. // Default to latest known project file version.
  55. intelVersion = "11.0";
  56. }
  57. else if(intelVersionNumber == 10)
  58. {
  59. // Version 10.x actually uses 9.10 in project files!
  60. intelVersion = "9.10";
  61. }
  62. else
  63. {
  64. // Version <= 9: use ProductVersion from registry.
  65. }
  66. this->IntelProjectVersion = strdup(intelVersion.c_str());
  67. }
  68. return this->IntelProjectVersion;
  69. }
  70. void cmGlobalVisualStudio7Generator
  71. ::EnableLanguage(std::vector<std::string>const & lang,
  72. cmMakefile *mf, bool optional)
  73. {
  74. mf->AddDefinition("CMAKE_GENERATOR_RC", "rc");
  75. mf->AddDefinition("CMAKE_GENERATOR_NO_COMPILER_ENV", "1");
  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. std::string const& cmGlobalVisualStudio7Generator::GetPlatformName() const
  242. {
  243. if(!this->GeneratorPlatform.empty())
  244. {
  245. return this->GeneratorPlatform;
  246. }
  247. return this->DefaultPlatformName;
  248. }
  249. //----------------------------------------------------------------------------
  250. bool cmGlobalVisualStudio7Generator::SetSystemName(std::string const& s,
  251. cmMakefile* mf)
  252. {
  253. mf->AddDefinition("CMAKE_VS_INTEL_Fortran_PROJECT_VERSION",
  254. this->GetIntelProjectVersion());
  255. return this->cmGlobalVisualStudioGenerator::SetSystemName(s, mf);
  256. }
  257. //----------------------------------------------------------------------------
  258. bool cmGlobalVisualStudio7Generator::SetGeneratorPlatform(std::string const& p,
  259. cmMakefile* mf)
  260. {
  261. if(this->GetPlatformName() == "x64")
  262. {
  263. mf->AddDefinition("CMAKE_FORCE_WIN64", "TRUE");
  264. }
  265. else if(this->GetPlatformName() == "Itanium")
  266. {
  267. mf->AddDefinition("CMAKE_FORCE_IA64", "TRUE");
  268. }
  269. mf->AddDefinition("CMAKE_VS_PLATFORM_NAME", this->GetPlatformName().c_str());
  270. return this->cmGlobalVisualStudioGenerator::SetGeneratorPlatform(p, mf);
  271. }
  272. void cmGlobalVisualStudio7Generator::GenerateConfigurations(cmMakefile* mf)
  273. {
  274. // process the configurations
  275. const char* ct
  276. = this->CMakeInstance->GetCacheDefinition("CMAKE_CONFIGURATION_TYPES");
  277. if ( ct )
  278. {
  279. std::vector<std::string> argsOut;
  280. cmSystemTools::ExpandListArgument(ct, argsOut);
  281. for(std::vector<std::string>::iterator i = argsOut.begin();
  282. i != argsOut.end(); ++i)
  283. {
  284. if(std::find(this->Configurations.begin(),
  285. this->Configurations.end(),
  286. *i) == this->Configurations.end())
  287. {
  288. this->Configurations.push_back(*i);
  289. }
  290. }
  291. }
  292. // default to at least Debug and Release
  293. if(this->Configurations.size() == 0)
  294. {
  295. this->Configurations.push_back("Debug");
  296. this->Configurations.push_back("Release");
  297. }
  298. // Reset the entry to have a semi-colon separated list.
  299. std::string configs = this->Configurations[0];
  300. for(unsigned int i=1; i < this->Configurations.size(); ++i)
  301. {
  302. configs += ";";
  303. configs += this->Configurations[i];
  304. }
  305. mf->AddCacheDefinition(
  306. "CMAKE_CONFIGURATION_TYPES",
  307. configs.c_str(),
  308. "Semicolon separated list of supported configuration types, "
  309. "only supports Debug, Release, MinSizeRel, and RelWithDebInfo, "
  310. "anything else will be ignored.",
  311. cmCacheManager::STRING);
  312. }
  313. void cmGlobalVisualStudio7Generator::Generate()
  314. {
  315. // first do the superclass method
  316. this->cmGlobalVisualStudioGenerator::Generate();
  317. // Now write out the DSW
  318. this->OutputSLNFile();
  319. // If any solution or project files changed during the generation,
  320. // tell Visual Studio to reload them...
  321. if(!cmSystemTools::GetErrorOccuredFlag())
  322. {
  323. this->CallVisualStudioMacro(MacroReload);
  324. }
  325. }
  326. void cmGlobalVisualStudio7Generator
  327. ::OutputSLNFile(cmLocalGenerator* root,
  328. std::vector<cmLocalGenerator*>& generators)
  329. {
  330. if(generators.size() == 0)
  331. {
  332. return;
  333. }
  334. this->CurrentProject = root->GetMakefile()->GetProjectName();
  335. std::string fname = root->GetMakefile()->GetStartOutputDirectory();
  336. fname += "/";
  337. fname += root->GetMakefile()->GetProjectName();
  338. fname += ".sln";
  339. cmGeneratedFileStream fout(fname.c_str());
  340. fout.SetCopyIfDifferent(true);
  341. if(!fout)
  342. {
  343. return;
  344. }
  345. this->WriteSLNFile(fout, root, generators);
  346. if (fout.Close())
  347. {
  348. this->FileReplacedDuringGenerate(fname);
  349. }
  350. }
  351. // output the SLN file
  352. void cmGlobalVisualStudio7Generator::OutputSLNFile()
  353. {
  354. std::map<std::string, std::vector<cmLocalGenerator*> >::iterator it;
  355. for(it = this->ProjectMap.begin(); it!= this->ProjectMap.end(); ++it)
  356. {
  357. this->OutputSLNFile(it->second[0], it->second);
  358. }
  359. }
  360. void cmGlobalVisualStudio7Generator::WriteTargetConfigurations(
  361. std::ostream& fout,
  362. OrderedTargetDependSet const& projectTargets)
  363. {
  364. // loop over again and write out configurations for each target
  365. // in the solution
  366. for(OrderedTargetDependSet::const_iterator tt =
  367. projectTargets.begin(); tt != projectTargets.end(); ++tt)
  368. {
  369. cmTarget const* target = *tt;
  370. if(target->GetType() == cmTarget::INTERFACE_LIBRARY)
  371. {
  372. continue;
  373. }
  374. const char* expath = target->GetProperty("EXTERNAL_MSPROJECT");
  375. if(expath)
  376. {
  377. std::set<std::string> allConfigurations(this->Configurations.begin(),
  378. this->Configurations.end());
  379. const char* mapping = target->GetProperty("VS_PLATFORM_MAPPING");
  380. this->WriteProjectConfigurations(
  381. fout, target->GetName().c_str(), target->GetType(),
  382. allConfigurations, mapping ? mapping : "");
  383. }
  384. else
  385. {
  386. const std::set<std::string>& configsPartOfDefaultBuild =
  387. this->IsPartOfDefaultBuild(projectTargets, target);
  388. const char *vcprojName =
  389. target->GetProperty("GENERATOR_FILE_NAME");
  390. if (vcprojName)
  391. {
  392. this->WriteProjectConfigurations(fout, vcprojName, target->GetType(),
  393. configsPartOfDefaultBuild);
  394. }
  395. }
  396. }
  397. }
  398. void cmGlobalVisualStudio7Generator::WriteTargetsToSolution(
  399. std::ostream& fout,
  400. cmLocalGenerator* root,
  401. OrderedTargetDependSet const& projectTargets)
  402. {
  403. VisualStudioFolders.clear();
  404. for(OrderedTargetDependSet::const_iterator tt =
  405. projectTargets.begin(); tt != projectTargets.end(); ++tt)
  406. {
  407. cmTarget const* target = *tt;
  408. if(target->GetType() == cmTarget::INTERFACE_LIBRARY)
  409. {
  410. continue;
  411. }
  412. bool written = false;
  413. // handle external vc project files
  414. const char* expath = target->GetProperty("EXTERNAL_MSPROJECT");
  415. if(expath)
  416. {
  417. std::string project = target->GetName();
  418. std::string location = expath;
  419. this->WriteExternalProject(fout,
  420. project.c_str(),
  421. location.c_str(),
  422. target->GetProperty("VS_PROJECT_TYPE"),
  423. target->GetUtilities());
  424. written = true;
  425. }
  426. else
  427. {
  428. const char *vcprojName =
  429. target->GetProperty("GENERATOR_FILE_NAME");
  430. if(vcprojName)
  431. {
  432. cmMakefile* tmf = target->GetMakefile();
  433. std::string dir = tmf->GetStartOutputDirectory();
  434. dir = root->Convert(dir.c_str(),
  435. cmLocalGenerator::START_OUTPUT);
  436. if(dir == ".")
  437. {
  438. dir = ""; // msbuild cannot handle ".\" prefix
  439. }
  440. this->WriteProject(fout, vcprojName, dir.c_str(),
  441. *target);
  442. written = true;
  443. }
  444. }
  445. // Create "solution folder" information from FOLDER target property
  446. //
  447. if (written && this->UseFolderProperty())
  448. {
  449. const char *targetFolder = target->GetProperty("FOLDER");
  450. if (targetFolder)
  451. {
  452. std::vector<cmsys::String> tokens =
  453. cmSystemTools::SplitString(targetFolder, '/', false);
  454. std::string cumulativePath = "";
  455. for(std::vector<cmsys::String>::iterator iter = tokens.begin();
  456. iter != tokens.end(); ++iter)
  457. {
  458. if(!iter->size())
  459. {
  460. continue;
  461. }
  462. if (cumulativePath.empty())
  463. {
  464. cumulativePath = "CMAKE_FOLDER_GUID_" + *iter;
  465. }
  466. else
  467. {
  468. VisualStudioFolders[cumulativePath].insert(
  469. cumulativePath + "/" + *iter);
  470. cumulativePath = cumulativePath + "/" + *iter;
  471. }
  472. this->CreateGUID(cumulativePath.c_str());
  473. }
  474. if (!cumulativePath.empty())
  475. {
  476. VisualStudioFolders[cumulativePath].insert(target->GetName());
  477. }
  478. }
  479. }
  480. }
  481. }
  482. void cmGlobalVisualStudio7Generator::WriteTargetDepends(
  483. std::ostream& fout,
  484. OrderedTargetDependSet const& projectTargets
  485. )
  486. {
  487. for(OrderedTargetDependSet::const_iterator tt =
  488. projectTargets.begin(); tt != projectTargets.end(); ++tt)
  489. {
  490. cmTarget const* target = *tt;
  491. if(target->GetType() == cmTarget::INTERFACE_LIBRARY)
  492. {
  493. continue;
  494. }
  495. cmMakefile* mf = target->GetMakefile();
  496. const char *vcprojName =
  497. target->GetProperty("GENERATOR_FILE_NAME");
  498. if (vcprojName)
  499. {
  500. std::string dir = mf->GetStartDirectory();
  501. this->WriteProjectDepends(fout, vcprojName,
  502. dir.c_str(), *target);
  503. }
  504. }
  505. }
  506. //----------------------------------------------------------------------------
  507. // Write a SLN file to the stream
  508. void cmGlobalVisualStudio7Generator
  509. ::WriteSLNFile(std::ostream& fout,
  510. cmLocalGenerator* root,
  511. std::vector<cmLocalGenerator*>& generators)
  512. {
  513. // Write out the header for a SLN file
  514. this->WriteSLNHeader(fout);
  515. // Collect all targets under this root generator and the transitive
  516. // closure of their dependencies.
  517. TargetDependSet projectTargets;
  518. TargetDependSet originalTargets;
  519. this->GetTargetSets(projectTargets, originalTargets, root, generators);
  520. OrderedTargetDependSet orderedProjectTargets(projectTargets);
  521. this->WriteTargetsToSolution(fout, root, orderedProjectTargets);
  522. bool useFolderProperty = this->UseFolderProperty();
  523. if (useFolderProperty)
  524. {
  525. this->WriteFolders(fout);
  526. }
  527. // Write out the configurations information for the solution
  528. fout << "Global\n"
  529. << "\tGlobalSection(SolutionConfiguration) = preSolution\n";
  530. int c = 0;
  531. for(std::vector<std::string>::iterator i = this->Configurations.begin();
  532. i != this->Configurations.end(); ++i)
  533. {
  534. fout << "\t\tConfigName." << c << " = " << *i << "\n";
  535. c++;
  536. }
  537. fout << "\tEndGlobalSection\n";
  538. // Write out project(target) depends
  539. fout << "\tGlobalSection(ProjectDependencies) = postSolution\n";
  540. this->WriteTargetDepends(fout, orderedProjectTargets);
  541. fout << "\tEndGlobalSection\n";
  542. if (useFolderProperty)
  543. {
  544. // Write out project folders
  545. fout << "\tGlobalSection(NestedProjects) = preSolution\n";
  546. this->WriteFoldersContent(fout);
  547. fout << "\tEndGlobalSection\n";
  548. }
  549. // Write out the configurations for all the targets in the project
  550. fout << "\tGlobalSection(ProjectConfiguration) = postSolution\n";
  551. this->WriteTargetConfigurations(fout, orderedProjectTargets);
  552. fout << "\tEndGlobalSection\n";
  553. // Write out global sections
  554. this->WriteSLNGlobalSections(fout, root);
  555. // Write the footer for the SLN file
  556. this->WriteSLNFooter(fout);
  557. }
  558. //----------------------------------------------------------------------------
  559. void cmGlobalVisualStudio7Generator::WriteFolders(std::ostream& fout)
  560. {
  561. const char *prefix = "CMAKE_FOLDER_GUID_";
  562. const std::string::size_type skip_prefix = strlen(prefix);
  563. std::string guidProjectTypeFolder = "2150E333-8FDC-42A3-9474-1A3956D46DE8";
  564. for(std::map<std::string,std::set<std::string> >::iterator iter =
  565. VisualStudioFolders.begin(); iter != VisualStudioFolders.end(); ++iter)
  566. {
  567. std::string fullName = iter->first;
  568. std::string guid = this->GetGUID(fullName.c_str());
  569. cmSystemTools::ReplaceString(fullName, "/", "\\");
  570. if (cmSystemTools::StringStartsWith(fullName.c_str(), prefix))
  571. {
  572. fullName = fullName.substr(skip_prefix);
  573. }
  574. std::string nameOnly = cmSystemTools::GetFilenameName(fullName);
  575. fout << "Project(\"{" <<
  576. guidProjectTypeFolder << "}\") = \"" <<
  577. nameOnly << "\", \"" <<
  578. fullName << "\", \"{" <<
  579. guid <<
  580. "}\"\nEndProject\n";
  581. }
  582. }
  583. //----------------------------------------------------------------------------
  584. void cmGlobalVisualStudio7Generator::WriteFoldersContent(std::ostream& fout)
  585. {
  586. for(std::map<std::string,std::set<std::string> >::iterator iter =
  587. VisualStudioFolders.begin(); iter != VisualStudioFolders.end(); ++iter)
  588. {
  589. std::string key(iter->first);
  590. std::string guidParent(this->GetGUID(key.c_str()));
  591. for(std::set<std::string>::iterator it = iter->second.begin();
  592. it != iter->second.end(); ++it)
  593. {
  594. std::string value(*it);
  595. std::string guid(this->GetGUID(value.c_str()));
  596. fout << "\t\t{" << guid << "} = {" << guidParent << "}\n";
  597. }
  598. }
  599. }
  600. //----------------------------------------------------------------------------
  601. std::string
  602. cmGlobalVisualStudio7Generator::ConvertToSolutionPath(const char* path)
  603. {
  604. // Convert to backslashes. Do not use ConvertToOutputPath because
  605. // we will add quoting ourselves, and we know these projects always
  606. // use windows slashes.
  607. std::string d = path;
  608. std::string::size_type pos = 0;
  609. while((pos = d.find('/', pos)) != d.npos)
  610. {
  611. d[pos++] = '\\';
  612. }
  613. return d;
  614. }
  615. // Write a dsp file into the SLN file,
  616. // Note, that dependencies from executables to
  617. // the libraries it uses are also done here
  618. void cmGlobalVisualStudio7Generator::WriteProject(std::ostream& fout,
  619. const std::string& dspname,
  620. const char* dir, cmTarget const& target)
  621. {
  622. // check to see if this is a fortran build
  623. const char* ext = ".vcproj";
  624. const char* project =
  625. "Project(\"{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}\") = \"";
  626. if(this->TargetIsFortranOnly(target))
  627. {
  628. ext = ".vfproj";
  629. project = "Project(\"{6989167D-11E4-40FE-8C1A-2192A86A7E90}\") = \"";
  630. }
  631. fout << project
  632. << dspname << "\", \""
  633. << this->ConvertToSolutionPath(dir) << (dir[0]? "\\":"")
  634. << dspname << ext << "\", \"{"
  635. << this->GetGUID(dspname) << "}\"\nEndProject\n";
  636. UtilityDependsMap::iterator ui = this->UtilityDepends.find(&target);
  637. if(ui != this->UtilityDepends.end())
  638. {
  639. const char* uname = ui->second.c_str();
  640. fout << "Project(\"{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}\") = \""
  641. << uname << "\", \""
  642. << this->ConvertToSolutionPath(dir) << (dir[0]? "\\":"")
  643. << uname << ".vcproj" << "\", \"{"
  644. << this->GetGUID(uname) << "}\"\n"
  645. << "EndProject\n";
  646. }
  647. }
  648. // Write a dsp file into the SLN file,
  649. // Note, that dependencies from executables to
  650. // the libraries it uses are also done here
  651. void
  652. cmGlobalVisualStudio7Generator
  653. ::WriteProjectDepends(std::ostream& fout,
  654. const std::string& dspname,
  655. const char*, cmTarget const& target)
  656. {
  657. int depcount = 0;
  658. std::string dspguid = this->GetGUID(dspname);
  659. VSDependSet const& depends = this->VSTargetDepends[&target];
  660. for(VSDependSet::const_iterator di = depends.begin();
  661. di != depends.end(); ++di)
  662. {
  663. const char* name = di->c_str();
  664. std::string guid = this->GetGUID(name);
  665. if(guid.size() == 0)
  666. {
  667. std::string m = "Target: ";
  668. m += target.GetName();
  669. m += " depends on unknown target: ";
  670. m += name;
  671. cmSystemTools::Error(m.c_str());
  672. }
  673. fout << "\t\t{" << dspguid << "}." << depcount << " = {" << guid << "}\n";
  674. depcount++;
  675. }
  676. UtilityDependsMap::iterator ui = this->UtilityDepends.find(&target);
  677. if(ui != this->UtilityDepends.end())
  678. {
  679. const char* uname = ui->second.c_str();
  680. fout << "\t\t{" << this->GetGUID(uname) << "}.0 = {" << dspguid << "}\n";
  681. }
  682. }
  683. // Write a dsp file into the SLN file, Note, that dependencies from
  684. // executables to the libraries it uses are also done here
  685. void cmGlobalVisualStudio7Generator
  686. ::WriteProjectConfigurations(
  687. std::ostream& fout, const std::string& name, cmTarget::TargetType,
  688. const std::set<std::string>& configsPartOfDefaultBuild,
  689. const std::string& platformMapping)
  690. {
  691. const std::string& platformName =
  692. !platformMapping.empty() ? platformMapping : this->GetPlatformName();
  693. std::string guid = this->GetGUID(name);
  694. for(std::vector<std::string>::iterator i = this->Configurations.begin();
  695. i != this->Configurations.end(); ++i)
  696. {
  697. fout << "\t\t{" << guid << "}." << *i
  698. << ".ActiveCfg = " << *i << "|" << platformName << "\n";
  699. std::set<std::string>::const_iterator
  700. ci = configsPartOfDefaultBuild.find(*i);
  701. if(!(ci == configsPartOfDefaultBuild.end()))
  702. {
  703. fout << "\t\t{" << guid << "}." << *i
  704. << ".Build.0 = " << *i << "|" << platformName << "\n";
  705. }
  706. }
  707. }
  708. // Write a dsp file into the SLN file,
  709. // Note, that dependencies from executables to
  710. // the libraries it uses are also done here
  711. void cmGlobalVisualStudio7Generator::WriteExternalProject(std::ostream& fout,
  712. const std::string& name,
  713. const char* location,
  714. const char* typeGuid,
  715. const std::set<std::string>&)
  716. {
  717. fout << "Project("
  718. << "\"{"
  719. << (typeGuid ? typeGuid : this->ExternalProjectType(location))
  720. << "}\") = \""
  721. << name << "\", \""
  722. << this->ConvertToSolutionPath(location) << "\", \"{"
  723. << this->GetGUID(name)
  724. << "}\"\n";
  725. fout << "EndProject\n";
  726. }
  727. void cmGlobalVisualStudio7Generator
  728. ::WriteSLNGlobalSections(std::ostream& fout,
  729. cmLocalGenerator* root)
  730. {
  731. bool extensibilityGlobalsOverridden = false;
  732. bool extensibilityAddInsOverridden = false;
  733. const cmPropertyMap& props = root->GetMakefile()->GetProperties();
  734. for(cmPropertyMap::const_iterator itProp = props.begin();
  735. itProp != props.end(); ++itProp)
  736. {
  737. if(itProp->first.find("VS_GLOBAL_SECTION_") == 0)
  738. {
  739. std::string sectionType;
  740. std::string name = itProp->first.substr(18);
  741. if(name.find("PRE_") == 0)
  742. {
  743. name = name.substr(4);
  744. sectionType = "preSolution";
  745. }
  746. else if(name.find("POST_") == 0)
  747. {
  748. name = name.substr(5);
  749. sectionType = "postSolution";
  750. }
  751. else
  752. continue;
  753. if(!name.empty())
  754. {
  755. if(name == "ExtensibilityGlobals" && sectionType == "postSolution")
  756. extensibilityGlobalsOverridden = true;
  757. else if(name == "ExtensibilityAddIns" && sectionType == "postSolution")
  758. extensibilityAddInsOverridden = true;
  759. fout << "\tGlobalSection(" << name << ") = " << sectionType << "\n";
  760. std::vector<std::string> keyValuePairs;
  761. cmSystemTools::ExpandListArgument(itProp->second.GetValue(),
  762. keyValuePairs);
  763. for(std::vector<std::string>::const_iterator itPair =
  764. keyValuePairs.begin(); itPair != keyValuePairs.end(); ++itPair)
  765. {
  766. const std::string::size_type posEqual = itPair->find('=');
  767. if(posEqual != std::string::npos)
  768. {
  769. const std::string key =
  770. cmSystemTools::TrimWhitespace(itPair->substr(0, posEqual));
  771. const std::string value =
  772. cmSystemTools::TrimWhitespace(itPair->substr(posEqual + 1));
  773. fout << "\t\t" << key << " = " << value << "\n";
  774. }
  775. }
  776. fout << "\tEndGlobalSection\n";
  777. }
  778. }
  779. }
  780. if(!extensibilityGlobalsOverridden)
  781. fout << "\tGlobalSection(ExtensibilityGlobals) = postSolution\n"
  782. << "\tEndGlobalSection\n";
  783. if(!extensibilityAddInsOverridden)
  784. fout << "\tGlobalSection(ExtensibilityAddIns) = postSolution\n"
  785. << "\tEndGlobalSection\n";
  786. }
  787. // Standard end of dsw file
  788. void cmGlobalVisualStudio7Generator::WriteSLNFooter(std::ostream& fout)
  789. {
  790. fout << "EndGlobal\n";
  791. }
  792. // ouput standard header for dsw file
  793. void cmGlobalVisualStudio7Generator::WriteSLNHeader(std::ostream& fout)
  794. {
  795. fout << "Microsoft Visual Studio Solution File, Format Version 7.00\n";
  796. }
  797. //----------------------------------------------------------------------------
  798. std::string
  799. cmGlobalVisualStudio7Generator::WriteUtilityDepend(cmTarget const* target)
  800. {
  801. std::string pname = target->GetName();
  802. pname += "_UTILITY";
  803. std::string fname = target->GetMakefile()->GetStartOutputDirectory();
  804. fname += "/";
  805. fname += pname;
  806. fname += ".vcproj";
  807. cmGeneratedFileStream fout(fname.c_str());
  808. fout.SetCopyIfDifferent(true);
  809. this->CreateGUID(pname.c_str());
  810. std::string guid = this->GetGUID(pname.c_str());
  811. fout <<
  812. "<?xml version=\"1.0\" encoding = \""
  813. << this->Encoding() << "\"?>\n"
  814. "<VisualStudioProject\n"
  815. "\tProjectType=\"Visual C++\"\n"
  816. "\tVersion=\"" << this->GetIDEVersion() << "0\"\n"
  817. "\tName=\"" << pname << "\"\n"
  818. "\tProjectGUID=\"{" << guid << "}\"\n"
  819. "\tKeyword=\"Win32Proj\">\n"
  820. "\t<Platforms><Platform Name=\"Win32\"/></Platforms>\n"
  821. "\t<Configurations>\n"
  822. ;
  823. for(std::vector<std::string>::iterator i = this->Configurations.begin();
  824. i != this->Configurations.end(); ++i)
  825. {
  826. fout <<
  827. "\t\t<Configuration\n"
  828. "\t\t\tName=\"" << *i << "|Win32\"\n"
  829. "\t\t\tOutputDirectory=\"" << *i << "\"\n"
  830. "\t\t\tIntermediateDirectory=\"" << pname << ".dir\\" << *i << "\"\n"
  831. "\t\t\tConfigurationType=\"10\"\n"
  832. "\t\t\tUseOfMFC=\"0\"\n"
  833. "\t\t\tATLMinimizesCRunTimeLibraryUsage=\"FALSE\"\n"
  834. "\t\t\tCharacterSet=\"2\">\n"
  835. "\t\t</Configuration>\n"
  836. ;
  837. }
  838. fout <<
  839. "\t</Configurations>\n"
  840. "\t<Files></Files>\n"
  841. "\t<Globals></Globals>\n"
  842. "</VisualStudioProject>\n"
  843. ;
  844. if(fout.Close())
  845. {
  846. this->FileReplacedDuringGenerate(fname);
  847. }
  848. return pname;
  849. }
  850. std::string cmGlobalVisualStudio7Generator::GetGUID(const std::string& name)
  851. {
  852. std::string guidStoreName = name;
  853. guidStoreName += "_GUID_CMAKE";
  854. const char* storedGUID =
  855. this->CMakeInstance->GetCacheDefinition(guidStoreName.c_str());
  856. if(storedGUID)
  857. {
  858. return std::string(storedGUID);
  859. }
  860. cmSystemTools::Error("Unknown Target referenced : ",
  861. name.c_str());
  862. return "";
  863. }
  864. void cmGlobalVisualStudio7Generator::CreateGUID(const std::string& name)
  865. {
  866. std::string guidStoreName = name;
  867. guidStoreName += "_GUID_CMAKE";
  868. if(this->CMakeInstance->GetCacheDefinition(guidStoreName.c_str()))
  869. {
  870. return;
  871. }
  872. std::string ret;
  873. UUID uid;
  874. unsigned short *uidstr;
  875. UuidCreate(&uid);
  876. UuidToStringW(&uid,&uidstr);
  877. ret = cmsys::Encoding::ToNarrow(reinterpret_cast<wchar_t*>(uidstr));
  878. RpcStringFreeW(&uidstr);
  879. ret = cmSystemTools::UpperCase(ret);
  880. this->CMakeInstance->AddCacheEntry(guidStoreName.c_str(),
  881. ret.c_str(), "Stored GUID",
  882. cmCacheManager::INTERNAL);
  883. }
  884. std::vector<std::string> *cmGlobalVisualStudio7Generator::GetConfigurations()
  885. {
  886. return &this->Configurations;
  887. };
  888. //----------------------------------------------------------------------------
  889. void cmGlobalVisualStudio7Generator
  890. ::GetDocumentation(cmDocumentationEntry& entry)
  891. {
  892. entry.Name = cmGlobalVisualStudio7Generator::GetActualName();
  893. entry.Brief = "Generates Visual Studio .NET 2002 project files.";
  894. }
  895. //----------------------------------------------------------------------------
  896. void
  897. cmGlobalVisualStudio7Generator
  898. ::AppendDirectoryForConfig(const std::string& prefix,
  899. const std::string& config,
  900. const std::string& suffix,
  901. std::string& dir)
  902. {
  903. if(!config.empty())
  904. {
  905. dir += prefix;
  906. dir += config;
  907. dir += suffix;
  908. }
  909. }
  910. std::set<std::string>
  911. cmGlobalVisualStudio7Generator::IsPartOfDefaultBuild(
  912. OrderedTargetDependSet const& projectTargets, cmTarget const* target)
  913. {
  914. std::set<std::string> activeConfigs;
  915. // if it is a utilitiy target then only make it part of the
  916. // default build if another target depends on it
  917. int type = target->GetType();
  918. if (type == cmTarget::GLOBAL_TARGET)
  919. {
  920. return activeConfigs;
  921. }
  922. if(type == cmTarget::UTILITY && !this->IsDependedOn(projectTargets, target))
  923. {
  924. return activeConfigs;
  925. }
  926. // inspect EXCLUDE_FROM_DEFAULT_BUILD[_<CONFIG>] properties
  927. for(std::vector<std::string>::iterator i = this->Configurations.begin();
  928. i != this->Configurations.end(); ++i)
  929. {
  930. const char* propertyValue =
  931. target->GetFeature("EXCLUDE_FROM_DEFAULT_BUILD", i->c_str());
  932. if(cmSystemTools::IsOff(propertyValue))
  933. {
  934. activeConfigs.insert(*i);
  935. }
  936. }
  937. return activeConfigs;
  938. }
  939. bool
  940. cmGlobalVisualStudio7Generator
  941. ::IsDependedOn(OrderedTargetDependSet const& projectTargets,
  942. cmTarget const* targetIn)
  943. {
  944. for (OrderedTargetDependSet::const_iterator l = projectTargets.begin();
  945. l != projectTargets.end(); ++l)
  946. {
  947. cmTarget const& target = **l;
  948. TargetDependSet const& tgtdeps = this->GetTargetDirectDepends(target);
  949. if(tgtdeps.count(targetIn))
  950. {
  951. return true;
  952. }
  953. }
  954. return false;
  955. }
  956. //----------------------------------------------------------------------------
  957. static cmVS7FlagTable cmVS7ExtraFlagTable[] =
  958. {
  959. // Precompiled header and related options. Note that the
  960. // UsePrecompiledHeader entries are marked as "Continue" so that the
  961. // corresponding PrecompiledHeaderThrough entry can be found.
  962. {"UsePrecompiledHeader", "YX", "Automatically Generate", "2",
  963. cmVS7FlagTable::UserValueIgnored | cmVS7FlagTable::Continue},
  964. {"PrecompiledHeaderThrough", "YX", "Precompiled Header Name", "",
  965. cmVS7FlagTable::UserValueRequired},
  966. {"UsePrecompiledHeader", "Yu", "Use Precompiled Header", "3",
  967. cmVS7FlagTable::UserValueIgnored | cmVS7FlagTable::Continue},
  968. {"PrecompiledHeaderThrough", "Yu", "Precompiled Header Name", "",
  969. cmVS7FlagTable::UserValueRequired},
  970. {"WholeProgramOptimization", "LTCG", "WholeProgramOptimization", "true", 0},
  971. // Exception handling mode. If no entries match, it will be FALSE.
  972. {"ExceptionHandling", "GX", "enable c++ exceptions", "true", 0},
  973. {"ExceptionHandling", "EHsc", "enable c++ exceptions", "true", 0},
  974. // The EHa option does not have an IDE setting. Let it go to false,
  975. // and have EHa passed on the command line by leaving out the table
  976. // entry.
  977. {0,0,0,0,0}
  978. };
  979. cmIDEFlagTable const* cmGlobalVisualStudio7Generator::GetExtraFlagTableVS7()
  980. {
  981. return cmVS7ExtraFlagTable;
  982. }
  983. std::string cmGlobalVisualStudio7Generator::Encoding()
  984. {
  985. std::ostringstream encoding;
  986. #ifdef CMAKE_ENCODING_UTF8
  987. encoding << "UTF-8";
  988. #else
  989. encoding << "Windows-1252";
  990. #endif
  991. return encoding.str();
  992. }