cmGlobalVisualStudio7Generator.cxx 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081
  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(
  42. const std::string& platformName)
  43. {
  44. this->IntelProjectVersion = 0;
  45. this->DevEnvCommandInitialized = false;
  46. this->MasmEnabled = false;
  47. if (platformName.empty())
  48. {
  49. this->DefaultPlatformName = "Win32";
  50. }
  51. else
  52. {
  53. this->DefaultPlatformName = platformName;
  54. }
  55. this->ExtraFlagTable = cmVS7ExtraFlagTable;
  56. this->Version = VS7;
  57. }
  58. cmGlobalVisualStudio7Generator::~cmGlobalVisualStudio7Generator()
  59. {
  60. free(this->IntelProjectVersion);
  61. }
  62. // Package GUID of Intel Visual Fortran plugin to VS IDE
  63. #define CM_INTEL_PLUGIN_GUID "{B68A201D-CB9B-47AF-A52F-7EEC72E217E4}"
  64. const char* cmGlobalVisualStudio7Generator::GetIntelProjectVersion()
  65. {
  66. if(!this->IntelProjectVersion)
  67. {
  68. // Compute the version of the Intel plugin to the VS IDE.
  69. // If the key does not exist then use a default guess.
  70. std::string intelVersion;
  71. std::string vskey = this->GetRegistryBase();
  72. vskey += "\\Packages\\" CM_INTEL_PLUGIN_GUID ";ProductVersion";
  73. cmSystemTools::ReadRegistryValue(vskey.c_str(), intelVersion,
  74. cmSystemTools::KeyWOW64_32);
  75. unsigned int intelVersionNumber = ~0u;
  76. sscanf(intelVersion.c_str(), "%u", &intelVersionNumber);
  77. if(intelVersionNumber >= 11)
  78. {
  79. // Default to latest known project file version.
  80. intelVersion = "11.0";
  81. }
  82. else if(intelVersionNumber == 10)
  83. {
  84. // Version 10.x actually uses 9.10 in project files!
  85. intelVersion = "9.10";
  86. }
  87. else
  88. {
  89. // Version <= 9: use ProductVersion from registry.
  90. }
  91. this->IntelProjectVersion = strdup(intelVersion.c_str());
  92. }
  93. return this->IntelProjectVersion;
  94. }
  95. void cmGlobalVisualStudio7Generator
  96. ::EnableLanguage(std::vector<std::string>const & lang,
  97. cmMakefile *mf, bool optional)
  98. {
  99. mf->AddDefinition("CMAKE_GENERATOR_RC", "rc");
  100. mf->AddDefinition("CMAKE_GENERATOR_NO_COMPILER_ENV", "1");
  101. if(!mf->GetDefinition("CMAKE_CONFIGURATION_TYPES"))
  102. {
  103. mf->AddCacheDefinition(
  104. "CMAKE_CONFIGURATION_TYPES",
  105. "Debug;Release;MinSizeRel;RelWithDebInfo",
  106. "Semicolon separated list of supported configuration types, "
  107. "only supports Debug, Release, MinSizeRel, and RelWithDebInfo, "
  108. "anything else will be ignored.",
  109. cmState::STRING);
  110. }
  111. // Create list of configurations requested by user's cache, if any.
  112. this->cmGlobalGenerator::EnableLanguage(lang, mf, optional);
  113. // if this environment variable is set, then copy it to
  114. // a static cache entry. It will be used by
  115. // cmLocalGenerator::ConstructScript, to add an extra PATH
  116. // to all custom commands. This is because the VS IDE
  117. // does not use the environment it is run in, and this allows
  118. // for running commands and using dll's that the IDE environment
  119. // does not know about.
  120. const char* extraPath = cmSystemTools::GetEnv("CMAKE_MSVCIDE_RUN_PATH");
  121. if(extraPath)
  122. {
  123. mf->AddCacheDefinition
  124. ("CMAKE_MSVCIDE_RUN_PATH", extraPath,
  125. "Saved environment variable CMAKE_MSVCIDE_RUN_PATH",
  126. cmState::STATIC);
  127. }
  128. }
  129. //----------------------------------------------------------------------------
  130. void cmGlobalVisualStudio7Generator::FindMakeProgram(cmMakefile* mf)
  131. {
  132. this->cmGlobalVisualStudioGenerator::FindMakeProgram(mf);
  133. mf->AddDefinition("CMAKE_VS_DEVENV_COMMAND",
  134. this->GetDevEnvCommand().c_str());
  135. }
  136. //----------------------------------------------------------------------------
  137. std::string const& cmGlobalVisualStudio7Generator::GetDevEnvCommand()
  138. {
  139. if(!this->DevEnvCommandInitialized)
  140. {
  141. this->DevEnvCommandInitialized = true;
  142. this->DevEnvCommand = this->FindDevEnvCommand();
  143. }
  144. return this->DevEnvCommand;
  145. }
  146. //----------------------------------------------------------------------------
  147. std::string cmGlobalVisualStudio7Generator::FindDevEnvCommand()
  148. {
  149. std::string vscmd;
  150. std::string vskey = this->GetRegistryBase() + ";InstallDir";
  151. if(cmSystemTools::ReadRegistryValue(vskey.c_str(), vscmd,
  152. cmSystemTools::KeyWOW64_32))
  153. {
  154. cmSystemTools::ConvertToUnixSlashes(vscmd);
  155. vscmd += "/";
  156. }
  157. vscmd += "devenv.com";
  158. return vscmd;
  159. }
  160. //----------------------------------------------------------------------------
  161. const char* cmGlobalVisualStudio7Generator::ExternalProjectType(
  162. const char* location)
  163. {
  164. std::string extension = cmSystemTools::GetFilenameLastExtension(location);
  165. if (extension == ".vbproj")
  166. {
  167. return "F184B08F-C81C-45F6-A57F-5ABD9991F28F";
  168. }
  169. else if (extension == ".csproj")
  170. {
  171. return "FAE04EC0-301F-11D3-BF4B-00C04F79EFBC";
  172. }
  173. else if (extension == ".fsproj")
  174. {
  175. return "F2A71F9B-5D33-465A-A702-920D77279786";
  176. }
  177. else if (extension == ".vdproj")
  178. {
  179. return "54435603-DBB4-11D2-8724-00A0C9A8B90C";
  180. }
  181. else if (extension == ".dbproj")
  182. {
  183. return "C8D11400-126E-41CD-887F-60BD40844F9E";
  184. }
  185. else if (extension == ".wixproj")
  186. {
  187. return "930C7802-8A8C-48F9-8165-68863BCCD9DD";
  188. }
  189. else if (extension == ".pyproj")
  190. {
  191. return "888888A0-9F3D-457C-B088-3A5042F75D52";
  192. }
  193. return "8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942";
  194. }
  195. //----------------------------------------------------------------------------
  196. void cmGlobalVisualStudio7Generator::GenerateBuildCommand(
  197. std::vector<std::string>& makeCommand,
  198. const std::string& makeProgram,
  199. const std::string& projectName,
  200. const std::string& /*projectDir*/,
  201. const std::string& targetName,
  202. const std::string& config,
  203. bool /*fast*/, bool /*verbose*/,
  204. std::vector<std::string> const& makeOptions)
  205. {
  206. // Select the caller- or user-preferred make program, else devenv.
  207. std::string makeProgramSelected =
  208. this->SelectMakeProgram(makeProgram, this->GetDevEnvCommand());
  209. // Ignore the above preference if it is msbuild.
  210. // Assume any other value is either a devenv or
  211. // command-line compatible with devenv.
  212. std::string makeProgramLower = makeProgramSelected;
  213. cmSystemTools::LowerCase(makeProgramLower);
  214. if(makeProgramLower.find("msbuild") != std::string::npos)
  215. {
  216. makeProgramSelected = this->GetDevEnvCommand();
  217. }
  218. makeCommand.push_back(makeProgramSelected);
  219. makeCommand.push_back(std::string(projectName) + ".sln");
  220. std::string realTarget = targetName;
  221. bool clean = false;
  222. if ( realTarget == "clean" )
  223. {
  224. clean = true;
  225. realTarget = "ALL_BUILD";
  226. }
  227. if(clean)
  228. {
  229. makeCommand.push_back("/clean");
  230. }
  231. else
  232. {
  233. makeCommand.push_back("/build");
  234. }
  235. if(!config.empty())
  236. {
  237. makeCommand.push_back(config);
  238. }
  239. else
  240. {
  241. makeCommand.push_back("Debug");
  242. }
  243. makeCommand.push_back("/project");
  244. if (!realTarget.empty())
  245. {
  246. makeCommand.push_back(realTarget);
  247. }
  248. else
  249. {
  250. makeCommand.push_back("ALL_BUILD");
  251. }
  252. makeCommand.insert(makeCommand.end(),
  253. makeOptions.begin(), makeOptions.end());
  254. }
  255. ///! Create a local generator appropriate to this Global Generator
  256. cmLocalGenerator *
  257. cmGlobalVisualStudio7Generator::CreateLocalGenerator(cmLocalGenerator* parent)
  258. {
  259. cmLocalVisualStudio7Generator *lg =
  260. new cmLocalVisualStudio7Generator(this, parent);
  261. return lg;
  262. }
  263. //----------------------------------------------------------------------------
  264. std::string const& cmGlobalVisualStudio7Generator::GetPlatformName() const
  265. {
  266. if(!this->GeneratorPlatform.empty())
  267. {
  268. return this->GeneratorPlatform;
  269. }
  270. return this->DefaultPlatformName;
  271. }
  272. //----------------------------------------------------------------------------
  273. bool cmGlobalVisualStudio7Generator::SetSystemName(std::string const& s,
  274. cmMakefile* mf)
  275. {
  276. mf->AddDefinition("CMAKE_VS_INTEL_Fortran_PROJECT_VERSION",
  277. this->GetIntelProjectVersion());
  278. return this->cmGlobalVisualStudioGenerator::SetSystemName(s, mf);
  279. }
  280. //----------------------------------------------------------------------------
  281. bool cmGlobalVisualStudio7Generator::SetGeneratorPlatform(std::string const& p,
  282. cmMakefile* mf)
  283. {
  284. if(this->GetPlatformName() == "x64")
  285. {
  286. mf->AddDefinition("CMAKE_FORCE_WIN64", "TRUE");
  287. }
  288. else if(this->GetPlatformName() == "Itanium")
  289. {
  290. mf->AddDefinition("CMAKE_FORCE_IA64", "TRUE");
  291. }
  292. mf->AddDefinition("CMAKE_VS_PLATFORM_NAME", this->GetPlatformName().c_str());
  293. return this->cmGlobalVisualStudioGenerator::SetGeneratorPlatform(p, mf);
  294. }
  295. void cmGlobalVisualStudio7Generator::Generate()
  296. {
  297. // first do the superclass method
  298. this->cmGlobalVisualStudioGenerator::Generate();
  299. // Now write out the DSW
  300. this->OutputSLNFile();
  301. // If any solution or project files changed during the generation,
  302. // tell Visual Studio to reload them...
  303. if(!cmSystemTools::GetErrorOccuredFlag())
  304. {
  305. this->CallVisualStudioMacro(MacroReload);
  306. }
  307. if (!this->CMakeInstance->GetIsInTryCompile() &&
  308. this->GetName() == "Visual Studio 7")
  309. {
  310. const char* cmakeWarnVS70 =
  311. this->CMakeInstance->GetState()->GetCacheEntryValue("CMAKE_WARN_VS70");
  312. if (!cmakeWarnVS70 || !cmSystemTools::IsOff(cmakeWarnVS70))
  313. {
  314. this->CMakeInstance->IssueMessage(
  315. cmake::WARNING,
  316. "The \"Visual Studio 7\" generator is deprecated "
  317. "and will be removed in a future version of CMake."
  318. "\n"
  319. "Add CMAKE_WARN_VS70=OFF to the cache to disable this warning."
  320. );
  321. }
  322. }
  323. }
  324. void cmGlobalVisualStudio7Generator
  325. ::OutputSLNFile(cmLocalGenerator* root,
  326. std::vector<cmLocalGenerator*>& generators)
  327. {
  328. if(generators.size() == 0)
  329. {
  330. return;
  331. }
  332. this->CurrentProject = root->GetMakefile()->GetProjectName();
  333. std::string fname = root->GetMakefile()->GetCurrentBinaryDirectory();
  334. fname += "/";
  335. fname += root->GetMakefile()->GetProjectName();
  336. fname += ".sln";
  337. cmGeneratedFileStream fout(fname.c_str());
  338. fout.SetCopyIfDifferent(true);
  339. if(!fout)
  340. {
  341. return;
  342. }
  343. this->WriteSLNFile(fout, root, generators);
  344. if (fout.Close())
  345. {
  346. this->FileReplacedDuringGenerate(fname);
  347. }
  348. }
  349. // output the SLN file
  350. void cmGlobalVisualStudio7Generator::OutputSLNFile()
  351. {
  352. std::map<std::string, std::vector<cmLocalGenerator*> >::iterator it;
  353. for(it = this->ProjectMap.begin(); it!= this->ProjectMap.end(); ++it)
  354. {
  355. this->OutputSLNFile(it->second[0], it->second);
  356. }
  357. }
  358. void cmGlobalVisualStudio7Generator::WriteTargetConfigurations(
  359. std::ostream& fout,
  360. std::vector<std::string> const& configs,
  361. OrderedTargetDependSet const& projectTargets)
  362. {
  363. // loop over again and write out configurations for each target
  364. // in the solution
  365. for(OrderedTargetDependSet::const_iterator tt =
  366. projectTargets.begin(); tt != projectTargets.end(); ++tt)
  367. {
  368. cmTarget const* target = *tt;
  369. if(target->GetType() == cmTarget::INTERFACE_LIBRARY)
  370. {
  371. continue;
  372. }
  373. const char* expath = target->GetProperty("EXTERNAL_MSPROJECT");
  374. if(expath)
  375. {
  376. std::set<std::string> allConfigurations(configs.begin(), configs.end());
  377. const char* mapping = target->GetProperty("VS_PLATFORM_MAPPING");
  378. this->WriteProjectConfigurations(
  379. fout, target->GetName().c_str(), target->GetType(),
  380. configs, allConfigurations, mapping ? mapping : "");
  381. }
  382. else
  383. {
  384. const std::set<std::string>& configsPartOfDefaultBuild =
  385. this->IsPartOfDefaultBuild(configs, projectTargets, target);
  386. const char *vcprojName =
  387. target->GetProperty("GENERATOR_FILE_NAME");
  388. if (vcprojName)
  389. {
  390. this->WriteProjectConfigurations(fout, vcprojName, target->GetType(),
  391. configs, configsPartOfDefaultBuild);
  392. }
  393. }
  394. }
  395. }
  396. void cmGlobalVisualStudio7Generator::WriteTargetsToSolution(
  397. std::ostream& fout,
  398. cmLocalGenerator* root,
  399. OrderedTargetDependSet const& projectTargets)
  400. {
  401. VisualStudioFolders.clear();
  402. for(OrderedTargetDependSet::const_iterator tt =
  403. projectTargets.begin(); tt != projectTargets.end(); ++tt)
  404. {
  405. cmTarget const* target = *tt;
  406. if(target->GetType() == cmTarget::INTERFACE_LIBRARY)
  407. {
  408. continue;
  409. }
  410. bool written = false;
  411. // handle external vc project files
  412. const char* expath = target->GetProperty("EXTERNAL_MSPROJECT");
  413. if(expath)
  414. {
  415. std::string project = target->GetName();
  416. std::string location = expath;
  417. this->WriteExternalProject(fout,
  418. project.c_str(),
  419. location.c_str(),
  420. target->GetProperty("VS_PROJECT_TYPE"),
  421. target->GetUtilities());
  422. written = true;
  423. }
  424. else
  425. {
  426. const char *vcprojName =
  427. target->GetProperty("GENERATOR_FILE_NAME");
  428. if(vcprojName)
  429. {
  430. cmMakefile* tmf = target->GetMakefile();
  431. std::string dir = tmf->GetCurrentBinaryDirectory();
  432. dir = root->Convert(dir.c_str(),
  433. cmLocalGenerator::START_OUTPUT);
  434. if(dir == ".")
  435. {
  436. dir = ""; // msbuild cannot handle ".\" prefix
  437. }
  438. this->WriteProject(fout, vcprojName, dir.c_str(),
  439. *target);
  440. written = true;
  441. }
  442. }
  443. // Create "solution folder" information from FOLDER target property
  444. //
  445. if (written && this->UseFolderProperty())
  446. {
  447. const char *targetFolder = target->GetProperty("FOLDER");
  448. if (targetFolder)
  449. {
  450. std::vector<cmsys::String> tokens =
  451. cmSystemTools::SplitString(targetFolder, '/', false);
  452. std::string cumulativePath = "";
  453. for(std::vector<cmsys::String>::iterator iter = tokens.begin();
  454. iter != tokens.end(); ++iter)
  455. {
  456. if(!iter->size())
  457. {
  458. continue;
  459. }
  460. if (cumulativePath.empty())
  461. {
  462. cumulativePath = "CMAKE_FOLDER_GUID_" + *iter;
  463. }
  464. else
  465. {
  466. VisualStudioFolders[cumulativePath].insert(
  467. cumulativePath + "/" + *iter);
  468. cumulativePath = cumulativePath + "/" + *iter;
  469. }
  470. this->CreateGUID(cumulativePath.c_str());
  471. }
  472. if (!cumulativePath.empty())
  473. {
  474. VisualStudioFolders[cumulativePath].insert(target->GetName());
  475. }
  476. }
  477. }
  478. }
  479. }
  480. void cmGlobalVisualStudio7Generator::WriteTargetDepends(
  481. std::ostream& fout,
  482. OrderedTargetDependSet const& projectTargets
  483. )
  484. {
  485. for(OrderedTargetDependSet::const_iterator tt =
  486. projectTargets.begin(); tt != projectTargets.end(); ++tt)
  487. {
  488. cmTarget const* target = *tt;
  489. if(target->GetType() == cmTarget::INTERFACE_LIBRARY)
  490. {
  491. continue;
  492. }
  493. cmMakefile* mf = target->GetMakefile();
  494. const char *vcprojName =
  495. target->GetProperty("GENERATOR_FILE_NAME");
  496. if (vcprojName)
  497. {
  498. std::string dir = mf->GetCurrentSourceDirectory();
  499. this->WriteProjectDepends(fout, vcprojName,
  500. dir.c_str(), *target);
  501. }
  502. }
  503. }
  504. //----------------------------------------------------------------------------
  505. // Write a SLN file to the stream
  506. void cmGlobalVisualStudio7Generator
  507. ::WriteSLNFile(std::ostream& fout,
  508. cmLocalGenerator* root,
  509. std::vector<cmLocalGenerator*>& generators)
  510. {
  511. std::vector<std::string> configs;
  512. root->GetMakefile()->GetConfigurations(configs);
  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 = configs.begin();
  532. i != configs.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, configs, 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. std::vector<std::string> const& configs,
  689. const std::set<std::string>& configsPartOfDefaultBuild,
  690. const std::string& platformMapping)
  691. {
  692. const std::string& platformName =
  693. !platformMapping.empty() ? platformMapping : this->GetPlatformName();
  694. std::string guid = this->GetGUID(name);
  695. for(std::vector<std::string>::const_iterator i = configs.begin();
  696. i != configs.end(); ++i)
  697. {
  698. fout << "\t\t{" << guid << "}." << *i
  699. << ".ActiveCfg = " << *i << "|" << platformName << "\n";
  700. std::set<std::string>::const_iterator
  701. ci = configsPartOfDefaultBuild.find(*i);
  702. if(!(ci == configsPartOfDefaultBuild.end()))
  703. {
  704. fout << "\t\t{" << guid << "}." << *i
  705. << ".Build.0 = " << *i << "|" << platformName << "\n";
  706. }
  707. }
  708. }
  709. // Write a dsp file into the SLN file,
  710. // Note, that dependencies from executables to
  711. // the libraries it uses are also done here
  712. void cmGlobalVisualStudio7Generator::WriteExternalProject(std::ostream& fout,
  713. const std::string& name,
  714. const char* location,
  715. const char* typeGuid,
  716. const std::set<std::string>&)
  717. {
  718. fout << "Project("
  719. << "\"{"
  720. << (typeGuid ? typeGuid : this->ExternalProjectType(location))
  721. << "}\") = \""
  722. << name << "\", \""
  723. << this->ConvertToSolutionPath(location) << "\", \"{"
  724. << this->GetGUID(name)
  725. << "}\"\n";
  726. fout << "EndProject\n";
  727. }
  728. void cmGlobalVisualStudio7Generator
  729. ::WriteSLNGlobalSections(std::ostream& fout,
  730. cmLocalGenerator* root)
  731. {
  732. bool extensibilityGlobalsOverridden = false;
  733. bool extensibilityAddInsOverridden = false;
  734. const cmPropertyMap& props = root->GetMakefile()->GetProperties();
  735. for(cmPropertyMap::const_iterator itProp = props.begin();
  736. itProp != props.end(); ++itProp)
  737. {
  738. if(itProp->first.find("VS_GLOBAL_SECTION_") == 0)
  739. {
  740. std::string sectionType;
  741. std::string name = itProp->first.substr(18);
  742. if(name.find("PRE_") == 0)
  743. {
  744. name = name.substr(4);
  745. sectionType = "preSolution";
  746. }
  747. else if(name.find("POST_") == 0)
  748. {
  749. name = name.substr(5);
  750. sectionType = "postSolution";
  751. }
  752. else
  753. continue;
  754. if(!name.empty())
  755. {
  756. if(name == "ExtensibilityGlobals" && sectionType == "postSolution")
  757. extensibilityGlobalsOverridden = true;
  758. else if(name == "ExtensibilityAddIns" && sectionType == "postSolution")
  759. extensibilityAddInsOverridden = true;
  760. fout << "\tGlobalSection(" << name << ") = " << sectionType << "\n";
  761. std::vector<std::string> keyValuePairs;
  762. cmSystemTools::ExpandListArgument(itProp->second.GetValue(),
  763. keyValuePairs);
  764. for(std::vector<std::string>::const_iterator itPair =
  765. keyValuePairs.begin(); itPair != keyValuePairs.end(); ++itPair)
  766. {
  767. const std::string::size_type posEqual = itPair->find('=');
  768. if(posEqual != std::string::npos)
  769. {
  770. const std::string key =
  771. cmSystemTools::TrimWhitespace(itPair->substr(0, posEqual));
  772. const std::string value =
  773. cmSystemTools::TrimWhitespace(itPair->substr(posEqual + 1));
  774. fout << "\t\t" << key << " = " << value << "\n";
  775. }
  776. }
  777. fout << "\tEndGlobalSection\n";
  778. }
  779. }
  780. }
  781. if(!extensibilityGlobalsOverridden)
  782. fout << "\tGlobalSection(ExtensibilityGlobals) = postSolution\n"
  783. << "\tEndGlobalSection\n";
  784. if(!extensibilityAddInsOverridden)
  785. fout << "\tGlobalSection(ExtensibilityAddIns) = postSolution\n"
  786. << "\tEndGlobalSection\n";
  787. }
  788. // Standard end of dsw file
  789. void cmGlobalVisualStudio7Generator::WriteSLNFooter(std::ostream& fout)
  790. {
  791. fout << "EndGlobal\n";
  792. }
  793. // ouput standard header for dsw file
  794. void cmGlobalVisualStudio7Generator::WriteSLNHeader(std::ostream& fout)
  795. {
  796. fout << "Microsoft Visual Studio Solution File, Format Version 7.00\n";
  797. }
  798. //----------------------------------------------------------------------------
  799. std::string
  800. cmGlobalVisualStudio7Generator::WriteUtilityDepend(cmTarget const* target)
  801. {
  802. std::vector<std::string> configs;
  803. target->GetMakefile()->GetConfigurations(configs);
  804. std::string pname = target->GetName();
  805. pname += "_UTILITY";
  806. std::string fname = target->GetMakefile()->GetCurrentBinaryDirectory();
  807. fname += "/";
  808. fname += pname;
  809. fname += ".vcproj";
  810. cmGeneratedFileStream fout(fname.c_str());
  811. fout.SetCopyIfDifferent(true);
  812. this->CreateGUID(pname.c_str());
  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. std::string cmGlobalVisualStudio7Generator::GetGUID(const std::string& name)
  854. {
  855. std::string guidStoreName = name;
  856. guidStoreName += "_GUID_CMAKE";
  857. const char* storedGUID =
  858. this->CMakeInstance->GetCacheDefinition(guidStoreName.c_str());
  859. if(storedGUID)
  860. {
  861. return std::string(storedGUID);
  862. }
  863. cmSystemTools::Error("Unknown Target referenced : ",
  864. name.c_str());
  865. return "";
  866. }
  867. void cmGlobalVisualStudio7Generator::CreateGUID(const std::string& name)
  868. {
  869. std::string guidStoreName = name;
  870. guidStoreName += "_GUID_CMAKE";
  871. if(this->CMakeInstance->GetCacheDefinition(guidStoreName.c_str()))
  872. {
  873. return;
  874. }
  875. std::string ret;
  876. UUID uid;
  877. unsigned short *uidstr;
  878. UuidCreate(&uid);
  879. UuidToStringW(&uid,&uidstr);
  880. ret = cmsys::Encoding::ToNarrow(reinterpret_cast<wchar_t*>(uidstr));
  881. RpcStringFreeW(&uidstr);
  882. ret = cmSystemTools::UpperCase(ret);
  883. this->CMakeInstance->AddCacheEntry(guidStoreName.c_str(),
  884. ret.c_str(), "Stored GUID",
  885. cmState::INTERNAL);
  886. }
  887. //----------------------------------------------------------------------------
  888. void cmGlobalVisualStudio7Generator
  889. ::GetDocumentation(cmDocumentationEntry& entry)
  890. {
  891. entry.Name = cmGlobalVisualStudio7Generator::GetActualName();
  892. entry.Brief = "Deprecated. Generates Visual Studio .NET 2002 project files.";
  893. }
  894. //----------------------------------------------------------------------------
  895. void
  896. cmGlobalVisualStudio7Generator
  897. ::AppendDirectoryForConfig(const std::string& prefix,
  898. const std::string& config,
  899. const std::string& suffix,
  900. std::string& dir)
  901. {
  902. if(!config.empty())
  903. {
  904. dir += prefix;
  905. dir += config;
  906. dir += suffix;
  907. }
  908. }
  909. std::set<std::string>
  910. cmGlobalVisualStudio7Generator::IsPartOfDefaultBuild(
  911. std::vector<std::string> const& configs,
  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. // check if INSTALL target is part of default build
  921. if(target->GetName() == "INSTALL")
  922. {
  923. // inspect CMAKE_VS_INCLUDE_INSTALL_TO_DEFAULT_BUILD properties
  924. for(std::vector<std::string>::const_iterator i = configs.begin();
  925. i != configs.end(); ++i)
  926. {
  927. const char* propertyValue = target->GetMakefile()
  928. ->GetDefinition("CMAKE_VS_INCLUDE_INSTALL_TO_DEFAULT_BUILD");
  929. cmGeneratorExpression ge;
  930. cmsys::auto_ptr<cmCompiledGeneratorExpression>
  931. cge = ge.Parse(propertyValue);
  932. if(cmSystemTools::IsOn(cge->Evaluate(target->GetMakefile(), *i)))
  933. {
  934. activeConfigs.insert(*i);
  935. }
  936. }
  937. }
  938. return activeConfigs;
  939. }
  940. if(type == cmTarget::UTILITY && !this->IsDependedOn(projectTargets, target))
  941. {
  942. return activeConfigs;
  943. }
  944. // inspect EXCLUDE_FROM_DEFAULT_BUILD[_<CONFIG>] properties
  945. for(std::vector<std::string>::const_iterator i = configs.begin();
  946. i != configs.end(); ++i)
  947. {
  948. const char* propertyValue =
  949. target->GetFeature("EXCLUDE_FROM_DEFAULT_BUILD", i->c_str());
  950. if(cmSystemTools::IsOff(propertyValue))
  951. {
  952. activeConfigs.insert(*i);
  953. }
  954. }
  955. return activeConfigs;
  956. }
  957. bool
  958. cmGlobalVisualStudio7Generator
  959. ::IsDependedOn(OrderedTargetDependSet const& projectTargets,
  960. cmTarget const* targetIn)
  961. {
  962. for (OrderedTargetDependSet::const_iterator l = projectTargets.begin();
  963. l != projectTargets.end(); ++l)
  964. {
  965. cmTarget const& target = **l;
  966. TargetDependSet const& tgtdeps = this->GetTargetDirectDepends(target);
  967. if(tgtdeps.count(targetIn))
  968. {
  969. return true;
  970. }
  971. }
  972. return false;
  973. }
  974. std::string cmGlobalVisualStudio7Generator::Encoding()
  975. {
  976. std::ostringstream encoding;
  977. #ifdef CMAKE_ENCODING_UTF8
  978. encoding << "UTF-8";
  979. #else
  980. encoding << "Windows-1252";
  981. #endif
  982. return encoding.str();
  983. }