cmGlobalVisualStudio8Generator.cxx 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538
  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 "cmGlobalVisualStudio8Generator.h"
  12. #include "cmLocalVisualStudio7Generator.h"
  13. #include "cmMakefile.h"
  14. #include "cmVisualStudioWCEPlatformParser.h"
  15. #include "cmake.h"
  16. #include "cmGeneratedFileStream.h"
  17. #include "cmSourceFile.h"
  18. static const char vs8generatorName[] = "Visual Studio 8 2005";
  19. class cmGlobalVisualStudio8Generator::Factory
  20. : public cmGlobalGeneratorFactory
  21. {
  22. public:
  23. virtual cmGlobalGenerator* CreateGlobalGenerator(
  24. const std::string& name) const {
  25. if(strncmp(name.c_str(), vs8generatorName,
  26. sizeof(vs8generatorName) - 1) != 0)
  27. {
  28. return 0;
  29. }
  30. const char* p = name.c_str() + sizeof(vs8generatorName) - 1;
  31. if(p[0] == '\0')
  32. {
  33. return new cmGlobalVisualStudio8Generator(
  34. name, "");
  35. }
  36. if(p[0] != ' ')
  37. {
  38. return 0;
  39. }
  40. ++p;
  41. if(!strcmp(p, "Win64"))
  42. {
  43. return new cmGlobalVisualStudio8Generator(
  44. name, "x64");
  45. }
  46. cmVisualStudioWCEPlatformParser parser(p);
  47. parser.ParseVersion("8.0");
  48. if (!parser.Found())
  49. {
  50. return 0;
  51. }
  52. cmGlobalVisualStudio8Generator* ret = new cmGlobalVisualStudio8Generator(
  53. name, p);
  54. ret->WindowsCEVersion = parser.GetOSVersion();
  55. return ret;
  56. }
  57. virtual void GetDocumentation(cmDocumentationEntry& entry) const {
  58. entry.Name = std::string(vs8generatorName) + " [arch]";
  59. entry.Brief =
  60. "Generates Visual Studio 2005 project files. "
  61. "Optional [arch] can be \"Win64\"."
  62. ;
  63. }
  64. virtual void GetGenerators(std::vector<std::string>& names) const {
  65. names.push_back(vs8generatorName);
  66. names.push_back(vs8generatorName + std::string(" Win64"));
  67. cmVisualStudioWCEPlatformParser parser;
  68. parser.ParseVersion("8.0");
  69. const std::vector<std::string>& availablePlatforms =
  70. parser.GetAvailablePlatforms();
  71. for(std::vector<std::string>::const_iterator i =
  72. availablePlatforms.begin(); i != availablePlatforms.end(); ++i)
  73. {
  74. names.push_back("Visual Studio 8 2005 " + *i);
  75. }
  76. }
  77. };
  78. //----------------------------------------------------------------------------
  79. cmGlobalGeneratorFactory* cmGlobalVisualStudio8Generator::NewFactory()
  80. {
  81. return new Factory;
  82. }
  83. //----------------------------------------------------------------------------
  84. cmGlobalVisualStudio8Generator::cmGlobalVisualStudio8Generator(
  85. const std::string& name, const std::string& platformName)
  86. : cmGlobalVisualStudio71Generator(platformName)
  87. {
  88. this->ProjectConfigurationSectionName = "ProjectConfigurationPlatforms";
  89. this->Name = name;
  90. this->ExtraFlagTable = this->GetExtraFlagTableVS8();
  91. }
  92. //----------------------------------------------------------------------------
  93. std::string cmGlobalVisualStudio8Generator::FindDevEnvCommand()
  94. {
  95. // First look for VCExpress.
  96. std::string vsxcmd;
  97. std::string vsxkey =
  98. "HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\VCExpress\\";
  99. vsxkey += this->GetIDEVersion();
  100. vsxkey += ";InstallDir";
  101. if(cmSystemTools::ReadRegistryValue(vsxkey.c_str(), vsxcmd,
  102. cmSystemTools::KeyWOW64_32))
  103. {
  104. cmSystemTools::ConvertToUnixSlashes(vsxcmd);
  105. vsxcmd += "/VCExpress.exe";
  106. return vsxcmd;
  107. }
  108. // Now look for devenv.
  109. return this->cmGlobalVisualStudio71Generator::FindDevEnvCommand();
  110. }
  111. //----------------------------------------------------------------------------
  112. ///! Create a local generator appropriate to this Global Generator
  113. cmLocalGenerator *
  114. cmGlobalVisualStudio8Generator::CreateLocalGenerator(cmLocalGenerator* parent)
  115. {
  116. cmLocalVisualStudio7Generator *lg =
  117. new cmLocalVisualStudio7Generator(cmLocalVisualStudioGenerator::VS8,
  118. this, parent);
  119. return lg;
  120. }
  121. //----------------------------------------------------------------------------
  122. void cmGlobalVisualStudio8Generator
  123. ::EnableLanguage(std::vector<std::string>const & lang,
  124. cmMakefile *mf, bool optional)
  125. {
  126. for(std::vector<std::string>::const_iterator it = lang.begin();
  127. it != lang.end(); ++it)
  128. {
  129. if(*it == "ASM_MASM")
  130. {
  131. this->MasmEnabled = true;
  132. }
  133. }
  134. this->AddPlatformDefinitions(mf);
  135. cmGlobalVisualStudio7Generator::EnableLanguage(lang, mf, optional);
  136. }
  137. //----------------------------------------------------------------------------
  138. void cmGlobalVisualStudio8Generator::AddPlatformDefinitions(cmMakefile* mf)
  139. {
  140. if(this->TargetsWindowsCE())
  141. {
  142. mf->AddDefinition("CMAKE_VS_WINCE_VERSION",
  143. this->WindowsCEVersion.c_str());
  144. }
  145. }
  146. //----------------------------------------------------------------------------
  147. bool cmGlobalVisualStudio8Generator::SetGeneratorPlatform(std::string const& p,
  148. cmMakefile* mf)
  149. {
  150. if(this->DefaultPlatformName == "Win32")
  151. {
  152. this->GeneratorPlatform = p;
  153. return this->cmGlobalVisualStudio7Generator::SetGeneratorPlatform("", mf);
  154. }
  155. else
  156. {
  157. return this->cmGlobalVisualStudio7Generator::SetGeneratorPlatform(p, mf);
  158. }
  159. }
  160. //----------------------------------------------------------------------------
  161. // ouput standard header for dsw file
  162. void cmGlobalVisualStudio8Generator::WriteSLNHeader(std::ostream& fout)
  163. {
  164. fout << "Microsoft Visual Studio Solution File, Format Version 9.00\n";
  165. fout << "# Visual Studio 2005\n";
  166. }
  167. //----------------------------------------------------------------------------
  168. void cmGlobalVisualStudio8Generator
  169. ::GetDocumentation(cmDocumentationEntry& entry)
  170. {
  171. entry.Name = cmGlobalVisualStudio8Generator::GetActualName();
  172. entry.Brief = "Generates Visual Studio 8 2005 project files.";
  173. }
  174. //----------------------------------------------------------------------------
  175. void cmGlobalVisualStudio8Generator::Configure()
  176. {
  177. this->cmGlobalVisualStudio7Generator::Configure();
  178. this->CreateGUID(CMAKE_CHECK_BUILD_SYSTEM_TARGET);
  179. }
  180. //----------------------------------------------------------------------------
  181. std::string cmGlobalVisualStudio8Generator::GetUserMacrosDirectory()
  182. {
  183. // Some VS8 sp0 versions cannot run macros.
  184. // See http://support.microsoft.com/kb/928209
  185. const char* vc8sp1Registry =
  186. "HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\VisualStudio\\8.0\\"
  187. "InstalledProducts\\KB926601;";
  188. const char* vc8exSP1Registry =
  189. "HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\VisualStudio\\8.0\\"
  190. "InstalledProducts\\KB926748;";
  191. std::string vc8sp1;
  192. if (!cmSystemTools::ReadRegistryValue(vc8sp1Registry, vc8sp1) &&
  193. !cmSystemTools::ReadRegistryValue(vc8exSP1Registry, vc8sp1))
  194. {
  195. return "";
  196. }
  197. std::string base;
  198. std::string path;
  199. // base begins with the VisualStudioProjectsLocation reg value...
  200. if (cmSystemTools::ReadRegistryValue(
  201. "HKEY_CURRENT_USER\\Software\\Microsoft\\VisualStudio\\8.0;"
  202. "VisualStudioProjectsLocation",
  203. base))
  204. {
  205. cmSystemTools::ConvertToUnixSlashes(base);
  206. // 8.0 macros folder:
  207. path = base + "/VSMacros80";
  208. }
  209. // path is (correctly) still empty if we did not read the base value from
  210. // the Registry value
  211. return path;
  212. }
  213. //----------------------------------------------------------------------------
  214. std::string cmGlobalVisualStudio8Generator::GetUserMacrosRegKeyBase()
  215. {
  216. return "Software\\Microsoft\\VisualStudio\\8.0\\vsmacros";
  217. }
  218. //----------------------------------------------------------------------------
  219. bool cmGlobalVisualStudio8Generator::AddCheckTarget()
  220. {
  221. // Add a special target on which all other targets depend that
  222. // checks the build system and optionally re-runs CMake.
  223. const char* no_working_directory = 0;
  224. std::vector<std::string> no_depends;
  225. std::vector<cmLocalGenerator*> const& generators = this->LocalGenerators;
  226. cmLocalVisualStudio7Generator* lg =
  227. static_cast<cmLocalVisualStudio7Generator*>(generators[0]);
  228. cmMakefile* mf = lg->GetMakefile();
  229. // Skip the target if no regeneration is to be done.
  230. if(mf->IsOn("CMAKE_SUPPRESS_REGENERATION"))
  231. {
  232. return false;
  233. }
  234. std::string cmake_command = mf->GetRequiredDefinition("CMAKE_COMMAND");
  235. cmCustomCommandLines noCommandLines;
  236. cmTarget* tgt =
  237. mf->AddUtilityCommand(CMAKE_CHECK_BUILD_SYSTEM_TARGET, false,
  238. no_working_directory, no_depends,
  239. noCommandLines);
  240. // Organize in the "predefined targets" folder:
  241. //
  242. if (this->UseFolderProperty())
  243. {
  244. tgt->SetProperty("FOLDER", this->GetPredefinedTargetsFolder());
  245. }
  246. // Create a list of all stamp files for this project.
  247. std::vector<std::string> stamps;
  248. std::string stampList = cmake::GetCMakeFilesDirectoryPostSlash();
  249. stampList += "generate.stamp.list";
  250. {
  251. std::string stampListFile =
  252. generators[0]->GetMakefile()->GetCurrentBinaryDirectory();
  253. stampListFile += "/";
  254. stampListFile += stampList;
  255. std::string stampFile;
  256. cmGeneratedFileStream fout(stampListFile.c_str());
  257. for(std::vector<cmLocalGenerator*>::const_iterator
  258. gi = generators.begin(); gi != generators.end(); ++gi)
  259. {
  260. stampFile = (*gi)->GetMakefile()->GetCurrentBinaryDirectory();
  261. stampFile += "/";
  262. stampFile += cmake::GetCMakeFilesDirectoryPostSlash();
  263. stampFile += "generate.stamp";
  264. fout << stampFile << "\n";
  265. stamps.push_back(stampFile);
  266. }
  267. }
  268. // Add a custom rule to re-run CMake if any input files changed.
  269. {
  270. // Collect the input files used to generate all targets in this
  271. // project.
  272. std::vector<std::string> listFiles;
  273. for(unsigned int j = 0; j < generators.size(); ++j)
  274. {
  275. cmMakefile* lmf = generators[j]->GetMakefile();
  276. listFiles.insert(listFiles.end(), lmf->GetListFiles().begin(),
  277. lmf->GetListFiles().end());
  278. }
  279. // Sort the list of input files and remove duplicates.
  280. std::sort(listFiles.begin(), listFiles.end(),
  281. std::less<std::string>());
  282. std::vector<std::string>::iterator new_end =
  283. std::unique(listFiles.begin(), listFiles.end());
  284. listFiles.erase(new_end, listFiles.end());
  285. // Create a rule to re-run CMake.
  286. std::string stampName = cmake::GetCMakeFilesDirectoryPostSlash();
  287. stampName += "generate.stamp";
  288. const char* dsprule = mf->GetRequiredDefinition("CMAKE_COMMAND");
  289. cmCustomCommandLine commandLine;
  290. commandLine.push_back(dsprule);
  291. std::string argH = "-H";
  292. argH += lg->Convert(mf->GetHomeDirectory(),
  293. cmLocalGenerator::START_OUTPUT,
  294. cmLocalGenerator::UNCHANGED, true);
  295. commandLine.push_back(argH);
  296. std::string argB = "-B";
  297. argB += lg->Convert(mf->GetHomeOutputDirectory(),
  298. cmLocalGenerator::START_OUTPUT,
  299. cmLocalGenerator::UNCHANGED, true);
  300. commandLine.push_back(argB);
  301. commandLine.push_back("--check-stamp-list");
  302. commandLine.push_back(stampList.c_str());
  303. commandLine.push_back("--vs-solution-file");
  304. commandLine.push_back("\"$(SolutionPath)\"");
  305. cmCustomCommandLines commandLines;
  306. commandLines.push_back(commandLine);
  307. // Add the rule. Note that we cannot use the CMakeLists.txt
  308. // file as the main dependency because it would get
  309. // overwritten by the CreateVCProjBuildRule.
  310. // (this could be avoided with per-target source files)
  311. std::string no_main_dependency = "";
  312. std::vector<std::string> no_byproducts;
  313. if(cmSourceFile* file =
  314. mf->AddCustomCommandToOutput(
  315. stamps, no_byproducts, listFiles,
  316. no_main_dependency, commandLines, "Checking Build System",
  317. no_working_directory, true))
  318. {
  319. tgt->AddSource(file->GetFullPath());
  320. }
  321. else
  322. {
  323. cmSystemTools::Error("Error adding rule for ", stamps[0].c_str());
  324. }
  325. }
  326. return true;
  327. }
  328. //----------------------------------------------------------------------------
  329. void cmGlobalVisualStudio8Generator::Generate()
  330. {
  331. if(this->AddCheckTarget())
  332. {
  333. // All targets depend on the build-system check target.
  334. for(TargetMap::const_iterator
  335. ti = this->TotalTargets.begin();
  336. ti != this->TotalTargets.end(); ++ti)
  337. {
  338. if(ti->first != CMAKE_CHECK_BUILD_SYSTEM_TARGET)
  339. {
  340. ti->second->AddUtility(CMAKE_CHECK_BUILD_SYSTEM_TARGET);
  341. }
  342. }
  343. }
  344. // Now perform the main generation.
  345. this->cmGlobalVisualStudio7Generator::Generate();
  346. }
  347. //----------------------------------------------------------------------------
  348. void
  349. cmGlobalVisualStudio8Generator
  350. ::WriteSolutionConfigurations(std::ostream& fout)
  351. {
  352. fout << "\tGlobalSection(SolutionConfigurationPlatforms) = preSolution\n";
  353. for(std::vector<std::string>::iterator i = this->Configurations.begin();
  354. i != this->Configurations.end(); ++i)
  355. {
  356. fout << "\t\t" << *i << "|" << this->GetPlatformName()
  357. << " = " << *i << "|" << this->GetPlatformName() << "\n";
  358. }
  359. fout << "\tEndGlobalSection\n";
  360. }
  361. //----------------------------------------------------------------------------
  362. void
  363. cmGlobalVisualStudio8Generator
  364. ::WriteProjectConfigurations(
  365. std::ostream& fout, const std::string& name, cmTarget::TargetType type,
  366. const std::set<std::string>& configsPartOfDefaultBuild,
  367. std::string const& platformMapping)
  368. {
  369. std::string guid = this->GetGUID(name);
  370. for(std::vector<std::string>::iterator i = this->Configurations.begin();
  371. i != this->Configurations.end(); ++i)
  372. {
  373. fout << "\t\t{" << guid << "}." << *i
  374. << "|" << this->GetPlatformName() << ".ActiveCfg = " << *i << "|"
  375. << (!platformMapping.empty()?
  376. platformMapping : this->GetPlatformName())
  377. << "\n";
  378. std::set<std::string>::const_iterator
  379. ci = configsPartOfDefaultBuild.find(*i);
  380. if(!(ci == configsPartOfDefaultBuild.end()))
  381. {
  382. fout << "\t\t{" << guid << "}." << *i
  383. << "|" << this->GetPlatformName() << ".Build.0 = " << *i << "|"
  384. << (!platformMapping.empty()?
  385. platformMapping : this->GetPlatformName())
  386. << "\n";
  387. }
  388. if(this->NeedsDeploy(type))
  389. {
  390. fout << "\t\t{" << guid << "}." << *i
  391. << "|" << this->GetPlatformName() << ".Deploy.0 = " << *i << "|"
  392. << (!platformMapping.empty()?
  393. platformMapping : this->GetPlatformName())
  394. << "\n";
  395. }
  396. }
  397. }
  398. //----------------------------------------------------------------------------
  399. bool
  400. cmGlobalVisualStudio8Generator::NeedsDeploy(cmTarget::TargetType type) const
  401. {
  402. bool needsDeploy = (type == cmTarget::EXECUTABLE ||
  403. type == cmTarget::SHARED_LIBRARY);
  404. return this->TargetsWindowsCE() && needsDeploy;
  405. }
  406. //----------------------------------------------------------------------------
  407. bool cmGlobalVisualStudio8Generator::ComputeTargetDepends()
  408. {
  409. // Skip over the cmGlobalVisualStudioGenerator implementation!
  410. // We do not need the support that VS <= 7.1 needs.
  411. return this->cmGlobalGenerator::ComputeTargetDepends();
  412. }
  413. //----------------------------------------------------------------------------
  414. void cmGlobalVisualStudio8Generator::WriteProjectDepends(
  415. std::ostream& fout, const std::string&, const char*, cmTarget const& t)
  416. {
  417. TargetDependSet const& unordered = this->GetTargetDirectDepends(t);
  418. OrderedTargetDependSet depends(unordered);
  419. for(OrderedTargetDependSet::const_iterator i = depends.begin();
  420. i != depends.end(); ++i)
  421. {
  422. if((*i)->GetType() == cmTarget::INTERFACE_LIBRARY)
  423. {
  424. continue;
  425. }
  426. std::string guid = this->GetGUID((*i)->GetName().c_str());
  427. fout << "\t\t{" << guid << "} = {" << guid << "}\n";
  428. }
  429. }
  430. //----------------------------------------------------------------------------
  431. bool cmGlobalVisualStudio8Generator::NeedLinkLibraryDependencies(
  432. cmTarget& target)
  433. {
  434. // Look for utility dependencies that magically link.
  435. for(std::set<std::string>::const_iterator ui =
  436. target.GetUtilities().begin();
  437. ui != target.GetUtilities().end(); ++ui)
  438. {
  439. if(cmTarget* depTarget = this->FindTarget(ui->c_str()))
  440. {
  441. if(depTarget->GetType() != cmTarget::INTERFACE_LIBRARY
  442. && depTarget->GetProperty("EXTERNAL_MSPROJECT"))
  443. {
  444. // This utility dependency names an external .vcproj target.
  445. // We use LinkLibraryDependencies="true" to link to it without
  446. // predicting the .lib file location or name.
  447. return true;
  448. }
  449. }
  450. }
  451. return false;
  452. }
  453. //----------------------------------------------------------------------------
  454. static cmVS7FlagTable cmVS8ExtraFlagTable[] =
  455. {
  456. {"CallingConvention", "Gd", "cdecl", "0", 0 },
  457. {"CallingConvention", "Gr", "fastcall", "1", 0 },
  458. {"CallingConvention", "Gz", "stdcall", "2", 0 },
  459. {"Detect64BitPortabilityProblems", "Wp64",
  460. "Detect 64Bit Portability Problems", "true", 0 },
  461. {"ErrorReporting", "errorReport:prompt", "Report immediately", "1", 0 },
  462. {"ErrorReporting", "errorReport:queue", "Queue for next login", "2", 0 },
  463. // Precompiled header and related options. Note that the
  464. // UsePrecompiledHeader entries are marked as "Continue" so that the
  465. // corresponding PrecompiledHeaderThrough entry can be found.
  466. {"UsePrecompiledHeader", "Yu", "Use Precompiled Header", "2",
  467. cmVS7FlagTable::UserValueIgnored | cmVS7FlagTable::Continue},
  468. {"PrecompiledHeaderThrough", "Yu", "Precompiled Header Name", "",
  469. cmVS7FlagTable::UserValueRequired},
  470. // There is no YX option in the VS8 IDE.
  471. // Exception handling mode. If no entries match, it will be FALSE.
  472. {"ExceptionHandling", "GX", "enable c++ exceptions", "1", 0},
  473. {"ExceptionHandling", "EHsc", "enable c++ exceptions", "1", 0},
  474. {"ExceptionHandling", "EHa", "enable SEH exceptions", "2", 0},
  475. {"EnablePREfast", "analyze", "", "true", 0},
  476. {"EnablePREfast", "analyze-", "", "false", 0},
  477. // Language options
  478. {"TreatWChar_tAsBuiltInType", "Zc:wchar_t",
  479. "wchar_t is a built-in type", "true", 0},
  480. {"TreatWChar_tAsBuiltInType", "Zc:wchar_t-",
  481. "wchar_t is not a built-in type", "false", 0},
  482. {0,0,0,0,0}
  483. };
  484. cmIDEFlagTable const* cmGlobalVisualStudio8Generator::GetExtraFlagTableVS8()
  485. {
  486. return cmVS8ExtraFlagTable;
  487. }