cmGlobalVisualStudio8Generator.cxx 18 KB

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