cmGlobalVisualStudio10Generator.cxx 18 KB

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