1
0

cmGlobalVisualStudio10Generator.cxx 55 KB

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