cmGlobalVisualStudio8Generator.cxx 17 KB

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