cmGlobalVisualStudio10Generator.cxx 12 KB

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