cmGlobalVisualStudio10Generator.cxx 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  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 "cmGlobalVisualStudio10Generator.h"
  12. #include "cmLocalVisualStudio10Generator.h"
  13. #include "cmMakefile.h"
  14. #include "cmSourceFile.h"
  15. #include "cmake.h"
  16. static const char vs10Win32generatorName[] = "Visual Studio 10";
  17. static const char vs10Win64generatorName[] = "Visual Studio 10 Win64";
  18. static const char vs10IA64generatorName[] = "Visual Studio 10 IA64";
  19. class cmGlobalVisualStudio10Generator::Factory
  20. : public cmGlobalGeneratorFactory
  21. {
  22. public:
  23. virtual cmGlobalGenerator* CreateGlobalGenerator(const char* name) const {
  24. if(!strcmp(name, vs10Win32generatorName))
  25. {
  26. return new cmGlobalVisualStudio10Generator(
  27. vs10Win32generatorName, NULL, NULL);
  28. }
  29. if(!strcmp(name, vs10Win64generatorName))
  30. {
  31. return new cmGlobalVisualStudio10Generator(
  32. vs10Win64generatorName, "x64", "CMAKE_FORCE_WIN64");
  33. }
  34. if(!strcmp(name, vs10IA64generatorName))
  35. {
  36. return new cmGlobalVisualStudio10Generator(
  37. vs10IA64generatorName, "Itanium", "CMAKE_FORCE_IA64");
  38. }
  39. return 0;
  40. }
  41. virtual void GetDocumentation(cmDocumentationEntry& entry) const {
  42. entry.Name = "Visual Studio 10";
  43. entry.Brief = "Generates Visual Studio 10 project files.";
  44. entry.Full =
  45. "It is possible to append a space followed by the platform name "
  46. "to create project files for a specific target platform. E.g. "
  47. "\"Visual Studio 10 Win64\" will create project files for "
  48. "the x64 processor; \"Visual Studio 10 IA64\" for Itanium.";
  49. }
  50. virtual void GetGenerators(std::vector<std::string>& names) const {
  51. names.push_back(vs10Win32generatorName);
  52. names.push_back(vs10Win64generatorName);
  53. names.push_back(vs10IA64generatorName); }
  54. };
  55. //----------------------------------------------------------------------------
  56. cmGlobalGeneratorFactory* cmGlobalVisualStudio10Generator::NewFactory()
  57. {
  58. return new Factory;
  59. }
  60. //----------------------------------------------------------------------------
  61. cmGlobalVisualStudio10Generator::cmGlobalVisualStudio10Generator(
  62. const char* name, const char* architectureId,
  63. const char* additionalPlatformDefinition)
  64. : cmGlobalVisualStudio8Generator(name, architectureId,
  65. additionalPlatformDefinition)
  66. {
  67. this->FindMakeProgramFile = "CMakeVS10FindMake.cmake";
  68. std::string vc10Express;
  69. this->ExpressEdition = cmSystemTools::ReadRegistryValue(
  70. "HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\VCExpress\\10.0\\Setup\\VC;"
  71. "ProductDir", vc10Express, cmSystemTools::KeyWOW64_32);
  72. }
  73. //----------------------------------------------------------------------------
  74. bool
  75. cmGlobalVisualStudio10Generator::SetGeneratorToolset(std::string const& ts)
  76. {
  77. this->PlatformToolset = ts;
  78. return true;
  79. }
  80. //----------------------------------------------------------------------------
  81. void cmGlobalVisualStudio10Generator::AddPlatformDefinitions(cmMakefile* mf)
  82. {
  83. cmGlobalVisualStudio8Generator::AddPlatformDefinitions(mf);
  84. if(!this->PlatformToolset.empty())
  85. {
  86. mf->AddDefinition("CMAKE_VS_PLATFORM_TOOLSET",
  87. this->PlatformToolset.c_str());
  88. }
  89. }
  90. //----------------------------------------------------------------------------
  91. void cmGlobalVisualStudio10Generator::WriteSLNHeader(std::ostream& fout)
  92. {
  93. fout << "Microsoft Visual Studio Solution File, Format Version 11.00\n";
  94. if (this->ExpressEdition)
  95. {
  96. fout << "# Visual C++ Express 2010\n";
  97. }
  98. else
  99. {
  100. fout << "# Visual Studio 2010\n";
  101. }
  102. }
  103. ///! Create a local generator appropriate to this Global Generator
  104. cmLocalGenerator *cmGlobalVisualStudio10Generator::CreateLocalGenerator()
  105. {
  106. cmLocalVisualStudio10Generator* lg =
  107. new cmLocalVisualStudio10Generator(cmLocalVisualStudioGenerator::VS10);
  108. lg->SetPlatformName(this->GetPlatformName());
  109. lg->SetGlobalGenerator(this);
  110. return lg;
  111. }
  112. //----------------------------------------------------------------------------
  113. void cmGlobalVisualStudio10Generator::Generate()
  114. {
  115. this->LongestSource = LongestSourcePath();
  116. this->cmGlobalVisualStudio8Generator::Generate();
  117. if(this->LongestSource.Length > 0)
  118. {
  119. cmMakefile* mf = this->LongestSource.Target->GetMakefile();
  120. cmOStringStream e;
  121. e <<
  122. "The binary and/or source directory paths may be too long to generate "
  123. "Visual Studio 10 files for this project. "
  124. "Consider choosing shorter directory names to build this project with "
  125. "Visual Studio 10. "
  126. "A more detailed explanation follows."
  127. "\n"
  128. "There is a bug in the VS 10 IDE that renders property dialog fields "
  129. "blank for files referenced by full path in the project file. "
  130. "However, CMake must reference at least one file by full path:\n"
  131. " " << this->LongestSource.SourceFile->GetFullPath() << "\n"
  132. "This is because some Visual Studio tools would append the relative "
  133. "path to the end of the referencing directory path, as in:\n"
  134. " " << mf->GetCurrentOutputDirectory() << "/"
  135. << this->LongestSource.SourceRel << "\n"
  136. "and then incorrectly complain that the file does not exist because "
  137. "the path length is too long for some internal buffer or API. "
  138. "To avoid this problem CMake must use a full path for this file "
  139. "which then triggers the VS 10 property dialog bug.";
  140. mf->IssueMessage(cmake::WARNING, e.str().c_str());
  141. }
  142. }
  143. //----------------------------------------------------------------------------
  144. void cmGlobalVisualStudio10Generator
  145. ::EnableLanguage(std::vector<std::string>const & lang,
  146. cmMakefile *mf, bool optional)
  147. {
  148. if(this->ArchitectureId == "Itanium" || this->ArchitectureId == "x64")
  149. {
  150. if(this->IsExpressEdition() && !this->Find64BitTools(mf))
  151. {
  152. return;
  153. }
  154. }
  155. cmGlobalVisualStudio8Generator::EnableLanguage(lang, mf, optional);
  156. }
  157. //----------------------------------------------------------------------------
  158. const char* cmGlobalVisualStudio10Generator::GetPlatformToolset()
  159. {
  160. if(!this->PlatformToolset.empty())
  161. {
  162. return this->PlatformToolset.c_str();
  163. }
  164. return 0;
  165. }
  166. //----------------------------------------------------------------------------
  167. std::string cmGlobalVisualStudio10Generator::GetUserMacrosDirectory()
  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\\10.0;"
  174. "VisualStudioProjectsLocation",
  175. base))
  176. {
  177. cmSystemTools::ConvertToUnixSlashes(base);
  178. // 9.0 macros folder:
  179. path = base + "/VSMacros80";
  180. // *NOT* a typo; right now in Visual Studio 2008 beta the macros
  181. // folder is VSMacros80... They may change it to 90 before final
  182. // release of 2008 or they may not... we'll have to keep our eyes
  183. // on it
  184. }
  185. // path is (correctly) still empty if we did not read the base value from
  186. // the Registry value
  187. return path;
  188. }
  189. //----------------------------------------------------------------------------
  190. std::string cmGlobalVisualStudio10Generator::GetUserMacrosRegKeyBase()
  191. {
  192. return "Software\\Microsoft\\VisualStudio\\10.0\\vsmacros";
  193. }
  194. std::string cmGlobalVisualStudio10Generator
  195. ::GenerateBuildCommand(const char* makeProgram,
  196. const char *projectName, const char *projectDir,
  197. const char* additionalOptions, const char *targetName,
  198. const char* config, bool ignoreErrors, bool fast)
  199. {
  200. // now build the test
  201. std::string makeCommand
  202. = cmSystemTools::ConvertToOutputPath(makeProgram);
  203. std::string lowerCaseCommand = makeCommand;
  204. cmSystemTools::LowerCase(lowerCaseCommand);
  205. // If makeProgram is devenv, parent class knows how to generate command:
  206. if (lowerCaseCommand.find("devenv") != std::string::npos ||
  207. lowerCaseCommand.find("VCExpress") != std::string::npos)
  208. {
  209. return cmGlobalVisualStudio7Generator::GenerateBuildCommand(makeProgram,
  210. projectName, projectDir, additionalOptions, targetName, config,
  211. ignoreErrors, fast);
  212. }
  213. // Otherwise, assume MSBuild command line, and construct accordingly.
  214. // if there are spaces in the makeCommand, assume a full path
  215. // and convert it to a path with no spaces in it as the
  216. // RunSingleCommand does not like spaces
  217. if(makeCommand.find(' ') != std::string::npos)
  218. {
  219. cmSystemTools::GetShortPath(makeCommand.c_str(), makeCommand);
  220. }
  221. // msbuild.exe CxxOnly.sln /t:Build /p:Configuration=Debug /target:ALL_BUILD
  222. if(!targetName || strlen(targetName) == 0)
  223. {
  224. targetName = "ALL_BUILD";
  225. }
  226. bool clean = false;
  227. if ( targetName && strcmp(targetName, "clean") == 0 )
  228. {
  229. clean = true;
  230. makeCommand += " ";
  231. makeCommand += projectName;
  232. makeCommand += ".sln ";
  233. makeCommand += "/t:Clean ";
  234. }
  235. else
  236. {
  237. makeCommand += " ";
  238. makeCommand += targetName;
  239. makeCommand += ".vcxproj ";
  240. }
  241. makeCommand += "/p:Configuration=";
  242. if(config && strlen(config))
  243. {
  244. makeCommand += config;
  245. }
  246. else
  247. {
  248. makeCommand += "Debug";
  249. }
  250. makeCommand += " /p:VisualStudioVersion=";
  251. makeCommand += this->GetIDEVersion();
  252. if ( additionalOptions )
  253. {
  254. makeCommand += " ";
  255. makeCommand += additionalOptions;
  256. }
  257. return makeCommand;
  258. }
  259. //----------------------------------------------------------------------------
  260. bool cmGlobalVisualStudio10Generator::Find64BitTools(cmMakefile* mf)
  261. {
  262. if(!this->PlatformToolset.empty())
  263. {
  264. return true;
  265. }
  266. // This edition does not come with 64-bit tools. Look for them.
  267. //
  268. // TODO: Detect available tools? x64\v100 exists but does not work?
  269. // KHLM\\SOFTWARE\\Microsoft\\MSBuild\\ToolsVersions\\4.0;VCTargetsPath
  270. // c:/Program Files (x86)/MSBuild/Microsoft.Cpp/v4.0/Platforms/
  271. // {Itanium,Win32,x64}/PlatformToolsets/{v100,v90,Windows7.1SDK}
  272. std::string winSDK_7_1;
  273. if(cmSystemTools::ReadRegistryValue(
  274. "HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Microsoft SDKs\\"
  275. "Windows\\v7.1;InstallationFolder", winSDK_7_1))
  276. {
  277. cmOStringStream m;
  278. m << "Found Windows SDK v7.1: " << winSDK_7_1;
  279. mf->DisplayStatus(m.str().c_str(), -1);
  280. this->PlatformToolset = "Windows7.1SDK";
  281. return true;
  282. }
  283. else
  284. {
  285. cmOStringStream e;
  286. e << "Cannot enable 64-bit tools with Visual Studio 2010 Express.\n"
  287. << "Install the Microsoft Windows SDK v7.1 to get 64-bit tools:\n"
  288. << " http://msdn.microsoft.com/en-us/windows/bb980924.aspx";
  289. mf->IssueMessage(cmake::FATAL_ERROR, e.str().c_str());
  290. cmSystemTools::SetFatalErrorOccured();
  291. return false;
  292. }
  293. }
  294. //----------------------------------------------------------------------------
  295. std::string
  296. cmGlobalVisualStudio10Generator
  297. ::GenerateRuleFile(std::string const& output) const
  298. {
  299. // The VS 10 generator needs to create the .rule files on disk.
  300. // Hide them away under the CMakeFiles directory.
  301. std::string ruleDir = this->GetCMakeInstance()->GetHomeOutputDirectory();
  302. ruleDir += cmake::GetCMakeFilesDirectory();
  303. ruleDir += "/";
  304. ruleDir += cmSystemTools::ComputeStringMD5(
  305. cmSystemTools::GetFilenamePath(output).c_str());
  306. std::string ruleFile = ruleDir + "/";
  307. ruleFile += cmSystemTools::GetFilenameName(output);
  308. ruleFile += ".rule";
  309. return ruleFile;
  310. }
  311. //----------------------------------------------------------------------------
  312. void cmGlobalVisualStudio10Generator::PathTooLong(
  313. cmTarget* target, cmSourceFile* sf, std::string const& sfRel)
  314. {
  315. size_t len = (strlen(target->GetMakefile()->GetCurrentOutputDirectory()) +
  316. 1 + sfRel.length());
  317. if(len > this->LongestSource.Length)
  318. {
  319. this->LongestSource.Length = len;
  320. this->LongestSource.Target = target;
  321. this->LongestSource.SourceFile = sf;
  322. this->LongestSource.SourceRel = sfRel;
  323. }
  324. }
  325. //----------------------------------------------------------------------------
  326. bool cmGlobalVisualStudio10Generator::UseFolderProperty()
  327. {
  328. return IsExpressEdition() ? false : cmGlobalGenerator::UseFolderProperty();
  329. }