cmGlobalVisualStudio10Generator.cxx 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031
  1. /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. file Copyright.txt or https://cmake.org/licensing for details. */
  3. #include "cmGlobalVisualStudio10Generator.h"
  4. #include "cmAlgorithms.h"
  5. #include "cmDocumentationEntry.h"
  6. #include "cmGeneratorTarget.h"
  7. #include "cmLocalVisualStudio10Generator.h"
  8. #include "cmMakefile.h"
  9. #include "cmSourceFile.h"
  10. #include "cmVS10CLFlagTable.h"
  11. #include "cmVS10CSharpFlagTable.h"
  12. #include "cmVS10CudaFlagTable.h"
  13. #include "cmVS10CudaHostFlagTable.h"
  14. #include "cmVS10LibFlagTable.h"
  15. #include "cmVS10LinkFlagTable.h"
  16. #include "cmVS10MASMFlagTable.h"
  17. #include "cmVS10NASMFlagTable.h"
  18. #include "cmVS10RCFlagTable.h"
  19. #include "cmVersion.h"
  20. #include "cmVisualStudioSlnData.h"
  21. #include "cmVisualStudioSlnParser.h"
  22. #include "cmXMLWriter.h"
  23. #include "cmake.h"
  24. #include "cmsys/FStream.hxx"
  25. #include "cmsys/Glob.hxx"
  26. #include "cmsys/RegularExpression.hxx"
  27. #include <algorithm>
  28. static const char vs10generatorName[] = "Visual Studio 10 2010";
  29. // Map generator name without year to name with year.
  30. static const char* cmVS10GenName(const std::string& name, std::string& genName)
  31. {
  32. if (strncmp(name.c_str(), vs10generatorName,
  33. sizeof(vs10generatorName) - 6) != 0) {
  34. return 0;
  35. }
  36. const char* p = name.c_str() + sizeof(vs10generatorName) - 6;
  37. if (cmHasLiteralPrefix(p, " 2010")) {
  38. p += 5;
  39. }
  40. genName = std::string(vs10generatorName) + p;
  41. return p;
  42. }
  43. class cmGlobalVisualStudio10Generator::Factory
  44. : public cmGlobalGeneratorFactory
  45. {
  46. public:
  47. cmGlobalGenerator* CreateGlobalGenerator(const std::string& name,
  48. cmake* cm) const CM_OVERRIDE
  49. {
  50. std::string genName;
  51. const char* p = cmVS10GenName(name, genName);
  52. if (!p) {
  53. return 0;
  54. }
  55. if (!*p) {
  56. return new cmGlobalVisualStudio10Generator(cm, genName, "");
  57. }
  58. if (*p++ != ' ') {
  59. return 0;
  60. }
  61. if (strcmp(p, "Win64") == 0) {
  62. return new cmGlobalVisualStudio10Generator(cm, genName, "x64");
  63. }
  64. if (strcmp(p, "IA64") == 0) {
  65. return new cmGlobalVisualStudio10Generator(cm, genName, "Itanium");
  66. }
  67. return 0;
  68. }
  69. void GetDocumentation(cmDocumentationEntry& entry) const CM_OVERRIDE
  70. {
  71. entry.Name = std::string(vs10generatorName) + " [arch]";
  72. entry.Brief = "Generates Visual Studio 2010 project files. "
  73. "Optional [arch] can be \"Win64\" or \"IA64\".";
  74. }
  75. void GetGenerators(std::vector<std::string>& names) const CM_OVERRIDE
  76. {
  77. names.push_back(vs10generatorName);
  78. names.push_back(vs10generatorName + std::string(" IA64"));
  79. names.push_back(vs10generatorName + std::string(" Win64"));
  80. }
  81. bool SupportsToolset() const CM_OVERRIDE { return true; }
  82. bool SupportsPlatform() const CM_OVERRIDE { return true; }
  83. };
  84. cmGlobalGeneratorFactory* cmGlobalVisualStudio10Generator::NewFactory()
  85. {
  86. return new Factory;
  87. }
  88. cmGlobalVisualStudio10Generator::cmGlobalVisualStudio10Generator(
  89. cmake* cm, const std::string& name, const std::string& platformName)
  90. : cmGlobalVisualStudio8Generator(cm, name, platformName)
  91. {
  92. std::string vc10Express;
  93. this->ExpressEdition = cmSystemTools::ReadRegistryValue(
  94. "HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\VCExpress\\10.0\\Setup\\VC;"
  95. "ProductDir",
  96. vc10Express, cmSystemTools::KeyWOW64_32);
  97. this->CudaEnabled = false;
  98. this->SystemIsWindowsCE = false;
  99. this->SystemIsWindowsPhone = false;
  100. this->SystemIsWindowsStore = false;
  101. this->MSBuildCommandInitialized = false;
  102. {
  103. std::string envPlatformToolset;
  104. if (cmSystemTools::GetEnv("PlatformToolset", envPlatformToolset) &&
  105. envPlatformToolset == "Windows7.1SDK") {
  106. // We are running from a Windows7.1SDK command prompt.
  107. this->DefaultPlatformToolset = "Windows7.1SDK";
  108. } else {
  109. this->DefaultPlatformToolset = "v100";
  110. }
  111. }
  112. this->DefaultClFlagTable = cmVS10CLFlagTable;
  113. this->DefaultCSharpFlagTable = cmVS10CSharpFlagTable;
  114. this->DefaultLibFlagTable = cmVS10LibFlagTable;
  115. this->DefaultLinkFlagTable = cmVS10LinkFlagTable;
  116. this->DefaultCudaFlagTable = cmVS10CudaFlagTable;
  117. this->DefaultCudaHostFlagTable = cmVS10CudaHostFlagTable;
  118. this->DefaultMasmFlagTable = cmVS10MASMFlagTable;
  119. this->DefaultNasmFlagTable = cmVS10NASMFlagTable;
  120. this->DefaultRcFlagTable = cmVS10RCFlagTable;
  121. this->Version = VS10;
  122. this->PlatformToolsetNeedsDebugEnum = false;
  123. }
  124. bool cmGlobalVisualStudio10Generator::MatchesGeneratorName(
  125. const std::string& name) const
  126. {
  127. std::string genName;
  128. if (cmVS10GenName(name, genName)) {
  129. return genName == this->GetName();
  130. }
  131. return false;
  132. }
  133. bool cmGlobalVisualStudio10Generator::SetSystemName(std::string const& s,
  134. cmMakefile* mf)
  135. {
  136. this->SystemName = s;
  137. this->SystemVersion = mf->GetSafeDefinition("CMAKE_SYSTEM_VERSION");
  138. if (!this->InitializeSystem(mf)) {
  139. return false;
  140. }
  141. return this->cmGlobalVisualStudio8Generator::SetSystemName(s, mf);
  142. }
  143. bool cmGlobalVisualStudio10Generator::SetGeneratorPlatform(
  144. std::string const& p, cmMakefile* mf)
  145. {
  146. if (!this->cmGlobalVisualStudio8Generator::SetGeneratorPlatform(p, mf)) {
  147. return false;
  148. }
  149. if (this->GetPlatformName() == "Itanium" ||
  150. this->GetPlatformName() == "x64") {
  151. if (this->IsExpressEdition() && !this->Find64BitTools(mf)) {
  152. return false;
  153. }
  154. }
  155. return true;
  156. }
  157. static void cmCudaToolVersion(std::string& s)
  158. {
  159. // "CUDA x.y.props" => "x.y"
  160. s = s.substr(5);
  161. s = s.substr(0, s.size() - 6);
  162. }
  163. bool cmGlobalVisualStudio10Generator::SetGeneratorToolset(
  164. std::string const& ts, cmMakefile* mf)
  165. {
  166. if (this->SystemIsWindowsCE && ts.empty() &&
  167. this->DefaultPlatformToolset.empty()) {
  168. std::ostringstream e;
  169. e << this->GetName() << " Windows CE version '" << this->SystemVersion
  170. << "' requires CMAKE_GENERATOR_TOOLSET to be set.";
  171. mf->IssueMessage(cmake::FATAL_ERROR, e.str());
  172. return false;
  173. }
  174. if (!this->ParseGeneratorToolset(ts, mf)) {
  175. return false;
  176. }
  177. if (!this->FindVCTargetsPath(mf)) {
  178. return false;
  179. }
  180. if (cmHasLiteralPrefix(this->GetPlatformToolsetString(), "v140")) {
  181. // The GenerateDebugInformation link setting for the v140 toolset
  182. // in VS 2015 was originally an enum with "No" and "Debug" values,
  183. // differing from the "false" and "true" values used in older toolsets.
  184. // A VS 2015 update changed it back. Parse the "link.xml" file to
  185. // discover which one we need.
  186. std::string const link_xml = this->VCTargetsPath + "/1033/link.xml";
  187. cmsys::ifstream fin(link_xml.c_str());
  188. std::string line;
  189. while (fin && cmSystemTools::GetLineFromStream(fin, line)) {
  190. if (line.find(" Switch=\"DEBUG\" ") != std::string::npos) {
  191. this->PlatformToolsetNeedsDebugEnum =
  192. line.find(" Name=\"Debug\" ") != std::string::npos;
  193. break;
  194. }
  195. }
  196. }
  197. if (this->GeneratorToolsetCuda.empty()) {
  198. // Find the highest available version of the CUDA tools.
  199. std::vector<std::string> cudaTools;
  200. std::string const bcDir = this->VCTargetsPath + "/BuildCustomizations";
  201. cmsys::Glob gl;
  202. gl.SetRelative(bcDir.c_str());
  203. if (gl.FindFiles(bcDir + "/CUDA *.props")) {
  204. cudaTools = gl.GetFiles();
  205. }
  206. if (!cudaTools.empty()) {
  207. std::for_each(cudaTools.begin(), cudaTools.end(), cmCudaToolVersion);
  208. std::sort(cudaTools.begin(), cudaTools.end(),
  209. cmSystemTools::VersionCompareGreater);
  210. this->GeneratorToolsetCuda = cudaTools.at(0);
  211. }
  212. }
  213. if (const char* toolset = this->GetPlatformToolset()) {
  214. mf->AddDefinition("CMAKE_VS_PLATFORM_TOOLSET", toolset);
  215. }
  216. if (const char* hostArch = this->GetPlatformToolsetHostArchitecture()) {
  217. mf->AddDefinition("CMAKE_VS_PLATFORM_TOOLSET_HOST_ARCHITECTURE", hostArch);
  218. }
  219. if (const char* cuda = this->GetPlatformToolsetCuda()) {
  220. mf->AddDefinition("CMAKE_VS_PLATFORM_TOOLSET_CUDA", cuda);
  221. }
  222. return true;
  223. }
  224. bool cmGlobalVisualStudio10Generator::ParseGeneratorToolset(
  225. std::string const& ts, cmMakefile* mf)
  226. {
  227. std::vector<std::string> const fields = cmSystemTools::tokenize(ts, ",");
  228. std::vector<std::string>::const_iterator fi = fields.begin();
  229. if (fi == fields.end()) {
  230. return true;
  231. }
  232. // The first field may be the VS platform toolset.
  233. if (fi->find('=') == fi->npos) {
  234. this->GeneratorToolset = *fi;
  235. ++fi;
  236. }
  237. std::set<std::string> handled;
  238. // The rest of the fields must be key=value pairs.
  239. for (; fi != fields.end(); ++fi) {
  240. std::string::size_type pos = fi->find('=');
  241. if (pos == fi->npos) {
  242. std::ostringstream e;
  243. /* clang-format off */
  244. e <<
  245. "Generator\n"
  246. " " << this->GetName() << "\n"
  247. "given toolset specification\n"
  248. " " << ts << "\n"
  249. "that contains a field after the first ',' with no '='."
  250. ;
  251. /* clang-format on */
  252. mf->IssueMessage(cmake::FATAL_ERROR, e.str());
  253. return false;
  254. }
  255. std::string const key = fi->substr(0, pos);
  256. std::string const value = fi->substr(pos + 1);
  257. if (!handled.insert(key).second) {
  258. std::ostringstream e;
  259. /* clang-format off */
  260. e <<
  261. "Generator\n"
  262. " " << this->GetName() << "\n"
  263. "given toolset specification\n"
  264. " " << ts << "\n"
  265. "that contains duplicate field key '" << key << "'."
  266. ;
  267. /* clang-format on */
  268. mf->IssueMessage(cmake::FATAL_ERROR, e.str());
  269. return false;
  270. }
  271. if (!this->ProcessGeneratorToolsetField(key, value)) {
  272. std::ostringstream e;
  273. /* clang-format off */
  274. e <<
  275. "Generator\n"
  276. " " << this->GetName() << "\n"
  277. "given toolset specification\n"
  278. " " << ts << "\n"
  279. "that contains invalid field '" << *fi << "'."
  280. ;
  281. /* clang-format on */
  282. mf->IssueMessage(cmake::FATAL_ERROR, e.str());
  283. return false;
  284. }
  285. }
  286. return true;
  287. }
  288. bool cmGlobalVisualStudio10Generator::ProcessGeneratorToolsetField(
  289. std::string const& key, std::string const& value)
  290. {
  291. if (key == "cuda") {
  292. this->GeneratorToolsetCuda = value;
  293. return true;
  294. }
  295. return false;
  296. }
  297. bool cmGlobalVisualStudio10Generator::InitializeSystem(cmMakefile* mf)
  298. {
  299. if (this->SystemName == "Windows") {
  300. if (!this->InitializeWindows(mf)) {
  301. return false;
  302. }
  303. } else if (this->SystemName == "WindowsCE") {
  304. this->SystemIsWindowsCE = true;
  305. if (!this->InitializeWindowsCE(mf)) {
  306. return false;
  307. }
  308. } else if (this->SystemName == "WindowsPhone") {
  309. this->SystemIsWindowsPhone = true;
  310. if (!this->InitializeWindowsPhone(mf)) {
  311. return false;
  312. }
  313. } else if (this->SystemName == "WindowsStore") {
  314. this->SystemIsWindowsStore = true;
  315. if (!this->InitializeWindowsStore(mf)) {
  316. return false;
  317. }
  318. } else if (this->SystemName == "Android") {
  319. if (this->DefaultPlatformName != "Win32") {
  320. std::ostringstream e;
  321. e << "CMAKE_SYSTEM_NAME is 'Android' but CMAKE_GENERATOR "
  322. << "specifies a platform too: '" << this->GetName() << "'";
  323. mf->IssueMessage(cmake::FATAL_ERROR, e.str());
  324. return false;
  325. }
  326. std::string v = this->GetInstalledNsightTegraVersion();
  327. if (v.empty()) {
  328. mf->IssueMessage(cmake::FATAL_ERROR,
  329. "CMAKE_SYSTEM_NAME is 'Android' but "
  330. "'NVIDIA Nsight Tegra Visual Studio Edition' "
  331. "is not installed.");
  332. return false;
  333. }
  334. this->DefaultPlatformName = "Tegra-Android";
  335. this->DefaultPlatformToolset = "Default";
  336. this->NsightTegraVersion = v;
  337. mf->AddDefinition("CMAKE_VS_NsightTegra_VERSION", v.c_str());
  338. }
  339. return true;
  340. }
  341. bool cmGlobalVisualStudio10Generator::InitializeWindows(cmMakefile*)
  342. {
  343. return true;
  344. }
  345. bool cmGlobalVisualStudio10Generator::InitializeWindowsCE(cmMakefile* mf)
  346. {
  347. if (this->DefaultPlatformName != "Win32") {
  348. std::ostringstream e;
  349. e << "CMAKE_SYSTEM_NAME is 'WindowsCE' but CMAKE_GENERATOR "
  350. << "specifies a platform too: '" << this->GetName() << "'";
  351. mf->IssueMessage(cmake::FATAL_ERROR, e.str());
  352. return false;
  353. }
  354. this->DefaultPlatformToolset = this->SelectWindowsCEToolset();
  355. return true;
  356. }
  357. bool cmGlobalVisualStudio10Generator::InitializeWindowsPhone(cmMakefile* mf)
  358. {
  359. std::ostringstream e;
  360. e << this->GetName() << " does not support Windows Phone.";
  361. mf->IssueMessage(cmake::FATAL_ERROR, e.str());
  362. return false;
  363. }
  364. bool cmGlobalVisualStudio10Generator::InitializeWindowsStore(cmMakefile* mf)
  365. {
  366. std::ostringstream e;
  367. e << this->GetName() << " does not support Windows Store.";
  368. mf->IssueMessage(cmake::FATAL_ERROR, e.str());
  369. return false;
  370. }
  371. bool cmGlobalVisualStudio10Generator::SelectWindowsPhoneToolset(
  372. std::string& toolset) const
  373. {
  374. toolset = "";
  375. return false;
  376. }
  377. bool cmGlobalVisualStudio10Generator::SelectWindowsStoreToolset(
  378. std::string& toolset) const
  379. {
  380. toolset = "";
  381. return false;
  382. }
  383. std::string cmGlobalVisualStudio10Generator::SelectWindowsCEToolset() const
  384. {
  385. if (this->SystemVersion == "8.0") {
  386. return "CE800";
  387. }
  388. return "";
  389. }
  390. void cmGlobalVisualStudio10Generator::WriteSLNHeader(std::ostream& fout)
  391. {
  392. fout << "Microsoft Visual Studio Solution File, Format Version 11.00\n";
  393. if (this->ExpressEdition) {
  394. fout << "# Visual C++ Express 2010\n";
  395. } else {
  396. fout << "# Visual Studio 2010\n";
  397. }
  398. }
  399. ///! Create a local generator appropriate to this Global Generator
  400. cmLocalGenerator* cmGlobalVisualStudio10Generator::CreateLocalGenerator(
  401. cmMakefile* mf)
  402. {
  403. return new cmLocalVisualStudio10Generator(this, mf);
  404. }
  405. void cmGlobalVisualStudio10Generator::Generate()
  406. {
  407. this->LongestSource = LongestSourcePath();
  408. this->cmGlobalVisualStudio8Generator::Generate();
  409. if (this->LongestSource.Length > 0) {
  410. cmLocalGenerator* lg = this->LongestSource.Target->GetLocalGenerator();
  411. std::ostringstream e;
  412. /* clang-format off */
  413. e <<
  414. "The binary and/or source directory paths may be too long to generate "
  415. "Visual Studio 10 files for this project. "
  416. "Consider choosing shorter directory names to build this project with "
  417. "Visual Studio 10. "
  418. "A more detailed explanation follows."
  419. "\n"
  420. "There is a bug in the VS 10 IDE that renders property dialog fields "
  421. "blank for files referenced by full path in the project file. "
  422. "However, CMake must reference at least one file by full path:\n"
  423. " " << this->LongestSource.SourceFile->GetFullPath() << "\n"
  424. "This is because some Visual Studio tools would append the relative "
  425. "path to the end of the referencing directory path, as in:\n"
  426. " " << lg->GetCurrentBinaryDirectory() << "/"
  427. << this->LongestSource.SourceRel << "\n"
  428. "and then incorrectly complain that the file does not exist because "
  429. "the path length is too long for some internal buffer or API. "
  430. "To avoid this problem CMake must use a full path for this file "
  431. "which then triggers the VS 10 property dialog bug.";
  432. /* clang-format on */
  433. lg->IssueMessage(cmake::WARNING, e.str().c_str());
  434. }
  435. }
  436. void cmGlobalVisualStudio10Generator::EnableLanguage(
  437. std::vector<std::string> const& lang, cmMakefile* mf, bool optional)
  438. {
  439. for (std::vector<std::string>::const_iterator it = lang.begin();
  440. it != lang.end(); ++it) {
  441. if (*it == "ASM_NASM") {
  442. this->NasmEnabled = true;
  443. }
  444. if (*it == "CUDA") {
  445. this->CudaEnabled = true;
  446. }
  447. }
  448. this->AddPlatformDefinitions(mf);
  449. cmGlobalVisualStudio8Generator::EnableLanguage(lang, mf, optional);
  450. }
  451. const char* cmGlobalVisualStudio10Generator::GetPlatformToolset() const
  452. {
  453. std::string const& toolset = this->GetPlatformToolsetString();
  454. if (toolset.empty()) {
  455. return CM_NULLPTR;
  456. }
  457. return toolset.c_str();
  458. }
  459. std::string const& cmGlobalVisualStudio10Generator::GetPlatformToolsetString()
  460. const
  461. {
  462. if (!this->GeneratorToolset.empty()) {
  463. return this->GeneratorToolset;
  464. }
  465. if (!this->DefaultPlatformToolset.empty()) {
  466. return this->DefaultPlatformToolset;
  467. }
  468. static std::string const empty;
  469. return empty;
  470. }
  471. const char*
  472. cmGlobalVisualStudio10Generator::GetPlatformToolsetHostArchitecture() const
  473. {
  474. if (!this->GeneratorToolsetHostArchitecture.empty()) {
  475. return this->GeneratorToolsetHostArchitecture.c_str();
  476. }
  477. return CM_NULLPTR;
  478. }
  479. const char* cmGlobalVisualStudio10Generator::GetPlatformToolsetCuda() const
  480. {
  481. if (!this->GeneratorToolsetCuda.empty()) {
  482. return this->GeneratorToolsetCuda.c_str();
  483. }
  484. return CM_NULLPTR;
  485. }
  486. std::string const&
  487. cmGlobalVisualStudio10Generator::GetPlatformToolsetCudaString() const
  488. {
  489. return this->GeneratorToolsetCuda;
  490. }
  491. bool cmGlobalVisualStudio10Generator::FindMakeProgram(cmMakefile* mf)
  492. {
  493. if (!this->cmGlobalVisualStudio8Generator::FindMakeProgram(mf)) {
  494. return false;
  495. }
  496. mf->AddDefinition("CMAKE_VS_MSBUILD_COMMAND",
  497. this->GetMSBuildCommand().c_str());
  498. return true;
  499. }
  500. std::string const& cmGlobalVisualStudio10Generator::GetMSBuildCommand()
  501. {
  502. if (!this->MSBuildCommandInitialized) {
  503. this->MSBuildCommandInitialized = true;
  504. this->MSBuildCommand = this->FindMSBuildCommand();
  505. }
  506. return this->MSBuildCommand;
  507. }
  508. std::string cmGlobalVisualStudio10Generator::FindMSBuildCommand()
  509. {
  510. std::string msbuild;
  511. std::string mskey;
  512. // Search in standard location.
  513. mskey = "HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\MSBuild\\ToolsVersions\\";
  514. mskey += this->GetToolsVersion();
  515. mskey += ";MSBuildToolsPath";
  516. if (cmSystemTools::ReadRegistryValue(mskey.c_str(), msbuild,
  517. cmSystemTools::KeyWOW64_32)) {
  518. cmSystemTools::ConvertToUnixSlashes(msbuild);
  519. msbuild += "/MSBuild.exe";
  520. if (cmSystemTools::FileExists(msbuild, true)) {
  521. return msbuild;
  522. }
  523. }
  524. msbuild = "MSBuild.exe";
  525. return msbuild;
  526. }
  527. std::string cmGlobalVisualStudio10Generator::FindDevEnvCommand()
  528. {
  529. if (this->ExpressEdition) {
  530. // Visual Studio Express >= 10 do not have "devenv.com" or
  531. // "VCExpress.exe" that we can use to build reliably.
  532. // Tell the caller it needs to use MSBuild instead.
  533. return "";
  534. }
  535. // Skip over the cmGlobalVisualStudio8Generator implementation because
  536. // we expect a real devenv and do not want to look for VCExpress.
  537. return this->cmGlobalVisualStudio71Generator::FindDevEnvCommand();
  538. }
  539. bool cmGlobalVisualStudio10Generator::FindVCTargetsPath(cmMakefile* mf)
  540. {
  541. // Skip this in special cases within our own test suite.
  542. if (this->GetPlatformName() == "Test Platform" ||
  543. this->GetPlatformToolsetString() == "Test Toolset") {
  544. return true;
  545. }
  546. std::string wd;
  547. if (!this->ConfiguredFilesPath.empty()) {
  548. // In a try-compile we are given the outer CMakeFiles directory.
  549. wd = this->ConfiguredFilesPath;
  550. } else {
  551. wd = this->GetCMakeInstance()->GetHomeOutputDirectory();
  552. wd += cmake::GetCMakeFilesDirectory();
  553. }
  554. wd += "/";
  555. wd += cmVersion::GetCMakeVersion();
  556. // We record the result persistently in a file.
  557. std::string const txt = wd + "/VCTargetsPath.txt";
  558. // If we have a recorded result, use it.
  559. {
  560. cmsys::ifstream fin(txt.c_str());
  561. if (fin && cmSystemTools::GetLineFromStream(fin, this->VCTargetsPath) &&
  562. cmSystemTools::FileIsDirectory(this->VCTargetsPath)) {
  563. cmSystemTools::ConvertToUnixSlashes(this->VCTargetsPath);
  564. return true;
  565. }
  566. }
  567. // Prepare the work directory.
  568. if (!cmSystemTools::MakeDirectory(wd)) {
  569. std::string e = "Failed to make directory:\n " + wd;
  570. mf->IssueMessage(cmake::FATAL_ERROR, e.c_str());
  571. cmSystemTools::SetFatalErrorOccured();
  572. return false;
  573. }
  574. // Generate a project file for MSBuild to tell us the VCTargetsPath value.
  575. std::string const vcxproj = "VCTargetsPath.vcxproj";
  576. {
  577. std::string const vcxprojAbs = wd + "/" + vcxproj;
  578. cmsys::ofstream fout(vcxprojAbs.c_str());
  579. cmXMLWriter xw(fout);
  580. /* clang-format off */
  581. xw.StartDocument();
  582. xw.StartElement("Project");
  583. xw.Attribute("DefaultTargets", "Build");
  584. xw.Attribute("ToolsVersion", "4.0");
  585. xw.Attribute("xmlns",
  586. "http://schemas.microsoft.com/developer/msbuild/2003");
  587. if (this->IsNsightTegra()) {
  588. xw.StartElement("PropertyGroup");
  589. xw.Attribute("Label", "NsightTegraProject");
  590. xw.StartElement("NsightTegraProjectRevisionNumber");
  591. xw.Content("6");
  592. xw.EndElement(); // NsightTegraProjectRevisionNumber
  593. xw.EndElement(); // PropertyGroup
  594. }
  595. xw.StartElement("ItemGroup");
  596. xw.Attribute("Label", "ProjectConfigurations");
  597. xw.StartElement("ProjectConfiguration");
  598. xw.Attribute("Include", "Debug|" + this->GetPlatformName());
  599. xw.StartElement("Configuration");
  600. xw.Content("Debug");
  601. xw.EndElement(); // Configuration
  602. xw.StartElement("Platform");
  603. xw.Content(this->GetPlatformName());
  604. xw.EndElement(); // Platform
  605. xw.EndElement(); // ProjectConfiguration
  606. xw.EndElement(); // ItemGroup
  607. xw.StartElement("PropertyGroup");
  608. xw.Attribute("Label", "Globals");
  609. xw.StartElement("ProjectGuid");
  610. xw.Content("{F3FC6D86-508D-3FB1-96D2-995F08B142EC}");
  611. xw.EndElement(); // ProjectGuid
  612. xw.StartElement("Keyword");
  613. xw.Content("Win32Proj");
  614. xw.EndElement(); // Keyword
  615. xw.StartElement("Platform");
  616. xw.Content(this->GetPlatformName());
  617. xw.EndElement(); // Platform
  618. if (this->GetSystemName() == "WindowsPhone") {
  619. xw.StartElement("ApplicationType");
  620. xw.Content("Windows Phone");
  621. xw.EndElement(); // ApplicationType
  622. xw.StartElement("ApplicationTypeRevision");
  623. xw.Content(this->GetSystemVersion());
  624. xw.EndElement(); // ApplicationTypeRevision
  625. } else if (this->GetSystemName() == "WindowsStore") {
  626. xw.StartElement("ApplicationType");
  627. xw.Content("Windows Store");
  628. xw.EndElement(); // ApplicationType
  629. xw.StartElement("ApplicationTypeRevision");
  630. xw.Content(this->GetSystemVersion());
  631. xw.EndElement(); // ApplicationTypeRevision
  632. }
  633. if (!this->WindowsTargetPlatformVersion.empty()) {
  634. xw.StartElement("WindowsTargetPlatformVersion");
  635. xw.Content(this->WindowsTargetPlatformVersion);
  636. xw.EndElement(); // WindowsTargetPlatformVersion
  637. }
  638. if (this->GetPlatformName() == "ARM") {
  639. xw.StartElement("WindowsSDKDesktopARMSupport");
  640. xw.Content("true");
  641. xw.EndElement(); // WindowsSDKDesktopARMSupport
  642. }
  643. xw.EndElement(); // PropertyGroup
  644. xw.StartElement("Import");
  645. xw.Attribute("Project",
  646. "$(VCTargetsPath)\\Microsoft.Cpp.Default.props");
  647. xw.EndElement(); // Import
  648. if (!this->GeneratorToolsetHostArchitecture.empty()) {
  649. xw.StartElement("PropertyGroup");
  650. xw.StartElement("PreferredToolArchitecture");
  651. xw.Content(this->GeneratorToolsetHostArchitecture);
  652. xw.EndElement(); // PreferredToolArchitecture
  653. xw.EndElement(); // PropertyGroup
  654. }
  655. xw.StartElement("PropertyGroup");
  656. xw.Attribute("Label", "Configuration");
  657. xw.StartElement("ConfigurationType");
  658. if (this->IsNsightTegra()) {
  659. // Tegra-Android platform does not understand "Utility".
  660. xw.Content("StaticLibrary");
  661. } else {
  662. xw.Content("Utility");
  663. }
  664. xw.EndElement(); // ConfigurationType
  665. xw.StartElement("CharacterSet");
  666. xw.Content("MultiByte");
  667. xw.EndElement(); // CharacterSet
  668. if (this->IsNsightTegra()) {
  669. xw.StartElement("NdkToolchainVersion");
  670. xw.Content(this->GetPlatformToolsetString());
  671. xw.EndElement(); // NdkToolchainVersion
  672. } else {
  673. xw.StartElement("PlatformToolset");
  674. xw.Content(this->GetPlatformToolsetString());
  675. xw.EndElement(); // PlatformToolset
  676. }
  677. xw.EndElement(); // PropertyGroup
  678. xw.StartElement("Import");
  679. xw.Attribute("Project", "$(VCTargetsPath)\\Microsoft.Cpp.props");
  680. xw.EndElement(); // Import
  681. xw.StartElement("ItemDefinitionGroup");
  682. xw.StartElement("PostBuildEvent");
  683. xw.StartElement("Command");
  684. xw.Content("echo VCTargetsPath=$(VCTargetsPath)");
  685. xw.EndElement(); // Command
  686. xw.EndElement(); // PostBuildEvent
  687. xw.EndElement(); // ItemDefinitionGroup
  688. xw.StartElement("Import");
  689. xw.Attribute("Project",
  690. "$(VCTargetsPath)\\Microsoft.Cpp.targets");
  691. xw.EndElement(); // Import
  692. xw.EndElement(); // Project
  693. xw.EndDocument();
  694. /* clang-format on */
  695. }
  696. std::vector<std::string> cmd;
  697. cmd.push_back(this->GetMSBuildCommand());
  698. cmd.push_back(vcxproj);
  699. cmd.push_back(std::string("/p:VisualStudioVersion=") +
  700. this->GetIDEVersion());
  701. std::string out;
  702. int ret = 0;
  703. cmsys::RegularExpression regex("\n *VCTargetsPath=([^%\r\n]+)[\r\n]");
  704. if (!cmSystemTools::RunSingleCommand(cmd, &out, &out, &ret, wd.c_str(),
  705. cmSystemTools::OUTPUT_NONE) ||
  706. ret != 0 || !regex.find(out)) {
  707. cmSystemTools::ReplaceString(out, "\n", "\n ");
  708. std::ostringstream e;
  709. /* clang-format off */
  710. e <<
  711. "Failed to run MSBuild command:\n"
  712. " " << cmd[0] << "\n"
  713. "to get the value of VCTargetsPath:\n"
  714. " " << out << "\n"
  715. ;
  716. /* clang-format on */
  717. if (ret != 0) {
  718. e << "Exit code: " << ret << "\n";
  719. }
  720. mf->IssueMessage(cmake::FATAL_ERROR, e.str().c_str());
  721. cmSystemTools::SetFatalErrorOccured();
  722. return false;
  723. }
  724. this->VCTargetsPath = regex.match(1);
  725. cmSystemTools::ConvertToUnixSlashes(this->VCTargetsPath);
  726. {
  727. cmsys::ofstream fout(txt.c_str());
  728. fout << this->VCTargetsPath << "\n";
  729. }
  730. return true;
  731. }
  732. void cmGlobalVisualStudio10Generator::GenerateBuildCommand(
  733. std::vector<std::string>& makeCommand, const std::string& makeProgram,
  734. const std::string& projectName, const std::string& projectDir,
  735. const std::string& targetName, const std::string& config, bool fast,
  736. bool verbose, std::vector<std::string> const& makeOptions)
  737. {
  738. // Select the caller- or user-preferred make program, else MSBuild.
  739. std::string makeProgramSelected =
  740. this->SelectMakeProgram(makeProgram, this->GetMSBuildCommand());
  741. // Check if the caller explicitly requested a devenv tool.
  742. std::string makeProgramLower = makeProgramSelected;
  743. cmSystemTools::LowerCase(makeProgramLower);
  744. bool useDevEnv = (makeProgramLower.find("devenv") != std::string::npos ||
  745. makeProgramLower.find("vcexpress") != std::string::npos);
  746. // MSBuild is preferred (and required for VS Express), but if the .sln has
  747. // an Intel Fortran .vfproj then we have to use devenv. Parse it to find out.
  748. cmSlnData slnData;
  749. {
  750. std::string slnFile;
  751. if (!projectDir.empty()) {
  752. slnFile = projectDir;
  753. slnFile += "/";
  754. }
  755. slnFile += projectName;
  756. slnFile += ".sln";
  757. cmVisualStudioSlnParser parser;
  758. if (parser.ParseFile(slnFile, slnData,
  759. cmVisualStudioSlnParser::DataGroupProjects)) {
  760. std::vector<cmSlnProjectEntry> slnProjects = slnData.GetProjects();
  761. for (std::vector<cmSlnProjectEntry>::iterator i = slnProjects.begin();
  762. !useDevEnv && i != slnProjects.end(); ++i) {
  763. std::string proj = i->GetRelativePath();
  764. if (proj.size() > 7 && proj.substr(proj.size() - 7) == ".vfproj") {
  765. useDevEnv = true;
  766. }
  767. }
  768. }
  769. }
  770. if (useDevEnv) {
  771. // Use devenv to build solutions containing Intel Fortran projects.
  772. cmGlobalVisualStudio7Generator::GenerateBuildCommand(
  773. makeCommand, makeProgram, projectName, projectDir, targetName, config,
  774. fast, verbose, makeOptions);
  775. return;
  776. }
  777. makeCommand.push_back(makeProgramSelected);
  778. std::string realTarget = targetName;
  779. // msbuild.exe CxxOnly.sln /t:Build /p:Configuration=Debug /target:ALL_BUILD
  780. if (realTarget.empty()) {
  781. realTarget = "ALL_BUILD";
  782. }
  783. if (realTarget == "clean") {
  784. makeCommand.push_back(std::string(projectName) + ".sln");
  785. makeCommand.push_back("/t:Clean");
  786. } else {
  787. std::string targetProject(realTarget);
  788. targetProject += ".vcxproj";
  789. if (targetProject.find('/') == std::string::npos) {
  790. // it might be in a subdir
  791. if (cmSlnProjectEntry const* proj =
  792. slnData.GetProjectByName(realTarget)) {
  793. targetProject = proj->GetRelativePath();
  794. cmSystemTools::ConvertToUnixSlashes(targetProject);
  795. }
  796. }
  797. makeCommand.push_back(targetProject);
  798. }
  799. std::string configArg = "/p:Configuration=";
  800. if (!config.empty()) {
  801. configArg += config;
  802. } else {
  803. configArg += "Debug";
  804. }
  805. makeCommand.push_back(configArg);
  806. makeCommand.push_back(std::string("/p:VisualStudioVersion=") +
  807. this->GetIDEVersion());
  808. makeCommand.insert(makeCommand.end(), makeOptions.begin(),
  809. makeOptions.end());
  810. }
  811. bool cmGlobalVisualStudio10Generator::Find64BitTools(cmMakefile* mf)
  812. {
  813. if (this->DefaultPlatformToolset == "v100") {
  814. // The v100 64-bit toolset does not exist in the express edition.
  815. this->DefaultPlatformToolset.clear();
  816. }
  817. if (this->GetPlatformToolset()) {
  818. return true;
  819. }
  820. // This edition does not come with 64-bit tools. Look for them.
  821. //
  822. // TODO: Detect available tools? x64\v100 exists but does not work?
  823. // HKLM\\SOFTWARE\\Microsoft\\MSBuild\\ToolsVersions\\4.0;VCTargetsPath
  824. // c:/Program Files (x86)/MSBuild/Microsoft.Cpp/v4.0/Platforms/
  825. // {Itanium,Win32,x64}/PlatformToolsets/{v100,v90,Windows7.1SDK}
  826. std::string winSDK_7_1;
  827. if (cmSystemTools::ReadRegistryValue(
  828. "HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Microsoft SDKs\\"
  829. "Windows\\v7.1;InstallationFolder",
  830. winSDK_7_1)) {
  831. std::ostringstream m;
  832. m << "Found Windows SDK v7.1: " << winSDK_7_1;
  833. mf->DisplayStatus(m.str().c_str(), -1);
  834. this->DefaultPlatformToolset = "Windows7.1SDK";
  835. return true;
  836. } else {
  837. std::ostringstream e;
  838. /* clang-format off */
  839. e << "Cannot enable 64-bit tools with Visual Studio 2010 Express.\n"
  840. << "Install the Microsoft Windows SDK v7.1 to get 64-bit tools:\n"
  841. << " http://msdn.microsoft.com/en-us/windows/bb980924.aspx";
  842. /* clang-format on */
  843. mf->IssueMessage(cmake::FATAL_ERROR, e.str().c_str());
  844. cmSystemTools::SetFatalErrorOccured();
  845. return false;
  846. }
  847. }
  848. std::string cmGlobalVisualStudio10Generator::GenerateRuleFile(
  849. std::string const& output) const
  850. {
  851. // The VS 10 generator needs to create the .rule files on disk.
  852. // Hide them away under the CMakeFiles directory.
  853. std::string ruleDir = this->GetCMakeInstance()->GetHomeOutputDirectory();
  854. ruleDir += cmake::GetCMakeFilesDirectory();
  855. ruleDir += "/";
  856. ruleDir += cmSystemTools::ComputeStringMD5(
  857. cmSystemTools::GetFilenamePath(output).c_str());
  858. std::string ruleFile = ruleDir + "/";
  859. ruleFile += cmSystemTools::GetFilenameName(output);
  860. ruleFile += ".rule";
  861. return ruleFile;
  862. }
  863. void cmGlobalVisualStudio10Generator::PathTooLong(cmGeneratorTarget* target,
  864. cmSourceFile const* sf,
  865. std::string const& sfRel)
  866. {
  867. size_t len =
  868. (strlen(target->GetLocalGenerator()->GetCurrentBinaryDirectory()) + 1 +
  869. sfRel.length());
  870. if (len > this->LongestSource.Length) {
  871. this->LongestSource.Length = len;
  872. this->LongestSource.Target = target;
  873. this->LongestSource.SourceFile = sf;
  874. this->LongestSource.SourceRel = sfRel;
  875. }
  876. }
  877. bool cmGlobalVisualStudio10Generator::IsNsightTegra() const
  878. {
  879. return !this->NsightTegraVersion.empty();
  880. }
  881. std::string cmGlobalVisualStudio10Generator::GetNsightTegraVersion() const
  882. {
  883. return this->NsightTegraVersion;
  884. }
  885. std::string cmGlobalVisualStudio10Generator::GetInstalledNsightTegraVersion()
  886. {
  887. std::string version;
  888. cmSystemTools::ReadRegistryValue(
  889. "HKEY_LOCAL_MACHINE\\SOFTWARE\\NVIDIA Corporation\\Nsight Tegra;"
  890. "Version",
  891. version, cmSystemTools::KeyWOW64_32);
  892. return version;
  893. }
  894. cmIDEFlagTable const* cmGlobalVisualStudio10Generator::GetClFlagTable() const
  895. {
  896. cmIDEFlagTable const* table = this->ToolsetOptions.GetClFlagTable(
  897. this->GetPlatformName(), this->GetPlatformToolsetString());
  898. return (table != CM_NULLPTR) ? table : this->DefaultClFlagTable;
  899. }
  900. cmIDEFlagTable const* cmGlobalVisualStudio10Generator::GetCSharpFlagTable()
  901. const
  902. {
  903. cmIDEFlagTable const* table = this->ToolsetOptions.GetCSharpFlagTable(
  904. this->GetPlatformName(), this->GetPlatformToolsetString());
  905. return (table != CM_NULLPTR) ? table : this->DefaultCSharpFlagTable;
  906. }
  907. cmIDEFlagTable const* cmGlobalVisualStudio10Generator::GetRcFlagTable() const
  908. {
  909. cmIDEFlagTable const* table = this->ToolsetOptions.GetRcFlagTable(
  910. this->GetPlatformName(), this->GetPlatformToolsetString());
  911. return (table != CM_NULLPTR) ? table : this->DefaultRcFlagTable;
  912. }
  913. cmIDEFlagTable const* cmGlobalVisualStudio10Generator::GetLibFlagTable() const
  914. {
  915. cmIDEFlagTable const* table = this->ToolsetOptions.GetLibFlagTable(
  916. this->GetPlatformName(), this->GetPlatformToolsetString());
  917. return (table != CM_NULLPTR) ? table : this->DefaultLibFlagTable;
  918. }
  919. cmIDEFlagTable const* cmGlobalVisualStudio10Generator::GetLinkFlagTable() const
  920. {
  921. cmIDEFlagTable const* table = this->ToolsetOptions.GetLinkFlagTable(
  922. this->GetPlatformName(), this->GetPlatformToolsetString());
  923. return (table != CM_NULLPTR) ? table : this->DefaultLinkFlagTable;
  924. }
  925. cmIDEFlagTable const* cmGlobalVisualStudio10Generator::GetCudaFlagTable() const
  926. {
  927. return this->DefaultCudaFlagTable;
  928. }
  929. cmIDEFlagTable const* cmGlobalVisualStudio10Generator::GetCudaHostFlagTable()
  930. const
  931. {
  932. return this->DefaultCudaHostFlagTable;
  933. }
  934. cmIDEFlagTable const* cmGlobalVisualStudio10Generator::GetMasmFlagTable() const
  935. {
  936. cmIDEFlagTable const* table = this->ToolsetOptions.GetMasmFlagTable(
  937. this->GetPlatformName(), this->GetPlatformToolsetString());
  938. return (table != CM_NULLPTR) ? table : this->DefaultMasmFlagTable;
  939. }
  940. cmIDEFlagTable const* cmGlobalVisualStudio10Generator::GetNasmFlagTable() const
  941. {
  942. return this->DefaultNasmFlagTable;
  943. }