cmGlobalVisualStudio8Generator.cxx 18 KB

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