cmGlobalVisualStudio10Generator.cxx 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042
  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 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 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 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 override { return true; }
  82. bool SupportsPlatform() const 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.clear();
  375. return false;
  376. }
  377. bool cmGlobalVisualStudio10Generator::SelectWindowsStoreToolset(
  378. std::string& toolset) const
  379. {
  380. toolset.clear();
  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::string const& it : lang) {
  440. if (it == "ASM_NASM") {
  441. this->NasmEnabled = true;
  442. }
  443. if (it == "CUDA") {
  444. this->CudaEnabled = true;
  445. }
  446. }
  447. this->AddPlatformDefinitions(mf);
  448. cmGlobalVisualStudio8Generator::EnableLanguage(lang, mf, optional);
  449. }
  450. const char* cmGlobalVisualStudio10Generator::GetPlatformToolset() const
  451. {
  452. std::string const& toolset = this->GetPlatformToolsetString();
  453. if (toolset.empty()) {
  454. return nullptr;
  455. }
  456. return toolset.c_str();
  457. }
  458. std::string const& cmGlobalVisualStudio10Generator::GetPlatformToolsetString()
  459. const
  460. {
  461. if (!this->GeneratorToolset.empty()) {
  462. return this->GeneratorToolset;
  463. }
  464. if (!this->DefaultPlatformToolset.empty()) {
  465. return this->DefaultPlatformToolset;
  466. }
  467. static std::string const empty;
  468. return empty;
  469. }
  470. const char*
  471. cmGlobalVisualStudio10Generator::GetPlatformToolsetHostArchitecture() const
  472. {
  473. if (!this->GeneratorToolsetHostArchitecture.empty()) {
  474. return this->GeneratorToolsetHostArchitecture.c_str();
  475. }
  476. return nullptr;
  477. }
  478. const char* cmGlobalVisualStudio10Generator::GetPlatformToolsetCuda() const
  479. {
  480. if (!this->GeneratorToolsetCuda.empty()) {
  481. return this->GeneratorToolsetCuda.c_str();
  482. }
  483. return nullptr;
  484. }
  485. std::string const&
  486. cmGlobalVisualStudio10Generator::GetPlatformToolsetCudaString() const
  487. {
  488. return this->GeneratorToolsetCuda;
  489. }
  490. bool cmGlobalVisualStudio10Generator::FindMakeProgram(cmMakefile* mf)
  491. {
  492. if (!this->cmGlobalVisualStudio8Generator::FindMakeProgram(mf)) {
  493. return false;
  494. }
  495. mf->AddDefinition("CMAKE_VS_MSBUILD_COMMAND",
  496. this->GetMSBuildCommand().c_str());
  497. return true;
  498. }
  499. std::string const& cmGlobalVisualStudio10Generator::GetMSBuildCommand()
  500. {
  501. if (!this->MSBuildCommandInitialized) {
  502. this->MSBuildCommandInitialized = true;
  503. this->MSBuildCommand = this->FindMSBuildCommand();
  504. }
  505. return this->MSBuildCommand;
  506. }
  507. std::string cmGlobalVisualStudio10Generator::FindMSBuildCommand()
  508. {
  509. std::string msbuild;
  510. std::string mskey;
  511. // Search in standard location.
  512. mskey = "HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\MSBuild\\ToolsVersions\\";
  513. mskey += this->GetToolsVersion();
  514. mskey += ";MSBuildToolsPath";
  515. if (cmSystemTools::ReadRegistryValue(mskey.c_str(), msbuild,
  516. cmSystemTools::KeyWOW64_32)) {
  517. cmSystemTools::ConvertToUnixSlashes(msbuild);
  518. msbuild += "/MSBuild.exe";
  519. if (cmSystemTools::FileExists(msbuild, true)) {
  520. return msbuild;
  521. }
  522. }
  523. msbuild = "MSBuild.exe";
  524. return msbuild;
  525. }
  526. std::string cmGlobalVisualStudio10Generator::FindDevEnvCommand()
  527. {
  528. if (this->ExpressEdition) {
  529. // Visual Studio Express >= 10 do not have "devenv.com" or
  530. // "VCExpress.exe" that we can use to build reliably.
  531. // Tell the caller it needs to use MSBuild instead.
  532. return "";
  533. }
  534. // Skip over the cmGlobalVisualStudio8Generator implementation because
  535. // we expect a real devenv and do not want to look for VCExpress.
  536. return this->cmGlobalVisualStudio71Generator::FindDevEnvCommand();
  537. }
  538. bool cmGlobalVisualStudio10Generator::FindVCTargetsPath(cmMakefile* mf)
  539. {
  540. // Skip this in special cases within our own test suite.
  541. if (this->GetPlatformName() == "Test Platform" ||
  542. this->GetPlatformToolsetString() == "Test Toolset") {
  543. return true;
  544. }
  545. std::string wd;
  546. if (!this->ConfiguredFilesPath.empty()) {
  547. // In a try-compile we are given the outer CMakeFiles directory.
  548. wd = this->ConfiguredFilesPath;
  549. } else {
  550. wd = this->GetCMakeInstance()->GetHomeOutputDirectory();
  551. wd += cmake::GetCMakeFilesDirectory();
  552. }
  553. wd += "/";
  554. wd += cmVersion::GetCMakeVersion();
  555. // We record the result persistently in a file.
  556. std::string const txt = wd + "/VCTargetsPath.txt";
  557. // If we have a recorded result, use it.
  558. {
  559. cmsys::ifstream fin(txt.c_str());
  560. if (fin && cmSystemTools::GetLineFromStream(fin, this->VCTargetsPath) &&
  561. cmSystemTools::FileIsDirectory(this->VCTargetsPath)) {
  562. cmSystemTools::ConvertToUnixSlashes(this->VCTargetsPath);
  563. return true;
  564. }
  565. }
  566. // Prepare the work directory.
  567. if (!cmSystemTools::MakeDirectory(wd)) {
  568. std::string e = "Failed to make directory:\n " + wd;
  569. mf->IssueMessage(cmake::FATAL_ERROR, e.c_str());
  570. cmSystemTools::SetFatalErrorOccured();
  571. return false;
  572. }
  573. // Generate a project file for MSBuild to tell us the VCTargetsPath value.
  574. std::string const vcxproj = "VCTargetsPath.vcxproj";
  575. {
  576. std::string const vcxprojAbs = wd + "/" + vcxproj;
  577. cmsys::ofstream fout(vcxprojAbs.c_str());
  578. cmXMLWriter xw(fout);
  579. /* clang-format off */
  580. xw.StartDocument();
  581. xw.StartElement("Project");
  582. xw.Attribute("DefaultTargets", "Build");
  583. xw.Attribute("ToolsVersion", "4.0");
  584. xw.Attribute("xmlns",
  585. "http://schemas.microsoft.com/developer/msbuild/2003");
  586. if (this->IsNsightTegra()) {
  587. xw.StartElement("PropertyGroup");
  588. xw.Attribute("Label", "NsightTegraProject");
  589. xw.StartElement("NsightTegraProjectRevisionNumber");
  590. xw.Content("6");
  591. xw.EndElement(); // NsightTegraProjectRevisionNumber
  592. xw.EndElement(); // PropertyGroup
  593. }
  594. xw.StartElement("ItemGroup");
  595. xw.Attribute("Label", "ProjectConfigurations");
  596. xw.StartElement("ProjectConfiguration");
  597. xw.Attribute("Include", "Debug|" + this->GetPlatformName());
  598. xw.StartElement("Configuration");
  599. xw.Content("Debug");
  600. xw.EndElement(); // Configuration
  601. xw.StartElement("Platform");
  602. xw.Content(this->GetPlatformName());
  603. xw.EndElement(); // Platform
  604. xw.EndElement(); // ProjectConfiguration
  605. xw.EndElement(); // ItemGroup
  606. xw.StartElement("PropertyGroup");
  607. xw.Attribute("Label", "Globals");
  608. xw.StartElement("ProjectGuid");
  609. xw.Content("{F3FC6D86-508D-3FB1-96D2-995F08B142EC}");
  610. xw.EndElement(); // ProjectGuid
  611. xw.StartElement("Keyword");
  612. xw.Content("Win32Proj");
  613. xw.EndElement(); // Keyword
  614. xw.StartElement("Platform");
  615. xw.Content(this->GetPlatformName());
  616. xw.EndElement(); // Platform
  617. if (this->GetSystemName() == "WindowsPhone") {
  618. xw.StartElement("ApplicationType");
  619. xw.Content("Windows Phone");
  620. xw.EndElement(); // ApplicationType
  621. xw.StartElement("ApplicationTypeRevision");
  622. xw.Content(this->GetSystemVersion());
  623. xw.EndElement(); // ApplicationTypeRevision
  624. } else if (this->GetSystemName() == "WindowsStore") {
  625. xw.StartElement("ApplicationType");
  626. xw.Content("Windows Store");
  627. xw.EndElement(); // ApplicationType
  628. xw.StartElement("ApplicationTypeRevision");
  629. xw.Content(this->GetSystemVersion());
  630. xw.EndElement(); // ApplicationTypeRevision
  631. }
  632. if (!this->WindowsTargetPlatformVersion.empty()) {
  633. xw.StartElement("WindowsTargetPlatformVersion");
  634. xw.Content(this->WindowsTargetPlatformVersion);
  635. xw.EndElement(); // WindowsTargetPlatformVersion
  636. }
  637. if (this->GetPlatformName() == "ARM64") {
  638. xw.StartElement("WindowsSDKDesktopARM64Support");
  639. xw.Content("true");
  640. xw.EndElement(); // WindowsSDK64DesktopARMSupport
  641. }
  642. else if (this->GetPlatformName() == "ARM") {
  643. xw.StartElement("WindowsSDKDesktopARMSupport");
  644. xw.Content("true");
  645. xw.EndElement(); // WindowsSDKDesktopARMSupport
  646. }
  647. xw.EndElement(); // PropertyGroup
  648. xw.StartElement("Import");
  649. xw.Attribute("Project",
  650. "$(VCTargetsPath)\\Microsoft.Cpp.Default.props");
  651. xw.EndElement(); // Import
  652. if (!this->GeneratorToolsetHostArchitecture.empty()) {
  653. xw.StartElement("PropertyGroup");
  654. xw.StartElement("PreferredToolArchitecture");
  655. xw.Content(this->GeneratorToolsetHostArchitecture);
  656. xw.EndElement(); // PreferredToolArchitecture
  657. xw.EndElement(); // PropertyGroup
  658. }
  659. xw.StartElement("PropertyGroup");
  660. xw.Attribute("Label", "Configuration");
  661. xw.StartElement("ConfigurationType");
  662. if (this->IsNsightTegra()) {
  663. // Tegra-Android platform does not understand "Utility".
  664. xw.Content("StaticLibrary");
  665. } else {
  666. xw.Content("Utility");
  667. }
  668. xw.EndElement(); // ConfigurationType
  669. xw.StartElement("CharacterSet");
  670. xw.Content("MultiByte");
  671. xw.EndElement(); // CharacterSet
  672. if (this->IsNsightTegra()) {
  673. xw.StartElement("NdkToolchainVersion");
  674. xw.Content(this->GetPlatformToolsetString());
  675. xw.EndElement(); // NdkToolchainVersion
  676. } else {
  677. xw.StartElement("PlatformToolset");
  678. xw.Content(this->GetPlatformToolsetString());
  679. xw.EndElement(); // PlatformToolset
  680. }
  681. xw.EndElement(); // PropertyGroup
  682. xw.StartElement("Import");
  683. xw.Attribute("Project", "$(VCTargetsPath)\\Microsoft.Cpp.props");
  684. xw.EndElement(); // Import
  685. xw.StartElement("ItemDefinitionGroup");
  686. xw.StartElement("PostBuildEvent");
  687. xw.StartElement("Command");
  688. xw.Content("echo VCTargetsPath=$(VCTargetsPath)");
  689. xw.EndElement(); // Command
  690. xw.EndElement(); // PostBuildEvent
  691. xw.EndElement(); // ItemDefinitionGroup
  692. xw.StartElement("Import");
  693. xw.Attribute("Project",
  694. "$(VCTargetsPath)\\Microsoft.Cpp.targets");
  695. xw.EndElement(); // Import
  696. xw.EndElement(); // Project
  697. xw.EndDocument();
  698. /* clang-format on */
  699. }
  700. std::vector<std::string> cmd;
  701. cmd.push_back(this->GetMSBuildCommand());
  702. cmd.push_back(vcxproj);
  703. cmd.push_back("/p:Configuration=Debug");
  704. cmd.push_back(std::string("/p:VisualStudioVersion=") +
  705. this->GetIDEVersion());
  706. std::string out;
  707. int ret = 0;
  708. cmsys::RegularExpression regex("\n *VCTargetsPath=([^%\r\n]+)[\r\n]");
  709. if (!cmSystemTools::RunSingleCommand(cmd, &out, &out, &ret, wd.c_str(),
  710. cmSystemTools::OUTPUT_NONE) ||
  711. ret != 0 || !regex.find(out)) {
  712. cmSystemTools::ReplaceString(out, "\n", "\n ");
  713. std::ostringstream e;
  714. /* clang-format off */
  715. e <<
  716. "Failed to run MSBuild command:\n"
  717. " " << cmd[0] << "\n"
  718. "to get the value of VCTargetsPath:\n"
  719. " " << out << "\n"
  720. ;
  721. /* clang-format on */
  722. if (ret != 0) {
  723. e << "Exit code: " << ret << "\n";
  724. }
  725. mf->IssueMessage(cmake::FATAL_ERROR, e.str().c_str());
  726. cmSystemTools::SetFatalErrorOccured();
  727. return false;
  728. }
  729. this->VCTargetsPath = regex.match(1);
  730. cmSystemTools::ConvertToUnixSlashes(this->VCTargetsPath);
  731. {
  732. cmsys::ofstream fout(txt.c_str());
  733. fout << this->VCTargetsPath << "\n";
  734. }
  735. return true;
  736. }
  737. void cmGlobalVisualStudio10Generator::GenerateBuildCommand(
  738. std::vector<std::string>& makeCommand, const std::string& makeProgram,
  739. const std::string& projectName, const std::string& projectDir,
  740. const std::string& targetName, const std::string& config, bool fast,
  741. bool verbose, std::vector<std::string> const& makeOptions)
  742. {
  743. // Select the caller- or user-preferred make program, else MSBuild.
  744. std::string makeProgramSelected =
  745. this->SelectMakeProgram(makeProgram, this->GetMSBuildCommand());
  746. // Check if the caller explicitly requested a devenv tool.
  747. std::string makeProgramLower = makeProgramSelected;
  748. cmSystemTools::LowerCase(makeProgramLower);
  749. bool useDevEnv = (makeProgramLower.find("devenv") != std::string::npos ||
  750. makeProgramLower.find("vcexpress") != std::string::npos);
  751. // MSBuild is preferred (and required for VS Express), but if the .sln has
  752. // an Intel Fortran .vfproj then we have to use devenv. Parse it to find out.
  753. cmSlnData slnData;
  754. {
  755. std::string slnFile;
  756. if (!projectDir.empty()) {
  757. slnFile = projectDir;
  758. slnFile += "/";
  759. }
  760. slnFile += projectName;
  761. slnFile += ".sln";
  762. cmVisualStudioSlnParser parser;
  763. if (parser.ParseFile(slnFile, slnData,
  764. cmVisualStudioSlnParser::DataGroupProjects)) {
  765. std::vector<cmSlnProjectEntry> slnProjects = slnData.GetProjects();
  766. for (std::vector<cmSlnProjectEntry>::const_iterator i =
  767. slnProjects.cbegin();
  768. !useDevEnv && i != slnProjects.cend(); ++i) {
  769. std::string proj = i->GetRelativePath();
  770. if (proj.size() > 7 && proj.substr(proj.size() - 7) == ".vfproj") {
  771. useDevEnv = true;
  772. }
  773. }
  774. }
  775. }
  776. if (useDevEnv) {
  777. // Use devenv to build solutions containing Intel Fortran projects.
  778. cmGlobalVisualStudio7Generator::GenerateBuildCommand(
  779. makeCommand, makeProgram, projectName, projectDir, targetName, config,
  780. fast, verbose, makeOptions);
  781. return;
  782. }
  783. makeCommand.push_back(makeProgramSelected);
  784. std::string realTarget = targetName;
  785. // msbuild.exe CxxOnly.sln /t:Build /p:Configuration=Debug /target:ALL_BUILD
  786. if (realTarget.empty()) {
  787. realTarget = "ALL_BUILD";
  788. }
  789. if (realTarget == "clean") {
  790. makeCommand.push_back(std::string(projectName) + ".sln");
  791. makeCommand.push_back("/t:Clean");
  792. } else {
  793. std::string targetProject(realTarget);
  794. targetProject += ".vcxproj";
  795. if (targetProject.find('/') == std::string::npos) {
  796. // it might be in a subdir
  797. if (cmSlnProjectEntry const* proj =
  798. slnData.GetProjectByName(realTarget)) {
  799. targetProject = proj->GetRelativePath();
  800. cmSystemTools::ConvertToUnixSlashes(targetProject);
  801. }
  802. }
  803. makeCommand.push_back(targetProject);
  804. }
  805. std::string configArg = "/p:Configuration=";
  806. if (!config.empty()) {
  807. configArg += config;
  808. } else {
  809. configArg += "Debug";
  810. }
  811. makeCommand.push_back(configArg);
  812. makeCommand.push_back(std::string("/p:VisualStudioVersion=") +
  813. this->GetIDEVersion());
  814. makeCommand.insert(makeCommand.end(), makeOptions.begin(),
  815. makeOptions.end());
  816. }
  817. bool cmGlobalVisualStudio10Generator::Find64BitTools(cmMakefile* mf)
  818. {
  819. if (this->DefaultPlatformToolset == "v100") {
  820. // The v100 64-bit toolset does not exist in the express edition.
  821. this->DefaultPlatformToolset.clear();
  822. }
  823. if (this->GetPlatformToolset()) {
  824. return true;
  825. }
  826. // This edition does not come with 64-bit tools. Look for them.
  827. //
  828. // TODO: Detect available tools? x64\v100 exists but does not work?
  829. // HKLM\\SOFTWARE\\Microsoft\\MSBuild\\ToolsVersions\\4.0;VCTargetsPath
  830. // c:/Program Files (x86)/MSBuild/Microsoft.Cpp/v4.0/Platforms/
  831. // {Itanium,Win32,x64}/PlatformToolsets/{v100,v90,Windows7.1SDK}
  832. std::string winSDK_7_1;
  833. if (cmSystemTools::ReadRegistryValue(
  834. "HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Microsoft SDKs\\"
  835. "Windows\\v7.1;InstallationFolder",
  836. winSDK_7_1)) {
  837. std::ostringstream m;
  838. m << "Found Windows SDK v7.1: " << winSDK_7_1;
  839. mf->DisplayStatus(m.str().c_str(), -1);
  840. this->DefaultPlatformToolset = "Windows7.1SDK";
  841. return true;
  842. } else {
  843. std::ostringstream e;
  844. /* clang-format off */
  845. e << "Cannot enable 64-bit tools with Visual Studio 2010 Express.\n"
  846. << "Install the Microsoft Windows SDK v7.1 to get 64-bit tools:\n"
  847. << " http://msdn.microsoft.com/en-us/windows/bb980924.aspx";
  848. /* clang-format on */
  849. mf->IssueMessage(cmake::FATAL_ERROR, e.str().c_str());
  850. cmSystemTools::SetFatalErrorOccured();
  851. return false;
  852. }
  853. }
  854. std::string cmGlobalVisualStudio10Generator::GenerateRuleFile(
  855. std::string const& output) const
  856. {
  857. // The VS 10 generator needs to create the .rule files on disk.
  858. // Hide them away under the CMakeFiles directory.
  859. std::string ruleDir = this->GetCMakeInstance()->GetHomeOutputDirectory();
  860. ruleDir += cmake::GetCMakeFilesDirectory();
  861. ruleDir += "/";
  862. ruleDir += cmSystemTools::ComputeStringMD5(
  863. cmSystemTools::GetFilenamePath(output).c_str());
  864. std::string ruleFile = ruleDir + "/";
  865. ruleFile += cmSystemTools::GetFilenameName(output);
  866. ruleFile += ".rule";
  867. return ruleFile;
  868. }
  869. void cmGlobalVisualStudio10Generator::PathTooLong(cmGeneratorTarget* target,
  870. cmSourceFile const* sf,
  871. std::string const& sfRel)
  872. {
  873. size_t len =
  874. (strlen(target->GetLocalGenerator()->GetCurrentBinaryDirectory()) + 1 +
  875. sfRel.length());
  876. if (len > this->LongestSource.Length) {
  877. this->LongestSource.Length = len;
  878. this->LongestSource.Target = target;
  879. this->LongestSource.SourceFile = sf;
  880. this->LongestSource.SourceRel = sfRel;
  881. }
  882. }
  883. std::string cmGlobalVisualStudio10Generator::Encoding()
  884. {
  885. return "utf-8";
  886. }
  887. bool cmGlobalVisualStudio10Generator::IsNsightTegra() const
  888. {
  889. return !this->NsightTegraVersion.empty();
  890. }
  891. std::string cmGlobalVisualStudio10Generator::GetNsightTegraVersion() const
  892. {
  893. return this->NsightTegraVersion;
  894. }
  895. std::string cmGlobalVisualStudio10Generator::GetInstalledNsightTegraVersion()
  896. {
  897. std::string version;
  898. cmSystemTools::ReadRegistryValue(
  899. "HKEY_LOCAL_MACHINE\\SOFTWARE\\NVIDIA Corporation\\Nsight Tegra;"
  900. "Version",
  901. version, cmSystemTools::KeyWOW64_32);
  902. return version;
  903. }
  904. cmIDEFlagTable const* cmGlobalVisualStudio10Generator::GetClFlagTable() const
  905. {
  906. cmIDEFlagTable const* table = this->ToolsetOptions.GetClFlagTable(
  907. this->GetPlatformName(), this->GetPlatformToolsetString());
  908. return (table != nullptr) ? table : this->DefaultClFlagTable;
  909. }
  910. cmIDEFlagTable const* cmGlobalVisualStudio10Generator::GetCSharpFlagTable()
  911. const
  912. {
  913. cmIDEFlagTable const* table = this->ToolsetOptions.GetCSharpFlagTable(
  914. this->GetPlatformName(), this->GetPlatformToolsetString());
  915. return (table != nullptr) ? table : this->DefaultCSharpFlagTable;
  916. }
  917. cmIDEFlagTable const* cmGlobalVisualStudio10Generator::GetRcFlagTable() const
  918. {
  919. cmIDEFlagTable const* table = this->ToolsetOptions.GetRcFlagTable(
  920. this->GetPlatformName(), this->GetPlatformToolsetString());
  921. return (table != nullptr) ? table : this->DefaultRcFlagTable;
  922. }
  923. cmIDEFlagTable const* cmGlobalVisualStudio10Generator::GetLibFlagTable() const
  924. {
  925. cmIDEFlagTable const* table = this->ToolsetOptions.GetLibFlagTable(
  926. this->GetPlatformName(), this->GetPlatformToolsetString());
  927. return (table != nullptr) ? table : this->DefaultLibFlagTable;
  928. }
  929. cmIDEFlagTable const* cmGlobalVisualStudio10Generator::GetLinkFlagTable() const
  930. {
  931. cmIDEFlagTable const* table = this->ToolsetOptions.GetLinkFlagTable(
  932. this->GetPlatformName(), this->GetPlatformToolsetString());
  933. return (table != nullptr) ? table : this->DefaultLinkFlagTable;
  934. }
  935. cmIDEFlagTable const* cmGlobalVisualStudio10Generator::GetCudaFlagTable() const
  936. {
  937. return this->DefaultCudaFlagTable;
  938. }
  939. cmIDEFlagTable const* cmGlobalVisualStudio10Generator::GetCudaHostFlagTable()
  940. const
  941. {
  942. return this->DefaultCudaHostFlagTable;
  943. }
  944. cmIDEFlagTable const* cmGlobalVisualStudio10Generator::GetMasmFlagTable() const
  945. {
  946. cmIDEFlagTable const* table = this->ToolsetOptions.GetMasmFlagTable(
  947. this->GetPlatformName(), this->GetPlatformToolsetString());
  948. return (table != nullptr) ? table : this->DefaultMasmFlagTable;
  949. }
  950. cmIDEFlagTable const* cmGlobalVisualStudio10Generator::GetNasmFlagTable() const
  951. {
  952. return this->DefaultNasmFlagTable;
  953. }