cmGlobalVisualStudio10Generator.cxx 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480
  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 "cmVisualStudioSlnData.h"
  16. #include "cmVisualStudioSlnParser.h"
  17. #include "cmake.h"
  18. static const char vs10generatorName[] = "Visual Studio 10 2010";
  19. // Map generator name without year to name with year.
  20. static const char* cmVS10GenName(const char* name, std::string& genName)
  21. {
  22. if(strncmp(name, vs10generatorName, sizeof(vs10generatorName)-6) != 0)
  23. {
  24. return 0;
  25. }
  26. const char* p = name + sizeof(vs10generatorName) - 6;
  27. if(strncmp(p, " 2010", 5) == 0)
  28. {
  29. p += 5;
  30. }
  31. genName = std::string(vs10generatorName) + p;
  32. return p;
  33. }
  34. class cmGlobalVisualStudio10Generator::Factory
  35. : public cmGlobalGeneratorFactory
  36. {
  37. public:
  38. virtual cmGlobalGenerator* CreateGlobalGenerator(const char* name) const
  39. {
  40. std::string genName;
  41. const char* p = cmVS10GenName(name, genName);
  42. if(!p)
  43. { return 0; }
  44. name = genName.c_str();
  45. if(strcmp(p, "") == 0)
  46. {
  47. return new cmGlobalVisualStudio10Generator(
  48. name, NULL, NULL);
  49. }
  50. if(strcmp(p, " Win64") == 0)
  51. {
  52. return new cmGlobalVisualStudio10Generator(
  53. name, "x64", "CMAKE_FORCE_WIN64");
  54. }
  55. if(strcmp(p, " IA64") == 0)
  56. {
  57. return new cmGlobalVisualStudio10Generator(
  58. name, "Itanium", "CMAKE_FORCE_IA64");
  59. }
  60. return 0;
  61. }
  62. virtual void GetDocumentation(cmDocumentationEntry& entry) const
  63. {
  64. entry.Name = vs10generatorName;
  65. entry.Brief = "Generates Visual Studio 10 (VS 2010) project files.";
  66. }
  67. virtual void GetGenerators(std::vector<std::string>& names) const
  68. {
  69. names.push_back(vs10generatorName);
  70. names.push_back(vs10generatorName + std::string(" IA64"));
  71. names.push_back(vs10generatorName + std::string(" Win64"));
  72. }
  73. };
  74. //----------------------------------------------------------------------------
  75. cmGlobalGeneratorFactory* cmGlobalVisualStudio10Generator::NewFactory()
  76. {
  77. return new Factory;
  78. }
  79. //----------------------------------------------------------------------------
  80. cmGlobalVisualStudio10Generator::cmGlobalVisualStudio10Generator(
  81. const char* name, const char* platformName,
  82. const char* additionalPlatformDefinition)
  83. : cmGlobalVisualStudio8Generator(name, platformName,
  84. additionalPlatformDefinition)
  85. {
  86. this->FindMakeProgramFile = "CMakeVS10FindMake.cmake";
  87. std::string vc10Express;
  88. this->ExpressEdition = cmSystemTools::ReadRegistryValue(
  89. "HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\VCExpress\\10.0\\Setup\\VC;"
  90. "ProductDir", vc10Express, cmSystemTools::KeyWOW64_32);
  91. this->MasmEnabled = false;
  92. this->MSBuildCommandInitialized = false;
  93. }
  94. //----------------------------------------------------------------------------
  95. bool
  96. cmGlobalVisualStudio10Generator::MatchesGeneratorName(const char* name) const
  97. {
  98. std::string genName;
  99. if(cmVS10GenName(name, genName))
  100. {
  101. return genName == this->GetName();
  102. }
  103. return false;
  104. }
  105. //----------------------------------------------------------------------------
  106. bool
  107. cmGlobalVisualStudio10Generator::SetGeneratorToolset(std::string const& ts)
  108. {
  109. this->PlatformToolset = ts;
  110. return true;
  111. }
  112. //----------------------------------------------------------------------------
  113. void cmGlobalVisualStudio10Generator::AddPlatformDefinitions(cmMakefile* mf)
  114. {
  115. cmGlobalVisualStudio8Generator::AddPlatformDefinitions(mf);
  116. if(!this->PlatformToolset.empty())
  117. {
  118. mf->AddDefinition("CMAKE_VS_PLATFORM_TOOLSET",
  119. this->PlatformToolset.c_str());
  120. }
  121. }
  122. //----------------------------------------------------------------------------
  123. void cmGlobalVisualStudio10Generator::WriteSLNHeader(std::ostream& fout)
  124. {
  125. fout << "Microsoft Visual Studio Solution File, Format Version 11.00\n";
  126. if (this->ExpressEdition)
  127. {
  128. fout << "# Visual C++ Express 2010\n";
  129. }
  130. else
  131. {
  132. fout << "# Visual Studio 2010\n";
  133. }
  134. }
  135. ///! Create a local generator appropriate to this Global Generator
  136. cmLocalGenerator *cmGlobalVisualStudio10Generator::CreateLocalGenerator()
  137. {
  138. cmLocalVisualStudio10Generator* lg =
  139. new cmLocalVisualStudio10Generator(cmLocalVisualStudioGenerator::VS10);
  140. lg->SetPlatformName(this->GetPlatformName());
  141. lg->SetGlobalGenerator(this);
  142. return lg;
  143. }
  144. //----------------------------------------------------------------------------
  145. void cmGlobalVisualStudio10Generator::Generate()
  146. {
  147. this->LongestSource = LongestSourcePath();
  148. this->cmGlobalVisualStudio8Generator::Generate();
  149. if(this->LongestSource.Length > 0)
  150. {
  151. cmMakefile* mf = this->LongestSource.Target->GetMakefile();
  152. cmOStringStream e;
  153. e <<
  154. "The binary and/or source directory paths may be too long to generate "
  155. "Visual Studio 10 files for this project. "
  156. "Consider choosing shorter directory names to build this project with "
  157. "Visual Studio 10. "
  158. "A more detailed explanation follows."
  159. "\n"
  160. "There is a bug in the VS 10 IDE that renders property dialog fields "
  161. "blank for files referenced by full path in the project file. "
  162. "However, CMake must reference at least one file by full path:\n"
  163. " " << this->LongestSource.SourceFile->GetFullPath() << "\n"
  164. "This is because some Visual Studio tools would append the relative "
  165. "path to the end of the referencing directory path, as in:\n"
  166. " " << mf->GetCurrentOutputDirectory() << "/"
  167. << this->LongestSource.SourceRel << "\n"
  168. "and then incorrectly complain that the file does not exist because "
  169. "the path length is too long for some internal buffer or API. "
  170. "To avoid this problem CMake must use a full path for this file "
  171. "which then triggers the VS 10 property dialog bug.";
  172. mf->IssueMessage(cmake::WARNING, e.str().c_str());
  173. }
  174. }
  175. //----------------------------------------------------------------------------
  176. void cmGlobalVisualStudio10Generator
  177. ::EnableLanguage(std::vector<std::string>const & lang,
  178. cmMakefile *mf, bool optional)
  179. {
  180. if(this->PlatformName == "Itanium" || this->PlatformName == "x64")
  181. {
  182. if(this->IsExpressEdition() && !this->Find64BitTools(mf))
  183. {
  184. return;
  185. }
  186. }
  187. for(std::vector<std::string>::const_iterator it = lang.begin();
  188. it != lang.end(); ++it)
  189. {
  190. if(*it == "ASM_MASM")
  191. {
  192. this->MasmEnabled = true;
  193. }
  194. }
  195. cmGlobalVisualStudio8Generator::EnableLanguage(lang, mf, optional);
  196. }
  197. //----------------------------------------------------------------------------
  198. const char* cmGlobalVisualStudio10Generator::GetPlatformToolset()
  199. {
  200. if(!this->PlatformToolset.empty())
  201. {
  202. return this->PlatformToolset.c_str();
  203. }
  204. return 0;
  205. }
  206. //----------------------------------------------------------------------------
  207. std::string cmGlobalVisualStudio10Generator::GetUserMacrosDirectory()
  208. {
  209. std::string base;
  210. std::string path;
  211. // base begins with the VisualStudioProjectsLocation reg value...
  212. if (cmSystemTools::ReadRegistryValue(
  213. "HKEY_CURRENT_USER\\Software\\Microsoft\\VisualStudio\\10.0;"
  214. "VisualStudioProjectsLocation",
  215. base))
  216. {
  217. cmSystemTools::ConvertToUnixSlashes(base);
  218. // 9.0 macros folder:
  219. path = base + "/VSMacros80";
  220. // *NOT* a typo; right now in Visual Studio 2008 beta the macros
  221. // folder is VSMacros80... They may change it to 90 before final
  222. // release of 2008 or they may not... we'll have to keep our eyes
  223. // on it
  224. }
  225. // path is (correctly) still empty if we did not read the base value from
  226. // the Registry value
  227. return path;
  228. }
  229. //----------------------------------------------------------------------------
  230. std::string cmGlobalVisualStudio10Generator::GetUserMacrosRegKeyBase()
  231. {
  232. return "Software\\Microsoft\\VisualStudio\\10.0\\vsmacros";
  233. }
  234. //----------------------------------------------------------------------------
  235. std::string const& cmGlobalVisualStudio10Generator::GetMSBuildCommand()
  236. {
  237. if(!this->MSBuildCommandInitialized)
  238. {
  239. this->MSBuildCommandInitialized = true;
  240. this->MSBuildCommand = this->FindMSBuildCommand();
  241. }
  242. return this->MSBuildCommand;
  243. }
  244. //----------------------------------------------------------------------------
  245. std::string cmGlobalVisualStudio10Generator::FindMSBuildCommand()
  246. {
  247. std::string msbuild;
  248. std::string mskey =
  249. "HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\MSBuild\\ToolsVersions\\";
  250. mskey += this->GetToolsVersion();
  251. mskey += ";MSBuildToolsPath";
  252. if(cmSystemTools::ReadRegistryValue(mskey.c_str(), msbuild,
  253. cmSystemTools::KeyWOW64_32))
  254. {
  255. cmSystemTools::ConvertToUnixSlashes(msbuild);
  256. msbuild += "/";
  257. }
  258. msbuild += "MSBuild.exe";
  259. return msbuild;
  260. }
  261. //----------------------------------------------------------------------------
  262. std::string cmGlobalVisualStudio10Generator::FindDevEnvCommand()
  263. {
  264. if(this->ExpressEdition)
  265. {
  266. // Visual Studio Express >= 10 do not have "devenv.com" or
  267. // "VCExpress.exe" that we can use to build reliably.
  268. // Tell the caller it needs to use MSBuild instead.
  269. return "";
  270. }
  271. // Skip over the cmGlobalVisualStudio8Generator implementation because
  272. // we expect a real devenv and do not want to look for VCExpress.
  273. return this->cmGlobalVisualStudio71Generator::FindDevEnvCommand();
  274. }
  275. //----------------------------------------------------------------------------
  276. void cmGlobalVisualStudio10Generator::GenerateBuildCommand(
  277. std::vector<std::string>& makeCommand,
  278. const char* makeProgram,
  279. const char* projectName,
  280. const char* projectDir,
  281. const char* targetName,
  282. const char* config,
  283. bool fast,
  284. std::vector<std::string> const& makeOptions)
  285. {
  286. // Select the caller- or user-preferred make program, else MSBuild.
  287. std::string makeProgramSelected =
  288. this->SelectMakeProgram(makeProgram, this->GetMSBuildCommand());
  289. // Check if the caller explicitly requested a devenv tool.
  290. std::string makeProgramLower = makeProgramSelected;
  291. cmSystemTools::LowerCase(makeProgramLower);
  292. bool useDevEnv =
  293. (makeProgramLower.find("devenv") != std::string::npos ||
  294. makeProgramLower.find("vcexpress") != std::string::npos);
  295. // MSBuild is preferred (and required for VS Express), but if the .sln has
  296. // an Intel Fortran .vfproj then we have to use devenv. Parse it to find out.
  297. cmSlnData slnData;
  298. {
  299. std::string slnFile;
  300. if(projectDir && *projectDir)
  301. {
  302. slnFile = projectDir;
  303. slnFile += "/";
  304. }
  305. slnFile += projectName;
  306. slnFile += ".sln";
  307. cmVisualStudioSlnParser parser;
  308. if(parser.ParseFile(slnFile, slnData,
  309. cmVisualStudioSlnParser::DataGroupProjects))
  310. {
  311. std::vector<cmSlnProjectEntry> slnProjects = slnData.GetProjects();
  312. for(std::vector<cmSlnProjectEntry>::iterator i = slnProjects.begin();
  313. !useDevEnv && i != slnProjects.end(); ++i)
  314. {
  315. std::string proj = i->GetRelativePath();
  316. if(proj.size() > 7 &&
  317. proj.substr(proj.size()-7) == ".vfproj")
  318. {
  319. useDevEnv = true;
  320. }
  321. }
  322. }
  323. }
  324. if(useDevEnv)
  325. {
  326. // Use devenv to build solutions containing Intel Fortran projects.
  327. cmGlobalVisualStudio7Generator::GenerateBuildCommand(
  328. makeCommand, makeProgram, projectName, projectDir,
  329. targetName, config, fast, makeOptions);
  330. return;
  331. }
  332. makeCommand.push_back(makeProgramSelected);
  333. // msbuild.exe CxxOnly.sln /t:Build /p:Configuration=Debug /target:ALL_BUILD
  334. if(!targetName || strlen(targetName) == 0)
  335. {
  336. targetName = "ALL_BUILD";
  337. }
  338. if ( targetName && strcmp(targetName, "clean") == 0 )
  339. {
  340. makeCommand.push_back(std::string(projectName)+".sln");
  341. makeCommand.push_back("/t:Clean");
  342. }
  343. else
  344. {
  345. std::string targetProject(targetName);
  346. targetProject += ".vcxproj";
  347. if (targetProject.find('/') == std::string::npos)
  348. {
  349. // it might be in a subdir
  350. if (cmSlnProjectEntry const* proj =
  351. slnData.GetProjectByName(targetName))
  352. {
  353. targetProject = proj->GetRelativePath();
  354. cmSystemTools::ConvertToUnixSlashes(targetProject);
  355. }
  356. }
  357. makeCommand.push_back(targetProject);
  358. }
  359. std::string configArg = "/p:Configuration=";
  360. if(config && strlen(config))
  361. {
  362. configArg += config;
  363. }
  364. else
  365. {
  366. configArg += "Debug";
  367. }
  368. makeCommand.push_back(configArg);
  369. makeCommand.push_back(std::string("/p:VisualStudioVersion=")+
  370. this->GetIDEVersion());
  371. makeCommand.insert(makeCommand.end(),
  372. makeOptions.begin(), makeOptions.end());
  373. }
  374. //----------------------------------------------------------------------------
  375. bool cmGlobalVisualStudio10Generator::Find64BitTools(cmMakefile* mf)
  376. {
  377. if(!this->PlatformToolset.empty())
  378. {
  379. return true;
  380. }
  381. // This edition does not come with 64-bit tools. Look for them.
  382. //
  383. // TODO: Detect available tools? x64\v100 exists but does not work?
  384. // KHLM\\SOFTWARE\\Microsoft\\MSBuild\\ToolsVersions\\4.0;VCTargetsPath
  385. // c:/Program Files (x86)/MSBuild/Microsoft.Cpp/v4.0/Platforms/
  386. // {Itanium,Win32,x64}/PlatformToolsets/{v100,v90,Windows7.1SDK}
  387. std::string winSDK_7_1;
  388. if(cmSystemTools::ReadRegistryValue(
  389. "HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Microsoft SDKs\\"
  390. "Windows\\v7.1;InstallationFolder", winSDK_7_1))
  391. {
  392. cmOStringStream m;
  393. m << "Found Windows SDK v7.1: " << winSDK_7_1;
  394. mf->DisplayStatus(m.str().c_str(), -1);
  395. this->PlatformToolset = "Windows7.1SDK";
  396. return true;
  397. }
  398. else
  399. {
  400. cmOStringStream e;
  401. e << "Cannot enable 64-bit tools with Visual Studio 2010 Express.\n"
  402. << "Install the Microsoft Windows SDK v7.1 to get 64-bit tools:\n"
  403. << " http://msdn.microsoft.com/en-us/windows/bb980924.aspx";
  404. mf->IssueMessage(cmake::FATAL_ERROR, e.str().c_str());
  405. cmSystemTools::SetFatalErrorOccured();
  406. return false;
  407. }
  408. }
  409. //----------------------------------------------------------------------------
  410. std::string
  411. cmGlobalVisualStudio10Generator
  412. ::GenerateRuleFile(std::string const& output) const
  413. {
  414. // The VS 10 generator needs to create the .rule files on disk.
  415. // Hide them away under the CMakeFiles directory.
  416. std::string ruleDir = this->GetCMakeInstance()->GetHomeOutputDirectory();
  417. ruleDir += cmake::GetCMakeFilesDirectory();
  418. ruleDir += "/";
  419. ruleDir += cmSystemTools::ComputeStringMD5(
  420. cmSystemTools::GetFilenamePath(output).c_str());
  421. std::string ruleFile = ruleDir + "/";
  422. ruleFile += cmSystemTools::GetFilenameName(output);
  423. ruleFile += ".rule";
  424. return ruleFile;
  425. }
  426. //----------------------------------------------------------------------------
  427. void cmGlobalVisualStudio10Generator::PathTooLong(
  428. cmTarget* target, cmSourceFile* sf, std::string const& sfRel)
  429. {
  430. size_t len = (strlen(target->GetMakefile()->GetCurrentOutputDirectory()) +
  431. 1 + sfRel.length());
  432. if(len > this->LongestSource.Length)
  433. {
  434. this->LongestSource.Length = len;
  435. this->LongestSource.Target = target;
  436. this->LongestSource.SourceFile = sf;
  437. this->LongestSource.SourceRel = sfRel;
  438. }
  439. }
  440. //----------------------------------------------------------------------------
  441. bool cmGlobalVisualStudio10Generator::UseFolderProperty()
  442. {
  443. return IsExpressEdition() ? false : cmGlobalGenerator::UseFolderProperty();
  444. }