cmGlobalVisualStudio10Generator.cxx 10 KB

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