cmGlobalVisualStudio10Generator.cxx 16 KB

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