cmGlobalVisualStudio8Generator.cxx 18 KB

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