cmGlobalVisualStudio7Generator.cxx 35 KB

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