cmGlobalVisualStudio8Generator.cxx 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524
  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. tgt->Compute();
  228. cmGeneratorTarget* gt = new cmGeneratorTarget(tgt, lg);
  229. mf->AddGeneratorTarget(tgt, gt);
  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 += mf->GetHomeDirectory();
  282. commandLine.push_back(argH);
  283. std::string argB = "-B";
  284. argB += mf->GetHomeOutputDirectory();
  285. commandLine.push_back(argB);
  286. commandLine.push_back("--check-stamp-list");
  287. commandLine.push_back(stampList.c_str());
  288. commandLine.push_back("--vs-solution-file");
  289. commandLine.push_back("\"$(SolutionPath)\"");
  290. cmCustomCommandLines commandLines;
  291. commandLines.push_back(commandLine);
  292. // Add the rule. Note that we cannot use the CMakeLists.txt
  293. // file as the main dependency because it would get
  294. // overwritten by the CreateVCProjBuildRule.
  295. // (this could be avoided with per-target source files)
  296. std::string no_main_dependency = "";
  297. std::vector<std::string> no_byproducts;
  298. if(cmSourceFile* file =
  299. mf->AddCustomCommandToOutput(
  300. stamps, no_byproducts, listFiles,
  301. no_main_dependency, commandLines, "Checking Build System",
  302. no_working_directory, true))
  303. {
  304. gt->AddSource(file->GetFullPath());
  305. }
  306. else
  307. {
  308. cmSystemTools::Error("Error adding rule for ", stamps[0].c_str());
  309. }
  310. }
  311. return true;
  312. }
  313. //----------------------------------------------------------------------------
  314. void cmGlobalVisualStudio8Generator::AddExtraIDETargets()
  315. {
  316. cmGlobalVisualStudio7Generator::AddExtraIDETargets();
  317. if(this->AddCheckTarget())
  318. {
  319. // All targets depend on the build-system check target.
  320. for(TargetMap::const_iterator
  321. ti = this->TotalTargets.begin();
  322. ti != this->TotalTargets.end(); ++ti)
  323. {
  324. if(ti->first != CMAKE_CHECK_BUILD_SYSTEM_TARGET)
  325. {
  326. ti->second->AddUtility(CMAKE_CHECK_BUILD_SYSTEM_TARGET);
  327. }
  328. }
  329. }
  330. }
  331. //----------------------------------------------------------------------------
  332. void
  333. cmGlobalVisualStudio8Generator
  334. ::WriteSolutionConfigurations(std::ostream& fout,
  335. std::vector<std::string> const& configs)
  336. {
  337. fout << "\tGlobalSection(SolutionConfigurationPlatforms) = preSolution\n";
  338. for(std::vector<std::string>::const_iterator i = configs.begin();
  339. i != configs.end(); ++i)
  340. {
  341. fout << "\t\t" << *i << "|" << this->GetPlatformName()
  342. << " = " << *i << "|" << this->GetPlatformName() << "\n";
  343. }
  344. fout << "\tEndGlobalSection\n";
  345. }
  346. //----------------------------------------------------------------------------
  347. void
  348. cmGlobalVisualStudio8Generator
  349. ::WriteProjectConfigurations(
  350. std::ostream& fout, const std::string& name, cmTarget::TargetType type,
  351. std::vector<std::string> const& configs,
  352. const std::set<std::string>& configsPartOfDefaultBuild,
  353. std::string const& platformMapping)
  354. {
  355. std::string guid = this->GetGUID(name);
  356. for(std::vector<std::string>::const_iterator i = configs.begin();
  357. i != configs.end(); ++i)
  358. {
  359. fout << "\t\t{" << guid << "}." << *i
  360. << "|" << this->GetPlatformName() << ".ActiveCfg = " << *i << "|"
  361. << (!platformMapping.empty()?
  362. platformMapping : this->GetPlatformName())
  363. << "\n";
  364. std::set<std::string>::const_iterator
  365. ci = configsPartOfDefaultBuild.find(*i);
  366. if(!(ci == configsPartOfDefaultBuild.end()))
  367. {
  368. fout << "\t\t{" << guid << "}." << *i
  369. << "|" << this->GetPlatformName() << ".Build.0 = " << *i << "|"
  370. << (!platformMapping.empty()?
  371. platformMapping : this->GetPlatformName())
  372. << "\n";
  373. }
  374. if(this->NeedsDeploy(type))
  375. {
  376. fout << "\t\t{" << guid << "}." << *i
  377. << "|" << this->GetPlatformName() << ".Deploy.0 = " << *i << "|"
  378. << (!platformMapping.empty()?
  379. platformMapping : this->GetPlatformName())
  380. << "\n";
  381. }
  382. }
  383. }
  384. //----------------------------------------------------------------------------
  385. bool
  386. cmGlobalVisualStudio8Generator::NeedsDeploy(cmTarget::TargetType type) const
  387. {
  388. bool needsDeploy = (type == cmTarget::EXECUTABLE ||
  389. type == cmTarget::SHARED_LIBRARY);
  390. return this->TargetsWindowsCE() && needsDeploy;
  391. }
  392. //----------------------------------------------------------------------------
  393. bool cmGlobalVisualStudio8Generator::ComputeTargetDepends()
  394. {
  395. // Skip over the cmGlobalVisualStudioGenerator implementation!
  396. // We do not need the support that VS <= 7.1 needs.
  397. return this->cmGlobalGenerator::ComputeTargetDepends();
  398. }
  399. //----------------------------------------------------------------------------
  400. void cmGlobalVisualStudio8Generator::WriteProjectDepends(
  401. std::ostream& fout, const std::string&, const char*, cmTarget const& t)
  402. {
  403. cmGeneratorTarget* gt = this->GetGeneratorTarget(&t);
  404. TargetDependSet const& unordered = this->GetTargetDirectDepends(gt);
  405. OrderedTargetDependSet depends(unordered, std::string());
  406. for(OrderedTargetDependSet::const_iterator i = depends.begin();
  407. i != depends.end(); ++i)
  408. {
  409. if((*i)->GetType() == cmTarget::INTERFACE_LIBRARY)
  410. {
  411. continue;
  412. }
  413. std::string guid = this->GetGUID((*i)->GetName().c_str());
  414. fout << "\t\t{" << guid << "} = {" << guid << "}\n";
  415. }
  416. }
  417. //----------------------------------------------------------------------------
  418. bool cmGlobalVisualStudio8Generator::NeedLinkLibraryDependencies(
  419. cmTarget& target)
  420. {
  421. // Look for utility dependencies that magically link.
  422. for(std::set<std::string>::const_iterator ui =
  423. target.GetUtilities().begin();
  424. ui != target.GetUtilities().end(); ++ui)
  425. {
  426. if(cmTarget* depTarget = this->FindTarget(ui->c_str()))
  427. {
  428. if(depTarget->GetType() != cmTarget::INTERFACE_LIBRARY
  429. && depTarget->GetProperty("EXTERNAL_MSPROJECT"))
  430. {
  431. // This utility dependency names an external .vcproj target.
  432. // We use LinkLibraryDependencies="true" to link to it without
  433. // predicting the .lib file location or name.
  434. return true;
  435. }
  436. }
  437. }
  438. return false;
  439. }
  440. //----------------------------------------------------------------------------
  441. static cmVS7FlagTable cmVS8ExtraFlagTable[] =
  442. {
  443. {"CallingConvention", "Gd", "cdecl", "0", 0 },
  444. {"CallingConvention", "Gr", "fastcall", "1", 0 },
  445. {"CallingConvention", "Gz", "stdcall", "2", 0 },
  446. {"Detect64BitPortabilityProblems", "Wp64",
  447. "Detect 64Bit Portability Problems", "true", 0 },
  448. {"ErrorReporting", "errorReport:prompt", "Report immediately", "1", 0 },
  449. {"ErrorReporting", "errorReport:queue", "Queue for next login", "2", 0 },
  450. // Precompiled header and related options. Note that the
  451. // UsePrecompiledHeader entries are marked as "Continue" so that the
  452. // corresponding PrecompiledHeaderThrough entry can be found.
  453. {"UsePrecompiledHeader", "Yu", "Use Precompiled Header", "2",
  454. cmVS7FlagTable::UserValueIgnored | cmVS7FlagTable::Continue},
  455. {"PrecompiledHeaderThrough", "Yu", "Precompiled Header Name", "",
  456. cmVS7FlagTable::UserValueRequired},
  457. // There is no YX option in the VS8 IDE.
  458. // Exception handling mode. If no entries match, it will be FALSE.
  459. {"ExceptionHandling", "GX", "enable c++ exceptions", "1", 0},
  460. {"ExceptionHandling", "EHsc", "enable c++ exceptions", "1", 0},
  461. {"ExceptionHandling", "EHa", "enable SEH exceptions", "2", 0},
  462. {"EnablePREfast", "analyze", "", "true", 0},
  463. {"EnablePREfast", "analyze-", "", "false", 0},
  464. // Language options
  465. {"TreatWChar_tAsBuiltInType", "Zc:wchar_t",
  466. "wchar_t is a built-in type", "true", 0},
  467. {"TreatWChar_tAsBuiltInType", "Zc:wchar_t-",
  468. "wchar_t is not a built-in type", "false", 0},
  469. {0,0,0,0,0}
  470. };
  471. cmIDEFlagTable const* cmGlobalVisualStudio8Generator::GetExtraFlagTableVS8()
  472. {
  473. return cmVS8ExtraFlagTable;
  474. }