cmGlobalVisualStudio10Generator.cxx 54 KB

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