cmGlobalVisualStudio10Generator.cxx 54 KB

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