cmGlobalVisualStudio8Generator.cxx 17 KB

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