cmGlobalVisualStudio10Generator.cxx 44 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338
  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 = cmSystemTools::tokenize(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.c_str());
  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",
  601. this->GetMSBuildCommand().c_str());
  602. return true;
  603. }
  604. std::string const& cmGlobalVisualStudio10Generator::GetMSBuildCommand()
  605. {
  606. if (!this->MSBuildCommandInitialized) {
  607. this->MSBuildCommandInitialized = true;
  608. this->MSBuildCommand = this->FindMSBuildCommand();
  609. }
  610. return this->MSBuildCommand;
  611. }
  612. std::string cmGlobalVisualStudio10Generator::FindMSBuildCommand()
  613. {
  614. std::string msbuild;
  615. std::string mskey;
  616. // Search in standard location.
  617. mskey = "HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\MSBuild\\ToolsVersions\\";
  618. mskey += this->GetToolsVersion();
  619. mskey += ";MSBuildToolsPath";
  620. if (cmSystemTools::ReadRegistryValue(mskey.c_str(), msbuild,
  621. cmSystemTools::KeyWOW64_32)) {
  622. cmSystemTools::ConvertToUnixSlashes(msbuild);
  623. msbuild += "/MSBuild.exe";
  624. if (cmSystemTools::FileExists(msbuild, true)) {
  625. return msbuild;
  626. }
  627. }
  628. msbuild = "MSBuild.exe";
  629. return msbuild;
  630. }
  631. std::string cmGlobalVisualStudio10Generator::FindDevEnvCommand()
  632. {
  633. if (this->ExpressEdition) {
  634. // Visual Studio Express >= 10 do not have "devenv.com" or
  635. // "VCExpress.exe" that we can use to build reliably.
  636. // Tell the caller it needs to use MSBuild instead.
  637. return "";
  638. }
  639. // Skip over the cmGlobalVisualStudio8Generator implementation because
  640. // we expect a real devenv and do not want to look for VCExpress.
  641. return this->cmGlobalVisualStudio71Generator::FindDevEnvCommand();
  642. }
  643. bool cmGlobalVisualStudio10Generator::FindVCTargetsPath(cmMakefile* mf)
  644. {
  645. // Skip this in special cases within our own test suite.
  646. if (this->GetPlatformName() == "Test Platform" ||
  647. this->GetPlatformToolsetString() == "Test Toolset") {
  648. return true;
  649. }
  650. std::string wd;
  651. if (!this->ConfiguredFilesPath.empty()) {
  652. // In a try-compile we are given the outer CMakeFiles directory.
  653. wd = this->ConfiguredFilesPath;
  654. } else {
  655. wd = this->GetCMakeInstance()->GetHomeOutputDirectory();
  656. wd += "/CMakeFiles";
  657. }
  658. wd += "/";
  659. wd += cmVersion::GetCMakeVersion();
  660. // We record the result persistently in a file.
  661. std::string const txt = wd + "/VCTargetsPath.txt";
  662. // If we have a recorded result, use it.
  663. {
  664. cmsys::ifstream fin(txt.c_str());
  665. if (fin && cmSystemTools::GetLineFromStream(fin, this->VCTargetsPath) &&
  666. cmSystemTools::FileIsDirectory(this->VCTargetsPath)) {
  667. cmSystemTools::ConvertToUnixSlashes(this->VCTargetsPath);
  668. return true;
  669. }
  670. }
  671. // Prepare the work directory.
  672. if (!cmSystemTools::MakeDirectory(wd)) {
  673. std::string e = "Failed to make directory:\n " + wd;
  674. mf->IssueMessage(MessageType::FATAL_ERROR, e.c_str());
  675. cmSystemTools::SetFatalErrorOccured();
  676. return false;
  677. }
  678. // Generate a project file for MSBuild to tell us the VCTargetsPath value.
  679. std::string const vcxproj = "VCTargetsPath.vcxproj";
  680. {
  681. std::string const vcxprojAbs = wd + "/" + vcxproj;
  682. cmsys::ofstream fout(vcxprojAbs.c_str());
  683. cmXMLWriter xw(fout);
  684. cmXMLDocument doc(xw);
  685. cmXMLElement eprj(doc, "Project");
  686. eprj.Attribute("DefaultTargets", "Build");
  687. eprj.Attribute("ToolsVersion", "4.0");
  688. eprj.Attribute("xmlns",
  689. "http://schemas.microsoft.com/developer/msbuild/2003");
  690. if (this->IsNsightTegra()) {
  691. cmXMLElement epg(eprj, "PropertyGroup");
  692. epg.Attribute("Label", "NsightTegraProject");
  693. cmXMLElement(epg, "NsightTegraProjectRevisionNumber").Content("6");
  694. }
  695. {
  696. cmXMLElement eig(eprj, "ItemGroup");
  697. eig.Attribute("Label", "ProjectConfigurations");
  698. cmXMLElement epc(eig, "ProjectConfiguration");
  699. epc.Attribute("Include", "Debug|" + this->GetPlatformName());
  700. cmXMLElement(epc, "Configuration").Content("Debug");
  701. cmXMLElement(epc, "Platform").Content(this->GetPlatformName());
  702. }
  703. {
  704. cmXMLElement epg(eprj, "PropertyGroup");
  705. epg.Attribute("Label", "Globals");
  706. cmXMLElement(epg, "ProjectGuid")
  707. .Content("{F3FC6D86-508D-3FB1-96D2-995F08B142EC}");
  708. cmXMLElement(epg, "Keyword").Content("Win32Proj");
  709. cmXMLElement(epg, "Platform").Content(this->GetPlatformName());
  710. if (this->GetSystemName() == "WindowsPhone") {
  711. cmXMLElement(epg, "ApplicationType").Content("Windows Phone");
  712. cmXMLElement(epg, "ApplicationTypeRevision")
  713. .Content(this->GetSystemVersion());
  714. } else if (this->GetSystemName() == "WindowsStore") {
  715. cmXMLElement(epg, "ApplicationType").Content("Windows Store");
  716. cmXMLElement(epg, "ApplicationTypeRevision")
  717. .Content(this->GetSystemVersion());
  718. }
  719. if (!this->WindowsTargetPlatformVersion.empty()) {
  720. cmXMLElement(epg, "WindowsTargetPlatformVersion")
  721. .Content(this->WindowsTargetPlatformVersion);
  722. }
  723. if (this->GetPlatformName() == "ARM64") {
  724. cmXMLElement(epg, "WindowsSDKDesktopARM64Support").Content("true");
  725. } else if (this->GetPlatformName() == "ARM") {
  726. cmXMLElement(epg, "WindowsSDKDesktopARMSupport").Content("true");
  727. }
  728. }
  729. cmXMLElement(eprj, "Import")
  730. .Attribute("Project", "$(VCTargetsPath)\\Microsoft.Cpp.Default.props");
  731. if (const char* hostArch = this->GetPlatformToolsetHostArchitecture()) {
  732. cmXMLElement epg(eprj, "PropertyGroup");
  733. cmXMLElement(epg, "PreferredToolArchitecture").Content(hostArch);
  734. }
  735. {
  736. cmXMLElement epg(eprj, "PropertyGroup");
  737. epg.Attribute("Label", "Configuration");
  738. {
  739. cmXMLElement ect(epg, "ConfigurationType");
  740. if (this->IsNsightTegra()) {
  741. // Tegra-Android platform does not understand "Utility".
  742. ect.Content("StaticLibrary");
  743. } else {
  744. ect.Content("Utility");
  745. }
  746. }
  747. cmXMLElement(epg, "CharacterSet").Content("MultiByte");
  748. if (this->IsNsightTegra()) {
  749. cmXMLElement(epg, "NdkToolchainVersion")
  750. .Content(this->GetPlatformToolsetString());
  751. } else {
  752. cmXMLElement(epg, "PlatformToolset")
  753. .Content(this->GetPlatformToolsetString());
  754. }
  755. }
  756. cmXMLElement(eprj, "Import")
  757. .Attribute("Project", "$(VCTargetsPath)\\Microsoft.Cpp.props");
  758. {
  759. cmXMLElement eidg(eprj, "ItemDefinitionGroup");
  760. cmXMLElement epbe(eidg, "PostBuildEvent");
  761. cmXMLElement(epbe, "Command")
  762. .Content("echo VCTargetsPath=$(VCTargetsPath)");
  763. }
  764. cmXMLElement(eprj, "Import")
  765. .Attribute("Project", "$(VCTargetsPath)\\Microsoft.Cpp.targets");
  766. }
  767. std::vector<std::string> cmd;
  768. cmd.push_back(this->GetMSBuildCommand());
  769. cmd.push_back(vcxproj);
  770. cmd.push_back("/p:Configuration=Debug");
  771. cmd.push_back(std::string("/p:VisualStudioVersion=") +
  772. this->GetIDEVersion());
  773. std::string out;
  774. int ret = 0;
  775. cmsys::RegularExpression regex("\n *VCTargetsPath=([^%\r\n]+)[\r\n]");
  776. if (!cmSystemTools::RunSingleCommand(cmd, &out, &out, &ret, wd.c_str(),
  777. cmSystemTools::OUTPUT_NONE) ||
  778. ret != 0 || !regex.find(out)) {
  779. cmSystemTools::ReplaceString(out, "\n", "\n ");
  780. std::ostringstream e;
  781. /* clang-format off */
  782. e <<
  783. "Failed to run MSBuild command:\n"
  784. " " << cmd[0] << "\n"
  785. "to get the value of VCTargetsPath:\n"
  786. " " << out << "\n"
  787. ;
  788. /* clang-format on */
  789. if (ret != 0) {
  790. e << "Exit code: " << ret << "\n";
  791. }
  792. mf->IssueMessage(MessageType::FATAL_ERROR, e.str().c_str());
  793. cmSystemTools::SetFatalErrorOccured();
  794. return false;
  795. }
  796. this->VCTargetsPath = regex.match(1);
  797. cmSystemTools::ConvertToUnixSlashes(this->VCTargetsPath);
  798. {
  799. cmsys::ofstream fout(txt.c_str());
  800. fout << this->VCTargetsPath << "\n";
  801. }
  802. return true;
  803. }
  804. std::vector<cmGlobalGenerator::GeneratedMakeCommand>
  805. cmGlobalVisualStudio10Generator::GenerateBuildCommand(
  806. const std::string& makeProgram, const std::string& projectName,
  807. const std::string& projectDir, std::vector<std::string> const& targetNames,
  808. const std::string& config, bool fast, int jobs, bool verbose,
  809. std::vector<std::string> const& makeOptions)
  810. {
  811. std::vector<GeneratedMakeCommand> makeCommands;
  812. // Select the caller- or user-preferred make program, else MSBuild.
  813. std::string makeProgramSelected =
  814. this->SelectMakeProgram(makeProgram, this->GetMSBuildCommand());
  815. // Check if the caller explicitly requested a devenv tool.
  816. std::string makeProgramLower = makeProgramSelected;
  817. cmSystemTools::LowerCase(makeProgramLower);
  818. bool useDevEnv = (makeProgramLower.find("devenv") != std::string::npos ||
  819. makeProgramLower.find("vcexpress") != std::string::npos);
  820. // Workaround to convince VCExpress.exe to produce output.
  821. const bool requiresOutputForward =
  822. (makeProgramLower.find("vcexpress") != std::string::npos);
  823. // MSBuild is preferred (and required for VS Express), but if the .sln has
  824. // an Intel Fortran .vfproj then we have to use devenv. Parse it to find out.
  825. cmSlnData slnData;
  826. {
  827. std::string slnFile;
  828. if (!projectDir.empty()) {
  829. slnFile = projectDir;
  830. slnFile += "/";
  831. }
  832. slnFile += projectName;
  833. slnFile += ".sln";
  834. cmVisualStudioSlnParser parser;
  835. if (parser.ParseFile(slnFile, slnData,
  836. cmVisualStudioSlnParser::DataGroupProjects)) {
  837. std::vector<cmSlnProjectEntry> slnProjects = slnData.GetProjects();
  838. for (cmSlnProjectEntry const& project : slnProjects) {
  839. if (useDevEnv) {
  840. break;
  841. }
  842. std::string proj = project.GetRelativePath();
  843. if (proj.size() > 7 && proj.substr(proj.size() - 7) == ".vfproj") {
  844. useDevEnv = true;
  845. }
  846. }
  847. }
  848. }
  849. if (useDevEnv) {
  850. // Use devenv to build solutions containing Intel Fortran projects.
  851. return cmGlobalVisualStudio7Generator::GenerateBuildCommand(
  852. makeProgram, projectName, projectDir, targetNames, config, fast, jobs,
  853. verbose, makeOptions);
  854. }
  855. std::vector<std::string> realTargetNames = targetNames;
  856. if (targetNames.empty() ||
  857. ((targetNames.size() == 1) && targetNames.front().empty())) {
  858. realTargetNames = { "ALL_BUILD" };
  859. }
  860. for (const auto& tname : realTargetNames) {
  861. // msbuild.exe CxxOnly.sln /t:Build /p:Configuration=Debug
  862. // /target:ALL_BUILD
  863. // /m
  864. if (tname.empty()) {
  865. continue;
  866. }
  867. GeneratedMakeCommand makeCommand;
  868. makeCommand.RequiresOutputForward = requiresOutputForward;
  869. makeCommand.Add(makeProgramSelected);
  870. if (tname == "clean") {
  871. makeCommand.Add(std::string(projectName) + ".sln");
  872. makeCommand.Add("/t:Clean");
  873. } else {
  874. std::string targetProject(tname);
  875. targetProject += ".vcxproj";
  876. if (targetProject.find('/') == std::string::npos) {
  877. // it might be in a subdir
  878. if (cmSlnProjectEntry const* proj = slnData.GetProjectByName(tname)) {
  879. targetProject = proj->GetRelativePath();
  880. cmSystemTools::ConvertToUnixSlashes(targetProject);
  881. }
  882. }
  883. makeCommand.Add(std::move(targetProject));
  884. }
  885. std::string configArg = "/p:Configuration=";
  886. if (!config.empty()) {
  887. configArg += config;
  888. } else {
  889. configArg += "Debug";
  890. }
  891. makeCommand.Add(configArg);
  892. makeCommand.Add(std::string("/p:Platform=") + this->GetPlatformName());
  893. makeCommand.Add(std::string("/p:VisualStudioVersion=") +
  894. this->GetIDEVersion());
  895. if (jobs != cmake::NO_BUILD_PARALLEL_LEVEL) {
  896. if (jobs == cmake::DEFAULT_BUILD_PARALLEL_LEVEL) {
  897. makeCommand.Add("/m");
  898. } else {
  899. makeCommand.Add(std::string("/m:") + std::to_string(jobs));
  900. }
  901. // Having msbuild.exe and cl.exe using multiple jobs is discouraged
  902. makeCommand.Add("/p:CL_MPCount=1");
  903. }
  904. // Respect the verbosity: 'n' normal will show build commands
  905. // 'm' minimal only the build step's title
  906. makeCommand.Add(std::string("/v:") + ((verbose) ? "n" : "m"));
  907. makeCommand.Add(makeOptions.begin(), makeOptions.end());
  908. makeCommands.emplace_back(std::move(makeCommand));
  909. }
  910. return makeCommands;
  911. }
  912. bool cmGlobalVisualStudio10Generator::Find64BitTools(cmMakefile* mf)
  913. {
  914. if (this->DefaultPlatformToolset == "v100") {
  915. // The v100 64-bit toolset does not exist in the express edition.
  916. this->DefaultPlatformToolset.clear();
  917. }
  918. if (this->GetPlatformToolset()) {
  919. return true;
  920. }
  921. // This edition does not come with 64-bit tools. Look for them.
  922. //
  923. // TODO: Detect available tools? x64\v100 exists but does not work?
  924. // HKLM\\SOFTWARE\\Microsoft\\MSBuild\\ToolsVersions\\4.0;VCTargetsPath
  925. // c:/Program Files (x86)/MSBuild/Microsoft.Cpp/v4.0/Platforms/
  926. // {Itanium,Win32,x64}/PlatformToolsets/{v100,v90,Windows7.1SDK}
  927. std::string winSDK_7_1;
  928. if (cmSystemTools::ReadRegistryValue(
  929. "HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Microsoft SDKs\\"
  930. "Windows\\v7.1;InstallationFolder",
  931. winSDK_7_1)) {
  932. std::ostringstream m;
  933. m << "Found Windows SDK v7.1: " << winSDK_7_1;
  934. mf->DisplayStatus(m.str(), -1);
  935. this->DefaultPlatformToolset = "Windows7.1SDK";
  936. return true;
  937. } else {
  938. std::ostringstream e;
  939. /* clang-format off */
  940. e << "Cannot enable 64-bit tools with Visual Studio 2010 Express.\n"
  941. << "Install the Microsoft Windows SDK v7.1 to get 64-bit tools:\n"
  942. << " http://msdn.microsoft.com/en-us/windows/bb980924.aspx";
  943. /* clang-format on */
  944. mf->IssueMessage(MessageType::FATAL_ERROR, e.str().c_str());
  945. cmSystemTools::SetFatalErrorOccured();
  946. return false;
  947. }
  948. }
  949. std::string cmGlobalVisualStudio10Generator::GenerateRuleFile(
  950. std::string const& output) const
  951. {
  952. // The VS 10 generator needs to create the .rule files on disk.
  953. // Hide them away under the CMakeFiles directory.
  954. std::string ruleDir = this->GetCMakeInstance()->GetHomeOutputDirectory();
  955. ruleDir += "/CMakeFiles";
  956. ruleDir += "/";
  957. ruleDir += cmSystemTools::ComputeStringMD5(
  958. cmSystemTools::GetFilenamePath(output).c_str());
  959. std::string ruleFile = ruleDir + "/";
  960. ruleFile += cmSystemTools::GetFilenameName(output);
  961. ruleFile += ".rule";
  962. return ruleFile;
  963. }
  964. void cmGlobalVisualStudio10Generator::PathTooLong(cmGeneratorTarget* target,
  965. cmSourceFile const* sf,
  966. std::string const& sfRel)
  967. {
  968. size_t len =
  969. (target->GetLocalGenerator()->GetCurrentBinaryDirectory().length() + 1 +
  970. sfRel.length());
  971. if (len > this->LongestSource.Length) {
  972. this->LongestSource.Length = len;
  973. this->LongestSource.Target = target;
  974. this->LongestSource.SourceFile = sf;
  975. this->LongestSource.SourceRel = sfRel;
  976. }
  977. }
  978. std::string cmGlobalVisualStudio10Generator::Encoding()
  979. {
  980. return "utf-8";
  981. }
  982. const char* cmGlobalVisualStudio10Generator::GetToolsVersion() const
  983. {
  984. switch (this->Version) {
  985. case cmGlobalVisualStudioGenerator::VS9:
  986. case cmGlobalVisualStudioGenerator::VS10:
  987. case cmGlobalVisualStudioGenerator::VS11:
  988. return "4.0";
  989. // in Visual Studio 2013 they detached the MSBuild tools version
  990. // from the .Net Framework version and instead made it have it's own
  991. // version number
  992. case cmGlobalVisualStudioGenerator::VS12:
  993. return "12.0";
  994. case cmGlobalVisualStudioGenerator::VS14:
  995. return "14.0";
  996. case cmGlobalVisualStudioGenerator::VS15:
  997. return "15.0";
  998. case cmGlobalVisualStudioGenerator::VS16:
  999. return "16.0";
  1000. }
  1001. return "";
  1002. }
  1003. bool cmGlobalVisualStudio10Generator::IsNsightTegra() const
  1004. {
  1005. return !this->NsightTegraVersion.empty();
  1006. }
  1007. std::string cmGlobalVisualStudio10Generator::GetNsightTegraVersion() const
  1008. {
  1009. return this->NsightTegraVersion;
  1010. }
  1011. std::string cmGlobalVisualStudio10Generator::GetInstalledNsightTegraVersion()
  1012. {
  1013. std::string version;
  1014. cmSystemTools::ReadRegistryValue(
  1015. "HKEY_LOCAL_MACHINE\\SOFTWARE\\NVIDIA Corporation\\Nsight Tegra;"
  1016. "Version",
  1017. version, cmSystemTools::KeyWOW64_32);
  1018. return version;
  1019. }
  1020. std::string cmGlobalVisualStudio10Generator::GetApplicationTypeRevision() const
  1021. {
  1022. // Return the first two '.'-separated components of the Windows version.
  1023. std::string::size_type end1 = this->SystemVersion.find('.');
  1024. std::string::size_type end2 =
  1025. end1 == std::string::npos ? end1 : this->SystemVersion.find('.', end1 + 1);
  1026. return this->SystemVersion.substr(0, end2);
  1027. }
  1028. static std::string cmLoadFlagTableString(Json::Value entry, const char* field)
  1029. {
  1030. if (entry.isMember(field)) {
  1031. auto string = entry[field];
  1032. if (string.isConvertibleTo(Json::ValueType::stringValue)) {
  1033. return string.asString();
  1034. }
  1035. }
  1036. return "";
  1037. }
  1038. static unsigned int cmLoadFlagTableSpecial(Json::Value entry,
  1039. const char* field)
  1040. {
  1041. unsigned int value = 0;
  1042. if (entry.isMember(field)) {
  1043. auto specials = entry[field];
  1044. if (specials.isArray()) {
  1045. for (auto const& special : specials) {
  1046. std::string s = special.asString();
  1047. if (s == "UserValue") {
  1048. value |= cmIDEFlagTable::UserValue;
  1049. } else if (s == "UserIgnored") {
  1050. value |= cmIDEFlagTable::UserIgnored;
  1051. } else if (s == "UserRequired") {
  1052. value |= cmIDEFlagTable::UserRequired;
  1053. } else if (s == "Continue") {
  1054. value |= cmIDEFlagTable::Continue;
  1055. } else if (s == "SemicolonAppendable") {
  1056. value |= cmIDEFlagTable::SemicolonAppendable;
  1057. } else if (s == "UserFollowing") {
  1058. value |= cmIDEFlagTable::UserFollowing;
  1059. } else if (s == "CaseInsensitive") {
  1060. value |= cmIDEFlagTable::CaseInsensitive;
  1061. } else if (s == "SpaceAppendable") {
  1062. value |= cmIDEFlagTable::SpaceAppendable;
  1063. } else if (s == "CommaAppendable") {
  1064. value |= cmIDEFlagTable::CommaAppendable;
  1065. }
  1066. }
  1067. }
  1068. }
  1069. return value;
  1070. }
  1071. static cmIDEFlagTable const* cmLoadFlagTableJson(
  1072. std::string const& flagJsonPath)
  1073. {
  1074. cmIDEFlagTable* ret = nullptr;
  1075. auto savedFlagIterator = loadedFlagJsonFiles.find(flagJsonPath);
  1076. if (savedFlagIterator != loadedFlagJsonFiles.end()) {
  1077. ret = savedFlagIterator->second.data();
  1078. } else {
  1079. Json::Reader reader;
  1080. cmsys::ifstream stream;
  1081. stream.open(flagJsonPath.c_str(), std::ios_base::in);
  1082. if (stream) {
  1083. Json::Value flags;
  1084. if (reader.parse(stream, flags, false) && flags.isArray()) {
  1085. std::vector<cmIDEFlagTable> flagTable;
  1086. for (auto const& flag : flags) {
  1087. cmIDEFlagTable flagEntry;
  1088. flagEntry.IDEName = cmLoadFlagTableString(flag, "name");
  1089. flagEntry.commandFlag = cmLoadFlagTableString(flag, "switch");
  1090. flagEntry.comment = cmLoadFlagTableString(flag, "comment");
  1091. flagEntry.value = cmLoadFlagTableString(flag, "value");
  1092. flagEntry.special = cmLoadFlagTableSpecial(flag, "flags");
  1093. flagTable.push_back(flagEntry);
  1094. }
  1095. cmIDEFlagTable endFlag{ "", "", "", "", 0 };
  1096. flagTable.push_back(endFlag);
  1097. loadedFlagJsonFiles[flagJsonPath] = flagTable;
  1098. ret = loadedFlagJsonFiles[flagJsonPath].data();
  1099. }
  1100. }
  1101. }
  1102. return ret;
  1103. }
  1104. static std::string cmGetFlagTableName(std::string const& toolsetName,
  1105. std::string const& table)
  1106. {
  1107. return cmSystemTools::GetCMakeRoot() + "/Templates/MSBuild/FlagTables/" +
  1108. toolsetName + "_" + table + ".json";
  1109. }
  1110. cmIDEFlagTable const* cmGlobalVisualStudio10Generator::LoadFlagTable(
  1111. std::string const& optionsName, std::string const& toolsetName,
  1112. std::string const& defaultName, std::string const& table) const
  1113. {
  1114. cmIDEFlagTable const* ret = nullptr;
  1115. std::string filename;
  1116. if (!optionsName.empty()) {
  1117. filename = cmGetFlagTableName(optionsName, table);
  1118. ret = cmLoadFlagTableJson(filename);
  1119. } else {
  1120. filename = cmGetFlagTableName(toolsetName, table);
  1121. if (cmSystemTools::FileExists(filename)) {
  1122. ret = cmLoadFlagTableJson(filename);
  1123. } else {
  1124. filename = cmGetFlagTableName(defaultName, table);
  1125. ret = cmLoadFlagTableJson(filename);
  1126. }
  1127. }
  1128. if (!ret) {
  1129. cmMakefile* mf = this->GetCurrentMakefile();
  1130. std::ostringstream e;
  1131. /* clang-format off */
  1132. e << "JSON flag table \"" << filename <<
  1133. "\" could not be loaded.\n";
  1134. /* clang-format on */
  1135. mf->IssueMessage(MessageType::FATAL_ERROR, e.str().c_str());
  1136. }
  1137. return ret;
  1138. }
  1139. cmIDEFlagTable const* cmGlobalVisualStudio10Generator::GetClFlagTable() const
  1140. {
  1141. std::string optionsName = this->ToolsetOptions.GetClFlagTableName(
  1142. this->GetPlatformName(), this->GetPlatformToolsetString());
  1143. std::string toolsetName = this->ToolsetOptions.GetToolsetName(
  1144. this->GetPlatformName(), this->GetPlatformToolsetString());
  1145. std::string defaultName = this->ToolsetOptions.GetToolsetName(
  1146. this->GetPlatformName(), this->DefaultCLFlagTableName);
  1147. return LoadFlagTable(optionsName, toolsetName, defaultName, "CL");
  1148. }
  1149. cmIDEFlagTable const* cmGlobalVisualStudio10Generator::GetCSharpFlagTable()
  1150. const
  1151. {
  1152. std::string optionsName = this->ToolsetOptions.GetCSharpFlagTableName(
  1153. this->GetPlatformName(), this->GetPlatformToolsetString());
  1154. std::string toolsetName = this->ToolsetOptions.GetToolsetName(
  1155. this->GetPlatformName(), this->GetPlatformToolsetString());
  1156. std::string defaultName = this->ToolsetOptions.GetToolsetName(
  1157. this->GetPlatformName(), this->DefaultCSharpFlagTableName);
  1158. return LoadFlagTable(optionsName, toolsetName, defaultName, "CSharp");
  1159. }
  1160. cmIDEFlagTable const* cmGlobalVisualStudio10Generator::GetRcFlagTable() const
  1161. {
  1162. std::string optionsName = this->ToolsetOptions.GetRcFlagTableName(
  1163. this->GetPlatformName(), this->GetPlatformToolsetString());
  1164. std::string toolsetName = this->ToolsetOptions.GetToolsetName(
  1165. this->GetPlatformName(), this->GetPlatformToolsetString());
  1166. std::string defaultName = this->ToolsetOptions.GetToolsetName(
  1167. this->GetPlatformName(), this->DefaultRCFlagTableName);
  1168. return LoadFlagTable(optionsName, toolsetName, defaultName, "RC");
  1169. }
  1170. cmIDEFlagTable const* cmGlobalVisualStudio10Generator::GetLibFlagTable() const
  1171. {
  1172. std::string optionsName = this->ToolsetOptions.GetLibFlagTableName(
  1173. this->GetPlatformName(), this->GetPlatformToolsetString());
  1174. std::string toolsetName = this->ToolsetOptions.GetToolsetName(
  1175. this->GetPlatformName(), this->GetPlatformToolsetString());
  1176. std::string defaultName = this->ToolsetOptions.GetToolsetName(
  1177. this->GetPlatformName(), this->DefaultLibFlagTableName);
  1178. return LoadFlagTable(optionsName, toolsetName, defaultName, "LIB");
  1179. }
  1180. cmIDEFlagTable const* cmGlobalVisualStudio10Generator::GetLinkFlagTable() const
  1181. {
  1182. std::string optionsName = this->ToolsetOptions.GetLinkFlagTableName(
  1183. this->GetPlatformName(), this->GetPlatformToolsetString());
  1184. std::string toolsetName = this->ToolsetOptions.GetToolsetName(
  1185. this->GetPlatformName(), this->GetPlatformToolsetString());
  1186. std::string defaultName = this->ToolsetOptions.GetToolsetName(
  1187. this->GetPlatformName(), this->DefaultLinkFlagTableName);
  1188. return LoadFlagTable(optionsName, toolsetName, defaultName, "Link");
  1189. }
  1190. cmIDEFlagTable const* cmGlobalVisualStudio10Generator::GetCudaFlagTable() const
  1191. {
  1192. std::string toolsetName = this->ToolsetOptions.GetToolsetName(
  1193. this->GetPlatformName(), this->GetPlatformToolsetString());
  1194. std::string defaultName = this->ToolsetOptions.GetToolsetName(
  1195. this->GetPlatformName(), this->DefaultCudaFlagTableName);
  1196. return LoadFlagTable("", toolsetName, defaultName, "Cuda");
  1197. }
  1198. cmIDEFlagTable const* cmGlobalVisualStudio10Generator::GetCudaHostFlagTable()
  1199. const
  1200. {
  1201. std::string toolsetName = this->ToolsetOptions.GetToolsetName(
  1202. this->GetPlatformName(), this->GetPlatformToolsetString());
  1203. std::string defaultName = this->ToolsetOptions.GetToolsetName(
  1204. this->GetPlatformName(), this->DefaultCudaHostFlagTableName);
  1205. return LoadFlagTable("", toolsetName, defaultName, "CudaHost");
  1206. }
  1207. cmIDEFlagTable const* cmGlobalVisualStudio10Generator::GetMasmFlagTable() const
  1208. {
  1209. std::string optionsName = this->ToolsetOptions.GetMasmFlagTableName(
  1210. this->GetPlatformName(), this->GetPlatformToolsetString());
  1211. std::string toolsetName = this->ToolsetOptions.GetToolsetName(
  1212. this->GetPlatformName(), this->GetPlatformToolsetString());
  1213. std::string defaultName = this->ToolsetOptions.GetToolsetName(
  1214. this->GetPlatformName(), this->DefaultMasmFlagTableName);
  1215. return LoadFlagTable(optionsName, toolsetName, defaultName, "MASM");
  1216. }
  1217. cmIDEFlagTable const* cmGlobalVisualStudio10Generator::GetNasmFlagTable() const
  1218. {
  1219. std::string toolsetName = this->ToolsetOptions.GetToolsetName(
  1220. this->GetPlatformName(), this->GetPlatformToolsetString());
  1221. std::string defaultName = this->ToolsetOptions.GetToolsetName(
  1222. this->GetPlatformName(), this->DefaultNasmFlagTableName);
  1223. return LoadFlagTable("", toolsetName, defaultName, "NASM");
  1224. }