cmGlobalVisualStudio10Generator.cxx 44 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337
  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 "cmMessageType.h"
  10. #include "cmSourceFile.h"
  11. #include "cmVersion.h"
  12. #include "cmVisualStudioSlnData.h"
  13. #include "cmVisualStudioSlnParser.h"
  14. #include "cmXMLWriter.h"
  15. #include "cm_jsoncpp_reader.h"
  16. #include "cmake.h"
  17. #include "cmsys/FStream.hxx"
  18. #include "cmsys/Glob.hxx"
  19. #include "cmsys/RegularExpression.hxx"
  20. #include <algorithm>
  21. static const char vs10generatorName[] = "Visual Studio 10 2010";
  22. static std::map<std::string, std::vector<cmIDEFlagTable>> loadedFlagJsonFiles;
  23. // Map generator name without year to name with year.
  24. static const char* cmVS10GenName(const std::string& name, std::string& genName)
  25. {
  26. if (strncmp(name.c_str(), vs10generatorName,
  27. sizeof(vs10generatorName) - 6) != 0) {
  28. return 0;
  29. }
  30. const char* p = name.c_str() + sizeof(vs10generatorName) - 6;
  31. if (cmHasLiteralPrefix(p, " 2010")) {
  32. p += 5;
  33. }
  34. genName = std::string(vs10generatorName) + p;
  35. return p;
  36. }
  37. class cmGlobalVisualStudio10Generator::Factory
  38. : public cmGlobalGeneratorFactory
  39. {
  40. public:
  41. cmGlobalGenerator* CreateGlobalGenerator(const std::string& name,
  42. cmake* cm) const override
  43. {
  44. std::string genName;
  45. const char* p = cmVS10GenName(name, genName);
  46. if (!p) {
  47. return 0;
  48. }
  49. if (!*p) {
  50. return new cmGlobalVisualStudio10Generator(cm, genName, "");
  51. }
  52. if (*p++ != ' ') {
  53. return 0;
  54. }
  55. if (strcmp(p, "Win64") == 0) {
  56. return new cmGlobalVisualStudio10Generator(cm, genName, "x64");
  57. }
  58. if (strcmp(p, "IA64") == 0) {
  59. return new cmGlobalVisualStudio10Generator(cm, genName, "Itanium");
  60. }
  61. return 0;
  62. }
  63. void GetDocumentation(cmDocumentationEntry& entry) const override
  64. {
  65. entry.Name = std::string(vs10generatorName) + " [arch]";
  66. entry.Brief = "Generates Visual Studio 2010 project files. "
  67. "Optional [arch] can be \"Win64\" or \"IA64\".";
  68. }
  69. std::vector<std::string> GetGeneratorNames() const override
  70. {
  71. std::vector<std::string> names;
  72. names.push_back(vs10generatorName);
  73. return names;
  74. }
  75. std::vector<std::string> GetGeneratorNamesWithPlatform() const override
  76. {
  77. std::vector<std::string> names;
  78. names.push_back(vs10generatorName + std::string(" IA64"));
  79. names.push_back(vs10generatorName + std::string(" Win64"));
  80. return names;
  81. }
  82. bool SupportsToolset() const override { return true; }
  83. bool SupportsPlatform() const override { return true; }
  84. std::vector<std::string> GetKnownPlatforms() const override
  85. {
  86. std::vector<std::string> platforms;
  87. platforms.emplace_back("x64");
  88. platforms.emplace_back("Win32");
  89. platforms.emplace_back("Itanium");
  90. return platforms;
  91. }
  92. std::string GetDefaultPlatformName() const override { return "Win32"; }
  93. };
  94. cmGlobalGeneratorFactory* cmGlobalVisualStudio10Generator::NewFactory()
  95. {
  96. return new Factory;
  97. }
  98. cmGlobalVisualStudio10Generator::cmGlobalVisualStudio10Generator(
  99. cmake* cm, const std::string& name,
  100. std::string const& platformInGeneratorName)
  101. : cmGlobalVisualStudio8Generator(cm, name, platformInGeneratorName)
  102. {
  103. std::string vc10Express;
  104. this->ExpressEdition = cmSystemTools::ReadRegistryValue(
  105. "HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\VCExpress\\10.0\\Setup\\VC;"
  106. "ProductDir",
  107. vc10Express, cmSystemTools::KeyWOW64_32);
  108. this->CudaEnabled = false;
  109. this->SystemIsWindowsCE = false;
  110. this->SystemIsWindowsPhone = false;
  111. this->SystemIsWindowsStore = false;
  112. this->MSBuildCommandInitialized = false;
  113. {
  114. std::string envPlatformToolset;
  115. if (cmSystemTools::GetEnv("PlatformToolset", envPlatformToolset) &&
  116. envPlatformToolset == "Windows7.1SDK") {
  117. // We are running from a Windows7.1SDK command prompt.
  118. this->DefaultPlatformToolset = "Windows7.1SDK";
  119. } else {
  120. this->DefaultPlatformToolset = "v100";
  121. }
  122. }
  123. this->DefaultCLFlagTableName = "v10";
  124. this->DefaultCSharpFlagTableName = "v10";
  125. this->DefaultLibFlagTableName = "v10";
  126. this->DefaultLinkFlagTableName = "v10";
  127. this->DefaultCudaFlagTableName = "v10";
  128. this->DefaultCudaHostFlagTableName = "v10";
  129. this->DefaultMasmFlagTableName = "v10";
  130. this->DefaultNasmFlagTableName = "v10";
  131. this->DefaultRCFlagTableName = "v10";
  132. this->Version = VS10;
  133. this->PlatformToolsetNeedsDebugEnum = false;
  134. }
  135. bool cmGlobalVisualStudio10Generator::MatchesGeneratorName(
  136. const std::string& name) const
  137. {
  138. std::string genName;
  139. if (cmVS10GenName(name, genName)) {
  140. return genName == this->GetName();
  141. }
  142. return false;
  143. }
  144. bool cmGlobalVisualStudio10Generator::SetSystemName(std::string const& s,
  145. cmMakefile* mf)
  146. {
  147. this->SystemName = s;
  148. this->SystemVersion = mf->GetSafeDefinition("CMAKE_SYSTEM_VERSION");
  149. if (!this->InitializeSystem(mf)) {
  150. return false;
  151. }
  152. return this->cmGlobalVisualStudio8Generator::SetSystemName(s, mf);
  153. }
  154. bool cmGlobalVisualStudio10Generator::SetGeneratorPlatform(
  155. std::string const& p, cmMakefile* mf)
  156. {
  157. if (!this->cmGlobalVisualStudio8Generator::SetGeneratorPlatform(p, mf)) {
  158. return false;
  159. }
  160. if (this->GetPlatformName() == "Itanium" ||
  161. this->GetPlatformName() == "x64") {
  162. if (this->IsExpressEdition() && !this->Find64BitTools(mf)) {
  163. return false;
  164. }
  165. }
  166. return true;
  167. }
  168. static void cmCudaToolVersion(std::string& s)
  169. {
  170. // "CUDA x.y.props" => "x.y"
  171. s = s.substr(5);
  172. s = s.substr(0, s.size() - 6);
  173. }
  174. bool cmGlobalVisualStudio10Generator::SetGeneratorToolset(
  175. std::string const& ts, cmMakefile* mf)
  176. {
  177. if (this->SystemIsWindowsCE && ts.empty() &&
  178. this->DefaultPlatformToolset.empty()) {
  179. std::ostringstream e;
  180. e << this->GetName() << " Windows CE version '" << this->SystemVersion
  181. << "' requires CMAKE_GENERATOR_TOOLSET to be set.";
  182. mf->IssueMessage(MessageType::FATAL_ERROR, e.str());
  183. return false;
  184. }
  185. if (!this->ParseGeneratorToolset(ts, mf)) {
  186. return false;
  187. }
  188. if (!this->FindVCTargetsPath(mf)) {
  189. return false;
  190. }
  191. if (cmHasLiteralPrefix(this->GetPlatformToolsetString(), "v140")) {
  192. // The GenerateDebugInformation link setting for the v140 toolset
  193. // in VS 2015 was originally an enum with "No" and "Debug" values,
  194. // differing from the "false" and "true" values used in older toolsets.
  195. // A VS 2015 update changed it back. Parse the "link.xml" file to
  196. // discover which one we need.
  197. std::string const link_xml = this->VCTargetsPath + "/1033/link.xml";
  198. cmsys::ifstream fin(link_xml.c_str());
  199. std::string line;
  200. while (fin && cmSystemTools::GetLineFromStream(fin, line)) {
  201. if (line.find(" Switch=\"DEBUG\" ") != std::string::npos) {
  202. this->PlatformToolsetNeedsDebugEnum =
  203. line.find(" Name=\"Debug\" ") != std::string::npos;
  204. break;
  205. }
  206. }
  207. }
  208. if (this->GeneratorToolsetCuda.empty()) {
  209. // Find the highest available version of the CUDA tools.
  210. std::vector<std::string> cudaTools;
  211. std::string const bcDir = this->VCTargetsPath + "/BuildCustomizations";
  212. cmsys::Glob gl;
  213. gl.SetRelative(bcDir.c_str());
  214. if (gl.FindFiles(bcDir + "/CUDA *.props")) {
  215. cudaTools = gl.GetFiles();
  216. }
  217. if (!cudaTools.empty()) {
  218. std::for_each(cudaTools.begin(), cudaTools.end(), cmCudaToolVersion);
  219. std::sort(cudaTools.begin(), cudaTools.end(),
  220. cmSystemTools::VersionCompareGreater);
  221. this->GeneratorToolsetCuda = cudaTools.at(0);
  222. }
  223. }
  224. if (!this->GeneratorToolsetVersion.empty() &&
  225. this->GeneratorToolsetVersion != "Test Toolset Version") {
  226. // If a specific minor version of the toolset was requested, verify that it
  227. // is compatible to the major version and that is exists on disk.
  228. // If not clear the value.
  229. std::string version = this->GeneratorToolsetVersion;
  230. cmsys::RegularExpression regex("[0-9][0-9]\\.[0-9][0-9]");
  231. if (regex.find(version)) {
  232. version = "v" + version.erase(2, 1);
  233. } else {
  234. // Version not recognized. Clear it.
  235. version.clear();
  236. }
  237. if (version.find(this->GetPlatformToolsetString()) != 0) {
  238. std::ostringstream e;
  239. /* clang-format off */
  240. e <<
  241. "Generator\n"
  242. " " << this->GetName() << "\n"
  243. "given toolset and version specification\n"
  244. " " << this->GetPlatformToolsetString() << ",version=" <<
  245. this->GeneratorToolsetVersion << "\n"
  246. "contains an invalid version specification."
  247. ;
  248. /* clang-format on */
  249. mf->IssueMessage(MessageType::FATAL_ERROR, e.str());
  250. // Clear the configured tool-set
  251. this->GeneratorToolsetVersion.clear();
  252. }
  253. bool const isDefaultToolset =
  254. this->IsDefaultToolset(this->GeneratorToolsetVersion);
  255. if (isDefaultToolset) {
  256. // If the given version is the default toolset, remove the setting
  257. this->GeneratorToolsetVersion.clear();
  258. } else {
  259. std::string const toolsetPath = this->GetAuxiliaryToolset();
  260. if (!toolsetPath.empty() && !cmSystemTools::FileExists(toolsetPath)) {
  261. std::ostringstream e;
  262. /* clang-format off */
  263. e <<
  264. "Generator\n"
  265. " " << this->GetName() << "\n"
  266. "given toolset and version specification\n"
  267. " " << this->GetPlatformToolsetString() << ",version=" <<
  268. this->GeneratorToolsetVersion << "\n"
  269. "does not seem to be installed at\n" <<
  270. " " << toolsetPath;
  271. ;
  272. /* clang-format on */
  273. mf->IssueMessage(MessageType::FATAL_ERROR, e.str());
  274. // Clear the configured tool-set
  275. this->GeneratorToolsetVersion.clear();
  276. }
  277. }
  278. }
  279. if (const char* toolset = this->GetPlatformToolset()) {
  280. mf->AddDefinition("CMAKE_VS_PLATFORM_TOOLSET", toolset);
  281. }
  282. if (const char* version = this->GetPlatformToolsetVersion()) {
  283. mf->AddDefinition("CMAKE_VS_PLATFORM_TOOLSET_VERSION", version);
  284. }
  285. if (const char* hostArch = this->GetPlatformToolsetHostArchitecture()) {
  286. mf->AddDefinition("CMAKE_VS_PLATFORM_TOOLSET_HOST_ARCHITECTURE", hostArch);
  287. }
  288. if (const char* cuda = this->GetPlatformToolsetCuda()) {
  289. mf->AddDefinition("CMAKE_VS_PLATFORM_TOOLSET_CUDA", cuda);
  290. }
  291. return true;
  292. }
  293. bool cmGlobalVisualStudio10Generator::ParseGeneratorToolset(
  294. std::string const& ts, cmMakefile* mf)
  295. {
  296. std::vector<std::string> const fields = cmTokenize(ts, ",");
  297. std::vector<std::string>::const_iterator fi = fields.begin();
  298. if (fi == fields.end()) {
  299. return true;
  300. }
  301. // The first field may be the VS platform toolset.
  302. if (fi->find('=') == fi->npos) {
  303. this->GeneratorToolset = *fi;
  304. ++fi;
  305. }
  306. std::set<std::string> handled;
  307. // The rest of the fields must be key=value pairs.
  308. for (; fi != fields.end(); ++fi) {
  309. std::string::size_type pos = fi->find('=');
  310. if (pos == fi->npos) {
  311. std::ostringstream e;
  312. /* clang-format off */
  313. e <<
  314. "Generator\n"
  315. " " << this->GetName() << "\n"
  316. "given toolset specification\n"
  317. " " << ts << "\n"
  318. "that contains a field after the first ',' with no '='."
  319. ;
  320. /* clang-format on */
  321. mf->IssueMessage(MessageType::FATAL_ERROR, e.str());
  322. return false;
  323. }
  324. std::string const key = fi->substr(0, pos);
  325. std::string const value = fi->substr(pos + 1);
  326. if (!handled.insert(key).second) {
  327. std::ostringstream e;
  328. /* clang-format off */
  329. e <<
  330. "Generator\n"
  331. " " << this->GetName() << "\n"
  332. "given toolset specification\n"
  333. " " << ts << "\n"
  334. "that contains duplicate field key '" << key << "'."
  335. ;
  336. /* clang-format on */
  337. mf->IssueMessage(MessageType::FATAL_ERROR, e.str());
  338. return false;
  339. }
  340. if (!this->ProcessGeneratorToolsetField(key, value)) {
  341. std::ostringstream e;
  342. /* clang-format off */
  343. e <<
  344. "Generator\n"
  345. " " << this->GetName() << "\n"
  346. "given toolset specification\n"
  347. " " << ts << "\n"
  348. "that contains invalid field '" << *fi << "'."
  349. ;
  350. /* clang-format on */
  351. mf->IssueMessage(MessageType::FATAL_ERROR, e.str());
  352. return false;
  353. }
  354. }
  355. return true;
  356. }
  357. bool cmGlobalVisualStudio10Generator::ProcessGeneratorToolsetField(
  358. std::string const& key, std::string const& value)
  359. {
  360. if (key == "cuda") {
  361. this->GeneratorToolsetCuda = value;
  362. return true;
  363. }
  364. if (key == "version") {
  365. this->GeneratorToolsetVersion = value;
  366. return true;
  367. }
  368. return false;
  369. }
  370. bool cmGlobalVisualStudio10Generator::InitializeSystem(cmMakefile* mf)
  371. {
  372. if (this->SystemName == "Windows") {
  373. if (!this->InitializeWindows(mf)) {
  374. return false;
  375. }
  376. } else if (this->SystemName == "WindowsCE") {
  377. this->SystemIsWindowsCE = true;
  378. if (!this->InitializeWindowsCE(mf)) {
  379. return false;
  380. }
  381. } else if (this->SystemName == "WindowsPhone") {
  382. this->SystemIsWindowsPhone = true;
  383. if (!this->InitializeWindowsPhone(mf)) {
  384. return false;
  385. }
  386. } else if (this->SystemName == "WindowsStore") {
  387. this->SystemIsWindowsStore = true;
  388. if (!this->InitializeWindowsStore(mf)) {
  389. return false;
  390. }
  391. } else if (this->SystemName == "Android") {
  392. if (this->PlatformInGeneratorName) {
  393. std::ostringstream e;
  394. e << "CMAKE_SYSTEM_NAME is 'Android' but CMAKE_GENERATOR "
  395. << "specifies a platform too: '" << this->GetName() << "'";
  396. mf->IssueMessage(MessageType::FATAL_ERROR, e.str());
  397. return false;
  398. }
  399. std::string v = this->GetInstalledNsightTegraVersion();
  400. if (v.empty()) {
  401. mf->IssueMessage(MessageType::FATAL_ERROR,
  402. "CMAKE_SYSTEM_NAME is 'Android' but "
  403. "'NVIDIA Nsight Tegra Visual Studio Edition' "
  404. "is not installed.");
  405. return false;
  406. }
  407. this->DefaultPlatformName = "Tegra-Android";
  408. this->DefaultPlatformToolset = "Default";
  409. this->NsightTegraVersion = v;
  410. mf->AddDefinition("CMAKE_VS_NsightTegra_VERSION", v);
  411. }
  412. return true;
  413. }
  414. bool cmGlobalVisualStudio10Generator::InitializeWindows(cmMakefile*)
  415. {
  416. return true;
  417. }
  418. bool cmGlobalVisualStudio10Generator::InitializeWindowsCE(cmMakefile* mf)
  419. {
  420. if (this->PlatformInGeneratorName) {
  421. std::ostringstream e;
  422. e << "CMAKE_SYSTEM_NAME is 'WindowsCE' but CMAKE_GENERATOR "
  423. << "specifies a platform too: '" << this->GetName() << "'";
  424. mf->IssueMessage(MessageType::FATAL_ERROR, e.str());
  425. return false;
  426. }
  427. this->DefaultPlatformToolset = this->SelectWindowsCEToolset();
  428. return true;
  429. }
  430. bool cmGlobalVisualStudio10Generator::InitializeWindowsPhone(cmMakefile* mf)
  431. {
  432. std::ostringstream e;
  433. e << this->GetName() << " does not support Windows Phone.";
  434. mf->IssueMessage(MessageType::FATAL_ERROR, e.str());
  435. return false;
  436. }
  437. bool cmGlobalVisualStudio10Generator::InitializeWindowsStore(cmMakefile* mf)
  438. {
  439. std::ostringstream e;
  440. e << this->GetName() << " does not support Windows Store.";
  441. mf->IssueMessage(MessageType::FATAL_ERROR, e.str());
  442. return false;
  443. }
  444. bool cmGlobalVisualStudio10Generator::SelectWindowsPhoneToolset(
  445. std::string& toolset) const
  446. {
  447. toolset.clear();
  448. return false;
  449. }
  450. bool cmGlobalVisualStudio10Generator::SelectWindowsStoreToolset(
  451. std::string& toolset) const
  452. {
  453. toolset.clear();
  454. return false;
  455. }
  456. std::string cmGlobalVisualStudio10Generator::SelectWindowsCEToolset() const
  457. {
  458. if (this->SystemVersion == "8.0") {
  459. return "CE800";
  460. }
  461. return "";
  462. }
  463. //! Create a local generator appropriate to this Global Generator
  464. cmLocalGenerator* cmGlobalVisualStudio10Generator::CreateLocalGenerator(
  465. cmMakefile* mf)
  466. {
  467. return new cmLocalVisualStudio10Generator(this, mf);
  468. }
  469. void cmGlobalVisualStudio10Generator::Generate()
  470. {
  471. this->LongestSource = LongestSourcePath();
  472. this->cmGlobalVisualStudio8Generator::Generate();
  473. if (this->LongestSource.Length > 0) {
  474. cmLocalGenerator* lg = this->LongestSource.Target->GetLocalGenerator();
  475. std::ostringstream e;
  476. /* clang-format off */
  477. e <<
  478. "The binary and/or source directory paths may be too long to generate "
  479. "Visual Studio 10 files for this project. "
  480. "Consider choosing shorter directory names to build this project with "
  481. "Visual Studio 10. "
  482. "A more detailed explanation follows."
  483. "\n"
  484. "There is a bug in the VS 10 IDE that renders property dialog fields "
  485. "blank for files referenced by full path in the project file. "
  486. "However, CMake must reference at least one file by full path:\n"
  487. " " << this->LongestSource.SourceFile->GetFullPath() << "\n"
  488. "This is because some Visual Studio tools would append the relative "
  489. "path to the end of the referencing directory path, as in:\n"
  490. " " << lg->GetCurrentBinaryDirectory() << "/"
  491. << this->LongestSource.SourceRel << "\n"
  492. "and then incorrectly complain that the file does not exist because "
  493. "the path length is too long for some internal buffer or API. "
  494. "To avoid this problem CMake must use a full path for this file "
  495. "which then triggers the VS 10 property dialog bug.";
  496. /* clang-format on */
  497. lg->IssueMessage(MessageType::WARNING, e.str().c_str());
  498. }
  499. }
  500. void cmGlobalVisualStudio10Generator::EnableLanguage(
  501. std::vector<std::string> const& lang, cmMakefile* mf, bool optional)
  502. {
  503. for (std::string const& it : lang) {
  504. if (it == "ASM_NASM") {
  505. this->NasmEnabled = true;
  506. }
  507. if (it == "CUDA") {
  508. this->CudaEnabled = true;
  509. }
  510. }
  511. this->AddPlatformDefinitions(mf);
  512. cmGlobalVisualStudio8Generator::EnableLanguage(lang, mf, optional);
  513. }
  514. const char* cmGlobalVisualStudio10Generator::GetPlatformToolset() const
  515. {
  516. std::string const& toolset = this->GetPlatformToolsetString();
  517. if (toolset.empty()) {
  518. return nullptr;
  519. }
  520. return toolset.c_str();
  521. }
  522. std::string const& cmGlobalVisualStudio10Generator::GetPlatformToolsetString()
  523. const
  524. {
  525. if (!this->GeneratorToolset.empty()) {
  526. return this->GeneratorToolset;
  527. }
  528. if (!this->DefaultPlatformToolset.empty()) {
  529. return this->DefaultPlatformToolset;
  530. }
  531. static std::string const empty;
  532. return empty;
  533. }
  534. const char* cmGlobalVisualStudio10Generator::GetPlatformToolsetVersion() const
  535. {
  536. std::string const& version = this->GetPlatformToolsetVersionString();
  537. if (version.empty()) {
  538. return nullptr;
  539. }
  540. return version.c_str();
  541. }
  542. std::string const&
  543. cmGlobalVisualStudio10Generator::GetPlatformToolsetVersionString() const
  544. {
  545. if (!this->GeneratorToolsetVersion.empty()) {
  546. return this->GeneratorToolsetVersion;
  547. }
  548. static std::string const empty;
  549. return empty;
  550. }
  551. const char*
  552. cmGlobalVisualStudio10Generator::GetPlatformToolsetHostArchitecture() const
  553. {
  554. std::string const& hostArch =
  555. this->GetPlatformToolsetHostArchitectureString();
  556. if (hostArch.empty()) {
  557. return nullptr;
  558. }
  559. return hostArch.c_str();
  560. }
  561. std::string const&
  562. cmGlobalVisualStudio10Generator::GetPlatformToolsetHostArchitectureString()
  563. const
  564. {
  565. if (!this->GeneratorToolsetHostArchitecture.empty()) {
  566. return this->GeneratorToolsetHostArchitecture;
  567. }
  568. if (!this->DefaultPlatformToolsetHostArchitecture.empty()) {
  569. return this->DefaultPlatformToolsetHostArchitecture;
  570. }
  571. static std::string const empty;
  572. return empty;
  573. }
  574. const char* cmGlobalVisualStudio10Generator::GetPlatformToolsetCuda() const
  575. {
  576. if (!this->GeneratorToolsetCuda.empty()) {
  577. return this->GeneratorToolsetCuda.c_str();
  578. }
  579. return nullptr;
  580. }
  581. std::string const&
  582. cmGlobalVisualStudio10Generator::GetPlatformToolsetCudaString() const
  583. {
  584. return this->GeneratorToolsetCuda;
  585. }
  586. bool cmGlobalVisualStudio10Generator::IsDefaultToolset(
  587. const std::string&) const
  588. {
  589. return true;
  590. }
  591. std::string cmGlobalVisualStudio10Generator::GetAuxiliaryToolset() const
  592. {
  593. return {};
  594. }
  595. bool cmGlobalVisualStudio10Generator::FindMakeProgram(cmMakefile* mf)
  596. {
  597. if (!this->cmGlobalVisualStudio8Generator::FindMakeProgram(mf)) {
  598. return false;
  599. }
  600. mf->AddDefinition("CMAKE_VS_MSBUILD_COMMAND", this->GetMSBuildCommand());
  601. return true;
  602. }
  603. std::string const& cmGlobalVisualStudio10Generator::GetMSBuildCommand()
  604. {
  605. if (!this->MSBuildCommandInitialized) {
  606. this->MSBuildCommandInitialized = true;
  607. this->MSBuildCommand = this->FindMSBuildCommand();
  608. }
  609. return this->MSBuildCommand;
  610. }
  611. std::string cmGlobalVisualStudio10Generator::FindMSBuildCommand()
  612. {
  613. std::string msbuild;
  614. std::string mskey;
  615. // Search in standard location.
  616. mskey = "HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\MSBuild\\ToolsVersions\\";
  617. mskey += this->GetToolsVersion();
  618. mskey += ";MSBuildToolsPath";
  619. if (cmSystemTools::ReadRegistryValue(mskey.c_str(), msbuild,
  620. cmSystemTools::KeyWOW64_32)) {
  621. cmSystemTools::ConvertToUnixSlashes(msbuild);
  622. msbuild += "/MSBuild.exe";
  623. if (cmSystemTools::FileExists(msbuild, true)) {
  624. return msbuild;
  625. }
  626. }
  627. msbuild = "MSBuild.exe";
  628. return msbuild;
  629. }
  630. std::string cmGlobalVisualStudio10Generator::FindDevEnvCommand()
  631. {
  632. if (this->ExpressEdition) {
  633. // Visual Studio Express >= 10 do not have "devenv.com" or
  634. // "VCExpress.exe" that we can use to build reliably.
  635. // Tell the caller it needs to use MSBuild instead.
  636. return "";
  637. }
  638. // Skip over the cmGlobalVisualStudio8Generator implementation because
  639. // we expect a real devenv and do not want to look for VCExpress.
  640. return this->cmGlobalVisualStudio71Generator::FindDevEnvCommand();
  641. }
  642. bool cmGlobalVisualStudio10Generator::FindVCTargetsPath(cmMakefile* mf)
  643. {
  644. // Skip this in special cases within our own test suite.
  645. if (this->GetPlatformName() == "Test Platform" ||
  646. this->GetPlatformToolsetString() == "Test Toolset") {
  647. return true;
  648. }
  649. std::string wd;
  650. if (!this->ConfiguredFilesPath.empty()) {
  651. // In a try-compile we are given the outer CMakeFiles directory.
  652. wd = this->ConfiguredFilesPath;
  653. } else {
  654. wd = this->GetCMakeInstance()->GetHomeOutputDirectory();
  655. wd += "/CMakeFiles";
  656. }
  657. wd += "/";
  658. wd += cmVersion::GetCMakeVersion();
  659. // We record the result persistently in a file.
  660. std::string const txt = wd + "/VCTargetsPath.txt";
  661. // If we have a recorded result, use it.
  662. {
  663. cmsys::ifstream fin(txt.c_str());
  664. if (fin && cmSystemTools::GetLineFromStream(fin, this->VCTargetsPath) &&
  665. cmSystemTools::FileIsDirectory(this->VCTargetsPath)) {
  666. cmSystemTools::ConvertToUnixSlashes(this->VCTargetsPath);
  667. return true;
  668. }
  669. }
  670. // Prepare the work directory.
  671. if (!cmSystemTools::MakeDirectory(wd)) {
  672. std::string e = "Failed to make directory:\n " + wd;
  673. mf->IssueMessage(MessageType::FATAL_ERROR, e.c_str());
  674. cmSystemTools::SetFatalErrorOccured();
  675. return false;
  676. }
  677. // Generate a project file for MSBuild to tell us the VCTargetsPath value.
  678. std::string const vcxproj = "VCTargetsPath.vcxproj";
  679. {
  680. std::string const vcxprojAbs = wd + "/" + vcxproj;
  681. cmsys::ofstream fout(vcxprojAbs.c_str());
  682. cmXMLWriter xw(fout);
  683. cmXMLDocument doc(xw);
  684. cmXMLElement eprj(doc, "Project");
  685. eprj.Attribute("DefaultTargets", "Build");
  686. eprj.Attribute("ToolsVersion", "4.0");
  687. eprj.Attribute("xmlns",
  688. "http://schemas.microsoft.com/developer/msbuild/2003");
  689. if (this->IsNsightTegra()) {
  690. cmXMLElement epg(eprj, "PropertyGroup");
  691. epg.Attribute("Label", "NsightTegraProject");
  692. cmXMLElement(epg, "NsightTegraProjectRevisionNumber").Content("6");
  693. }
  694. {
  695. cmXMLElement eig(eprj, "ItemGroup");
  696. eig.Attribute("Label", "ProjectConfigurations");
  697. cmXMLElement epc(eig, "ProjectConfiguration");
  698. epc.Attribute("Include", "Debug|" + this->GetPlatformName());
  699. cmXMLElement(epc, "Configuration").Content("Debug");
  700. cmXMLElement(epc, "Platform").Content(this->GetPlatformName());
  701. }
  702. {
  703. cmXMLElement epg(eprj, "PropertyGroup");
  704. epg.Attribute("Label", "Globals");
  705. cmXMLElement(epg, "ProjectGuid")
  706. .Content("{F3FC6D86-508D-3FB1-96D2-995F08B142EC}");
  707. cmXMLElement(epg, "Keyword").Content("Win32Proj");
  708. cmXMLElement(epg, "Platform").Content(this->GetPlatformName());
  709. if (this->GetSystemName() == "WindowsPhone") {
  710. cmXMLElement(epg, "ApplicationType").Content("Windows Phone");
  711. cmXMLElement(epg, "ApplicationTypeRevision")
  712. .Content(this->GetApplicationTypeRevision());
  713. } else if (this->GetSystemName() == "WindowsStore") {
  714. cmXMLElement(epg, "ApplicationType").Content("Windows Store");
  715. cmXMLElement(epg, "ApplicationTypeRevision")
  716. .Content(this->GetApplicationTypeRevision());
  717. }
  718. if (!this->WindowsTargetPlatformVersion.empty()) {
  719. cmXMLElement(epg, "WindowsTargetPlatformVersion")
  720. .Content(this->WindowsTargetPlatformVersion);
  721. }
  722. if (this->GetPlatformName() == "ARM64") {
  723. cmXMLElement(epg, "WindowsSDKDesktopARM64Support").Content("true");
  724. } else if (this->GetPlatformName() == "ARM") {
  725. cmXMLElement(epg, "WindowsSDKDesktopARMSupport").Content("true");
  726. }
  727. }
  728. cmXMLElement(eprj, "Import")
  729. .Attribute("Project", "$(VCTargetsPath)\\Microsoft.Cpp.Default.props");
  730. if (const char* hostArch = this->GetPlatformToolsetHostArchitecture()) {
  731. cmXMLElement epg(eprj, "PropertyGroup");
  732. cmXMLElement(epg, "PreferredToolArchitecture").Content(hostArch);
  733. }
  734. {
  735. cmXMLElement epg(eprj, "PropertyGroup");
  736. epg.Attribute("Label", "Configuration");
  737. {
  738. cmXMLElement ect(epg, "ConfigurationType");
  739. if (this->IsNsightTegra()) {
  740. // Tegra-Android platform does not understand "Utility".
  741. ect.Content("StaticLibrary");
  742. } else {
  743. ect.Content("Utility");
  744. }
  745. }
  746. cmXMLElement(epg, "CharacterSet").Content("MultiByte");
  747. if (this->IsNsightTegra()) {
  748. cmXMLElement(epg, "NdkToolchainVersion")
  749. .Content(this->GetPlatformToolsetString());
  750. } else {
  751. cmXMLElement(epg, "PlatformToolset")
  752. .Content(this->GetPlatformToolsetString());
  753. }
  754. }
  755. cmXMLElement(eprj, "Import")
  756. .Attribute("Project", "$(VCTargetsPath)\\Microsoft.Cpp.props");
  757. {
  758. cmXMLElement eidg(eprj, "ItemDefinitionGroup");
  759. cmXMLElement epbe(eidg, "PostBuildEvent");
  760. cmXMLElement(epbe, "Command")
  761. .Content("echo VCTargetsPath=$(VCTargetsPath)");
  762. }
  763. cmXMLElement(eprj, "Import")
  764. .Attribute("Project", "$(VCTargetsPath)\\Microsoft.Cpp.targets");
  765. }
  766. std::vector<std::string> cmd;
  767. cmd.push_back(this->GetMSBuildCommand());
  768. cmd.push_back(vcxproj);
  769. cmd.push_back("/p:Configuration=Debug");
  770. cmd.push_back(std::string("/p:VisualStudioVersion=") +
  771. this->GetIDEVersion());
  772. std::string out;
  773. int ret = 0;
  774. cmsys::RegularExpression regex("\n *VCTargetsPath=([^%\r\n]+)[\r\n]");
  775. if (!cmSystemTools::RunSingleCommand(cmd, &out, &out, &ret, wd.c_str(),
  776. cmSystemTools::OUTPUT_NONE) ||
  777. ret != 0 || !regex.find(out)) {
  778. cmSystemTools::ReplaceString(out, "\n", "\n ");
  779. std::ostringstream e;
  780. /* clang-format off */
  781. e <<
  782. "Failed to run MSBuild command:\n"
  783. " " << cmd[0] << "\n"
  784. "to get the value of VCTargetsPath:\n"
  785. " " << out << "\n"
  786. ;
  787. /* clang-format on */
  788. if (ret != 0) {
  789. e << "Exit code: " << ret << "\n";
  790. }
  791. mf->IssueMessage(MessageType::FATAL_ERROR, e.str().c_str());
  792. cmSystemTools::SetFatalErrorOccured();
  793. return false;
  794. }
  795. this->VCTargetsPath = regex.match(1);
  796. cmSystemTools::ConvertToUnixSlashes(this->VCTargetsPath);
  797. {
  798. cmsys::ofstream fout(txt.c_str());
  799. fout << this->VCTargetsPath << "\n";
  800. }
  801. return true;
  802. }
  803. std::vector<cmGlobalGenerator::GeneratedMakeCommand>
  804. cmGlobalVisualStudio10Generator::GenerateBuildCommand(
  805. const std::string& makeProgram, const std::string& projectName,
  806. const std::string& projectDir, std::vector<std::string> const& targetNames,
  807. const std::string& config, bool fast, int jobs, bool verbose,
  808. std::vector<std::string> const& makeOptions)
  809. {
  810. std::vector<GeneratedMakeCommand> makeCommands;
  811. // Select the caller- or user-preferred make program, else MSBuild.
  812. std::string makeProgramSelected =
  813. this->SelectMakeProgram(makeProgram, this->GetMSBuildCommand());
  814. // Check if the caller explicitly requested a devenv tool.
  815. std::string makeProgramLower = makeProgramSelected;
  816. cmSystemTools::LowerCase(makeProgramLower);
  817. bool useDevEnv = (makeProgramLower.find("devenv") != std::string::npos ||
  818. makeProgramLower.find("vcexpress") != std::string::npos);
  819. // Workaround to convince VCExpress.exe to produce output.
  820. const bool requiresOutputForward =
  821. (makeProgramLower.find("vcexpress") != std::string::npos);
  822. // MSBuild is preferred (and required for VS Express), but if the .sln has
  823. // an Intel Fortran .vfproj then we have to use devenv. Parse it to find out.
  824. cmSlnData slnData;
  825. {
  826. std::string slnFile;
  827. if (!projectDir.empty()) {
  828. slnFile = projectDir;
  829. slnFile += "/";
  830. }
  831. slnFile += projectName;
  832. slnFile += ".sln";
  833. cmVisualStudioSlnParser parser;
  834. if (parser.ParseFile(slnFile, slnData,
  835. cmVisualStudioSlnParser::DataGroupProjects)) {
  836. std::vector<cmSlnProjectEntry> slnProjects = slnData.GetProjects();
  837. for (cmSlnProjectEntry const& project : slnProjects) {
  838. if (useDevEnv) {
  839. break;
  840. }
  841. std::string proj = project.GetRelativePath();
  842. if (proj.size() > 7 && proj.substr(proj.size() - 7) == ".vfproj") {
  843. useDevEnv = true;
  844. }
  845. }
  846. }
  847. }
  848. if (useDevEnv) {
  849. // Use devenv to build solutions containing Intel Fortran projects.
  850. return cmGlobalVisualStudio7Generator::GenerateBuildCommand(
  851. makeProgram, projectName, projectDir, targetNames, config, fast, jobs,
  852. verbose, makeOptions);
  853. }
  854. std::vector<std::string> realTargetNames = targetNames;
  855. if (targetNames.empty() ||
  856. ((targetNames.size() == 1) && targetNames.front().empty())) {
  857. realTargetNames = { "ALL_BUILD" };
  858. }
  859. for (const auto& tname : realTargetNames) {
  860. // msbuild.exe CxxOnly.sln /t:Build /p:Configuration=Debug
  861. // /target:ALL_BUILD
  862. // /m
  863. if (tname.empty()) {
  864. continue;
  865. }
  866. GeneratedMakeCommand makeCommand;
  867. makeCommand.RequiresOutputForward = requiresOutputForward;
  868. makeCommand.Add(makeProgramSelected);
  869. if (tname == "clean") {
  870. makeCommand.Add(std::string(projectName) + ".sln");
  871. makeCommand.Add("/t:Clean");
  872. } else {
  873. std::string targetProject(tname);
  874. targetProject += ".vcxproj";
  875. if (targetProject.find('/') == std::string::npos) {
  876. // it might be in a subdir
  877. if (cmSlnProjectEntry const* proj = slnData.GetProjectByName(tname)) {
  878. targetProject = proj->GetRelativePath();
  879. cmSystemTools::ConvertToUnixSlashes(targetProject);
  880. }
  881. }
  882. makeCommand.Add(std::move(targetProject));
  883. }
  884. std::string configArg = "/p:Configuration=";
  885. if (!config.empty()) {
  886. configArg += config;
  887. } else {
  888. configArg += "Debug";
  889. }
  890. makeCommand.Add(configArg);
  891. makeCommand.Add(std::string("/p:Platform=") + this->GetPlatformName());
  892. makeCommand.Add(std::string("/p:VisualStudioVersion=") +
  893. this->GetIDEVersion());
  894. if (jobs != cmake::NO_BUILD_PARALLEL_LEVEL) {
  895. if (jobs == cmake::DEFAULT_BUILD_PARALLEL_LEVEL) {
  896. makeCommand.Add("/m");
  897. } else {
  898. makeCommand.Add(std::string("/m:") + std::to_string(jobs));
  899. }
  900. // Having msbuild.exe and cl.exe using multiple jobs is discouraged
  901. makeCommand.Add("/p:CL_MPCount=1");
  902. }
  903. // Respect the verbosity: 'n' normal will show build commands
  904. // 'm' minimal only the build step's title
  905. makeCommand.Add(std::string("/v:") + ((verbose) ? "n" : "m"));
  906. makeCommand.Add(makeOptions.begin(), makeOptions.end());
  907. makeCommands.emplace_back(std::move(makeCommand));
  908. }
  909. return makeCommands;
  910. }
  911. bool cmGlobalVisualStudio10Generator::Find64BitTools(cmMakefile* mf)
  912. {
  913. if (this->DefaultPlatformToolset == "v100") {
  914. // The v100 64-bit toolset does not exist in the express edition.
  915. this->DefaultPlatformToolset.clear();
  916. }
  917. if (this->GetPlatformToolset()) {
  918. return true;
  919. }
  920. // This edition does not come with 64-bit tools. Look for them.
  921. //
  922. // TODO: Detect available tools? x64\v100 exists but does not work?
  923. // HKLM\\SOFTWARE\\Microsoft\\MSBuild\\ToolsVersions\\4.0;VCTargetsPath
  924. // c:/Program Files (x86)/MSBuild/Microsoft.Cpp/v4.0/Platforms/
  925. // {Itanium,Win32,x64}/PlatformToolsets/{v100,v90,Windows7.1SDK}
  926. std::string winSDK_7_1;
  927. if (cmSystemTools::ReadRegistryValue(
  928. "HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Microsoft SDKs\\"
  929. "Windows\\v7.1;InstallationFolder",
  930. winSDK_7_1)) {
  931. std::ostringstream m;
  932. m << "Found Windows SDK v7.1: " << winSDK_7_1;
  933. mf->DisplayStatus(m.str(), -1);
  934. this->DefaultPlatformToolset = "Windows7.1SDK";
  935. return true;
  936. } else {
  937. std::ostringstream e;
  938. /* clang-format off */
  939. e << "Cannot enable 64-bit tools with Visual Studio 2010 Express.\n"
  940. << "Install the Microsoft Windows SDK v7.1 to get 64-bit tools:\n"
  941. << " http://msdn.microsoft.com/en-us/windows/bb980924.aspx";
  942. /* clang-format on */
  943. mf->IssueMessage(MessageType::FATAL_ERROR, e.str().c_str());
  944. cmSystemTools::SetFatalErrorOccured();
  945. return false;
  946. }
  947. }
  948. std::string cmGlobalVisualStudio10Generator::GenerateRuleFile(
  949. std::string const& output) const
  950. {
  951. // The VS 10 generator needs to create the .rule files on disk.
  952. // Hide them away under the CMakeFiles directory.
  953. std::string ruleDir = this->GetCMakeInstance()->GetHomeOutputDirectory();
  954. ruleDir += "/CMakeFiles";
  955. ruleDir += "/";
  956. ruleDir += cmSystemTools::ComputeStringMD5(
  957. cmSystemTools::GetFilenamePath(output).c_str());
  958. std::string ruleFile = ruleDir + "/";
  959. ruleFile += cmSystemTools::GetFilenameName(output);
  960. ruleFile += ".rule";
  961. return ruleFile;
  962. }
  963. void cmGlobalVisualStudio10Generator::PathTooLong(cmGeneratorTarget* target,
  964. cmSourceFile const* sf,
  965. std::string const& sfRel)
  966. {
  967. size_t len =
  968. (target->GetLocalGenerator()->GetCurrentBinaryDirectory().length() + 1 +
  969. sfRel.length());
  970. if (len > this->LongestSource.Length) {
  971. this->LongestSource.Length = len;
  972. this->LongestSource.Target = target;
  973. this->LongestSource.SourceFile = sf;
  974. this->LongestSource.SourceRel = sfRel;
  975. }
  976. }
  977. std::string cmGlobalVisualStudio10Generator::Encoding()
  978. {
  979. return "utf-8";
  980. }
  981. const char* cmGlobalVisualStudio10Generator::GetToolsVersion() const
  982. {
  983. switch (this->Version) {
  984. case cmGlobalVisualStudioGenerator::VS9:
  985. case cmGlobalVisualStudioGenerator::VS10:
  986. case cmGlobalVisualStudioGenerator::VS11:
  987. return "4.0";
  988. // in Visual Studio 2013 they detached the MSBuild tools version
  989. // from the .Net Framework version and instead made it have it's own
  990. // version number
  991. case cmGlobalVisualStudioGenerator::VS12:
  992. return "12.0";
  993. case cmGlobalVisualStudioGenerator::VS14:
  994. return "14.0";
  995. case cmGlobalVisualStudioGenerator::VS15:
  996. return "15.0";
  997. case cmGlobalVisualStudioGenerator::VS16:
  998. return "16.0";
  999. }
  1000. return "";
  1001. }
  1002. bool cmGlobalVisualStudio10Generator::IsNsightTegra() const
  1003. {
  1004. return !this->NsightTegraVersion.empty();
  1005. }
  1006. std::string cmGlobalVisualStudio10Generator::GetNsightTegraVersion() const
  1007. {
  1008. return this->NsightTegraVersion;
  1009. }
  1010. std::string cmGlobalVisualStudio10Generator::GetInstalledNsightTegraVersion()
  1011. {
  1012. std::string version;
  1013. cmSystemTools::ReadRegistryValue(
  1014. "HKEY_LOCAL_MACHINE\\SOFTWARE\\NVIDIA Corporation\\Nsight Tegra;"
  1015. "Version",
  1016. version, cmSystemTools::KeyWOW64_32);
  1017. return version;
  1018. }
  1019. std::string cmGlobalVisualStudio10Generator::GetApplicationTypeRevision() const
  1020. {
  1021. // Return the first two '.'-separated components of the Windows version.
  1022. std::string::size_type end1 = this->SystemVersion.find('.');
  1023. std::string::size_type end2 =
  1024. end1 == std::string::npos ? end1 : this->SystemVersion.find('.', end1 + 1);
  1025. return this->SystemVersion.substr(0, end2);
  1026. }
  1027. static std::string cmLoadFlagTableString(Json::Value entry, const char* field)
  1028. {
  1029. if (entry.isMember(field)) {
  1030. auto string = entry[field];
  1031. if (string.isConvertibleTo(Json::ValueType::stringValue)) {
  1032. return string.asString();
  1033. }
  1034. }
  1035. return "";
  1036. }
  1037. static unsigned int cmLoadFlagTableSpecial(Json::Value entry,
  1038. const char* field)
  1039. {
  1040. unsigned int value = 0;
  1041. if (entry.isMember(field)) {
  1042. auto specials = entry[field];
  1043. if (specials.isArray()) {
  1044. for (auto const& special : specials) {
  1045. std::string s = special.asString();
  1046. if (s == "UserValue") {
  1047. value |= cmIDEFlagTable::UserValue;
  1048. } else if (s == "UserIgnored") {
  1049. value |= cmIDEFlagTable::UserIgnored;
  1050. } else if (s == "UserRequired") {
  1051. value |= cmIDEFlagTable::UserRequired;
  1052. } else if (s == "Continue") {
  1053. value |= cmIDEFlagTable::Continue;
  1054. } else if (s == "SemicolonAppendable") {
  1055. value |= cmIDEFlagTable::SemicolonAppendable;
  1056. } else if (s == "UserFollowing") {
  1057. value |= cmIDEFlagTable::UserFollowing;
  1058. } else if (s == "CaseInsensitive") {
  1059. value |= cmIDEFlagTable::CaseInsensitive;
  1060. } else if (s == "SpaceAppendable") {
  1061. value |= cmIDEFlagTable::SpaceAppendable;
  1062. } else if (s == "CommaAppendable") {
  1063. value |= cmIDEFlagTable::CommaAppendable;
  1064. }
  1065. }
  1066. }
  1067. }
  1068. return value;
  1069. }
  1070. static cmIDEFlagTable const* cmLoadFlagTableJson(
  1071. std::string const& flagJsonPath)
  1072. {
  1073. cmIDEFlagTable* ret = nullptr;
  1074. auto savedFlagIterator = loadedFlagJsonFiles.find(flagJsonPath);
  1075. if (savedFlagIterator != loadedFlagJsonFiles.end()) {
  1076. ret = savedFlagIterator->second.data();
  1077. } else {
  1078. Json::Reader reader;
  1079. cmsys::ifstream stream;
  1080. stream.open(flagJsonPath.c_str(), std::ios_base::in);
  1081. if (stream) {
  1082. Json::Value flags;
  1083. if (reader.parse(stream, flags, false) && flags.isArray()) {
  1084. std::vector<cmIDEFlagTable> flagTable;
  1085. for (auto const& flag : flags) {
  1086. cmIDEFlagTable flagEntry;
  1087. flagEntry.IDEName = cmLoadFlagTableString(flag, "name");
  1088. flagEntry.commandFlag = cmLoadFlagTableString(flag, "switch");
  1089. flagEntry.comment = cmLoadFlagTableString(flag, "comment");
  1090. flagEntry.value = cmLoadFlagTableString(flag, "value");
  1091. flagEntry.special = cmLoadFlagTableSpecial(flag, "flags");
  1092. flagTable.push_back(flagEntry);
  1093. }
  1094. cmIDEFlagTable endFlag{ "", "", "", "", 0 };
  1095. flagTable.push_back(endFlag);
  1096. loadedFlagJsonFiles[flagJsonPath] = flagTable;
  1097. ret = loadedFlagJsonFiles[flagJsonPath].data();
  1098. }
  1099. }
  1100. }
  1101. return ret;
  1102. }
  1103. static std::string cmGetFlagTableName(std::string const& toolsetName,
  1104. std::string const& table)
  1105. {
  1106. return cmSystemTools::GetCMakeRoot() + "/Templates/MSBuild/FlagTables/" +
  1107. toolsetName + "_" + table + ".json";
  1108. }
  1109. cmIDEFlagTable const* cmGlobalVisualStudio10Generator::LoadFlagTable(
  1110. std::string const& optionsName, std::string const& toolsetName,
  1111. std::string const& defaultName, std::string const& table) const
  1112. {
  1113. cmIDEFlagTable const* ret = nullptr;
  1114. std::string filename;
  1115. if (!optionsName.empty()) {
  1116. filename = cmGetFlagTableName(optionsName, table);
  1117. ret = cmLoadFlagTableJson(filename);
  1118. } else {
  1119. filename = cmGetFlagTableName(toolsetName, table);
  1120. if (cmSystemTools::FileExists(filename)) {
  1121. ret = cmLoadFlagTableJson(filename);
  1122. } else {
  1123. filename = cmGetFlagTableName(defaultName, table);
  1124. ret = cmLoadFlagTableJson(filename);
  1125. }
  1126. }
  1127. if (!ret) {
  1128. cmMakefile* mf = this->GetCurrentMakefile();
  1129. std::ostringstream e;
  1130. /* clang-format off */
  1131. e << "JSON flag table \"" << filename <<
  1132. "\" could not be loaded.\n";
  1133. /* clang-format on */
  1134. mf->IssueMessage(MessageType::FATAL_ERROR, e.str().c_str());
  1135. }
  1136. return ret;
  1137. }
  1138. cmIDEFlagTable const* cmGlobalVisualStudio10Generator::GetClFlagTable() const
  1139. {
  1140. std::string optionsName = this->ToolsetOptions.GetClFlagTableName(
  1141. this->GetPlatformName(), this->GetPlatformToolsetString());
  1142. std::string toolsetName = this->ToolsetOptions.GetToolsetName(
  1143. this->GetPlatformName(), this->GetPlatformToolsetString());
  1144. std::string defaultName = this->ToolsetOptions.GetToolsetName(
  1145. this->GetPlatformName(), this->DefaultCLFlagTableName);
  1146. return LoadFlagTable(optionsName, toolsetName, defaultName, "CL");
  1147. }
  1148. cmIDEFlagTable const* cmGlobalVisualStudio10Generator::GetCSharpFlagTable()
  1149. const
  1150. {
  1151. std::string optionsName = this->ToolsetOptions.GetCSharpFlagTableName(
  1152. this->GetPlatformName(), this->GetPlatformToolsetString());
  1153. std::string toolsetName = this->ToolsetOptions.GetToolsetName(
  1154. this->GetPlatformName(), this->GetPlatformToolsetString());
  1155. std::string defaultName = this->ToolsetOptions.GetToolsetName(
  1156. this->GetPlatformName(), this->DefaultCSharpFlagTableName);
  1157. return LoadFlagTable(optionsName, toolsetName, defaultName, "CSharp");
  1158. }
  1159. cmIDEFlagTable const* cmGlobalVisualStudio10Generator::GetRcFlagTable() const
  1160. {
  1161. std::string optionsName = this->ToolsetOptions.GetRcFlagTableName(
  1162. this->GetPlatformName(), this->GetPlatformToolsetString());
  1163. std::string toolsetName = this->ToolsetOptions.GetToolsetName(
  1164. this->GetPlatformName(), this->GetPlatformToolsetString());
  1165. std::string defaultName = this->ToolsetOptions.GetToolsetName(
  1166. this->GetPlatformName(), this->DefaultRCFlagTableName);
  1167. return LoadFlagTable(optionsName, toolsetName, defaultName, "RC");
  1168. }
  1169. cmIDEFlagTable const* cmGlobalVisualStudio10Generator::GetLibFlagTable() const
  1170. {
  1171. std::string optionsName = this->ToolsetOptions.GetLibFlagTableName(
  1172. this->GetPlatformName(), this->GetPlatformToolsetString());
  1173. std::string toolsetName = this->ToolsetOptions.GetToolsetName(
  1174. this->GetPlatformName(), this->GetPlatformToolsetString());
  1175. std::string defaultName = this->ToolsetOptions.GetToolsetName(
  1176. this->GetPlatformName(), this->DefaultLibFlagTableName);
  1177. return LoadFlagTable(optionsName, toolsetName, defaultName, "LIB");
  1178. }
  1179. cmIDEFlagTable const* cmGlobalVisualStudio10Generator::GetLinkFlagTable() const
  1180. {
  1181. std::string optionsName = this->ToolsetOptions.GetLinkFlagTableName(
  1182. this->GetPlatformName(), this->GetPlatformToolsetString());
  1183. std::string toolsetName = this->ToolsetOptions.GetToolsetName(
  1184. this->GetPlatformName(), this->GetPlatformToolsetString());
  1185. std::string defaultName = this->ToolsetOptions.GetToolsetName(
  1186. this->GetPlatformName(), this->DefaultLinkFlagTableName);
  1187. return LoadFlagTable(optionsName, toolsetName, defaultName, "Link");
  1188. }
  1189. cmIDEFlagTable const* cmGlobalVisualStudio10Generator::GetCudaFlagTable() const
  1190. {
  1191. std::string toolsetName = this->ToolsetOptions.GetToolsetName(
  1192. this->GetPlatformName(), this->GetPlatformToolsetString());
  1193. std::string defaultName = this->ToolsetOptions.GetToolsetName(
  1194. this->GetPlatformName(), this->DefaultCudaFlagTableName);
  1195. return LoadFlagTable("", toolsetName, defaultName, "Cuda");
  1196. }
  1197. cmIDEFlagTable const* cmGlobalVisualStudio10Generator::GetCudaHostFlagTable()
  1198. const
  1199. {
  1200. std::string toolsetName = this->ToolsetOptions.GetToolsetName(
  1201. this->GetPlatformName(), this->GetPlatformToolsetString());
  1202. std::string defaultName = this->ToolsetOptions.GetToolsetName(
  1203. this->GetPlatformName(), this->DefaultCudaHostFlagTableName);
  1204. return LoadFlagTable("", toolsetName, defaultName, "CudaHost");
  1205. }
  1206. cmIDEFlagTable const* cmGlobalVisualStudio10Generator::GetMasmFlagTable() const
  1207. {
  1208. std::string optionsName = this->ToolsetOptions.GetMasmFlagTableName(
  1209. this->GetPlatformName(), this->GetPlatformToolsetString());
  1210. std::string toolsetName = this->ToolsetOptions.GetToolsetName(
  1211. this->GetPlatformName(), this->GetPlatformToolsetString());
  1212. std::string defaultName = this->ToolsetOptions.GetToolsetName(
  1213. this->GetPlatformName(), this->DefaultMasmFlagTableName);
  1214. return LoadFlagTable(optionsName, toolsetName, defaultName, "MASM");
  1215. }
  1216. cmIDEFlagTable const* cmGlobalVisualStudio10Generator::GetNasmFlagTable() const
  1217. {
  1218. std::string toolsetName = this->ToolsetOptions.GetToolsetName(
  1219. this->GetPlatformName(), this->GetPlatformToolsetString());
  1220. std::string defaultName = this->ToolsetOptions.GetToolsetName(
  1221. this->GetPlatformName(), this->DefaultNasmFlagTableName);
  1222. return LoadFlagTable("", toolsetName, defaultName, "NASM");
  1223. }