cmGlobalVisualStudio10Generator.cxx 10 KB

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