cmGlobalVisualStudio10Generator.cxx 54 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690
  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 <algorithm>
  5. #include <cstring>
  6. #include <map>
  7. #include <sstream>
  8. #include <utility>
  9. #include <cm/memory>
  10. #include <cmext/string_view>
  11. #include <cm3p/json/reader.h>
  12. #include <cm3p/json/value.h>
  13. #include "cmsys/FStream.hxx"
  14. #include "cmsys/Glob.hxx"
  15. #include "cmsys/RegularExpression.hxx"
  16. #include "cmDocumentationEntry.h"
  17. #include "cmGeneratorTarget.h"
  18. #include "cmGlobalGenerator.h"
  19. #include "cmGlobalVisualStudio71Generator.h"
  20. #include "cmGlobalVisualStudio7Generator.h"
  21. #include "cmGlobalVisualStudioGenerator.h"
  22. #include "cmIDEFlagTable.h"
  23. #include "cmLocalGenerator.h"
  24. #include "cmLocalVisualStudio10Generator.h"
  25. #include "cmMakefile.h"
  26. #include "cmMessageType.h"
  27. #include "cmSourceFile.h"
  28. #include "cmStringAlgorithms.h"
  29. #include "cmSystemTools.h"
  30. #include "cmVersion.h"
  31. #include "cmVisualStudioSlnData.h"
  32. #include "cmVisualStudioSlnParser.h"
  33. #include "cmXMLWriter.h"
  34. #include "cmake.h"
  35. static std::map<std::string, std::vector<cmIDEFlagTable>> loadedFlagJsonFiles;
  36. static void ConvertToWindowsSlashes(std::string& s)
  37. {
  38. // first convert all of the slashes
  39. for (auto& ch : s) {
  40. if (ch == '/') {
  41. ch = '\\';
  42. }
  43. }
  44. }
  45. cmGlobalVisualStudio10Generator::cmGlobalVisualStudio10Generator(
  46. cmake* cm, const std::string& name,
  47. std::string const& platformInGeneratorName)
  48. : cmGlobalVisualStudio8Generator(cm, name, platformInGeneratorName)
  49. {
  50. this->DefaultCudaFlagTableName = "v10";
  51. this->DefaultCudaHostFlagTableName = "v10";
  52. this->DefaultMarmasmFlagTableName = "v10";
  53. this->DefaultNasmFlagTableName = "v10";
  54. }
  55. bool cmGlobalVisualStudio10Generator::SetSystemName(std::string const& s,
  56. cmMakefile* mf)
  57. {
  58. this->SystemName = s;
  59. this->SystemVersion = mf->GetSafeDefinition("CMAKE_SYSTEM_VERSION");
  60. if (!this->InitializeSystem(mf)) {
  61. return false;
  62. }
  63. return this->cmGlobalVisualStudio8Generator::SetSystemName(s, mf);
  64. }
  65. static void cmCudaToolVersion(std::string& s)
  66. {
  67. // "CUDA x.y.props" => "x.y"
  68. s = s.substr(5);
  69. s = s.substr(0, s.size() - 6);
  70. }
  71. bool cmGlobalVisualStudio10Generator::SetGeneratorToolset(
  72. std::string const& ts, bool build, cmMakefile* mf)
  73. {
  74. if (this->SystemIsWindowsCE && ts.empty() &&
  75. this->DefaultPlatformToolset.empty()) {
  76. mf->IssueMessage(
  77. MessageType::FATAL_ERROR,
  78. cmStrCat(this->GetName(), " Windows CE version '", this->SystemVersion,
  79. "' requires CMAKE_GENERATOR_TOOLSET to be set."));
  80. return false;
  81. }
  82. if (!this->ParseGeneratorToolset(ts, mf)) {
  83. return false;
  84. }
  85. if (build) {
  86. return true;
  87. }
  88. if (this->CustomVCTargetsPath.empty() && !this->FindVCTargetsPath(mf)) {
  89. return false;
  90. }
  91. if (!this->CustomFlagTableDir.empty() &&
  92. !(cmSystemTools::FileIsFullPath(this->CustomFlagTableDir) &&
  93. cmSystemTools::FileIsDirectory(this->CustomFlagTableDir))) {
  94. mf->IssueMessage(
  95. MessageType::FATAL_ERROR,
  96. cmStrCat("Generator\n"
  97. " ",
  98. this->GetName(),
  99. "\n"
  100. "given toolset\n"
  101. " customFlagTableDir=",
  102. this->CustomFlagTableDir,
  103. "\n"
  104. "that is not an absolute path to an existing directory."));
  105. cmSystemTools::SetFatalErrorOccurred();
  106. return false;
  107. }
  108. if (cmHasLiteralPrefix(this->GetPlatformToolsetString(), "v140")) {
  109. // The GenerateDebugInformation link setting for the v140 toolset
  110. // in VS 2015 was originally an enum with "No" and "Debug" values,
  111. // differing from the "false" and "true" values used in older toolsets.
  112. // A VS 2015 update changed it back. Parse the "link.xml" file to
  113. // discover which one we need.
  114. std::string const link_xml =
  115. cmStrCat(this->VCTargetsPath, "/1033/link.xml");
  116. cmsys::ifstream fin(link_xml.c_str());
  117. std::string line;
  118. while (fin && cmSystemTools::GetLineFromStream(fin, line)) {
  119. if (line.find(" Switch=\"DEBUG\" ") != std::string::npos) {
  120. this->PlatformToolsetNeedsDebugEnum =
  121. line.find(" Name=\"Debug\" ") != std::string::npos;
  122. break;
  123. }
  124. }
  125. }
  126. this->SupportsUnityBuilds =
  127. this->Version >= cmGlobalVisualStudioGenerator::VSVersion::VS16 ||
  128. (this->Version == cmGlobalVisualStudioGenerator::VSVersion::VS15 &&
  129. cmSystemTools::PathExists(
  130. cmStrCat(this->VCTargetsPath, "/Microsoft.Cpp.Unity.targets")));
  131. if (this->GeneratorToolsetCuda.empty()) {
  132. // Find the highest available version of the CUDA tools.
  133. std::vector<std::string> cudaTools;
  134. std::string bcDir;
  135. if (this->GeneratorToolsetCudaCustomDir.empty()) {
  136. bcDir = cmStrCat(this->VCTargetsPath, "/BuildCustomizations");
  137. } else {
  138. bcDir = cmStrCat(this->GetPlatformToolsetCudaCustomDirString(),
  139. this->GetPlatformToolsetCudaVSIntegrationSubdirString(),
  140. "extras\\visual_studio_integration\\MSBuildExtensions");
  141. cmSystemTools::ConvertToUnixSlashes(bcDir);
  142. }
  143. cmsys::Glob gl;
  144. gl.SetRelative(bcDir.c_str());
  145. if (gl.FindFiles(cmStrCat(bcDir, "/CUDA *.props"))) {
  146. cudaTools = gl.GetFiles();
  147. }
  148. if (!cudaTools.empty()) {
  149. std::for_each(cudaTools.begin(), cudaTools.end(), cmCudaToolVersion);
  150. std::sort(cudaTools.begin(), cudaTools.end(),
  151. cmSystemTools::VersionCompareGreater);
  152. this->GeneratorToolsetCuda = cudaTools.at(0);
  153. } else if (!this->GeneratorToolsetCudaCustomDir.empty()) {
  154. // Generate an error if Visual Studio integration files are not found
  155. // inside of custom cuda toolset.
  156. mf->IssueMessage(
  157. MessageType::FATAL_ERROR,
  158. cmStrCat("Generator\n"
  159. " ",
  160. this->GetName(),
  161. "\n"
  162. "given toolset\n"
  163. " cuda=",
  164. this->GeneratorToolsetCudaCustomDir,
  165. "\n"
  166. "cannot detect Visual Studio integration files in path\n"
  167. " ",
  168. bcDir));
  169. // Clear the configured tool-set
  170. this->GeneratorToolsetCuda.clear();
  171. }
  172. }
  173. if (!this->GeneratorToolsetVersion.empty() &&
  174. this->GeneratorToolsetVersion != "Test Toolset Version"_s) {
  175. // If a specific minor version of the toolset was requested, verify that it
  176. // is compatible to the major version and that is exists on disk.
  177. // If not clear the value.
  178. std::string versionToolset = this->GeneratorToolsetVersion;
  179. cmsys::RegularExpression regex("[0-9][0-9]\\.[0-9][0-9]");
  180. if (regex.find(versionToolset)) {
  181. versionToolset = cmStrCat('v', versionToolset.erase(2, 1));
  182. } else {
  183. // Version not recognized. Clear it.
  184. versionToolset.clear();
  185. }
  186. if (!cmHasPrefix(versionToolset, this->GetPlatformToolsetString())) {
  187. mf->IssueMessage(MessageType::FATAL_ERROR,
  188. cmStrCat("Generator\n"
  189. " ",
  190. this->GetName(),
  191. "\n"
  192. "given toolset and version specification\n"
  193. " ",
  194. this->GetPlatformToolsetString(),
  195. ",version=", this->GeneratorToolsetVersion,
  196. "\n"
  197. "contains an invalid version specification."));
  198. // Clear the configured tool-set
  199. this->GeneratorToolsetVersion.clear();
  200. }
  201. std::string auxProps;
  202. switch (this->FindAuxToolset(this->GeneratorToolsetVersion, auxProps)) {
  203. case AuxToolset::None:
  204. this->GeneratorToolsetVersionProps = {};
  205. break;
  206. case AuxToolset::Default:
  207. // The given version is the default toolset. Remove the setting.
  208. this->GeneratorToolsetVersion.clear();
  209. this->GeneratorToolsetVersionProps = {};
  210. break;
  211. case AuxToolset::PropsExist:
  212. this->GeneratorToolsetVersionProps = std::move(auxProps);
  213. break;
  214. case AuxToolset::PropsMissing: {
  215. mf->IssueMessage(MessageType::FATAL_ERROR,
  216. cmStrCat("Generator\n"
  217. " ",
  218. this->GetName(),
  219. "\n"
  220. "given toolset and version specification\n"
  221. " ",
  222. this->GetPlatformToolsetString(),
  223. ",version=", this->GeneratorToolsetVersion,
  224. "\n"
  225. "does not seem to be installed at\n"
  226. " ",
  227. auxProps));
  228. // Clear the configured tool-set
  229. this->GeneratorToolsetVersion.clear();
  230. this->GeneratorToolsetVersionProps = {};
  231. } break;
  232. case AuxToolset::PropsIndeterminate: {
  233. mf->IssueMessage(
  234. MessageType::FATAL_ERROR,
  235. cmStrCat("Generator\n"
  236. " ",
  237. this->GetName(),
  238. "\n"
  239. "given toolset and version specification\n"
  240. " ",
  241. this->GetPlatformToolsetString(),
  242. ",version=", this->GeneratorToolsetVersion,
  243. "\n"
  244. "has multiple matches installed at\n",
  245. " ", auxProps, "\n",
  246. "The toolset and version specification must resolve \n"
  247. "to a single installed toolset"));
  248. // Clear the configured tool-set
  249. this->GeneratorToolsetVersion.clear();
  250. this->GeneratorToolsetVersionProps = {};
  251. } break;
  252. }
  253. }
  254. if (const char* toolset = this->GetPlatformToolset()) {
  255. mf->AddDefinition("CMAKE_VS_PLATFORM_TOOLSET", toolset);
  256. }
  257. if (!this->GeneratorToolsetVersion.empty()) {
  258. mf->AddDefinition("CMAKE_VS_PLATFORM_TOOLSET_VERSION",
  259. this->GeneratorToolsetVersion);
  260. }
  261. if (const char* hostArch = this->GetPlatformToolsetHostArchitecture()) {
  262. mf->AddDefinition("CMAKE_VS_PLATFORM_TOOLSET_HOST_ARCHITECTURE", hostArch);
  263. }
  264. if (const char* cuda = this->GetPlatformToolsetCuda()) {
  265. mf->AddDefinition("CMAKE_VS_PLATFORM_TOOLSET_CUDA", cuda);
  266. }
  267. if (const char* cudaDir = this->GetPlatformToolsetCudaCustomDir()) {
  268. mf->AddDefinition("CMAKE_VS_PLATFORM_TOOLSET_CUDA_CUSTOM_DIR", cudaDir);
  269. }
  270. if (const char* vcTargetsDir = this->GetCustomVCTargetsPath()) {
  271. mf->AddDefinition("CMAKE_VS_PLATFORM_TOOLSET_VCTARGETS_CUSTOM_DIR",
  272. vcTargetsDir);
  273. }
  274. return true;
  275. }
  276. bool cmGlobalVisualStudio10Generator::ParseGeneratorToolset(
  277. std::string const& ts, cmMakefile* mf)
  278. {
  279. std::vector<std::string> const fields = cmTokenize(ts, ",");
  280. auto fi = fields.begin();
  281. if (fi == fields.end()) {
  282. return true;
  283. }
  284. // The first field may be the VS platform toolset.
  285. if (fi->find('=') == fi->npos) {
  286. this->GeneratorToolset = *fi;
  287. ++fi;
  288. }
  289. std::set<std::string> handled;
  290. // The rest of the fields must be key=value pairs.
  291. for (; fi != fields.end(); ++fi) {
  292. std::string::size_type pos = fi->find('=');
  293. if (pos == fi->npos) {
  294. mf->IssueMessage(
  295. MessageType::FATAL_ERROR,
  296. cmStrCat("Generator\n"
  297. " ",
  298. this->GetName(),
  299. "\n"
  300. "given toolset specification\n"
  301. " ",
  302. ts,
  303. "\n"
  304. "that contains a field after the first ',' with no '='."));
  305. return false;
  306. }
  307. std::string const key = fi->substr(0, pos);
  308. std::string const value = fi->substr(pos + 1);
  309. if (!handled.insert(key).second) {
  310. mf->IssueMessage(MessageType::FATAL_ERROR,
  311. cmStrCat("Generator\n"
  312. " ",
  313. this->GetName(),
  314. "\n"
  315. "given toolset specification\n"
  316. " ",
  317. ts,
  318. "\n"
  319. "that contains duplicate field key '",
  320. key, "'."));
  321. return false;
  322. }
  323. if (!this->ProcessGeneratorToolsetField(key, value)) {
  324. mf->IssueMessage(MessageType::FATAL_ERROR,
  325. cmStrCat("Generator\n"
  326. " ",
  327. this->GetName(),
  328. "\n"
  329. "given toolset specification\n"
  330. " ",
  331. ts,
  332. "\n"
  333. "that contains invalid field '",
  334. *fi, "'."));
  335. return false;
  336. }
  337. }
  338. return true;
  339. }
  340. bool cmGlobalVisualStudio10Generator::ProcessGeneratorToolsetField(
  341. std::string const& key, std::string const& value)
  342. {
  343. if (key == "cuda"_s) {
  344. /* test if cuda toolset is path to custom dir or cuda version */
  345. auto pos = value.find_first_not_of("0123456789.");
  346. if (pos != std::string::npos) {
  347. this->GeneratorToolsetCudaCustomDir = value;
  348. /* ensure trailing backslash for easy path joining */
  349. if (this->GeneratorToolsetCudaCustomDir.back() != '\\') {
  350. this->GeneratorToolsetCudaCustomDir.push_back('\\');
  351. }
  352. /* check for legacy toolkit folder structure */
  353. if (cmsys::SystemTools::FileIsDirectory(
  354. cmStrCat(this->GeneratorToolsetCudaCustomDir, "nvcc"))) {
  355. this->GeneratorToolsetCudaNvccSubdir = "nvcc\\";
  356. }
  357. if (cmsys::SystemTools::FileIsDirectory(
  358. cmStrCat(this->GeneratorToolsetCudaCustomDir,
  359. "CUDAVisualStudioIntegration"))) {
  360. this->GeneratorToolsetCudaVSIntegrationSubdir =
  361. "CUDAVisualStudioIntegration\\";
  362. }
  363. } else {
  364. this->GeneratorToolsetCuda = value;
  365. }
  366. return true;
  367. }
  368. if (key == "customFlagTableDir"_s) {
  369. this->CustomFlagTableDir = value;
  370. cmSystemTools::ConvertToUnixSlashes(this->CustomFlagTableDir);
  371. return true;
  372. }
  373. if (key == "version"_s) {
  374. this->GeneratorToolsetVersion = value;
  375. return true;
  376. }
  377. if (key == "VCTargetsPath"_s) {
  378. this->CustomVCTargetsPath = value;
  379. ConvertToWindowsSlashes(this->CustomVCTargetsPath);
  380. return true;
  381. }
  382. return false;
  383. }
  384. bool cmGlobalVisualStudio10Generator::InitializeSystem(cmMakefile* mf)
  385. {
  386. if (this->SystemName == "Windows"_s) {
  387. if (!this->InitializeWindows(mf)) {
  388. return false;
  389. }
  390. } else if (this->SystemName == "WindowsCE"_s) {
  391. this->SystemIsWindowsCE = true;
  392. if (!this->InitializeWindowsCE(mf)) {
  393. return false;
  394. }
  395. } else if (this->SystemName == "WindowsPhone"_s) {
  396. this->SystemIsWindowsPhone = true;
  397. if (!this->InitializeWindowsPhone(mf)) {
  398. return false;
  399. }
  400. } else if (this->SystemName == "WindowsStore"_s) {
  401. this->SystemIsWindowsStore = true;
  402. if (!this->InitializeWindowsStore(mf)) {
  403. return false;
  404. }
  405. } else if (this->SystemName == "Android"_s) {
  406. if (this->PlatformInGeneratorName) {
  407. mf->IssueMessage(
  408. MessageType::FATAL_ERROR,
  409. cmStrCat("CMAKE_SYSTEM_NAME is 'Android' but CMAKE_GENERATOR ",
  410. "specifies a platform too: '", this->GetName(), '\''));
  411. return false;
  412. }
  413. if (mf->GetSafeDefinition("CMAKE_GENERATOR_PLATFORM") ==
  414. "Tegra-Android"_s) {
  415. if (!this->InitializeTegraAndroid(mf)) {
  416. return false;
  417. }
  418. } else {
  419. this->SystemIsAndroid = true;
  420. if (!this->InitializeAndroid(mf)) {
  421. return false;
  422. }
  423. }
  424. }
  425. return true;
  426. }
  427. bool cmGlobalVisualStudio10Generator::InitializeWindows(cmMakefile*)
  428. {
  429. return true;
  430. }
  431. bool cmGlobalVisualStudio10Generator::InitializeWindowsCE(cmMakefile* mf)
  432. {
  433. if (this->PlatformInGeneratorName) {
  434. mf->IssueMessage(
  435. MessageType::FATAL_ERROR,
  436. cmStrCat("CMAKE_SYSTEM_NAME is 'WindowsCE' but CMAKE_GENERATOR ",
  437. "specifies a platform too: '", this->GetName(), '\''));
  438. return false;
  439. }
  440. this->DefaultPlatformToolset = this->SelectWindowsCEToolset();
  441. if (this->GetVersion() == cmGlobalVisualStudioGenerator::VSVersion::VS12) {
  442. // VS 12 .NET CF defaults to .NET framework 3.9 for Windows CE.
  443. this->DefaultTargetFrameworkVersion = "v3.9";
  444. this->DefaultTargetFrameworkIdentifier = "WindowsEmbeddedCompact";
  445. this->DefaultTargetFrameworkTargetsVersion = "v8.0";
  446. }
  447. return true;
  448. }
  449. bool cmGlobalVisualStudio10Generator::InitializeWindowsPhone(cmMakefile* mf)
  450. {
  451. mf->IssueMessage(
  452. MessageType::FATAL_ERROR,
  453. cmStrCat(this->GetName(), " does not support Windows Phone."));
  454. return false;
  455. }
  456. bool cmGlobalVisualStudio10Generator::InitializeWindowsStore(cmMakefile* mf)
  457. {
  458. mf->IssueMessage(
  459. MessageType::FATAL_ERROR,
  460. cmStrCat(this->GetName(), " does not support Windows Store."));
  461. return false;
  462. }
  463. bool cmGlobalVisualStudio10Generator::InitializeTegraAndroid(cmMakefile* mf)
  464. {
  465. std::string v =
  466. cmGlobalVisualStudio10Generator::GetInstalledNsightTegraVersion();
  467. if (v.empty()) {
  468. mf->IssueMessage(MessageType::FATAL_ERROR,
  469. "CMAKE_SYSTEM_NAME is 'Android' but "
  470. "'NVIDIA Nsight Tegra Visual Studio Edition' "
  471. "is not installed.");
  472. return false;
  473. }
  474. this->DefaultPlatformName = "Tegra-Android";
  475. this->DefaultPlatformToolset = "Default";
  476. this->NsightTegraVersion = v;
  477. mf->AddDefinition("CMAKE_VS_NsightTegra_VERSION", v);
  478. return true;
  479. }
  480. bool cmGlobalVisualStudio10Generator::InitializeAndroid(cmMakefile* mf)
  481. {
  482. mf->IssueMessage(MessageType::FATAL_ERROR,
  483. cmStrCat(this->GetName(), " does not support Android."));
  484. return false;
  485. }
  486. bool cmGlobalVisualStudio10Generator::InitializePlatform(cmMakefile* mf)
  487. {
  488. if (this->SystemName == "Windows"_s ||
  489. this->SystemName == "WindowsStore"_s) {
  490. if (!this->InitializePlatformWindows(mf)) {
  491. return false;
  492. }
  493. } else if (!this->SystemName.empty() &&
  494. !this->VerifyNoGeneratorPlatformVersion(mf)) {
  495. return false;
  496. }
  497. return this->cmGlobalVisualStudio8Generator::InitializePlatform(mf);
  498. }
  499. bool cmGlobalVisualStudio10Generator::InitializePlatformWindows(cmMakefile*)
  500. {
  501. return true;
  502. }
  503. bool cmGlobalVisualStudio10Generator::VerifyNoGeneratorPlatformVersion(
  504. cmMakefile*, cm::optional<std::string>) const
  505. {
  506. return true;
  507. }
  508. bool cmGlobalVisualStudio10Generator::SelectWindowsPhoneToolset(
  509. std::string& toolset) const
  510. {
  511. toolset.clear();
  512. return false;
  513. }
  514. bool cmGlobalVisualStudio10Generator::SelectWindowsStoreToolset(
  515. std::string& toolset) const
  516. {
  517. toolset.clear();
  518. return false;
  519. }
  520. std::string cmGlobalVisualStudio10Generator::SelectWindowsCEToolset() const
  521. {
  522. if (this->SystemVersion == "8.0"_s) {
  523. return "CE800";
  524. }
  525. return "";
  526. }
  527. //! Create a local generator appropriate to this Global Generator
  528. std::unique_ptr<cmLocalGenerator>
  529. cmGlobalVisualStudio10Generator::CreateLocalGenerator(cmMakefile* mf)
  530. {
  531. return std::unique_ptr<cmLocalGenerator>(
  532. cm::make_unique<cmLocalVisualStudio10Generator>(this, mf));
  533. }
  534. void cmGlobalVisualStudio10Generator::Generate()
  535. {
  536. this->LongestSource = LongestSourcePath();
  537. this->cmGlobalVisualStudio8Generator::Generate();
  538. if (!this->AndroidExecutableWarnings.empty() &&
  539. !this->CMakeInstance->GetIsInTryCompile()) {
  540. std::ostringstream e;
  541. /* clang-format off */
  542. e <<
  543. "You are using Visual Studio tools for Android, which does not support "
  544. "standalone executables. However, the following executable targets do "
  545. "not have the ANDROID_GUI property set, and thus will not be built as "
  546. "expected. They will be built as shared libraries with executable "
  547. "filenames:\n"
  548. " ";
  549. /* clang-format on */
  550. bool first = true;
  551. for (auto const& name : this->AndroidExecutableWarnings) {
  552. if (!first) {
  553. e << ", ";
  554. }
  555. first = false;
  556. e << name;
  557. }
  558. this->CMakeInstance->IssueMessage(MessageType::WARNING, e.str());
  559. }
  560. if (this->LongestSource.Length > 0) {
  561. cmLocalGenerator* lg = this->LongestSource.Target->GetLocalGenerator();
  562. lg->IssueMessage(
  563. MessageType::WARNING,
  564. cmStrCat(
  565. "The binary and/or source directory paths may be too long to generate "
  566. "Visual Studio 10 files for this project. "
  567. "Consider choosing shorter directory names to build this project with "
  568. "Visual Studio 10. "
  569. "A more detailed explanation follows."
  570. "\n"
  571. "There is a bug in the VS 10 IDE that renders property dialog fields "
  572. "blank for files referenced by full path in the project file. "
  573. "However, CMake must reference at least one file by full path:\n"
  574. " ",
  575. this->LongestSource.SourceFile->GetFullPath(),
  576. "\n"
  577. "This is because some Visual Studio tools would append the relative "
  578. "path to the end of the referencing directory path, as in:\n"
  579. " ",
  580. lg->GetCurrentBinaryDirectory(), '/', this->LongestSource.SourceRel,
  581. "\n"
  582. "and then incorrectly complain that the file does not exist because "
  583. "the path length is too long for some internal buffer or API. "
  584. "To avoid this problem CMake must use a full path for this file "
  585. "which then triggers the VS 10 property dialog bug."));
  586. }
  587. if (cmValue cached = this->CMakeInstance->GetState()->GetCacheEntryValue(
  588. "CMAKE_VS_NUGET_PACKAGE_RESTORE")) {
  589. this->CMakeInstance->MarkCliAsUsed("CMAKE_VS_NUGET_PACKAGE_RESTORE");
  590. }
  591. }
  592. void cmGlobalVisualStudio10Generator::EnableLanguage(
  593. std::vector<std::string> const& lang, cmMakefile* mf, bool optional)
  594. {
  595. for (std::string const& it : lang) {
  596. if (it == "ASM_NASM"_s) {
  597. this->NasmEnabled = true;
  598. }
  599. if (it == "CUDA"_s) {
  600. this->CudaEnabled = true;
  601. }
  602. }
  603. this->AddPlatformDefinitions(mf);
  604. cmGlobalVisualStudio8Generator::EnableLanguage(lang, mf, optional);
  605. }
  606. const char* cmGlobalVisualStudio10Generator::GetCustomVCTargetsPath() const
  607. {
  608. if (this->CustomVCTargetsPath.empty()) {
  609. return nullptr;
  610. }
  611. return this->CustomVCTargetsPath.c_str();
  612. }
  613. const char* cmGlobalVisualStudio10Generator::GetPlatformToolset() const
  614. {
  615. std::string const& toolset = this->GetPlatformToolsetString();
  616. if (toolset.empty()) {
  617. return nullptr;
  618. }
  619. return toolset.c_str();
  620. }
  621. std::string const& cmGlobalVisualStudio10Generator::GetPlatformToolsetString()
  622. const
  623. {
  624. if (!this->GeneratorToolset.empty()) {
  625. return this->GeneratorToolset;
  626. }
  627. if (this->SystemIsAndroid) {
  628. if (!this->DefaultAndroidToolset.empty()) {
  629. return this->DefaultAndroidToolset;
  630. }
  631. } else {
  632. if (!this->DefaultPlatformToolset.empty()) {
  633. return this->DefaultPlatformToolset;
  634. }
  635. }
  636. static std::string const empty;
  637. return empty;
  638. }
  639. std::string const&
  640. cmGlobalVisualStudio10Generator::GetPlatformToolsetVersionProps() const
  641. {
  642. return this->GeneratorToolsetVersionProps;
  643. }
  644. const char*
  645. cmGlobalVisualStudio10Generator::GetPlatformToolsetHostArchitecture() const
  646. {
  647. std::string const& hostArch =
  648. this->GetPlatformToolsetHostArchitectureString();
  649. if (hostArch.empty()) {
  650. return nullptr;
  651. }
  652. return hostArch.c_str();
  653. }
  654. std::string const&
  655. cmGlobalVisualStudio10Generator::GetPlatformToolsetHostArchitectureString()
  656. const
  657. {
  658. if (!this->GeneratorToolsetHostArchitecture.empty()) {
  659. return this->GeneratorToolsetHostArchitecture;
  660. }
  661. if (!this->DefaultPlatformToolsetHostArchitecture.empty()) {
  662. return this->DefaultPlatformToolsetHostArchitecture;
  663. }
  664. static std::string const empty;
  665. return empty;
  666. }
  667. const char* cmGlobalVisualStudio10Generator::GetPlatformToolsetCuda() const
  668. {
  669. if (!this->GeneratorToolsetCuda.empty()) {
  670. return this->GeneratorToolsetCuda.c_str();
  671. }
  672. return nullptr;
  673. }
  674. std::string const&
  675. cmGlobalVisualStudio10Generator::GetPlatformToolsetCudaString() const
  676. {
  677. return this->GeneratorToolsetCuda;
  678. }
  679. const char* cmGlobalVisualStudio10Generator::GetPlatformToolsetCudaCustomDir()
  680. const
  681. {
  682. if (!this->GeneratorToolsetCudaCustomDir.empty()) {
  683. return this->GeneratorToolsetCudaCustomDir.c_str();
  684. }
  685. return nullptr;
  686. }
  687. std::string const&
  688. cmGlobalVisualStudio10Generator::GetPlatformToolsetCudaCustomDirString() const
  689. {
  690. return this->GeneratorToolsetCudaCustomDir;
  691. }
  692. std::string const&
  693. cmGlobalVisualStudio10Generator::GetPlatformToolsetCudaNvccSubdirString() const
  694. {
  695. return this->GeneratorToolsetCudaNvccSubdir;
  696. }
  697. std::string const& cmGlobalVisualStudio10Generator::
  698. GetPlatformToolsetCudaVSIntegrationSubdirString() const
  699. {
  700. return this->GeneratorToolsetCudaVSIntegrationSubdir;
  701. }
  702. cmGlobalVisualStudio10Generator::AuxToolset
  703. cmGlobalVisualStudio10Generator::FindAuxToolset(std::string&,
  704. std::string&) const
  705. {
  706. return AuxToolset::None;
  707. }
  708. bool cmGlobalVisualStudio10Generator::FindMakeProgram(cmMakefile* mf)
  709. {
  710. if (!this->cmGlobalVisualStudio8Generator::FindMakeProgram(mf)) {
  711. return false;
  712. }
  713. mf->AddDefinition("CMAKE_VS_MSBUILD_COMMAND", this->GetMSBuildCommand());
  714. return true;
  715. }
  716. std::string const& cmGlobalVisualStudio10Generator::GetMSBuildCommand()
  717. {
  718. if (!this->MSBuildCommandInitialized) {
  719. this->MSBuildCommandInitialized = true;
  720. this->MSBuildCommand = this->FindMSBuildCommand();
  721. }
  722. return this->MSBuildCommand;
  723. }
  724. cm::optional<std::string>
  725. cmGlobalVisualStudio10Generator::FindMSBuildCommandEarly(cmMakefile*)
  726. {
  727. return this->GetMSBuildCommand();
  728. }
  729. std::string cmGlobalVisualStudio10Generator::FindMSBuildCommand()
  730. {
  731. std::string msbuild;
  732. std::string mskey;
  733. // Search in standard location.
  734. mskey =
  735. cmStrCat(R"(HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\MSBuild\ToolsVersions\)",
  736. this->GetToolsVersion(), ";MSBuildToolsPath");
  737. if (cmSystemTools::ReadRegistryValue(mskey, msbuild,
  738. cmSystemTools::KeyWOW64_32)) {
  739. cmSystemTools::ConvertToUnixSlashes(msbuild);
  740. msbuild += "/MSBuild.exe";
  741. if (cmSystemTools::FileExists(msbuild, true)) {
  742. return msbuild;
  743. }
  744. }
  745. msbuild = "MSBuild.exe";
  746. return msbuild;
  747. }
  748. std::string cmGlobalVisualStudio10Generator::FindDevEnvCommand()
  749. {
  750. if (this->ExpressEdition) {
  751. // Visual Studio Express >= 10 do not have "devenv.com" or
  752. // "VCExpress.exe" that we can use to build reliably.
  753. // Tell the caller it needs to use MSBuild instead.
  754. return "";
  755. }
  756. // Skip over the cmGlobalVisualStudio8Generator implementation because
  757. // we expect a real devenv and do not want to look for VCExpress.
  758. // NOLINTNEXTLINE(bugprone-parent-virtual-call)
  759. return this->cmGlobalVisualStudio71Generator::FindDevEnvCommand();
  760. }
  761. bool cmGlobalVisualStudio10Generator::FindVCTargetsPath(cmMakefile* mf)
  762. {
  763. // Skip this in special cases within our own test suite.
  764. if (this->GetPlatformName() == "Test Platform"_s ||
  765. this->GetPlatformToolsetString() == "Test Toolset"_s) {
  766. return true;
  767. }
  768. std::string wd;
  769. if (!this->ConfiguredFilesPath.empty()) {
  770. // In a try-compile we are given the outer CMakeFiles directory.
  771. wd = this->ConfiguredFilesPath;
  772. } else {
  773. wd = cmStrCat(this->GetCMakeInstance()->GetHomeOutputDirectory(),
  774. "/CMakeFiles");
  775. }
  776. wd += '/';
  777. wd += cmVersion::GetCMakeVersion();
  778. // We record the result persistently in a file.
  779. std::string const txt = cmStrCat(wd, "/VCTargetsPath.txt");
  780. // If we have a recorded result, use it.
  781. {
  782. cmsys::ifstream fin(txt.c_str());
  783. if (fin && cmSystemTools::GetLineFromStream(fin, this->VCTargetsPath) &&
  784. cmSystemTools::FileIsDirectory(this->VCTargetsPath)) {
  785. cmSystemTools::ConvertToUnixSlashes(this->VCTargetsPath);
  786. return true;
  787. }
  788. }
  789. // Prepare the work directory.
  790. if (!cmSystemTools::MakeDirectory(wd)) {
  791. mf->IssueMessage(MessageType::FATAL_ERROR,
  792. cmStrCat("Failed to make directory:\n ", wd));
  793. cmSystemTools::SetFatalErrorOccurred();
  794. return false;
  795. }
  796. // Generate a project file for MSBuild to tell us the VCTargetsPath value.
  797. std::string const vcxproj = "VCTargetsPath.vcxproj";
  798. {
  799. std::string const vcxprojAbs = cmStrCat(wd, '/', vcxproj);
  800. cmsys::ofstream fout(vcxprojAbs.c_str());
  801. cmXMLWriter xw(fout);
  802. cmXMLDocument doc(xw);
  803. cmXMLElement eprj(doc, "Project");
  804. eprj.Attribute("DefaultTargets", "Build");
  805. eprj.Attribute("ToolsVersion", "4.0");
  806. eprj.Attribute("xmlns",
  807. "http://schemas.microsoft.com/developer/msbuild/2003");
  808. if (this->IsNsightTegra()) {
  809. cmXMLElement epg(eprj, "PropertyGroup");
  810. epg.Attribute("Label", "NsightTegraProject");
  811. cmXMLElement(epg, "NsightTegraProjectRevisionNumber").Content("6");
  812. }
  813. {
  814. cmXMLElement eig(eprj, "ItemGroup");
  815. eig.Attribute("Label", "ProjectConfigurations");
  816. cmXMLElement epc(eig, "ProjectConfiguration");
  817. epc.Attribute("Include", cmStrCat("Debug|", this->GetPlatformName()));
  818. cmXMLElement(epc, "Configuration").Content("Debug");
  819. cmXMLElement(epc, "Platform").Content(this->GetPlatformName());
  820. }
  821. {
  822. cmXMLElement epg(eprj, "PropertyGroup");
  823. epg.Attribute("Label", "Globals");
  824. cmXMLElement(epg, "ProjectGuid")
  825. .Content("{F3FC6D86-508D-3FB1-96D2-995F08B142EC}");
  826. cmXMLElement(epg, "Keyword")
  827. .Content(mf->GetSafeDefinition("CMAKE_SYSTEM_NAME") == "Android"_s
  828. ? "Android"
  829. : "Win32Proj");
  830. cmXMLElement(epg, "Platform").Content(this->GetPlatformName());
  831. if (this->GetSystemName() == "WindowsPhone"_s) {
  832. cmXMLElement(epg, "ApplicationType").Content("Windows Phone");
  833. cmXMLElement(epg, "ApplicationTypeRevision")
  834. .Content(this->GetApplicationTypeRevision());
  835. } else if (this->GetSystemName() == "WindowsStore"_s) {
  836. cmXMLElement(epg, "ApplicationType").Content("Windows Store");
  837. cmXMLElement(epg, "ApplicationTypeRevision")
  838. .Content(this->GetApplicationTypeRevision());
  839. } else if (this->GetSystemName() == "Android"_s) {
  840. cmXMLElement(epg, "ApplicationType").Content("Android");
  841. cmXMLElement(epg, "ApplicationTypeRevision")
  842. .Content(this->GetApplicationTypeRevision());
  843. }
  844. if (!this->WindowsTargetPlatformVersion.empty()) {
  845. cmXMLElement(epg, "WindowsTargetPlatformVersion")
  846. .Content(this->WindowsTargetPlatformVersion);
  847. }
  848. if (this->GetSystemName() != "Android"_s) {
  849. if (this->GetPlatformName() == "ARM64"_s) {
  850. cmXMLElement(epg, "WindowsSDKDesktopARM64Support").Content("true");
  851. } else if (this->GetPlatformName() == "ARM"_s) {
  852. cmXMLElement(epg, "WindowsSDKDesktopARMSupport").Content("true");
  853. }
  854. }
  855. }
  856. cmXMLElement(eprj, "Import")
  857. .Attribute("Project", "$(VCTargetsPath)\\Microsoft.Cpp.Default.props");
  858. if (const char* hostArch = this->GetPlatformToolsetHostArchitecture()) {
  859. cmXMLElement epg(eprj, "PropertyGroup");
  860. cmXMLElement(epg, "PreferredToolArchitecture").Content(hostArch);
  861. }
  862. {
  863. cmXMLElement epg(eprj, "PropertyGroup");
  864. epg.Attribute("Label", "Configuration");
  865. {
  866. cmXMLElement ect(epg, "ConfigurationType");
  867. if (this->IsNsightTegra()) {
  868. // Tegra-Android platform does not understand "Utility".
  869. ect.Content("StaticLibrary");
  870. } else {
  871. ect.Content("Utility");
  872. }
  873. }
  874. cmXMLElement(epg, "CharacterSet").Content("MultiByte");
  875. if (this->IsNsightTegra()) {
  876. cmXMLElement(epg, "NdkToolchainVersion")
  877. .Content(this->GetPlatformToolsetString());
  878. } else {
  879. cmXMLElement(epg, "PlatformToolset")
  880. .Content(this->GetPlatformToolsetString());
  881. }
  882. }
  883. cmXMLElement(eprj, "Import")
  884. .Attribute("Project", "$(VCTargetsPath)\\Microsoft.Cpp.props");
  885. {
  886. cmXMLElement eidg(eprj, "ItemDefinitionGroup");
  887. cmXMLElement epbe(eidg, "PostBuildEvent");
  888. cmXMLElement(epbe, "Command")
  889. .Content("echo VCTargetsPath=$(VCTargetsPath)");
  890. }
  891. cmXMLElement(eprj, "Import")
  892. .Attribute("Project", "$(VCTargetsPath)\\Microsoft.Cpp.targets");
  893. }
  894. std::vector<std::string> cmd;
  895. cmd.push_back(this->GetMSBuildCommand());
  896. cmd.push_back(vcxproj);
  897. cmd.emplace_back("/p:Configuration=Debug");
  898. cmd.emplace_back(cmStrCat("/p:Platform=", this->GetPlatformName()));
  899. cmd.emplace_back(cmStrCat("/p:VisualStudioVersion=", this->GetIDEVersion()));
  900. std::string out;
  901. int ret = 0;
  902. cmsys::RegularExpression regex("\n *VCTargetsPath=([^%\r\n]+)[\r\n]");
  903. if (!cmSystemTools::RunSingleCommand(cmd, &out, &out, &ret, wd.c_str(),
  904. cmSystemTools::OUTPUT_NONE) ||
  905. ret != 0 || !regex.find(out)) {
  906. cmSystemTools::ReplaceString(out, "\n", "\n ");
  907. std::ostringstream e;
  908. /* clang-format off */
  909. e <<
  910. "Failed to run MSBuild command:\n"
  911. " " << cmd[0] << "\n"
  912. "to get the value of VCTargetsPath:\n"
  913. " " << out << "\n"
  914. ;
  915. /* clang-format on */
  916. if (ret != 0) {
  917. e << "Exit code: " << ret << "\n";
  918. }
  919. mf->IssueMessage(MessageType::FATAL_ERROR, e.str());
  920. cmSystemTools::SetFatalErrorOccurred();
  921. return false;
  922. }
  923. this->VCTargetsPath = regex.match(1);
  924. cmSystemTools::ConvertToUnixSlashes(this->VCTargetsPath);
  925. {
  926. cmsys::ofstream fout(txt.c_str());
  927. fout << this->VCTargetsPath << "\n";
  928. }
  929. return true;
  930. }
  931. std::vector<cmGlobalGenerator::GeneratedMakeCommand>
  932. cmGlobalVisualStudio10Generator::GenerateBuildCommand(
  933. const std::string& makeProgram, const std::string& projectName,
  934. const std::string& projectDir, std::vector<std::string> const& targetNames,
  935. const std::string& config, int jobs, bool verbose,
  936. const cmBuildOptions& buildOptions,
  937. std::vector<std::string> const& makeOptions)
  938. {
  939. std::vector<GeneratedMakeCommand> makeCommands;
  940. // Select the caller- or user-preferred make program, else MSBuild.
  941. std::string makeProgramSelected =
  942. this->SelectMakeProgram(makeProgram, this->GetMSBuildCommand());
  943. // Check if the caller explicitly requested a devenv tool.
  944. std::string makeProgramLower = makeProgramSelected;
  945. cmSystemTools::LowerCase(makeProgramLower);
  946. bool useDevEnv = (makeProgramLower.find("devenv") != std::string::npos ||
  947. makeProgramLower.find("vcexpress") != std::string::npos);
  948. // Workaround to convince VCExpress.exe to produce output.
  949. const bool requiresOutputForward =
  950. (makeProgramLower.find("vcexpress") != std::string::npos);
  951. // MSBuild is preferred (and required for VS Express), but if the .sln has
  952. // an Intel Fortran .vfproj then we have to use devenv. Parse it to find out.
  953. cmSlnData slnData;
  954. {
  955. std::string slnFile;
  956. if (!projectDir.empty()) {
  957. slnFile = cmStrCat(projectDir, '/');
  958. }
  959. slnFile += projectName;
  960. slnFile += ".sln";
  961. cmVisualStudioSlnParser parser;
  962. if (parser.ParseFile(slnFile, slnData,
  963. cmVisualStudioSlnParser::DataGroupAll)) {
  964. std::vector<cmSlnProjectEntry> slnProjects = slnData.GetProjects();
  965. for (cmSlnProjectEntry const& project : slnProjects) {
  966. if (useDevEnv) {
  967. break;
  968. }
  969. std::string proj = project.GetRelativePath();
  970. if (proj.size() > 7 && proj.substr(proj.size() - 7) == ".vfproj"_s) {
  971. useDevEnv = true;
  972. }
  973. }
  974. }
  975. }
  976. if (useDevEnv) {
  977. // Use devenv to build solutions containing Intel Fortran projects.
  978. return cmGlobalVisualStudio7Generator::GenerateBuildCommand(
  979. makeProgram, projectName, projectDir, targetNames, config, jobs, verbose,
  980. buildOptions, makeOptions);
  981. }
  982. std::vector<std::string> realTargetNames = targetNames;
  983. if (targetNames.empty() ||
  984. ((targetNames.size() == 1) && targetNames.front().empty())) {
  985. realTargetNames = { "ALL_BUILD" };
  986. }
  987. for (const auto& tname : realTargetNames) {
  988. // msbuild.exe CxxOnly.sln /t:Build /p:Configuration=Debug
  989. // /target:ALL_BUILD
  990. // /m
  991. if (tname.empty()) {
  992. continue;
  993. }
  994. GeneratedMakeCommand makeCommand;
  995. makeCommand.RequiresOutputForward = requiresOutputForward;
  996. makeCommand.Add(makeProgramSelected);
  997. cm::optional<cmSlnProjectEntry> proj = cm::nullopt;
  998. if (tname == "clean"_s) {
  999. makeCommand.Add(cmStrCat(projectName, ".sln"));
  1000. makeCommand.Add("/t:Clean");
  1001. } else {
  1002. std::string targetProject = cmStrCat(tname, ".vcxproj");
  1003. proj = slnData.GetProjectByName(tname);
  1004. if (targetProject.find('/') == std::string::npos) {
  1005. // it might be in a subdir
  1006. if (proj) {
  1007. targetProject = proj->GetRelativePath();
  1008. cmSystemTools::ConvertToUnixSlashes(targetProject);
  1009. }
  1010. }
  1011. makeCommand.Add(targetProject);
  1012. // Check if we do need a restore at all (i.e. if there are package
  1013. // references and restore has not been disabled by a command line option.
  1014. PackageResolveMode restoreMode = buildOptions.ResolveMode;
  1015. bool requiresRestore = true;
  1016. if (restoreMode == PackageResolveMode::Disable) {
  1017. requiresRestore = false;
  1018. } else if (cmValue cached =
  1019. this->CMakeInstance->GetState()->GetCacheEntryValue(
  1020. cmStrCat(tname, "_REQUIRES_VS_PACKAGE_RESTORE"))) {
  1021. requiresRestore = cached.IsOn();
  1022. } else {
  1023. // There are no package references defined.
  1024. requiresRestore = false;
  1025. }
  1026. // If a restore is required, evaluate the restore mode.
  1027. if (requiresRestore) {
  1028. if (restoreMode == PackageResolveMode::OnlyResolve) {
  1029. // Only invoke the restore target on the project.
  1030. makeCommand.Add("/t:Restore");
  1031. } else {
  1032. // Invoke restore target, unless it has been explicitly disabled.
  1033. bool restorePackages = true;
  1034. if (this->Version < VSVersion::VS15) {
  1035. // Package restore is only supported starting from Visual Studio
  1036. // 2017. Package restore must be executed manually using NuGet
  1037. // shell for older versions.
  1038. this->CMakeInstance->IssueMessage(
  1039. MessageType::WARNING,
  1040. "Restoring package references is only supported for Visual "
  1041. "Studio 2017 and later. You have to manually restore the "
  1042. "packages using NuGet before building the project.");
  1043. restorePackages = false;
  1044. } else if (restoreMode == PackageResolveMode::Default) {
  1045. // Decide if a restore is performed, based on a cache variable.
  1046. if (cmValue cached =
  1047. this->CMakeInstance->GetState()->GetCacheEntryValue(
  1048. "CMAKE_VS_NUGET_PACKAGE_RESTORE")) {
  1049. restorePackages = cached.IsOn();
  1050. }
  1051. }
  1052. if (restorePackages) {
  1053. if (this->IsMsBuildRestoreSupported()) {
  1054. makeCommand.Add("/restore");
  1055. } else {
  1056. GeneratedMakeCommand restoreCommand;
  1057. restoreCommand.Add(makeProgramSelected);
  1058. restoreCommand.Add(targetProject);
  1059. restoreCommand.Add("/t:Restore");
  1060. makeCommands.emplace_back(restoreCommand);
  1061. }
  1062. }
  1063. }
  1064. }
  1065. }
  1066. std::string plainConfig = config;
  1067. if (config.empty()) {
  1068. plainConfig = "Debug";
  1069. }
  1070. std::string platform = GetPlatformName();
  1071. if (proj) {
  1072. std::string extension =
  1073. cmSystemTools::GetFilenameLastExtension(proj->GetRelativePath());
  1074. extension = cmSystemTools::LowerCase(extension);
  1075. if (extension == ".csproj"_s) {
  1076. // Use correct platform name
  1077. platform =
  1078. slnData.GetConfigurationTarget(tname, plainConfig, platform);
  1079. }
  1080. }
  1081. makeCommand.Add(cmStrCat("/p:Configuration=", plainConfig));
  1082. makeCommand.Add(cmStrCat("/p:Platform=", platform));
  1083. makeCommand.Add(
  1084. cmStrCat("/p:VisualStudioVersion=", this->GetIDEVersion()));
  1085. if (jobs != cmake::NO_BUILD_PARALLEL_LEVEL) {
  1086. if (jobs == cmake::DEFAULT_BUILD_PARALLEL_LEVEL) {
  1087. makeCommand.Add("/m");
  1088. } else {
  1089. makeCommand.Add(cmStrCat("/m:", std::to_string(jobs)));
  1090. }
  1091. }
  1092. // Respect the verbosity: 'n' normal will show build commands
  1093. // 'm' minimal only the build step's title
  1094. makeCommand.Add(cmStrCat("/v:", ((verbose) ? "n" : "m")));
  1095. makeCommand.Add(makeOptions.begin(), makeOptions.end());
  1096. makeCommands.emplace_back(std::move(makeCommand));
  1097. }
  1098. return makeCommands;
  1099. }
  1100. std::string cmGlobalVisualStudio10Generator::GenerateRuleFile(
  1101. std::string const& output) const
  1102. {
  1103. // The VS 10 generator needs to create the .rule files on disk.
  1104. // Hide them away under the CMakeFiles directory.
  1105. std::string ruleDir = cmStrCat(
  1106. this->GetCMakeInstance()->GetHomeOutputDirectory(), "/CMakeFiles/",
  1107. cmSystemTools::ComputeStringMD5(cmSystemTools::GetFilenamePath(output)));
  1108. std::string ruleFile =
  1109. cmStrCat(ruleDir, '/', cmSystemTools::GetFilenameName(output), ".rule");
  1110. return ruleFile;
  1111. }
  1112. void cmGlobalVisualStudio10Generator::PathTooLong(cmGeneratorTarget* target,
  1113. cmSourceFile const* sf,
  1114. std::string const& sfRel)
  1115. {
  1116. size_t len =
  1117. (target->GetLocalGenerator()->GetCurrentBinaryDirectory().length() + 1 +
  1118. sfRel.length());
  1119. if (len > this->LongestSource.Length) {
  1120. this->LongestSource.Length = len;
  1121. this->LongestSource.Target = target;
  1122. this->LongestSource.SourceFile = sf;
  1123. this->LongestSource.SourceRel = sfRel;
  1124. }
  1125. }
  1126. std::string cmGlobalVisualStudio10Generator::Encoding()
  1127. {
  1128. return "utf-8";
  1129. }
  1130. const char* cmGlobalVisualStudio10Generator::GetToolsVersion() const
  1131. {
  1132. switch (this->Version) {
  1133. case cmGlobalVisualStudioGenerator::VSVersion::VS9:
  1134. return "4.0";
  1135. // in Visual Studio 2013 they detached the MSBuild tools version
  1136. // from the .Net Framework version and instead made it have it's own
  1137. // version number
  1138. case cmGlobalVisualStudioGenerator::VSVersion::VS12:
  1139. return "12.0";
  1140. case cmGlobalVisualStudioGenerator::VSVersion::VS14:
  1141. return "14.0";
  1142. case cmGlobalVisualStudioGenerator::VSVersion::VS15:
  1143. return "15.0";
  1144. case cmGlobalVisualStudioGenerator::VSVersion::VS16:
  1145. return "16.0";
  1146. case cmGlobalVisualStudioGenerator::VSVersion::VS17:
  1147. return "17.0";
  1148. }
  1149. return "";
  1150. }
  1151. bool cmGlobalVisualStudio10Generator::IsNsightTegra() const
  1152. {
  1153. return !this->NsightTegraVersion.empty();
  1154. }
  1155. std::string cmGlobalVisualStudio10Generator::GetNsightTegraVersion() const
  1156. {
  1157. return this->NsightTegraVersion;
  1158. }
  1159. std::string cmGlobalVisualStudio10Generator::GetInstalledNsightTegraVersion()
  1160. {
  1161. std::string version;
  1162. cmSystemTools::ReadRegistryValue(
  1163. "HKEY_LOCAL_MACHINE\\SOFTWARE\\NVIDIA Corporation\\Nsight Tegra;"
  1164. "Version",
  1165. version, cmSystemTools::KeyWOW64_32);
  1166. return version;
  1167. }
  1168. std::string cmGlobalVisualStudio10Generator::GetApplicationTypeRevision() const
  1169. {
  1170. if (this->GetSystemName() == "Android"_s) {
  1171. return this->GetAndroidApplicationTypeRevision();
  1172. }
  1173. // Return the first two '.'-separated components of the Windows version.
  1174. std::string::size_type end1 = this->SystemVersion.find('.');
  1175. std::string::size_type end2 =
  1176. end1 == std::string::npos ? end1 : this->SystemVersion.find('.', end1 + 1);
  1177. return this->SystemVersion.substr(0, end2);
  1178. }
  1179. static std::string cmLoadFlagTableString(Json::Value entry, const char* field)
  1180. {
  1181. if (entry.isMember(field)) {
  1182. auto string = entry[field];
  1183. if (string.isConvertibleTo(Json::ValueType::stringValue)) {
  1184. return string.asString();
  1185. }
  1186. }
  1187. return "";
  1188. }
  1189. static unsigned int cmLoadFlagTableSpecial(Json::Value entry,
  1190. const char* field)
  1191. {
  1192. unsigned int value = 0;
  1193. if (entry.isMember(field)) {
  1194. auto specials = entry[field];
  1195. if (specials.isArray()) {
  1196. for (auto const& special : specials) {
  1197. std::string s = special.asString();
  1198. if (s == "UserValue"_s) {
  1199. value |= cmIDEFlagTable::UserValue;
  1200. } else if (s == "UserIgnored"_s) {
  1201. value |= cmIDEFlagTable::UserIgnored;
  1202. } else if (s == "UserRequired"_s) {
  1203. value |= cmIDEFlagTable::UserRequired;
  1204. } else if (s == "Continue"_s) {
  1205. value |= cmIDEFlagTable::Continue;
  1206. } else if (s == "SemicolonAppendable"_s) {
  1207. value |= cmIDEFlagTable::SemicolonAppendable;
  1208. } else if (s == "UserFollowing"_s) {
  1209. value |= cmIDEFlagTable::UserFollowing;
  1210. } else if (s == "CaseInsensitive"_s) {
  1211. value |= cmIDEFlagTable::CaseInsensitive;
  1212. } else if (s == "SpaceAppendable"_s) {
  1213. value |= cmIDEFlagTable::SpaceAppendable;
  1214. } else if (s == "CommaAppendable"_s) {
  1215. value |= cmIDEFlagTable::CommaAppendable;
  1216. }
  1217. }
  1218. }
  1219. }
  1220. return value;
  1221. }
  1222. namespace {
  1223. cmIDEFlagTable const* cmLoadFlagTableJson(std::string const& flagJsonPath,
  1224. cm::optional<std::string> vsVer)
  1225. {
  1226. cmIDEFlagTable* ret = nullptr;
  1227. auto savedFlagIterator = loadedFlagJsonFiles.find(flagJsonPath);
  1228. if (savedFlagIterator != loadedFlagJsonFiles.end()) {
  1229. ret = savedFlagIterator->second.data();
  1230. } else {
  1231. Json::Reader reader;
  1232. cmsys::ifstream stream;
  1233. stream.open(flagJsonPath.c_str(), std::ios_base::in);
  1234. if (stream) {
  1235. Json::Value flags;
  1236. if (reader.parse(stream, flags, false) && flags.isArray()) {
  1237. std::vector<cmIDEFlagTable> flagTable;
  1238. for (auto const& flag : flags) {
  1239. Json::Value const& vsminJson = flag["vsmin"];
  1240. if (vsminJson.isString()) {
  1241. std::string const& vsmin = vsminJson.asString();
  1242. if (!vsmin.empty()) {
  1243. if (!vsVer ||
  1244. cmSystemTools::VersionCompareGreater(vsmin, *vsVer)) {
  1245. continue;
  1246. }
  1247. }
  1248. }
  1249. cmIDEFlagTable flagEntry;
  1250. flagEntry.IDEName = cmLoadFlagTableString(flag, "name");
  1251. flagEntry.commandFlag = cmLoadFlagTableString(flag, "switch");
  1252. flagEntry.comment = cmLoadFlagTableString(flag, "comment");
  1253. flagEntry.value = cmLoadFlagTableString(flag, "value");
  1254. flagEntry.special = cmLoadFlagTableSpecial(flag, "flags");
  1255. flagTable.push_back(flagEntry);
  1256. }
  1257. cmIDEFlagTable endFlag{ "", "", "", "", 0 };
  1258. flagTable.push_back(endFlag);
  1259. loadedFlagJsonFiles[flagJsonPath] = flagTable;
  1260. ret = loadedFlagJsonFiles[flagJsonPath].data();
  1261. }
  1262. }
  1263. }
  1264. return ret;
  1265. }
  1266. }
  1267. cm::optional<std::string> cmGlobalVisualStudio10Generator::FindFlagTable(
  1268. cm::string_view toolsetName, cm::string_view table) const
  1269. {
  1270. if (!this->CustomFlagTableDir.empty()) {
  1271. std::string customFlagTableFile =
  1272. cmStrCat(this->CustomFlagTableDir, '/', this->GetPlatformName(), '_',
  1273. toolsetName, '_', table, ".json");
  1274. if (cmSystemTools::FileExists(customFlagTableFile)) {
  1275. return customFlagTableFile;
  1276. }
  1277. customFlagTableFile =
  1278. cmStrCat(this->CustomFlagTableDir, '/', this->GetPlatformName(), '_',
  1279. table, ".json");
  1280. if (cmSystemTools::FileExists(customFlagTableFile)) {
  1281. return customFlagTableFile;
  1282. }
  1283. }
  1284. std::string fullPath =
  1285. cmStrCat(cmSystemTools::GetCMakeRoot(), "/Templates/MSBuild/FlagTables/",
  1286. toolsetName, '_', table, ".json");
  1287. if (cmSystemTools::FileExists(fullPath)) {
  1288. return fullPath;
  1289. }
  1290. return {};
  1291. }
  1292. cmIDEFlagTable const* cmGlobalVisualStudio10Generator::LoadFlagTable(
  1293. std::string const& toolSpecificName, std::string const& defaultName,
  1294. std::string const& table) const
  1295. {
  1296. cmMakefile* mf = this->GetCurrentMakefile();
  1297. std::string filename;
  1298. if (!toolSpecificName.empty()) {
  1299. if (cm::optional<std::string> found =
  1300. this->FindFlagTable(toolSpecificName, table)) {
  1301. filename = std::move(*found);
  1302. } else {
  1303. mf->IssueMessage(MessageType::FATAL_ERROR,
  1304. cmStrCat("JSON flag table for ", table,
  1305. " not found for toolset ", toolSpecificName));
  1306. return nullptr;
  1307. }
  1308. } else {
  1309. std::string const& genericName =
  1310. this->CanonicalToolsetName(this->GetPlatformToolsetString());
  1311. cm::optional<std::string> found = this->FindFlagTable(genericName, table);
  1312. if (!found) {
  1313. found = this->FindFlagTable(defaultName, table);
  1314. }
  1315. if (found) {
  1316. filename = std::move(*found);
  1317. } else {
  1318. mf->IssueMessage(MessageType::FATAL_ERROR,
  1319. cmStrCat("JSON flag table for ", table,
  1320. " not found for toolset ", genericName, " ",
  1321. defaultName));
  1322. return nullptr;
  1323. }
  1324. }
  1325. cm::optional<std::string> vsVer = this->GetVSInstanceVersion();
  1326. if (cmIDEFlagTable const* ret = cmLoadFlagTableJson(filename, vsVer)) {
  1327. return ret;
  1328. }
  1329. mf->IssueMessage(
  1330. MessageType::FATAL_ERROR,
  1331. cmStrCat("JSON flag table could not be loaded:\n ", filename));
  1332. return nullptr;
  1333. }
  1334. cmIDEFlagTable const* cmGlobalVisualStudio10Generator::GetClFlagTable() const
  1335. {
  1336. return LoadFlagTable(this->GetClFlagTableName(),
  1337. this->DefaultCLFlagTableName, "CL");
  1338. }
  1339. cmIDEFlagTable const* cmGlobalVisualStudio10Generator::GetCSharpFlagTable()
  1340. const
  1341. {
  1342. return LoadFlagTable(this->GetCSharpFlagTableName(),
  1343. this->DefaultCSharpFlagTableName, "CSharp");
  1344. }
  1345. cmIDEFlagTable const* cmGlobalVisualStudio10Generator::GetRcFlagTable() const
  1346. {
  1347. return LoadFlagTable(this->GetRcFlagTableName(),
  1348. this->DefaultRCFlagTableName, "RC");
  1349. }
  1350. cmIDEFlagTable const* cmGlobalVisualStudio10Generator::GetLibFlagTable() const
  1351. {
  1352. return LoadFlagTable(this->GetLibFlagTableName(),
  1353. this->DefaultLibFlagTableName, "LIB");
  1354. }
  1355. cmIDEFlagTable const* cmGlobalVisualStudio10Generator::GetLinkFlagTable() const
  1356. {
  1357. return LoadFlagTable(this->GetLinkFlagTableName(),
  1358. this->DefaultLinkFlagTableName, "Link");
  1359. }
  1360. cmIDEFlagTable const* cmGlobalVisualStudio10Generator::GetCudaFlagTable() const
  1361. {
  1362. return LoadFlagTable(std::string(), this->DefaultCudaFlagTableName, "Cuda");
  1363. }
  1364. cmIDEFlagTable const* cmGlobalVisualStudio10Generator::GetCudaHostFlagTable()
  1365. const
  1366. {
  1367. return LoadFlagTable(std::string(), this->DefaultCudaHostFlagTableName,
  1368. "CudaHost");
  1369. }
  1370. cmIDEFlagTable const* cmGlobalVisualStudio10Generator::GetMarmasmFlagTable()
  1371. const
  1372. {
  1373. return LoadFlagTable(std::string(), this->DefaultMarmasmFlagTableName,
  1374. "MARMASM");
  1375. }
  1376. cmIDEFlagTable const* cmGlobalVisualStudio10Generator::GetMasmFlagTable() const
  1377. {
  1378. return LoadFlagTable(this->GetMasmFlagTableName(),
  1379. this->DefaultMasmFlagTableName, "MASM");
  1380. }
  1381. cmIDEFlagTable const* cmGlobalVisualStudio10Generator::GetNasmFlagTable() const
  1382. {
  1383. return LoadFlagTable(std::string(), this->DefaultNasmFlagTableName, "NASM");
  1384. }
  1385. bool cmGlobalVisualStudio10Generator::IsMsBuildRestoreSupported() const
  1386. {
  1387. if (this->Version >= VSVersion::VS16) {
  1388. return true;
  1389. }
  1390. static std::string const vsVer15_7_5 = "15.7.27703.2042";
  1391. cm::optional<std::string> vsVer = this->GetVSInstanceVersion();
  1392. return (vsVer &&
  1393. cmSystemTools::VersionCompareGreaterEq(*vsVer, vsVer15_7_5));
  1394. }
  1395. bool cmGlobalVisualStudio10Generator::IsBuildInParallelSupported() const
  1396. {
  1397. if (this->Version >= VSVersion::VS16) {
  1398. return true;
  1399. }
  1400. static std::string const vsVer15_8_0 = "15.8.27705.0";
  1401. cm::optional<std::string> vsVer = this->GetVSInstanceVersion();
  1402. return (vsVer &&
  1403. cmSystemTools::VersionCompareGreaterEq(*vsVer, vsVer15_8_0));
  1404. }
  1405. std::string cmGlobalVisualStudio10Generator::GetClFlagTableName() const
  1406. {
  1407. std::string const& toolset = this->GetPlatformToolsetString();
  1408. std::string const useToolset = this->CanonicalToolsetName(toolset);
  1409. if (toolset == "v142"_s) {
  1410. return "v142";
  1411. }
  1412. if (toolset == "v141"_s) {
  1413. return "v141";
  1414. }
  1415. if (useToolset == "v140"_s) {
  1416. return "v140";
  1417. }
  1418. if (useToolset == "v120"_s) {
  1419. return "v12";
  1420. }
  1421. if (useToolset == "v110"_s) {
  1422. return "v11";
  1423. }
  1424. if (useToolset == "v100"_s) {
  1425. return "v10";
  1426. }
  1427. return "";
  1428. }
  1429. std::string cmGlobalVisualStudio10Generator::GetCSharpFlagTableName() const
  1430. {
  1431. std::string const& toolset = this->GetPlatformToolsetString();
  1432. std::string const useToolset = this->CanonicalToolsetName(toolset);
  1433. if (useToolset == "v142"_s) {
  1434. return "v142";
  1435. }
  1436. if (useToolset == "v141"_s) {
  1437. return "v141";
  1438. }
  1439. if (useToolset == "v140"_s) {
  1440. return "v140";
  1441. }
  1442. if (useToolset == "v120"_s) {
  1443. return "v12";
  1444. }
  1445. if (useToolset == "v110"_s) {
  1446. return "v11";
  1447. }
  1448. if (useToolset == "v100"_s) {
  1449. return "v10";
  1450. }
  1451. return "";
  1452. }
  1453. std::string cmGlobalVisualStudio10Generator::GetRcFlagTableName() const
  1454. {
  1455. std::string const& toolset = this->GetPlatformToolsetString();
  1456. std::string const useToolset = this->CanonicalToolsetName(toolset);
  1457. if ((useToolset == "v140"_s) || (useToolset == "v141"_s) ||
  1458. (useToolset == "v142"_s)) {
  1459. return "v14";
  1460. }
  1461. if (useToolset == "v120"_s) {
  1462. return "v12";
  1463. }
  1464. if (useToolset == "v110"_s) {
  1465. return "v11";
  1466. }
  1467. if (useToolset == "v100"_s) {
  1468. return "v10";
  1469. }
  1470. return "";
  1471. }
  1472. std::string cmGlobalVisualStudio10Generator::GetLibFlagTableName() const
  1473. {
  1474. std::string const& toolset = this->GetPlatformToolsetString();
  1475. std::string const useToolset = this->CanonicalToolsetName(toolset);
  1476. if ((useToolset == "v140"_s) || (useToolset == "v141"_s) ||
  1477. (useToolset == "v142"_s)) {
  1478. return "v14";
  1479. }
  1480. if (useToolset == "v120"_s) {
  1481. return "v12";
  1482. }
  1483. if (useToolset == "v110"_s) {
  1484. return "v11";
  1485. }
  1486. if (useToolset == "v100"_s) {
  1487. return "v10";
  1488. }
  1489. return "";
  1490. }
  1491. std::string cmGlobalVisualStudio10Generator::GetLinkFlagTableName() const
  1492. {
  1493. std::string const& toolset = this->GetPlatformToolsetString();
  1494. std::string const useToolset = this->CanonicalToolsetName(toolset);
  1495. if (useToolset == "v142"_s) {
  1496. return "v142";
  1497. }
  1498. if (useToolset == "v141"_s) {
  1499. return "v141";
  1500. }
  1501. if (useToolset == "v140"_s) {
  1502. return "v140";
  1503. }
  1504. if (useToolset == "v120"_s) {
  1505. return "v12";
  1506. }
  1507. if (useToolset == "v110"_s) {
  1508. return "v11";
  1509. }
  1510. if (useToolset == "v100"_s) {
  1511. return "v10";
  1512. }
  1513. return "";
  1514. }
  1515. std::string cmGlobalVisualStudio10Generator::GetMasmFlagTableName() const
  1516. {
  1517. std::string const& toolset = this->GetPlatformToolsetString();
  1518. std::string const useToolset = this->CanonicalToolsetName(toolset);
  1519. if ((useToolset == "v140"_s) || (useToolset == "v141"_s) ||
  1520. (useToolset == "v142"_s)) {
  1521. return "v14";
  1522. }
  1523. if (useToolset == "v120"_s) {
  1524. return "v12";
  1525. }
  1526. if (useToolset == "v110"_s) {
  1527. return "v11";
  1528. }
  1529. if (useToolset == "v100"_s) {
  1530. return "v10";
  1531. }
  1532. return "";
  1533. }
  1534. std::string cmGlobalVisualStudio10Generator::CanonicalToolsetName(
  1535. std::string const& toolset) const
  1536. {
  1537. std::size_t length = toolset.length();
  1538. if (cmHasLiteralSuffix(toolset, "_xp")) {
  1539. length -= 3;
  1540. }
  1541. return toolset.substr(0, length);
  1542. }