cmGlobalVisualStudio10Generator.cxx 16 KB

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