cmGlobalVisualStudio7Generator.cxx 34 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. //----------------------------------------------------------------------------
  19. static cmVS7FlagTable cmVS7ExtraFlagTable[] =
  20. {
  21. // Precompiled header and related options. Note that the
  22. // UsePrecompiledHeader entries are marked as "Continue" so that the
  23. // corresponding PrecompiledHeaderThrough entry can be found.
  24. {"UsePrecompiledHeader", "YX", "Automatically Generate", "2",
  25. cmVS7FlagTable::UserValueIgnored | cmVS7FlagTable::Continue},
  26. {"PrecompiledHeaderThrough", "YX", "Precompiled Header Name", "",
  27. cmVS7FlagTable::UserValueRequired},
  28. {"UsePrecompiledHeader", "Yu", "Use Precompiled Header", "3",
  29. cmVS7FlagTable::UserValueIgnored | cmVS7FlagTable::Continue},
  30. {"PrecompiledHeaderThrough", "Yu", "Precompiled Header Name", "",
  31. cmVS7FlagTable::UserValueRequired},
  32. {"WholeProgramOptimization", "LTCG", "WholeProgramOptimization", "true", 0},
  33. // Exception handling mode. If no entries match, it will be FALSE.
  34. {"ExceptionHandling", "GX", "enable c++ exceptions", "true", 0},
  35. {"ExceptionHandling", "EHsc", "enable c++ exceptions", "true", 0},
  36. // The EHa option does not have an IDE setting. Let it go to false,
  37. // and have EHa passed on the command line by leaving out the table
  38. // entry.
  39. {0,0,0,0,0}
  40. };
  41. cmGlobalVisualStudio7Generator::cmGlobalVisualStudio7Generator(cmake *cm,
  42. const std::string& platformName)
  43. : cmGlobalVisualStudioGenerator(cm)
  44. {
  45. this->IntelProjectVersion = 0;
  46. this->DevEnvCommandInitialized = false;
  47. this->MasmEnabled = false;
  48. if (platformName.empty())
  49. {
  50. this->DefaultPlatformName = "Win32";
  51. }
  52. else
  53. {
  54. this->DefaultPlatformName = platformName;
  55. }
  56. this->ExtraFlagTable = cmVS7ExtraFlagTable;
  57. this->Version = VS7;
  58. }
  59. cmGlobalVisualStudio7Generator::~cmGlobalVisualStudio7Generator()
  60. {
  61. free(this->IntelProjectVersion);
  62. }
  63. // Package GUID of Intel Visual Fortran plugin to VS IDE
  64. #define CM_INTEL_PLUGIN_GUID "{B68A201D-CB9B-47AF-A52F-7EEC72E217E4}"
  65. const char* cmGlobalVisualStudio7Generator::GetIntelProjectVersion()
  66. {
  67. if(!this->IntelProjectVersion)
  68. {
  69. // Compute the version of the Intel plugin to the VS IDE.
  70. // If the key does not exist then use a default guess.
  71. std::string intelVersion;
  72. std::string vskey = this->GetRegistryBase();
  73. vskey += "\\Packages\\" CM_INTEL_PLUGIN_GUID ";ProductVersion";
  74. cmSystemTools::ReadRegistryValue(vskey.c_str(), intelVersion,
  75. cmSystemTools::KeyWOW64_32);
  76. unsigned int intelVersionNumber = ~0u;
  77. sscanf(intelVersion.c_str(), "%u", &intelVersionNumber);
  78. if(intelVersionNumber >= 11)
  79. {
  80. // Default to latest known project file version.
  81. intelVersion = "11.0";
  82. }
  83. else if(intelVersionNumber == 10)
  84. {
  85. // Version 10.x actually uses 9.10 in project files!
  86. intelVersion = "9.10";
  87. }
  88. else
  89. {
  90. // Version <= 9: use ProductVersion from registry.
  91. }
  92. this->IntelProjectVersion = strdup(intelVersion.c_str());
  93. }
  94. return this->IntelProjectVersion;
  95. }
  96. void cmGlobalVisualStudio7Generator
  97. ::EnableLanguage(std::vector<std::string>const & lang,
  98. cmMakefile *mf, bool optional)
  99. {
  100. mf->AddDefinition("CMAKE_GENERATOR_RC", "rc");
  101. mf->AddDefinition("CMAKE_GENERATOR_NO_COMPILER_ENV", "1");
  102. if(!mf->GetDefinition("CMAKE_CONFIGURATION_TYPES"))
  103. {
  104. mf->AddCacheDefinition(
  105. "CMAKE_CONFIGURATION_TYPES",
  106. "Debug;Release;MinSizeRel;RelWithDebInfo",
  107. "Semicolon separated list of supported configuration types, "
  108. "only supports Debug, Release, MinSizeRel, and RelWithDebInfo, "
  109. "anything else will be ignored.",
  110. cmState::STRING);
  111. }
  112. // Create list of configurations requested by user's cache, if any.
  113. this->cmGlobalGenerator::EnableLanguage(lang, mf, optional);
  114. // if this environment variable is set, then copy it to
  115. // a static cache entry. It will be used by
  116. // cmLocalGenerator::ConstructScript, to add an extra PATH
  117. // to all custom commands. This is because the VS IDE
  118. // does not use the environment it is run in, and this allows
  119. // for running commands and using dll's that the IDE environment
  120. // does not know about.
  121. const char* extraPath = cmSystemTools::GetEnv("CMAKE_MSVCIDE_RUN_PATH");
  122. if(extraPath)
  123. {
  124. mf->AddCacheDefinition
  125. ("CMAKE_MSVCIDE_RUN_PATH", extraPath,
  126. "Saved environment variable CMAKE_MSVCIDE_RUN_PATH",
  127. cmState::STATIC);
  128. }
  129. }
  130. //----------------------------------------------------------------------------
  131. void cmGlobalVisualStudio7Generator::FindMakeProgram(cmMakefile* mf)
  132. {
  133. this->cmGlobalVisualStudioGenerator::FindMakeProgram(mf);
  134. mf->AddDefinition("CMAKE_VS_DEVENV_COMMAND",
  135. this->GetDevEnvCommand().c_str());
  136. }
  137. //----------------------------------------------------------------------------
  138. std::string const& cmGlobalVisualStudio7Generator::GetDevEnvCommand()
  139. {
  140. if(!this->DevEnvCommandInitialized)
  141. {
  142. this->DevEnvCommandInitialized = true;
  143. this->DevEnvCommand = this->FindDevEnvCommand();
  144. }
  145. return this->DevEnvCommand;
  146. }
  147. //----------------------------------------------------------------------------
  148. std::string cmGlobalVisualStudio7Generator::FindDevEnvCommand()
  149. {
  150. std::string vscmd;
  151. std::string vskey = this->GetRegistryBase() + ";InstallDir";
  152. if(cmSystemTools::ReadRegistryValue(vskey.c_str(), vscmd,
  153. cmSystemTools::KeyWOW64_32))
  154. {
  155. cmSystemTools::ConvertToUnixSlashes(vscmd);
  156. vscmd += "/";
  157. }
  158. vscmd += "devenv.com";
  159. return vscmd;
  160. }
  161. //----------------------------------------------------------------------------
  162. const char* cmGlobalVisualStudio7Generator::ExternalProjectType(
  163. const char* location)
  164. {
  165. std::string extension = cmSystemTools::GetFilenameLastExtension(location);
  166. if (extension == ".vbproj")
  167. {
  168. return "F184B08F-C81C-45F6-A57F-5ABD9991F28F";
  169. }
  170. else if (extension == ".csproj")
  171. {
  172. return "FAE04EC0-301F-11D3-BF4B-00C04F79EFBC";
  173. }
  174. else if (extension == ".fsproj")
  175. {
  176. return "F2A71F9B-5D33-465A-A702-920D77279786";
  177. }
  178. else if (extension == ".vdproj")
  179. {
  180. return "54435603-DBB4-11D2-8724-00A0C9A8B90C";
  181. }
  182. else if (extension == ".dbproj")
  183. {
  184. return "C8D11400-126E-41CD-887F-60BD40844F9E";
  185. }
  186. else if (extension == ".wixproj")
  187. {
  188. return "930C7802-8A8C-48F9-8165-68863BCCD9DD";
  189. }
  190. else if (extension == ".pyproj")
  191. {
  192. return "888888A0-9F3D-457C-B088-3A5042F75D52";
  193. }
  194. return "8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942";
  195. }
  196. //----------------------------------------------------------------------------
  197. void cmGlobalVisualStudio7Generator::GenerateBuildCommand(
  198. std::vector<std::string>& makeCommand,
  199. const std::string& makeProgram,
  200. const std::string& projectName,
  201. const std::string& /*projectDir*/,
  202. const std::string& targetName,
  203. const std::string& config,
  204. bool /*fast*/, bool /*verbose*/,
  205. std::vector<std::string> const& makeOptions)
  206. {
  207. // Select the caller- or user-preferred make program, else devenv.
  208. std::string makeProgramSelected =
  209. this->SelectMakeProgram(makeProgram, this->GetDevEnvCommand());
  210. // Ignore the above preference if it is msbuild.
  211. // Assume any other value is either a devenv or
  212. // command-line compatible with devenv.
  213. std::string makeProgramLower = makeProgramSelected;
  214. cmSystemTools::LowerCase(makeProgramLower);
  215. if(makeProgramLower.find("msbuild") != std::string::npos)
  216. {
  217. makeProgramSelected = this->GetDevEnvCommand();
  218. }
  219. makeCommand.push_back(makeProgramSelected);
  220. makeCommand.push_back(std::string(projectName) + ".sln");
  221. std::string realTarget = targetName;
  222. bool clean = false;
  223. if ( realTarget == "clean" )
  224. {
  225. clean = true;
  226. realTarget = "ALL_BUILD";
  227. }
  228. if(clean)
  229. {
  230. makeCommand.push_back("/clean");
  231. }
  232. else
  233. {
  234. makeCommand.push_back("/build");
  235. }
  236. if(!config.empty())
  237. {
  238. makeCommand.push_back(config);
  239. }
  240. else
  241. {
  242. makeCommand.push_back("Debug");
  243. }
  244. makeCommand.push_back("/project");
  245. if (!realTarget.empty())
  246. {
  247. makeCommand.push_back(realTarget);
  248. }
  249. else
  250. {
  251. makeCommand.push_back("ALL_BUILD");
  252. }
  253. makeCommand.insert(makeCommand.end(),
  254. makeOptions.begin(), makeOptions.end());
  255. }
  256. ///! Create a local generator appropriate to this Global Generator
  257. cmLocalGenerator *
  258. cmGlobalVisualStudio7Generator::CreateLocalGenerator(cmLocalGenerator* parent,
  259. cmState::Snapshot snapshot)
  260. {
  261. cmLocalVisualStudio7Generator *lg =
  262. new cmLocalVisualStudio7Generator(this, parent, 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.size() == 0)
  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;
  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;
  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;
  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.size() == 0)
  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 cmPropertyMap& props = root->GetMakefile()->GetProperties();
  736. for(cmPropertyMap::const_iterator itProp = props.begin();
  737. itProp != props.end(); ++itProp)
  738. {
  739. if(itProp->first.find("VS_GLOBAL_SECTION_") == 0)
  740. {
  741. std::string sectionType;
  742. std::string name = itProp->first.substr(18);
  743. if(name.find("PRE_") == 0)
  744. {
  745. name = name.substr(4);
  746. sectionType = "preSolution";
  747. }
  748. else if(name.find("POST_") == 0)
  749. {
  750. name = name.substr(5);
  751. sectionType = "postSolution";
  752. }
  753. else
  754. continue;
  755. if(!name.empty())
  756. {
  757. if(name == "ExtensibilityGlobals" && sectionType == "postSolution")
  758. extensibilityGlobalsOverridden = true;
  759. else if(name == "ExtensibilityAddIns" && sectionType == "postSolution")
  760. extensibilityAddInsOverridden = true;
  761. fout << "\tGlobalSection(" << name << ") = " << sectionType << "\n";
  762. std::vector<std::string> keyValuePairs;
  763. cmSystemTools::ExpandListArgument(itProp->second.GetValue(),
  764. keyValuePairs);
  765. for(std::vector<std::string>::const_iterator itPair =
  766. keyValuePairs.begin(); itPair != keyValuePairs.end(); ++itPair)
  767. {
  768. const std::string::size_type posEqual = itPair->find('=');
  769. if(posEqual != std::string::npos)
  770. {
  771. const std::string key =
  772. cmSystemTools::TrimWhitespace(itPair->substr(0, posEqual));
  773. const std::string value =
  774. cmSystemTools::TrimWhitespace(itPair->substr(posEqual + 1));
  775. fout << "\t\t" << key << " = " << value << "\n";
  776. }
  777. }
  778. fout << "\tEndGlobalSection\n";
  779. }
  780. }
  781. }
  782. if(!extensibilityGlobalsOverridden)
  783. fout << "\tGlobalSection(ExtensibilityGlobals) = postSolution\n"
  784. << "\tEndGlobalSection\n";
  785. if(!extensibilityAddInsOverridden)
  786. fout << "\tGlobalSection(ExtensibilityAddIns) = postSolution\n"
  787. << "\tEndGlobalSection\n";
  788. }
  789. // Standard end of dsw file
  790. void cmGlobalVisualStudio7Generator::WriteSLNFooter(std::ostream& fout)
  791. {
  792. fout << "EndGlobal\n";
  793. }
  794. // ouput standard header for dsw file
  795. void cmGlobalVisualStudio7Generator::WriteSLNHeader(std::ostream& fout)
  796. {
  797. fout << "Microsoft Visual Studio Solution File, Format Version 7.00\n";
  798. }
  799. //----------------------------------------------------------------------------
  800. std::string
  801. cmGlobalVisualStudio7Generator::WriteUtilityDepend(cmTarget const* target)
  802. {
  803. std::vector<std::string> configs;
  804. target->GetMakefile()->GetConfigurations(configs);
  805. std::string pname = target->GetName();
  806. pname += "_UTILITY";
  807. std::string fname = target->GetMakefile()->GetCurrentBinaryDirectory();
  808. fname += "/";
  809. fname += pname;
  810. fname += ".vcproj";
  811. cmGeneratedFileStream fout(fname.c_str());
  812. fout.SetCopyIfDifferent(true);
  813. std::string guid = this->GetGUID(pname.c_str());
  814. fout <<
  815. "<?xml version=\"1.0\" encoding = \""
  816. << this->Encoding() << "\"?>\n"
  817. "<VisualStudioProject\n"
  818. "\tProjectType=\"Visual C++\"\n"
  819. "\tVersion=\"" << this->GetIDEVersion() << "0\"\n"
  820. "\tName=\"" << pname << "\"\n"
  821. "\tProjectGUID=\"{" << guid << "}\"\n"
  822. "\tKeyword=\"Win32Proj\">\n"
  823. "\t<Platforms><Platform Name=\"Win32\"/></Platforms>\n"
  824. "\t<Configurations>\n"
  825. ;
  826. for(std::vector<std::string>::iterator i = configs.begin();
  827. i != configs.end(); ++i)
  828. {
  829. fout <<
  830. "\t\t<Configuration\n"
  831. "\t\t\tName=\"" << *i << "|Win32\"\n"
  832. "\t\t\tOutputDirectory=\"" << *i << "\"\n"
  833. "\t\t\tIntermediateDirectory=\"" << pname << ".dir\\" << *i << "\"\n"
  834. "\t\t\tConfigurationType=\"10\"\n"
  835. "\t\t\tUseOfMFC=\"0\"\n"
  836. "\t\t\tATLMinimizesCRunTimeLibraryUsage=\"FALSE\"\n"
  837. "\t\t\tCharacterSet=\"2\">\n"
  838. "\t\t</Configuration>\n"
  839. ;
  840. }
  841. fout <<
  842. "\t</Configurations>\n"
  843. "\t<Files></Files>\n"
  844. "\t<Globals></Globals>\n"
  845. "</VisualStudioProject>\n"
  846. ;
  847. if(fout.Close())
  848. {
  849. this->FileReplacedDuringGenerate(fname);
  850. }
  851. return pname;
  852. }
  853. //----------------------------------------------------------------------------
  854. std::string cmGlobalVisualStudio7Generator::GetGUID(std::string const& name)
  855. {
  856. std::string const& guidStoreName = name + "_GUID_CMAKE";
  857. if (const char* storedGUID =
  858. this->CMakeInstance->GetCacheDefinition(guidStoreName.c_str()))
  859. {
  860. return std::string(storedGUID);
  861. }
  862. // Compute a GUID that is deterministic but unique to the build tree.
  863. std::string input = this->CMakeInstance->GetState()->GetBinaryDirectory();
  864. input += "|";
  865. input += name;
  866. std::string md5 = cmSystemTools::ComputeStringMD5(input);
  867. assert(md5.length() == 32);
  868. std::string const& guid =
  869. (md5.substr( 0,8)+"-"+
  870. md5.substr( 8,4)+"-"+
  871. md5.substr(12,4)+"-"+
  872. md5.substr(16,4)+"-"+
  873. md5.substr(20,12));
  874. return cmSystemTools::UpperCase(guid);
  875. }
  876. //----------------------------------------------------------------------------
  877. void cmGlobalVisualStudio7Generator
  878. ::GetDocumentation(cmDocumentationEntry& entry)
  879. {
  880. entry.Name = cmGlobalVisualStudio7Generator::GetActualName();
  881. entry.Brief = "Deprecated. Generates Visual Studio .NET 2002 project files.";
  882. }
  883. //----------------------------------------------------------------------------
  884. void
  885. cmGlobalVisualStudio7Generator
  886. ::AppendDirectoryForConfig(const std::string& prefix,
  887. const std::string& config,
  888. const std::string& suffix,
  889. std::string& dir)
  890. {
  891. if(!config.empty())
  892. {
  893. dir += prefix;
  894. dir += config;
  895. dir += suffix;
  896. }
  897. }
  898. std::set<std::string>
  899. cmGlobalVisualStudio7Generator::IsPartOfDefaultBuild(
  900. std::vector<std::string> const& configs,
  901. OrderedTargetDependSet const& projectTargets, cmTarget const* target)
  902. {
  903. std::set<std::string> activeConfigs;
  904. // if it is a utilitiy target then only make it part of the
  905. // default build if another target depends on it
  906. int type = target->GetType();
  907. if (type == cmTarget::GLOBAL_TARGET)
  908. {
  909. // check if INSTALL target is part of default build
  910. if(target->GetName() == "INSTALL")
  911. {
  912. // inspect CMAKE_VS_INCLUDE_INSTALL_TO_DEFAULT_BUILD properties
  913. for(std::vector<std::string>::const_iterator i = configs.begin();
  914. i != configs.end(); ++i)
  915. {
  916. const char* propertyValue = target->GetMakefile()
  917. ->GetDefinition("CMAKE_VS_INCLUDE_INSTALL_TO_DEFAULT_BUILD");
  918. cmGeneratorExpression ge;
  919. cmsys::auto_ptr<cmCompiledGeneratorExpression>
  920. cge = ge.Parse(propertyValue);
  921. if(cmSystemTools::IsOn(cge->Evaluate(target->GetMakefile(), *i)))
  922. {
  923. activeConfigs.insert(*i);
  924. }
  925. }
  926. }
  927. return activeConfigs;
  928. }
  929. if(type == cmTarget::UTILITY && !this->IsDependedOn(projectTargets, target))
  930. {
  931. return activeConfigs;
  932. }
  933. cmGeneratorTarget* gt = this->GetGeneratorTarget(target);
  934. // inspect EXCLUDE_FROM_DEFAULT_BUILD[_<CONFIG>] properties
  935. for(std::vector<std::string>::const_iterator i = configs.begin();
  936. i != configs.end(); ++i)
  937. {
  938. const char* propertyValue =
  939. gt->GetFeature("EXCLUDE_FROM_DEFAULT_BUILD", i->c_str());
  940. if(cmSystemTools::IsOff(propertyValue))
  941. {
  942. activeConfigs.insert(*i);
  943. }
  944. }
  945. return activeConfigs;
  946. }
  947. bool
  948. cmGlobalVisualStudio7Generator
  949. ::IsDependedOn(OrderedTargetDependSet const& projectTargets,
  950. cmTarget const* targetIn)
  951. {
  952. for (OrderedTargetDependSet::const_iterator l = projectTargets.begin();
  953. l != projectTargets.end(); ++l)
  954. {
  955. cmTarget const& target = **l;
  956. TargetDependSet const& tgtdeps = this->GetTargetDirectDepends(target);
  957. if(tgtdeps.count(targetIn))
  958. {
  959. return true;
  960. }
  961. }
  962. return false;
  963. }
  964. std::string cmGlobalVisualStudio7Generator::Encoding()
  965. {
  966. std::ostringstream encoding;
  967. #ifdef CMAKE_ENCODING_UTF8
  968. encoding << "UTF-8";
  969. #else
  970. encoding << "Windows-1252";
  971. #endif
  972. return encoding.str();
  973. }