cmGlobalVisualStudio10Generator.cxx 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115
  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 (!this->GeneratorToolsetVersion.empty() &&
  214. this->GeneratorToolsetVersion != "Test Toolset Version") {
  215. // If a specific minor version of the toolset was requested, verify that it
  216. // is compatible to the major version and that is exists on disk.
  217. // If not clear the value.
  218. std::string version = this->GeneratorToolsetVersion;
  219. cmsys::RegularExpression regex("[0-9][0-9]\\.[0-9][0-9]");
  220. if (regex.find(version)) {
  221. version = "v" + version.erase(2, 1);
  222. } else {
  223. // Version not recognized. Clear it.
  224. version.clear();
  225. }
  226. if (version.find(this->GetPlatformToolsetString()) != 0) {
  227. std::ostringstream e;
  228. /* clang-format off */
  229. e <<
  230. "Generator\n"
  231. " " << this->GetName() << "\n"
  232. "given toolset and version specification\n"
  233. " " << this->GetPlatformToolsetString() << ",version=" <<
  234. this->GeneratorToolsetVersion << "\n"
  235. "contains an invalid version specification."
  236. ;
  237. /* clang-format on */
  238. mf->IssueMessage(cmake::FATAL_ERROR, e.str());
  239. // Clear the configured tool-set
  240. this->GeneratorToolsetVersion.clear();
  241. }
  242. bool const isDefaultToolset =
  243. this->IsDefaultToolset(this->GeneratorToolsetVersion);
  244. if (isDefaultToolset) {
  245. // If the given version is the default toolset, remove the setting
  246. this->GeneratorToolsetVersion.clear();
  247. } else {
  248. std::string const toolsetPath = this->GetAuxiliaryToolset();
  249. if (!toolsetPath.empty() && !cmSystemTools::FileExists(toolsetPath)) {
  250. std::ostringstream e;
  251. /* clang-format off */
  252. e <<
  253. "Generator\n"
  254. " " << this->GetName() << "\n"
  255. "given toolset and version specification\n"
  256. " " << this->GetPlatformToolsetString() << ",version=" <<
  257. this->GeneratorToolsetVersion << "\n"
  258. "does not seem to be installed at\n" <<
  259. " " << toolsetPath;
  260. ;
  261. /* clang-format on */
  262. mf->IssueMessage(cmake::FATAL_ERROR, e.str());
  263. // Clear the configured tool-set
  264. this->GeneratorToolsetVersion.clear();
  265. }
  266. }
  267. }
  268. if (const char* toolset = this->GetPlatformToolset()) {
  269. mf->AddDefinition("CMAKE_VS_PLATFORM_TOOLSET", toolset);
  270. }
  271. if (const char* version = this->GetPlatformToolsetVersion()) {
  272. mf->AddDefinition("CMAKE_VS_PLATFORM_TOOLSET_VERSION", version);
  273. }
  274. if (const char* hostArch = this->GetPlatformToolsetHostArchitecture()) {
  275. mf->AddDefinition("CMAKE_VS_PLATFORM_TOOLSET_HOST_ARCHITECTURE", hostArch);
  276. }
  277. if (const char* cuda = this->GetPlatformToolsetCuda()) {
  278. mf->AddDefinition("CMAKE_VS_PLATFORM_TOOLSET_CUDA", cuda);
  279. }
  280. return true;
  281. }
  282. bool cmGlobalVisualStudio10Generator::ParseGeneratorToolset(
  283. std::string const& ts, cmMakefile* mf)
  284. {
  285. std::vector<std::string> const fields = cmSystemTools::tokenize(ts, ",");
  286. std::vector<std::string>::const_iterator fi = fields.begin();
  287. if (fi == fields.end()) {
  288. return true;
  289. }
  290. // The first field may be the VS platform toolset.
  291. if (fi->find('=') == fi->npos) {
  292. this->GeneratorToolset = *fi;
  293. ++fi;
  294. }
  295. std::set<std::string> handled;
  296. // The rest of the fields must be key=value pairs.
  297. for (; fi != fields.end(); ++fi) {
  298. std::string::size_type pos = fi->find('=');
  299. if (pos == fi->npos) {
  300. std::ostringstream e;
  301. /* clang-format off */
  302. e <<
  303. "Generator\n"
  304. " " << this->GetName() << "\n"
  305. "given toolset specification\n"
  306. " " << ts << "\n"
  307. "that contains a field after the first ',' with no '='."
  308. ;
  309. /* clang-format on */
  310. mf->IssueMessage(cmake::FATAL_ERROR, e.str());
  311. return false;
  312. }
  313. std::string const key = fi->substr(0, pos);
  314. std::string const value = fi->substr(pos + 1);
  315. if (!handled.insert(key).second) {
  316. std::ostringstream e;
  317. /* clang-format off */
  318. e <<
  319. "Generator\n"
  320. " " << this->GetName() << "\n"
  321. "given toolset specification\n"
  322. " " << ts << "\n"
  323. "that contains duplicate field key '" << key << "'."
  324. ;
  325. /* clang-format on */
  326. mf->IssueMessage(cmake::FATAL_ERROR, e.str());
  327. return false;
  328. }
  329. if (!this->ProcessGeneratorToolsetField(key, value)) {
  330. std::ostringstream e;
  331. /* clang-format off */
  332. e <<
  333. "Generator\n"
  334. " " << this->GetName() << "\n"
  335. "given toolset specification\n"
  336. " " << ts << "\n"
  337. "that contains invalid field '" << *fi << "'."
  338. ;
  339. /* clang-format on */
  340. mf->IssueMessage(cmake::FATAL_ERROR, e.str());
  341. return false;
  342. }
  343. }
  344. return true;
  345. }
  346. bool cmGlobalVisualStudio10Generator::ProcessGeneratorToolsetField(
  347. std::string const& key, std::string const& value)
  348. {
  349. if (key == "cuda") {
  350. this->GeneratorToolsetCuda = value;
  351. return true;
  352. }
  353. if (key == "version") {
  354. this->GeneratorToolsetVersion = value;
  355. return true;
  356. }
  357. return false;
  358. }
  359. bool cmGlobalVisualStudio10Generator::InitializeSystem(cmMakefile* mf)
  360. {
  361. if (this->SystemName == "Windows") {
  362. if (!this->InitializeWindows(mf)) {
  363. return false;
  364. }
  365. } else if (this->SystemName == "WindowsCE") {
  366. this->SystemIsWindowsCE = true;
  367. if (!this->InitializeWindowsCE(mf)) {
  368. return false;
  369. }
  370. } else if (this->SystemName == "WindowsPhone") {
  371. this->SystemIsWindowsPhone = true;
  372. if (!this->InitializeWindowsPhone(mf)) {
  373. return false;
  374. }
  375. } else if (this->SystemName == "WindowsStore") {
  376. this->SystemIsWindowsStore = true;
  377. if (!this->InitializeWindowsStore(mf)) {
  378. return false;
  379. }
  380. } else if (this->SystemName == "Android") {
  381. if (this->DefaultPlatformName != "Win32") {
  382. std::ostringstream e;
  383. e << "CMAKE_SYSTEM_NAME is 'Android' but CMAKE_GENERATOR "
  384. << "specifies a platform too: '" << this->GetName() << "'";
  385. mf->IssueMessage(cmake::FATAL_ERROR, e.str());
  386. return false;
  387. }
  388. std::string v = this->GetInstalledNsightTegraVersion();
  389. if (v.empty()) {
  390. mf->IssueMessage(cmake::FATAL_ERROR,
  391. "CMAKE_SYSTEM_NAME is 'Android' but "
  392. "'NVIDIA Nsight Tegra Visual Studio Edition' "
  393. "is not installed.");
  394. return false;
  395. }
  396. this->DefaultPlatformName = "Tegra-Android";
  397. this->DefaultPlatformToolset = "Default";
  398. this->NsightTegraVersion = v;
  399. mf->AddDefinition("CMAKE_VS_NsightTegra_VERSION", v.c_str());
  400. }
  401. return true;
  402. }
  403. bool cmGlobalVisualStudio10Generator::InitializeWindows(cmMakefile*)
  404. {
  405. return true;
  406. }
  407. bool cmGlobalVisualStudio10Generator::InitializeWindowsCE(cmMakefile* mf)
  408. {
  409. if (this->DefaultPlatformName != "Win32") {
  410. std::ostringstream e;
  411. e << "CMAKE_SYSTEM_NAME is 'WindowsCE' but CMAKE_GENERATOR "
  412. << "specifies a platform too: '" << this->GetName() << "'";
  413. mf->IssueMessage(cmake::FATAL_ERROR, e.str());
  414. return false;
  415. }
  416. this->DefaultPlatformToolset = this->SelectWindowsCEToolset();
  417. return true;
  418. }
  419. bool cmGlobalVisualStudio10Generator::InitializeWindowsPhone(cmMakefile* mf)
  420. {
  421. std::ostringstream e;
  422. e << this->GetName() << " does not support Windows Phone.";
  423. mf->IssueMessage(cmake::FATAL_ERROR, e.str());
  424. return false;
  425. }
  426. bool cmGlobalVisualStudio10Generator::InitializeWindowsStore(cmMakefile* mf)
  427. {
  428. std::ostringstream e;
  429. e << this->GetName() << " does not support Windows Store.";
  430. mf->IssueMessage(cmake::FATAL_ERROR, e.str());
  431. return false;
  432. }
  433. bool cmGlobalVisualStudio10Generator::SelectWindowsPhoneToolset(
  434. std::string& toolset) const
  435. {
  436. toolset.clear();
  437. return false;
  438. }
  439. bool cmGlobalVisualStudio10Generator::SelectWindowsStoreToolset(
  440. std::string& toolset) const
  441. {
  442. toolset.clear();
  443. return false;
  444. }
  445. std::string cmGlobalVisualStudio10Generator::SelectWindowsCEToolset() const
  446. {
  447. if (this->SystemVersion == "8.0") {
  448. return "CE800";
  449. }
  450. return "";
  451. }
  452. void cmGlobalVisualStudio10Generator::WriteSLNHeader(std::ostream& fout)
  453. {
  454. fout << "Microsoft Visual Studio Solution File, Format Version 11.00\n";
  455. if (this->ExpressEdition) {
  456. fout << "# Visual C++ Express 2010\n";
  457. } else {
  458. fout << "# Visual Studio 2010\n";
  459. }
  460. }
  461. ///! Create a local generator appropriate to this Global Generator
  462. cmLocalGenerator* cmGlobalVisualStudio10Generator::CreateLocalGenerator(
  463. cmMakefile* mf)
  464. {
  465. return new cmLocalVisualStudio10Generator(this, mf);
  466. }
  467. void cmGlobalVisualStudio10Generator::Generate()
  468. {
  469. this->LongestSource = LongestSourcePath();
  470. this->cmGlobalVisualStudio8Generator::Generate();
  471. if (this->LongestSource.Length > 0) {
  472. cmLocalGenerator* lg = this->LongestSource.Target->GetLocalGenerator();
  473. std::ostringstream e;
  474. /* clang-format off */
  475. e <<
  476. "The binary and/or source directory paths may be too long to generate "
  477. "Visual Studio 10 files for this project. "
  478. "Consider choosing shorter directory names to build this project with "
  479. "Visual Studio 10. "
  480. "A more detailed explanation follows."
  481. "\n"
  482. "There is a bug in the VS 10 IDE that renders property dialog fields "
  483. "blank for files referenced by full path in the project file. "
  484. "However, CMake must reference at least one file by full path:\n"
  485. " " << this->LongestSource.SourceFile->GetFullPath() << "\n"
  486. "This is because some Visual Studio tools would append the relative "
  487. "path to the end of the referencing directory path, as in:\n"
  488. " " << lg->GetCurrentBinaryDirectory() << "/"
  489. << this->LongestSource.SourceRel << "\n"
  490. "and then incorrectly complain that the file does not exist because "
  491. "the path length is too long for some internal buffer or API. "
  492. "To avoid this problem CMake must use a full path for this file "
  493. "which then triggers the VS 10 property dialog bug.";
  494. /* clang-format on */
  495. lg->IssueMessage(cmake::WARNING, e.str().c_str());
  496. }
  497. }
  498. void cmGlobalVisualStudio10Generator::EnableLanguage(
  499. std::vector<std::string> const& lang, cmMakefile* mf, bool optional)
  500. {
  501. for (std::string const& it : lang) {
  502. if (it == "ASM_NASM") {
  503. this->NasmEnabled = true;
  504. }
  505. if (it == "CUDA") {
  506. this->CudaEnabled = true;
  507. }
  508. }
  509. this->AddPlatformDefinitions(mf);
  510. cmGlobalVisualStudio8Generator::EnableLanguage(lang, mf, optional);
  511. }
  512. const char* cmGlobalVisualStudio10Generator::GetPlatformToolset() const
  513. {
  514. std::string const& toolset = this->GetPlatformToolsetString();
  515. if (toolset.empty()) {
  516. return nullptr;
  517. }
  518. return toolset.c_str();
  519. }
  520. std::string const& cmGlobalVisualStudio10Generator::GetPlatformToolsetString()
  521. const
  522. {
  523. if (!this->GeneratorToolset.empty()) {
  524. return this->GeneratorToolset;
  525. }
  526. if (!this->DefaultPlatformToolset.empty()) {
  527. return this->DefaultPlatformToolset;
  528. }
  529. static std::string const empty;
  530. return empty;
  531. }
  532. const char* cmGlobalVisualStudio10Generator::GetPlatformToolsetVersion() const
  533. {
  534. std::string const& version = this->GetPlatformToolsetVersionString();
  535. if (version.empty()) {
  536. return nullptr;
  537. }
  538. return version.c_str();
  539. }
  540. std::string const&
  541. cmGlobalVisualStudio10Generator::GetPlatformToolsetVersionString() const
  542. {
  543. if (!this->GeneratorToolsetVersion.empty()) {
  544. return this->GeneratorToolsetVersion;
  545. }
  546. static std::string const empty;
  547. return empty;
  548. }
  549. const char*
  550. cmGlobalVisualStudio10Generator::GetPlatformToolsetHostArchitecture() const
  551. {
  552. if (!this->GeneratorToolsetHostArchitecture.empty()) {
  553. return this->GeneratorToolsetHostArchitecture.c_str();
  554. }
  555. return nullptr;
  556. }
  557. const char* cmGlobalVisualStudio10Generator::GetPlatformToolsetCuda() const
  558. {
  559. if (!this->GeneratorToolsetCuda.empty()) {
  560. return this->GeneratorToolsetCuda.c_str();
  561. }
  562. return nullptr;
  563. }
  564. std::string const&
  565. cmGlobalVisualStudio10Generator::GetPlatformToolsetCudaString() const
  566. {
  567. return this->GeneratorToolsetCuda;
  568. }
  569. bool cmGlobalVisualStudio10Generator::IsDefaultToolset(
  570. const std::string&) const
  571. {
  572. return true;
  573. }
  574. std::string cmGlobalVisualStudio10Generator::GetAuxiliaryToolset() const
  575. {
  576. return {};
  577. }
  578. bool cmGlobalVisualStudio10Generator::FindMakeProgram(cmMakefile* mf)
  579. {
  580. if (!this->cmGlobalVisualStudio8Generator::FindMakeProgram(mf)) {
  581. return false;
  582. }
  583. mf->AddDefinition("CMAKE_VS_MSBUILD_COMMAND",
  584. this->GetMSBuildCommand().c_str());
  585. return true;
  586. }
  587. std::string const& cmGlobalVisualStudio10Generator::GetMSBuildCommand()
  588. {
  589. if (!this->MSBuildCommandInitialized) {
  590. this->MSBuildCommandInitialized = true;
  591. this->MSBuildCommand = this->FindMSBuildCommand();
  592. }
  593. return this->MSBuildCommand;
  594. }
  595. std::string cmGlobalVisualStudio10Generator::FindMSBuildCommand()
  596. {
  597. std::string msbuild;
  598. std::string mskey;
  599. // Search in standard location.
  600. mskey = "HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\MSBuild\\ToolsVersions\\";
  601. mskey += this->GetToolsVersion();
  602. mskey += ";MSBuildToolsPath";
  603. if (cmSystemTools::ReadRegistryValue(mskey.c_str(), msbuild,
  604. cmSystemTools::KeyWOW64_32)) {
  605. cmSystemTools::ConvertToUnixSlashes(msbuild);
  606. msbuild += "/MSBuild.exe";
  607. if (cmSystemTools::FileExists(msbuild, true)) {
  608. return msbuild;
  609. }
  610. }
  611. msbuild = "MSBuild.exe";
  612. return msbuild;
  613. }
  614. std::string cmGlobalVisualStudio10Generator::FindDevEnvCommand()
  615. {
  616. if (this->ExpressEdition) {
  617. // Visual Studio Express >= 10 do not have "devenv.com" or
  618. // "VCExpress.exe" that we can use to build reliably.
  619. // Tell the caller it needs to use MSBuild instead.
  620. return "";
  621. }
  622. // Skip over the cmGlobalVisualStudio8Generator implementation because
  623. // we expect a real devenv and do not want to look for VCExpress.
  624. return this->cmGlobalVisualStudio71Generator::FindDevEnvCommand();
  625. }
  626. bool cmGlobalVisualStudio10Generator::FindVCTargetsPath(cmMakefile* mf)
  627. {
  628. // Skip this in special cases within our own test suite.
  629. if (this->GetPlatformName() == "Test Platform" ||
  630. this->GetPlatformToolsetString() == "Test Toolset") {
  631. return true;
  632. }
  633. std::string wd;
  634. if (!this->ConfiguredFilesPath.empty()) {
  635. // In a try-compile we are given the outer CMakeFiles directory.
  636. wd = this->ConfiguredFilesPath;
  637. } else {
  638. wd = this->GetCMakeInstance()->GetHomeOutputDirectory();
  639. wd += cmake::GetCMakeFilesDirectory();
  640. }
  641. wd += "/";
  642. wd += cmVersion::GetCMakeVersion();
  643. // We record the result persistently in a file.
  644. std::string const txt = wd + "/VCTargetsPath.txt";
  645. // If we have a recorded result, use it.
  646. {
  647. cmsys::ifstream fin(txt.c_str());
  648. if (fin && cmSystemTools::GetLineFromStream(fin, this->VCTargetsPath) &&
  649. cmSystemTools::FileIsDirectory(this->VCTargetsPath)) {
  650. cmSystemTools::ConvertToUnixSlashes(this->VCTargetsPath);
  651. return true;
  652. }
  653. }
  654. // Prepare the work directory.
  655. if (!cmSystemTools::MakeDirectory(wd)) {
  656. std::string e = "Failed to make directory:\n " + wd;
  657. mf->IssueMessage(cmake::FATAL_ERROR, e.c_str());
  658. cmSystemTools::SetFatalErrorOccured();
  659. return false;
  660. }
  661. // Generate a project file for MSBuild to tell us the VCTargetsPath value.
  662. std::string const vcxproj = "VCTargetsPath.vcxproj";
  663. {
  664. std::string const vcxprojAbs = wd + "/" + vcxproj;
  665. cmsys::ofstream fout(vcxprojAbs.c_str());
  666. cmXMLWriter xw(fout);
  667. cmXMLDocument doc(xw);
  668. cmXMLElement eprj(doc, "Project");
  669. eprj.Attribute("DefaultTargets", "Build");
  670. eprj.Attribute("ToolsVersion", "4.0");
  671. eprj.Attribute("xmlns",
  672. "http://schemas.microsoft.com/developer/msbuild/2003");
  673. if (this->IsNsightTegra()) {
  674. cmXMLElement epg(eprj, "PropertyGroup");
  675. epg.Attribute("Label", "NsightTegraProject");
  676. cmXMLElement(epg, "NsightTegraProjectRevisionNumber").Content("6");
  677. }
  678. {
  679. cmXMLElement eig(eprj, "ItemGroup");
  680. eig.Attribute("Label", "ProjectConfigurations");
  681. cmXMLElement epc(eig, "ProjectConfiguration");
  682. epc.Attribute("Include", "Debug|" + this->GetPlatformName());
  683. cmXMLElement(epc, "Configuration").Content("Debug");
  684. cmXMLElement(epc, "Platform").Content(this->GetPlatformName());
  685. }
  686. {
  687. cmXMLElement epg(eprj, "PropertyGroup");
  688. epg.Attribute("Label", "Globals");
  689. cmXMLElement(epg, "ProjectGuid")
  690. .Content("{F3FC6D86-508D-3FB1-96D2-995F08B142EC}");
  691. cmXMLElement(epg, "Keyword").Content("Win32Proj");
  692. cmXMLElement(epg, "Platform").Content(this->GetPlatformName());
  693. if (this->GetSystemName() == "WindowsPhone") {
  694. cmXMLElement(epg, "ApplicationType").Content("Windows Phone");
  695. cmXMLElement(epg, "ApplicationTypeRevision")
  696. .Content(this->GetSystemVersion());
  697. } else if (this->GetSystemName() == "WindowsStore") {
  698. cmXMLElement(epg, "ApplicationType").Content("Windows Store");
  699. cmXMLElement(epg, "ApplicationTypeRevision")
  700. .Content(this->GetSystemVersion());
  701. }
  702. if (!this->WindowsTargetPlatformVersion.empty()) {
  703. cmXMLElement(epg, "WindowsTargetPlatformVersion")
  704. .Content(this->WindowsTargetPlatformVersion);
  705. }
  706. if (this->GetPlatformName() == "ARM64") {
  707. cmXMLElement(epg, "WindowsSDKDesktopARM64Support").Content("true");
  708. } else if (this->GetPlatformName() == "ARM") {
  709. cmXMLElement(epg, "WindowsSDKDesktopARMSupport").Content("true");
  710. }
  711. }
  712. cmXMLElement(eprj, "Import")
  713. .Attribute("Project", "$(VCTargetsPath)\\Microsoft.Cpp.Default.props");
  714. if (!this->GeneratorToolsetHostArchitecture.empty()) {
  715. cmXMLElement epg(eprj, "PropertyGroup");
  716. cmXMLElement(epg, "PreferredToolArchitecture")
  717. .Content(this->GeneratorToolsetHostArchitecture);
  718. }
  719. {
  720. cmXMLElement epg(eprj, "PropertyGroup");
  721. epg.Attribute("Label", "Configuration");
  722. {
  723. cmXMLElement ect(epg, "ConfigurationType");
  724. if (this->IsNsightTegra()) {
  725. // Tegra-Android platform does not understand "Utility".
  726. ect.Content("StaticLibrary");
  727. } else {
  728. ect.Content("Utility");
  729. }
  730. }
  731. cmXMLElement(epg, "CharacterSet").Content("MultiByte");
  732. if (this->IsNsightTegra()) {
  733. cmXMLElement(epg, "NdkToolchainVersion")
  734. .Content(this->GetPlatformToolsetString());
  735. } else {
  736. cmXMLElement(epg, "PlatformToolset")
  737. .Content(this->GetPlatformToolsetString());
  738. }
  739. }
  740. cmXMLElement(eprj, "Import")
  741. .Attribute("Project", "$(VCTargetsPath)\\Microsoft.Cpp.props");
  742. {
  743. cmXMLElement eidg(eprj, "ItemDefinitionGroup");
  744. cmXMLElement epbe(eidg, "PostBuildEvent");
  745. cmXMLElement(epbe, "Command")
  746. .Content("echo VCTargetsPath=$(VCTargetsPath)");
  747. }
  748. cmXMLElement(eprj, "Import")
  749. .Attribute("Project", "$(VCTargetsPath)\\Microsoft.Cpp.targets");
  750. }
  751. std::vector<std::string> cmd;
  752. cmd.push_back(this->GetMSBuildCommand());
  753. cmd.push_back(vcxproj);
  754. cmd.push_back("/p:Configuration=Debug");
  755. cmd.push_back(std::string("/p:VisualStudioVersion=") +
  756. this->GetIDEVersion());
  757. std::string out;
  758. int ret = 0;
  759. cmsys::RegularExpression regex("\n *VCTargetsPath=([^%\r\n]+)[\r\n]");
  760. if (!cmSystemTools::RunSingleCommand(cmd, &out, &out, &ret, wd.c_str(),
  761. cmSystemTools::OUTPUT_NONE) ||
  762. ret != 0 || !regex.find(out)) {
  763. cmSystemTools::ReplaceString(out, "\n", "\n ");
  764. std::ostringstream e;
  765. /* clang-format off */
  766. e <<
  767. "Failed to run MSBuild command:\n"
  768. " " << cmd[0] << "\n"
  769. "to get the value of VCTargetsPath:\n"
  770. " " << out << "\n"
  771. ;
  772. /* clang-format on */
  773. if (ret != 0) {
  774. e << "Exit code: " << ret << "\n";
  775. }
  776. mf->IssueMessage(cmake::FATAL_ERROR, e.str().c_str());
  777. cmSystemTools::SetFatalErrorOccured();
  778. return false;
  779. }
  780. this->VCTargetsPath = regex.match(1);
  781. cmSystemTools::ConvertToUnixSlashes(this->VCTargetsPath);
  782. {
  783. cmsys::ofstream fout(txt.c_str());
  784. fout << this->VCTargetsPath << "\n";
  785. }
  786. return true;
  787. }
  788. void cmGlobalVisualStudio10Generator::GenerateBuildCommand(
  789. std::vector<std::string>& makeCommand, const std::string& makeProgram,
  790. const std::string& projectName, const std::string& projectDir,
  791. const std::string& targetName, const std::string& config, bool fast,
  792. int jobs, bool verbose, std::vector<std::string> const& makeOptions)
  793. {
  794. // Select the caller- or user-preferred make program, else MSBuild.
  795. std::string makeProgramSelected =
  796. this->SelectMakeProgram(makeProgram, this->GetMSBuildCommand());
  797. // Check if the caller explicitly requested a devenv tool.
  798. std::string makeProgramLower = makeProgramSelected;
  799. cmSystemTools::LowerCase(makeProgramLower);
  800. bool useDevEnv = (makeProgramLower.find("devenv") != std::string::npos ||
  801. makeProgramLower.find("vcexpress") != std::string::npos);
  802. // MSBuild is preferred (and required for VS Express), but if the .sln has
  803. // an Intel Fortran .vfproj then we have to use devenv. Parse it to find out.
  804. cmSlnData slnData;
  805. {
  806. std::string slnFile;
  807. if (!projectDir.empty()) {
  808. slnFile = projectDir;
  809. slnFile += "/";
  810. }
  811. slnFile += projectName;
  812. slnFile += ".sln";
  813. cmVisualStudioSlnParser parser;
  814. if (parser.ParseFile(slnFile, slnData,
  815. cmVisualStudioSlnParser::DataGroupProjects)) {
  816. std::vector<cmSlnProjectEntry> slnProjects = slnData.GetProjects();
  817. for (std::vector<cmSlnProjectEntry>::const_iterator i =
  818. slnProjects.cbegin();
  819. !useDevEnv && i != slnProjects.cend(); ++i) {
  820. std::string proj = i->GetRelativePath();
  821. if (proj.size() > 7 && proj.substr(proj.size() - 7) == ".vfproj") {
  822. useDevEnv = true;
  823. }
  824. }
  825. }
  826. }
  827. if (useDevEnv) {
  828. // Use devenv to build solutions containing Intel Fortran projects.
  829. cmGlobalVisualStudio7Generator::GenerateBuildCommand(
  830. makeCommand, makeProgram, projectName, projectDir, targetName, config,
  831. fast, jobs, verbose, makeOptions);
  832. return;
  833. }
  834. makeCommand.push_back(makeProgramSelected);
  835. std::string realTarget = targetName;
  836. // msbuild.exe CxxOnly.sln /t:Build /p:Configuration=Debug /target:ALL_BUILD
  837. // /m
  838. if (realTarget.empty()) {
  839. realTarget = "ALL_BUILD";
  840. }
  841. if (realTarget == "clean") {
  842. makeCommand.push_back(std::string(projectName) + ".sln");
  843. makeCommand.push_back("/t:Clean");
  844. } else {
  845. std::string targetProject(realTarget);
  846. targetProject += ".vcxproj";
  847. if (targetProject.find('/') == std::string::npos) {
  848. // it might be in a subdir
  849. if (cmSlnProjectEntry const* proj =
  850. slnData.GetProjectByName(realTarget)) {
  851. targetProject = proj->GetRelativePath();
  852. cmSystemTools::ConvertToUnixSlashes(targetProject);
  853. }
  854. }
  855. makeCommand.push_back(targetProject);
  856. }
  857. std::string configArg = "/p:Configuration=";
  858. if (!config.empty()) {
  859. configArg += config;
  860. } else {
  861. configArg += "Debug";
  862. }
  863. makeCommand.push_back(configArg);
  864. makeCommand.push_back(std::string("/p:VisualStudioVersion=") +
  865. this->GetIDEVersion());
  866. if (jobs != cmake::NO_BUILD_PARALLEL_LEVEL) {
  867. if (jobs == cmake::DEFAULT_BUILD_PARALLEL_LEVEL) {
  868. makeCommand.push_back("/m");
  869. } else {
  870. makeCommand.push_back(std::string("/m:") + std::to_string(jobs));
  871. }
  872. // Having msbuild.exe and cl.exe using multiple jobs is discouraged
  873. makeCommand.push_back("/p:CL_MPCount=1");
  874. }
  875. makeCommand.insert(makeCommand.end(), makeOptions.begin(),
  876. makeOptions.end());
  877. }
  878. bool cmGlobalVisualStudio10Generator::Find64BitTools(cmMakefile* mf)
  879. {
  880. if (this->DefaultPlatformToolset == "v100") {
  881. // The v100 64-bit toolset does not exist in the express edition.
  882. this->DefaultPlatformToolset.clear();
  883. }
  884. if (this->GetPlatformToolset()) {
  885. return true;
  886. }
  887. // This edition does not come with 64-bit tools. Look for them.
  888. //
  889. // TODO: Detect available tools? x64\v100 exists but does not work?
  890. // HKLM\\SOFTWARE\\Microsoft\\MSBuild\\ToolsVersions\\4.0;VCTargetsPath
  891. // c:/Program Files (x86)/MSBuild/Microsoft.Cpp/v4.0/Platforms/
  892. // {Itanium,Win32,x64}/PlatformToolsets/{v100,v90,Windows7.1SDK}
  893. std::string winSDK_7_1;
  894. if (cmSystemTools::ReadRegistryValue(
  895. "HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Microsoft SDKs\\"
  896. "Windows\\v7.1;InstallationFolder",
  897. winSDK_7_1)) {
  898. std::ostringstream m;
  899. m << "Found Windows SDK v7.1: " << winSDK_7_1;
  900. mf->DisplayStatus(m.str().c_str(), -1);
  901. this->DefaultPlatformToolset = "Windows7.1SDK";
  902. return true;
  903. } else {
  904. std::ostringstream e;
  905. /* clang-format off */
  906. e << "Cannot enable 64-bit tools with Visual Studio 2010 Express.\n"
  907. << "Install the Microsoft Windows SDK v7.1 to get 64-bit tools:\n"
  908. << " http://msdn.microsoft.com/en-us/windows/bb980924.aspx";
  909. /* clang-format on */
  910. mf->IssueMessage(cmake::FATAL_ERROR, e.str().c_str());
  911. cmSystemTools::SetFatalErrorOccured();
  912. return false;
  913. }
  914. }
  915. std::string cmGlobalVisualStudio10Generator::GenerateRuleFile(
  916. std::string const& output) const
  917. {
  918. // The VS 10 generator needs to create the .rule files on disk.
  919. // Hide them away under the CMakeFiles directory.
  920. std::string ruleDir = this->GetCMakeInstance()->GetHomeOutputDirectory();
  921. ruleDir += cmake::GetCMakeFilesDirectory();
  922. ruleDir += "/";
  923. ruleDir += cmSystemTools::ComputeStringMD5(
  924. cmSystemTools::GetFilenamePath(output).c_str());
  925. std::string ruleFile = ruleDir + "/";
  926. ruleFile += cmSystemTools::GetFilenameName(output);
  927. ruleFile += ".rule";
  928. return ruleFile;
  929. }
  930. void cmGlobalVisualStudio10Generator::PathTooLong(cmGeneratorTarget* target,
  931. cmSourceFile const* sf,
  932. std::string const& sfRel)
  933. {
  934. size_t len =
  935. (target->GetLocalGenerator()->GetCurrentBinaryDirectory().length() + 1 +
  936. sfRel.length());
  937. if (len > this->LongestSource.Length) {
  938. this->LongestSource.Length = len;
  939. this->LongestSource.Target = target;
  940. this->LongestSource.SourceFile = sf;
  941. this->LongestSource.SourceRel = sfRel;
  942. }
  943. }
  944. std::string cmGlobalVisualStudio10Generator::Encoding()
  945. {
  946. return "utf-8";
  947. }
  948. bool cmGlobalVisualStudio10Generator::IsNsightTegra() const
  949. {
  950. return !this->NsightTegraVersion.empty();
  951. }
  952. std::string cmGlobalVisualStudio10Generator::GetNsightTegraVersion() const
  953. {
  954. return this->NsightTegraVersion;
  955. }
  956. std::string cmGlobalVisualStudio10Generator::GetInstalledNsightTegraVersion()
  957. {
  958. std::string version;
  959. cmSystemTools::ReadRegistryValue(
  960. "HKEY_LOCAL_MACHINE\\SOFTWARE\\NVIDIA Corporation\\Nsight Tegra;"
  961. "Version",
  962. version, cmSystemTools::KeyWOW64_32);
  963. return version;
  964. }
  965. cmIDEFlagTable const* cmGlobalVisualStudio10Generator::GetClFlagTable() const
  966. {
  967. cmIDEFlagTable const* table = this->ToolsetOptions.GetClFlagTable(
  968. this->GetPlatformName(), this->GetPlatformToolsetString());
  969. return (table != nullptr) ? table : this->DefaultClFlagTable;
  970. }
  971. cmIDEFlagTable const* cmGlobalVisualStudio10Generator::GetCSharpFlagTable()
  972. const
  973. {
  974. cmIDEFlagTable const* table = this->ToolsetOptions.GetCSharpFlagTable(
  975. this->GetPlatformName(), this->GetPlatformToolsetString());
  976. return (table != nullptr) ? table : this->DefaultCSharpFlagTable;
  977. }
  978. cmIDEFlagTable const* cmGlobalVisualStudio10Generator::GetRcFlagTable() const
  979. {
  980. cmIDEFlagTable const* table = this->ToolsetOptions.GetRcFlagTable(
  981. this->GetPlatformName(), this->GetPlatformToolsetString());
  982. return (table != nullptr) ? table : this->DefaultRcFlagTable;
  983. }
  984. cmIDEFlagTable const* cmGlobalVisualStudio10Generator::GetLibFlagTable() const
  985. {
  986. cmIDEFlagTable const* table = this->ToolsetOptions.GetLibFlagTable(
  987. this->GetPlatformName(), this->GetPlatformToolsetString());
  988. return (table != nullptr) ? table : this->DefaultLibFlagTable;
  989. }
  990. cmIDEFlagTable const* cmGlobalVisualStudio10Generator::GetLinkFlagTable() const
  991. {
  992. cmIDEFlagTable const* table = this->ToolsetOptions.GetLinkFlagTable(
  993. this->GetPlatformName(), this->GetPlatformToolsetString());
  994. return (table != nullptr) ? table : this->DefaultLinkFlagTable;
  995. }
  996. cmIDEFlagTable const* cmGlobalVisualStudio10Generator::GetCudaFlagTable() const
  997. {
  998. return this->DefaultCudaFlagTable;
  999. }
  1000. cmIDEFlagTable const* cmGlobalVisualStudio10Generator::GetCudaHostFlagTable()
  1001. const
  1002. {
  1003. return this->DefaultCudaHostFlagTable;
  1004. }
  1005. cmIDEFlagTable const* cmGlobalVisualStudio10Generator::GetMasmFlagTable() const
  1006. {
  1007. cmIDEFlagTable const* table = this->ToolsetOptions.GetMasmFlagTable(
  1008. this->GetPlatformName(), this->GetPlatformToolsetString());
  1009. return (table != nullptr) ? table : this->DefaultMasmFlagTable;
  1010. }
  1011. cmIDEFlagTable const* cmGlobalVisualStudio10Generator::GetNasmFlagTable() const
  1012. {
  1013. return this->DefaultNasmFlagTable;
  1014. }