cmGlobalVisualStudio10Generator.cxx 53 KB

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