cmGlobalVisualStudio7Generator.cxx 34 KB

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