cmGlobalVisualStudio8Generator.cxx 17 KB

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