cmGlobalVisualStudio10Generator.cxx 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  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(!strcmp(this->ArchitectureId, "Itanium") ||
  135. !strcmp(this->ArchitectureId, "x64"))
  136. {
  137. if(this->IsExpressEdition() && !this->Find64BitTools(mf))
  138. {
  139. return;
  140. }
  141. }
  142. cmGlobalVisualStudio8Generator::EnableLanguage(lang, mf, optional);
  143. }
  144. //----------------------------------------------------------------------------
  145. const char* cmGlobalVisualStudio10Generator::GetPlatformToolset()
  146. {
  147. if(!this->PlatformToolset.empty())
  148. {
  149. return this->PlatformToolset.c_str();
  150. }
  151. return 0;
  152. }
  153. //----------------------------------------------------------------------------
  154. std::string cmGlobalVisualStudio10Generator::GetUserMacrosDirectory()
  155. {
  156. std::string base;
  157. std::string path;
  158. // base begins with the VisualStudioProjectsLocation reg value...
  159. if (cmSystemTools::ReadRegistryValue(
  160. "HKEY_CURRENT_USER\\Software\\Microsoft\\VisualStudio\\10.0;"
  161. "VisualStudioProjectsLocation",
  162. base))
  163. {
  164. cmSystemTools::ConvertToUnixSlashes(base);
  165. // 9.0 macros folder:
  166. path = base + "/VSMacros80";
  167. // *NOT* a typo; right now in Visual Studio 2008 beta the macros
  168. // folder is VSMacros80... They may change it to 90 before final
  169. // release of 2008 or they may not... we'll have to keep our eyes
  170. // on it
  171. }
  172. // path is (correctly) still empty if we did not read the base value from
  173. // the Registry value
  174. return path;
  175. }
  176. //----------------------------------------------------------------------------
  177. std::string cmGlobalVisualStudio10Generator::GetUserMacrosRegKeyBase()
  178. {
  179. return "Software\\Microsoft\\VisualStudio\\10.0\\vsmacros";
  180. }
  181. std::string cmGlobalVisualStudio10Generator
  182. ::GenerateBuildCommand(const char* makeProgram,
  183. const char *projectName,
  184. const char* additionalOptions, const char *targetName,
  185. const char* config, bool ignoreErrors, bool fast)
  186. {
  187. // now build the test
  188. std::string makeCommand
  189. = cmSystemTools::ConvertToOutputPath(makeProgram);
  190. std::string lowerCaseCommand = makeCommand;
  191. cmSystemTools::LowerCase(lowerCaseCommand);
  192. // If makeProgram is devenv, parent class knows how to generate command:
  193. if (lowerCaseCommand.find("devenv") != std::string::npos ||
  194. lowerCaseCommand.find("VCExpress") != std::string::npos)
  195. {
  196. return cmGlobalVisualStudio7Generator::GenerateBuildCommand(makeProgram,
  197. projectName, additionalOptions, targetName, config, ignoreErrors, fast);
  198. }
  199. // Otherwise, assume MSBuild command line, and construct accordingly.
  200. // if there are spaces in the makeCommand, assume a full path
  201. // and convert it to a path with no spaces in it as the
  202. // RunSingleCommand does not like spaces
  203. if(makeCommand.find(' ') != std::string::npos)
  204. {
  205. cmSystemTools::GetShortPath(makeCommand.c_str(), makeCommand);
  206. }
  207. // msbuild.exe CxxOnly.sln /t:Build /p:Configuration=Debug /target:ALL_BUILD
  208. if(!targetName || strlen(targetName) == 0)
  209. {
  210. targetName = "ALL_BUILD";
  211. }
  212. bool clean = false;
  213. if ( targetName && strcmp(targetName, "clean") == 0 )
  214. {
  215. clean = true;
  216. makeCommand += " ";
  217. makeCommand += projectName;
  218. makeCommand += ".sln ";
  219. makeCommand += "/t:Clean ";
  220. }
  221. else
  222. {
  223. makeCommand += " ";
  224. makeCommand += targetName;
  225. makeCommand += ".vcxproj ";
  226. }
  227. makeCommand += "/p:Configuration=";
  228. if(config && strlen(config))
  229. {
  230. makeCommand += config;
  231. }
  232. else
  233. {
  234. makeCommand += "Debug";
  235. }
  236. makeCommand += " /p:VisualStudioVersion=";
  237. makeCommand += this->GetIDEVersion();
  238. if ( additionalOptions )
  239. {
  240. makeCommand += " ";
  241. makeCommand += additionalOptions;
  242. }
  243. return makeCommand;
  244. }
  245. //----------------------------------------------------------------------------
  246. bool cmGlobalVisualStudio10Generator::Find64BitTools(cmMakefile* mf)
  247. {
  248. if(!this->PlatformToolset.empty())
  249. {
  250. return true;
  251. }
  252. // This edition does not come with 64-bit tools. Look for them.
  253. //
  254. // TODO: Detect available tools? x64\v100 exists but does not work?
  255. // KHLM\\SOFTWARE\\Microsoft\\MSBuild\\ToolsVersions\\4.0;VCTargetsPath
  256. // c:/Program Files (x86)/MSBuild/Microsoft.Cpp/v4.0/Platforms/
  257. // {Itanium,Win32,x64}/PlatformToolsets/{v100,v90,Windows7.1SDK}
  258. std::string winSDK_7_1;
  259. if(cmSystemTools::ReadRegistryValue(
  260. "HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Microsoft SDKs\\"
  261. "Windows\\v7.1;InstallationFolder", winSDK_7_1))
  262. {
  263. cmOStringStream m;
  264. m << "Found Windows SDK v7.1: " << winSDK_7_1;
  265. mf->DisplayStatus(m.str().c_str(), -1);
  266. this->PlatformToolset = "Windows7.1SDK";
  267. return true;
  268. }
  269. else
  270. {
  271. cmOStringStream e;
  272. e << "Cannot enable 64-bit tools with Visual Studio 2010 Express.\n"
  273. << "Install the Microsoft Windows SDK v7.1 to get 64-bit tools:\n"
  274. << " http://msdn.microsoft.com/en-us/windows/bb980924.aspx";
  275. mf->IssueMessage(cmake::FATAL_ERROR, e.str().c_str());
  276. cmSystemTools::SetFatalErrorOccured();
  277. return false;
  278. }
  279. }
  280. //----------------------------------------------------------------------------
  281. std::string
  282. cmGlobalVisualStudio10Generator
  283. ::GenerateRuleFile(std::string const& output) const
  284. {
  285. // The VS 10 generator needs to create the .rule files on disk.
  286. // Hide them away under the CMakeFiles directory.
  287. std::string ruleDir = this->GetCMakeInstance()->GetHomeOutputDirectory();
  288. ruleDir += cmake::GetCMakeFilesDirectory();
  289. ruleDir += "/";
  290. ruleDir += cmSystemTools::ComputeStringMD5(
  291. cmSystemTools::GetFilenamePath(output).c_str());
  292. std::string ruleFile = ruleDir + "/";
  293. ruleFile += cmSystemTools::GetFilenameName(output);
  294. ruleFile += ".rule";
  295. return ruleFile;
  296. }
  297. //----------------------------------------------------------------------------
  298. void cmGlobalVisualStudio10Generator::PathTooLong(
  299. cmTarget* target, cmSourceFile* sf, std::string const& sfRel)
  300. {
  301. size_t len = (strlen(target->GetMakefile()->GetCurrentOutputDirectory()) +
  302. 1 + sfRel.length());
  303. if(len > this->LongestSource.Length)
  304. {
  305. this->LongestSource.Length = len;
  306. this->LongestSource.Target = target;
  307. this->LongestSource.SourceFile = sf;
  308. this->LongestSource.SourceRel = sfRel;
  309. }
  310. }
  311. //----------------------------------------------------------------------------
  312. bool cmGlobalVisualStudio10Generator::UseFolderProperty()
  313. {
  314. return IsExpressEdition() ? false : cmGlobalGenerator::UseFolderProperty();
  315. }