cmGlobalVisualStudio8Generator.cxx 16 KB

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