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