cmGlobalVisualStudio8Generator.cxx 18 KB

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