cmGlobalVisualStudio10Generator.cxx 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017
  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 "cmAlgorithms.h"
  5. #include "cmDocumentationEntry.h"
  6. #include "cmGeneratorTarget.h"
  7. #include "cmLocalVisualStudio10Generator.h"
  8. #include "cmMakefile.h"
  9. #include "cmSourceFile.h"
  10. #include "cmVS10CLFlagTable.h"
  11. #include "cmVS10CSharpFlagTable.h"
  12. #include "cmVS10CudaFlagTable.h"
  13. #include "cmVS10CudaHostFlagTable.h"
  14. #include "cmVS10LibFlagTable.h"
  15. #include "cmVS10LinkFlagTable.h"
  16. #include "cmVS10MASMFlagTable.h"
  17. #include "cmVS10NASMFlagTable.h"
  18. #include "cmVS10RCFlagTable.h"
  19. #include "cmVersion.h"
  20. #include "cmVisualStudioSlnData.h"
  21. #include "cmVisualStudioSlnParser.h"
  22. #include "cmXMLWriter.h"
  23. #include "cmake.h"
  24. #include "cmsys/FStream.hxx"
  25. #include "cmsys/Glob.hxx"
  26. #include "cmsys/RegularExpression.hxx"
  27. #include <algorithm>
  28. static const char vs10generatorName[] = "Visual Studio 10 2010";
  29. // Map generator name without year to name with year.
  30. static const char* cmVS10GenName(const std::string& name, std::string& genName)
  31. {
  32. if (strncmp(name.c_str(), vs10generatorName,
  33. sizeof(vs10generatorName) - 6) != 0) {
  34. return 0;
  35. }
  36. const char* p = name.c_str() + sizeof(vs10generatorName) - 6;
  37. if (cmHasLiteralPrefix(p, " 2010")) {
  38. p += 5;
  39. }
  40. genName = std::string(vs10generatorName) + p;
  41. return p;
  42. }
  43. class cmGlobalVisualStudio10Generator::Factory
  44. : public cmGlobalGeneratorFactory
  45. {
  46. public:
  47. cmGlobalGenerator* CreateGlobalGenerator(const std::string& name,
  48. cmake* cm) const override
  49. {
  50. std::string genName;
  51. const char* p = cmVS10GenName(name, genName);
  52. if (!p) {
  53. return 0;
  54. }
  55. if (!*p) {
  56. return new cmGlobalVisualStudio10Generator(cm, genName, "");
  57. }
  58. if (*p++ != ' ') {
  59. return 0;
  60. }
  61. if (strcmp(p, "Win64") == 0) {
  62. return new cmGlobalVisualStudio10Generator(cm, genName, "x64");
  63. }
  64. if (strcmp(p, "IA64") == 0) {
  65. return new cmGlobalVisualStudio10Generator(cm, genName, "Itanium");
  66. }
  67. return 0;
  68. }
  69. void GetDocumentation(cmDocumentationEntry& entry) const override
  70. {
  71. entry.Name = std::string(vs10generatorName) + " [arch]";
  72. entry.Brief = "Generates Visual Studio 2010 project files. "
  73. "Optional [arch] can be \"Win64\" or \"IA64\".";
  74. }
  75. void GetGenerators(std::vector<std::string>& names) const override
  76. {
  77. names.push_back(vs10generatorName);
  78. names.push_back(vs10generatorName + std::string(" IA64"));
  79. names.push_back(vs10generatorName + std::string(" Win64"));
  80. }
  81. bool SupportsToolset() const override { return true; }
  82. bool SupportsPlatform() const override { return true; }
  83. };
  84. cmGlobalGeneratorFactory* cmGlobalVisualStudio10Generator::NewFactory()
  85. {
  86. return new Factory;
  87. }
  88. cmGlobalVisualStudio10Generator::cmGlobalVisualStudio10Generator(
  89. cmake* cm, const std::string& name, const std::string& platformName)
  90. : cmGlobalVisualStudio8Generator(cm, name, platformName)
  91. {
  92. std::string vc10Express;
  93. this->ExpressEdition = cmSystemTools::ReadRegistryValue(
  94. "HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\VCExpress\\10.0\\Setup\\VC;"
  95. "ProductDir",
  96. vc10Express, cmSystemTools::KeyWOW64_32);
  97. this->CudaEnabled = false;
  98. this->SystemIsWindowsCE = false;
  99. this->SystemIsWindowsPhone = false;
  100. this->SystemIsWindowsStore = false;
  101. this->MSBuildCommandInitialized = false;
  102. {
  103. std::string envPlatformToolset;
  104. if (cmSystemTools::GetEnv("PlatformToolset", envPlatformToolset) &&
  105. envPlatformToolset == "Windows7.1SDK") {
  106. // We are running from a Windows7.1SDK command prompt.
  107. this->DefaultPlatformToolset = "Windows7.1SDK";
  108. } else {
  109. this->DefaultPlatformToolset = "v100";
  110. }
  111. }
  112. this->DefaultClFlagTable = cmVS10CLFlagTable;
  113. this->DefaultCSharpFlagTable = cmVS10CSharpFlagTable;
  114. this->DefaultLibFlagTable = cmVS10LibFlagTable;
  115. this->DefaultLinkFlagTable = cmVS10LinkFlagTable;
  116. this->DefaultCudaFlagTable = cmVS10CudaFlagTable;
  117. this->DefaultCudaHostFlagTable = cmVS10CudaHostFlagTable;
  118. this->DefaultMasmFlagTable = cmVS10MASMFlagTable;
  119. this->DefaultNasmFlagTable = cmVS10NASMFlagTable;
  120. this->DefaultRcFlagTable = cmVS10RCFlagTable;
  121. this->Version = VS10;
  122. this->PlatformToolsetNeedsDebugEnum = false;
  123. }
  124. bool cmGlobalVisualStudio10Generator::MatchesGeneratorName(
  125. const std::string& name) const
  126. {
  127. std::string genName;
  128. if (cmVS10GenName(name, genName)) {
  129. return genName == this->GetName();
  130. }
  131. return false;
  132. }
  133. bool cmGlobalVisualStudio10Generator::SetSystemName(std::string const& s,
  134. cmMakefile* mf)
  135. {
  136. this->SystemName = s;
  137. this->SystemVersion = mf->GetSafeDefinition("CMAKE_SYSTEM_VERSION");
  138. if (!this->InitializeSystem(mf)) {
  139. return false;
  140. }
  141. return this->cmGlobalVisualStudio8Generator::SetSystemName(s, mf);
  142. }
  143. bool cmGlobalVisualStudio10Generator::SetGeneratorPlatform(
  144. std::string const& p, cmMakefile* mf)
  145. {
  146. if (!this->cmGlobalVisualStudio8Generator::SetGeneratorPlatform(p, mf)) {
  147. return false;
  148. }
  149. if (this->GetPlatformName() == "Itanium" ||
  150. this->GetPlatformName() == "x64") {
  151. if (this->IsExpressEdition() && !this->Find64BitTools(mf)) {
  152. return false;
  153. }
  154. }
  155. return true;
  156. }
  157. static void cmCudaToolVersion(std::string& s)
  158. {
  159. // "CUDA x.y.props" => "x.y"
  160. s = s.substr(5);
  161. s = s.substr(0, s.size() - 6);
  162. }
  163. bool cmGlobalVisualStudio10Generator::SetGeneratorToolset(
  164. std::string const& ts, cmMakefile* mf)
  165. {
  166. if (this->SystemIsWindowsCE && ts.empty() &&
  167. this->DefaultPlatformToolset.empty()) {
  168. std::ostringstream e;
  169. e << this->GetName() << " Windows CE version '" << this->SystemVersion
  170. << "' requires CMAKE_GENERATOR_TOOLSET to be set.";
  171. mf->IssueMessage(cmake::FATAL_ERROR, e.str());
  172. return false;
  173. }
  174. if (!this->ParseGeneratorToolset(ts, mf)) {
  175. return false;
  176. }
  177. if (!this->FindVCTargetsPath(mf)) {
  178. return false;
  179. }
  180. if (cmHasLiteralPrefix(this->GetPlatformToolsetString(), "v140")) {
  181. // The GenerateDebugInformation link setting for the v140 toolset
  182. // in VS 2015 was originally an enum with "No" and "Debug" values,
  183. // differing from the "false" and "true" values used in older toolsets.
  184. // A VS 2015 update changed it back. Parse the "link.xml" file to
  185. // discover which one we need.
  186. std::string const link_xml = this->VCTargetsPath + "/1033/link.xml";
  187. cmsys::ifstream fin(link_xml.c_str());
  188. std::string line;
  189. while (fin && cmSystemTools::GetLineFromStream(fin, line)) {
  190. if (line.find(" Switch=\"DEBUG\" ") != std::string::npos) {
  191. this->PlatformToolsetNeedsDebugEnum =
  192. line.find(" Name=\"Debug\" ") != std::string::npos;
  193. break;
  194. }
  195. }
  196. }
  197. if (this->GeneratorToolsetCuda.empty()) {
  198. // Find the highest available version of the CUDA tools.
  199. std::vector<std::string> cudaTools;
  200. std::string const bcDir = this->VCTargetsPath + "/BuildCustomizations";
  201. cmsys::Glob gl;
  202. gl.SetRelative(bcDir.c_str());
  203. if (gl.FindFiles(bcDir + "/CUDA *.props")) {
  204. cudaTools = gl.GetFiles();
  205. }
  206. if (!cudaTools.empty()) {
  207. std::for_each(cudaTools.begin(), cudaTools.end(), cmCudaToolVersion);
  208. std::sort(cudaTools.begin(), cudaTools.end(),
  209. cmSystemTools::VersionCompareGreater);
  210. this->GeneratorToolsetCuda = cudaTools.at(0);
  211. }
  212. }
  213. if (const char* toolset = this->GetPlatformToolset()) {
  214. mf->AddDefinition("CMAKE_VS_PLATFORM_TOOLSET", toolset);
  215. }
  216. if (const char* hostArch = this->GetPlatformToolsetHostArchitecture()) {
  217. mf->AddDefinition("CMAKE_VS_PLATFORM_TOOLSET_HOST_ARCHITECTURE", hostArch);
  218. }
  219. if (const char* cuda = this->GetPlatformToolsetCuda()) {
  220. mf->AddDefinition("CMAKE_VS_PLATFORM_TOOLSET_CUDA", cuda);
  221. }
  222. return true;
  223. }
  224. bool cmGlobalVisualStudio10Generator::ParseGeneratorToolset(
  225. std::string const& ts, cmMakefile* mf)
  226. {
  227. std::vector<std::string> const fields = cmSystemTools::tokenize(ts, ",");
  228. std::vector<std::string>::const_iterator fi = fields.begin();
  229. if (fi == fields.end()) {
  230. return true;
  231. }
  232. // The first field may be the VS platform toolset.
  233. if (fi->find('=') == fi->npos) {
  234. this->GeneratorToolset = *fi;
  235. ++fi;
  236. }
  237. std::set<std::string> handled;
  238. // The rest of the fields must be key=value pairs.
  239. for (; fi != fields.end(); ++fi) {
  240. std::string::size_type pos = fi->find('=');
  241. if (pos == fi->npos) {
  242. std::ostringstream e;
  243. /* clang-format off */
  244. e <<
  245. "Generator\n"
  246. " " << this->GetName() << "\n"
  247. "given toolset specification\n"
  248. " " << ts << "\n"
  249. "that contains a field after the first ',' with no '='."
  250. ;
  251. /* clang-format on */
  252. mf->IssueMessage(cmake::FATAL_ERROR, e.str());
  253. return false;
  254. }
  255. std::string const key = fi->substr(0, pos);
  256. std::string const value = fi->substr(pos + 1);
  257. if (!handled.insert(key).second) {
  258. std::ostringstream e;
  259. /* clang-format off */
  260. e <<
  261. "Generator\n"
  262. " " << this->GetName() << "\n"
  263. "given toolset specification\n"
  264. " " << ts << "\n"
  265. "that contains duplicate field key '" << key << "'."
  266. ;
  267. /* clang-format on */
  268. mf->IssueMessage(cmake::FATAL_ERROR, e.str());
  269. return false;
  270. }
  271. if (!this->ProcessGeneratorToolsetField(key, value)) {
  272. std::ostringstream e;
  273. /* clang-format off */
  274. e <<
  275. "Generator\n"
  276. " " << this->GetName() << "\n"
  277. "given toolset specification\n"
  278. " " << ts << "\n"
  279. "that contains invalid field '" << *fi << "'."
  280. ;
  281. /* clang-format on */
  282. mf->IssueMessage(cmake::FATAL_ERROR, e.str());
  283. return false;
  284. }
  285. }
  286. return true;
  287. }
  288. bool cmGlobalVisualStudio10Generator::ProcessGeneratorToolsetField(
  289. std::string const& key, std::string const& value)
  290. {
  291. if (key == "cuda") {
  292. this->GeneratorToolsetCuda = value;
  293. return true;
  294. }
  295. return false;
  296. }
  297. bool cmGlobalVisualStudio10Generator::InitializeSystem(cmMakefile* mf)
  298. {
  299. if (this->SystemName == "Windows") {
  300. if (!this->InitializeWindows(mf)) {
  301. return false;
  302. }
  303. } else if (this->SystemName == "WindowsCE") {
  304. this->SystemIsWindowsCE = true;
  305. if (!this->InitializeWindowsCE(mf)) {
  306. return false;
  307. }
  308. } else if (this->SystemName == "WindowsPhone") {
  309. this->SystemIsWindowsPhone = true;
  310. if (!this->InitializeWindowsPhone(mf)) {
  311. return false;
  312. }
  313. } else if (this->SystemName == "WindowsStore") {
  314. this->SystemIsWindowsStore = true;
  315. if (!this->InitializeWindowsStore(mf)) {
  316. return false;
  317. }
  318. } else if (this->SystemName == "Android") {
  319. if (this->DefaultPlatformName != "Win32") {
  320. std::ostringstream e;
  321. e << "CMAKE_SYSTEM_NAME is 'Android' but CMAKE_GENERATOR "
  322. << "specifies a platform too: '" << this->GetName() << "'";
  323. mf->IssueMessage(cmake::FATAL_ERROR, e.str());
  324. return false;
  325. }
  326. std::string v = this->GetInstalledNsightTegraVersion();
  327. if (v.empty()) {
  328. mf->IssueMessage(cmake::FATAL_ERROR,
  329. "CMAKE_SYSTEM_NAME is 'Android' but "
  330. "'NVIDIA Nsight Tegra Visual Studio Edition' "
  331. "is not installed.");
  332. return false;
  333. }
  334. this->DefaultPlatformName = "Tegra-Android";
  335. this->DefaultPlatformToolset = "Default";
  336. this->NsightTegraVersion = v;
  337. mf->AddDefinition("CMAKE_VS_NsightTegra_VERSION", v.c_str());
  338. }
  339. return true;
  340. }
  341. bool cmGlobalVisualStudio10Generator::InitializeWindows(cmMakefile*)
  342. {
  343. return true;
  344. }
  345. bool cmGlobalVisualStudio10Generator::InitializeWindowsCE(cmMakefile* mf)
  346. {
  347. if (this->DefaultPlatformName != "Win32") {
  348. std::ostringstream e;
  349. e << "CMAKE_SYSTEM_NAME is 'WindowsCE' but CMAKE_GENERATOR "
  350. << "specifies a platform too: '" << this->GetName() << "'";
  351. mf->IssueMessage(cmake::FATAL_ERROR, e.str());
  352. return false;
  353. }
  354. this->DefaultPlatformToolset = this->SelectWindowsCEToolset();
  355. return true;
  356. }
  357. bool cmGlobalVisualStudio10Generator::InitializeWindowsPhone(cmMakefile* mf)
  358. {
  359. std::ostringstream e;
  360. e << this->GetName() << " does not support Windows Phone.";
  361. mf->IssueMessage(cmake::FATAL_ERROR, e.str());
  362. return false;
  363. }
  364. bool cmGlobalVisualStudio10Generator::InitializeWindowsStore(cmMakefile* mf)
  365. {
  366. std::ostringstream e;
  367. e << this->GetName() << " does not support Windows Store.";
  368. mf->IssueMessage(cmake::FATAL_ERROR, e.str());
  369. return false;
  370. }
  371. bool cmGlobalVisualStudio10Generator::SelectWindowsPhoneToolset(
  372. std::string& toolset) const
  373. {
  374. toolset.clear();
  375. return false;
  376. }
  377. bool cmGlobalVisualStudio10Generator::SelectWindowsStoreToolset(
  378. std::string& toolset) const
  379. {
  380. toolset.clear();
  381. return false;
  382. }
  383. std::string cmGlobalVisualStudio10Generator::SelectWindowsCEToolset() const
  384. {
  385. if (this->SystemVersion == "8.0") {
  386. return "CE800";
  387. }
  388. return "";
  389. }
  390. void cmGlobalVisualStudio10Generator::WriteSLNHeader(std::ostream& fout)
  391. {
  392. fout << "Microsoft Visual Studio Solution File, Format Version 11.00\n";
  393. if (this->ExpressEdition) {
  394. fout << "# Visual C++ Express 2010\n";
  395. } else {
  396. fout << "# Visual Studio 2010\n";
  397. }
  398. }
  399. ///! Create a local generator appropriate to this Global Generator
  400. cmLocalGenerator* cmGlobalVisualStudio10Generator::CreateLocalGenerator(
  401. cmMakefile* mf)
  402. {
  403. return new cmLocalVisualStudio10Generator(this, mf);
  404. }
  405. void cmGlobalVisualStudio10Generator::Generate()
  406. {
  407. this->LongestSource = LongestSourcePath();
  408. this->cmGlobalVisualStudio8Generator::Generate();
  409. if (this->LongestSource.Length > 0) {
  410. cmLocalGenerator* lg = this->LongestSource.Target->GetLocalGenerator();
  411. std::ostringstream e;
  412. /* clang-format off */
  413. e <<
  414. "The binary and/or source directory paths may be too long to generate "
  415. "Visual Studio 10 files for this project. "
  416. "Consider choosing shorter directory names to build this project with "
  417. "Visual Studio 10. "
  418. "A more detailed explanation follows."
  419. "\n"
  420. "There is a bug in the VS 10 IDE that renders property dialog fields "
  421. "blank for files referenced by full path in the project file. "
  422. "However, CMake must reference at least one file by full path:\n"
  423. " " << this->LongestSource.SourceFile->GetFullPath() << "\n"
  424. "This is because some Visual Studio tools would append the relative "
  425. "path to the end of the referencing directory path, as in:\n"
  426. " " << lg->GetCurrentBinaryDirectory() << "/"
  427. << this->LongestSource.SourceRel << "\n"
  428. "and then incorrectly complain that the file does not exist because "
  429. "the path length is too long for some internal buffer or API. "
  430. "To avoid this problem CMake must use a full path for this file "
  431. "which then triggers the VS 10 property dialog bug.";
  432. /* clang-format on */
  433. lg->IssueMessage(cmake::WARNING, e.str().c_str());
  434. }
  435. }
  436. void cmGlobalVisualStudio10Generator::EnableLanguage(
  437. std::vector<std::string> const& lang, cmMakefile* mf, bool optional)
  438. {
  439. for (std::string const& it : lang) {
  440. if (it == "ASM_NASM") {
  441. this->NasmEnabled = true;
  442. }
  443. if (it == "CUDA") {
  444. this->CudaEnabled = true;
  445. }
  446. }
  447. this->AddPlatformDefinitions(mf);
  448. cmGlobalVisualStudio8Generator::EnableLanguage(lang, mf, optional);
  449. }
  450. const char* cmGlobalVisualStudio10Generator::GetPlatformToolset() const
  451. {
  452. std::string const& toolset = this->GetPlatformToolsetString();
  453. if (toolset.empty()) {
  454. return nullptr;
  455. }
  456. return toolset.c_str();
  457. }
  458. std::string const& cmGlobalVisualStudio10Generator::GetPlatformToolsetString()
  459. const
  460. {
  461. if (!this->GeneratorToolset.empty()) {
  462. return this->GeneratorToolset;
  463. }
  464. if (!this->DefaultPlatformToolset.empty()) {
  465. return this->DefaultPlatformToolset;
  466. }
  467. static std::string const empty;
  468. return empty;
  469. }
  470. const char*
  471. cmGlobalVisualStudio10Generator::GetPlatformToolsetHostArchitecture() const
  472. {
  473. if (!this->GeneratorToolsetHostArchitecture.empty()) {
  474. return this->GeneratorToolsetHostArchitecture.c_str();
  475. }
  476. return nullptr;
  477. }
  478. const char* cmGlobalVisualStudio10Generator::GetPlatformToolsetCuda() const
  479. {
  480. if (!this->GeneratorToolsetCuda.empty()) {
  481. return this->GeneratorToolsetCuda.c_str();
  482. }
  483. return nullptr;
  484. }
  485. std::string const&
  486. cmGlobalVisualStudio10Generator::GetPlatformToolsetCudaString() const
  487. {
  488. return this->GeneratorToolsetCuda;
  489. }
  490. bool cmGlobalVisualStudio10Generator::FindMakeProgram(cmMakefile* mf)
  491. {
  492. if (!this->cmGlobalVisualStudio8Generator::FindMakeProgram(mf)) {
  493. return false;
  494. }
  495. mf->AddDefinition("CMAKE_VS_MSBUILD_COMMAND",
  496. this->GetMSBuildCommand().c_str());
  497. return true;
  498. }
  499. std::string const& cmGlobalVisualStudio10Generator::GetMSBuildCommand()
  500. {
  501. if (!this->MSBuildCommandInitialized) {
  502. this->MSBuildCommandInitialized = true;
  503. this->MSBuildCommand = this->FindMSBuildCommand();
  504. }
  505. return this->MSBuildCommand;
  506. }
  507. std::string cmGlobalVisualStudio10Generator::FindMSBuildCommand()
  508. {
  509. std::string msbuild;
  510. std::string mskey;
  511. // Search in standard location.
  512. mskey = "HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\MSBuild\\ToolsVersions\\";
  513. mskey += this->GetToolsVersion();
  514. mskey += ";MSBuildToolsPath";
  515. if (cmSystemTools::ReadRegistryValue(mskey.c_str(), msbuild,
  516. cmSystemTools::KeyWOW64_32)) {
  517. cmSystemTools::ConvertToUnixSlashes(msbuild);
  518. msbuild += "/MSBuild.exe";
  519. if (cmSystemTools::FileExists(msbuild, true)) {
  520. return msbuild;
  521. }
  522. }
  523. msbuild = "MSBuild.exe";
  524. return msbuild;
  525. }
  526. std::string cmGlobalVisualStudio10Generator::FindDevEnvCommand()
  527. {
  528. if (this->ExpressEdition) {
  529. // Visual Studio Express >= 10 do not have "devenv.com" or
  530. // "VCExpress.exe" that we can use to build reliably.
  531. // Tell the caller it needs to use MSBuild instead.
  532. return "";
  533. }
  534. // Skip over the cmGlobalVisualStudio8Generator implementation because
  535. // we expect a real devenv and do not want to look for VCExpress.
  536. return this->cmGlobalVisualStudio71Generator::FindDevEnvCommand();
  537. }
  538. bool cmGlobalVisualStudio10Generator::FindVCTargetsPath(cmMakefile* mf)
  539. {
  540. // Skip this in special cases within our own test suite.
  541. if (this->GetPlatformName() == "Test Platform" ||
  542. this->GetPlatformToolsetString() == "Test Toolset") {
  543. return true;
  544. }
  545. std::string wd;
  546. if (!this->ConfiguredFilesPath.empty()) {
  547. // In a try-compile we are given the outer CMakeFiles directory.
  548. wd = this->ConfiguredFilesPath;
  549. } else {
  550. wd = this->GetCMakeInstance()->GetHomeOutputDirectory();
  551. wd += cmake::GetCMakeFilesDirectory();
  552. }
  553. wd += "/";
  554. wd += cmVersion::GetCMakeVersion();
  555. // We record the result persistently in a file.
  556. std::string const txt = wd + "/VCTargetsPath.txt";
  557. // If we have a recorded result, use it.
  558. {
  559. cmsys::ifstream fin(txt.c_str());
  560. if (fin && cmSystemTools::GetLineFromStream(fin, this->VCTargetsPath) &&
  561. cmSystemTools::FileIsDirectory(this->VCTargetsPath)) {
  562. cmSystemTools::ConvertToUnixSlashes(this->VCTargetsPath);
  563. return true;
  564. }
  565. }
  566. // Prepare the work directory.
  567. if (!cmSystemTools::MakeDirectory(wd)) {
  568. std::string e = "Failed to make directory:\n " + wd;
  569. mf->IssueMessage(cmake::FATAL_ERROR, e.c_str());
  570. cmSystemTools::SetFatalErrorOccured();
  571. return false;
  572. }
  573. // Generate a project file for MSBuild to tell us the VCTargetsPath value.
  574. std::string const vcxproj = "VCTargetsPath.vcxproj";
  575. {
  576. std::string const vcxprojAbs = wd + "/" + vcxproj;
  577. cmsys::ofstream fout(vcxprojAbs.c_str());
  578. cmXMLWriter xw(fout);
  579. cmXMLDocument doc(xw);
  580. cmXMLElement eprj(doc, "Project");
  581. eprj.Attribute("DefaultTargets", "Build");
  582. eprj.Attribute("ToolsVersion", "4.0");
  583. eprj.Attribute("xmlns",
  584. "http://schemas.microsoft.com/developer/msbuild/2003");
  585. if (this->IsNsightTegra()) {
  586. cmXMLElement epg(eprj, "PropertyGroup");
  587. epg.Attribute("Label", "NsightTegraProject");
  588. cmXMLElement(epg, "NsightTegraProjectRevisionNumber").Content("6");
  589. }
  590. {
  591. cmXMLElement eig(eprj, "ItemGroup");
  592. eig.Attribute("Label", "ProjectConfigurations");
  593. cmXMLElement epc(eig, "ProjectConfiguration");
  594. epc.Attribute("Include", "Debug|" + this->GetPlatformName());
  595. cmXMLElement(epc, "Configuration").Content("Debug");
  596. cmXMLElement(epc, "Platform").Content(this->GetPlatformName());
  597. }
  598. {
  599. cmXMLElement epg(eprj, "PropertyGroup");
  600. epg.Attribute("Label", "Globals");
  601. cmXMLElement(epg, "ProjectGuid")
  602. .Content("{F3FC6D86-508D-3FB1-96D2-995F08B142EC}");
  603. cmXMLElement(epg, "Keyword").Content("Win32Proj");
  604. cmXMLElement(epg, "Platform").Content(this->GetPlatformName());
  605. if (this->GetSystemName() == "WindowsPhone") {
  606. cmXMLElement(epg, "ApplicationType").Content("Windows Phone");
  607. cmXMLElement(epg, "ApplicationTypeRevision")
  608. .Content(this->GetSystemVersion());
  609. } else if (this->GetSystemName() == "WindowsStore") {
  610. cmXMLElement(epg, "ApplicationType").Content("Windows Store");
  611. cmXMLElement(epg, "ApplicationTypeRevision")
  612. .Content(this->GetSystemVersion());
  613. }
  614. if (!this->WindowsTargetPlatformVersion.empty()) {
  615. cmXMLElement(epg, "WindowsTargetPlatformVersion")
  616. .Content(this->WindowsTargetPlatformVersion);
  617. }
  618. if (this->GetPlatformName() == "ARM64") {
  619. cmXMLElement(epg, "WindowsSDKDesktopARM64Support").Content("true");
  620. } else if (this->GetPlatformName() == "ARM") {
  621. cmXMLElement(epg, "WindowsSDKDesktopARMSupport").Content("true");
  622. }
  623. }
  624. cmXMLElement(eprj, "Import")
  625. .Attribute("Project", "$(VCTargetsPath)\\Microsoft.Cpp.Default.props");
  626. if (!this->GeneratorToolsetHostArchitecture.empty()) {
  627. cmXMLElement epg(eprj, "PropertyGroup");
  628. cmXMLElement(epg, "PreferredToolArchitecture")
  629. .Content(this->GeneratorToolsetHostArchitecture);
  630. }
  631. {
  632. cmXMLElement epg(eprj, "PropertyGroup");
  633. epg.Attribute("Label", "Configuration");
  634. {
  635. cmXMLElement ect(epg, "ConfigurationType");
  636. if (this->IsNsightTegra()) {
  637. // Tegra-Android platform does not understand "Utility".
  638. ect.Content("StaticLibrary");
  639. } else {
  640. ect.Content("Utility");
  641. }
  642. }
  643. cmXMLElement(epg, "CharacterSet").Content("MultiByte");
  644. if (this->IsNsightTegra()) {
  645. cmXMLElement(epg, "NdkToolchainVersion")
  646. .Content(this->GetPlatformToolsetString());
  647. } else {
  648. cmXMLElement(epg, "PlatformToolset")
  649. .Content(this->GetPlatformToolsetString());
  650. }
  651. }
  652. cmXMLElement(eprj, "Import")
  653. .Attribute("Project", "$(VCTargetsPath)\\Microsoft.Cpp.props");
  654. {
  655. cmXMLElement eidg(eprj, "ItemDefinitionGroup");
  656. cmXMLElement epbe(eidg, "PostBuildEvent");
  657. cmXMLElement(epbe, "Command")
  658. .Content("echo VCTargetsPath=$(VCTargetsPath)");
  659. }
  660. cmXMLElement(eprj, "Import")
  661. .Attribute("Project", "$(VCTargetsPath)\\Microsoft.Cpp.targets");
  662. }
  663. std::vector<std::string> cmd;
  664. cmd.push_back(this->GetMSBuildCommand());
  665. cmd.push_back(vcxproj);
  666. cmd.push_back("/p:Configuration=Debug");
  667. cmd.push_back(std::string("/p:VisualStudioVersion=") +
  668. this->GetIDEVersion());
  669. std::string out;
  670. int ret = 0;
  671. cmsys::RegularExpression regex("\n *VCTargetsPath=([^%\r\n]+)[\r\n]");
  672. if (!cmSystemTools::RunSingleCommand(cmd, &out, &out, &ret, wd.c_str(),
  673. cmSystemTools::OUTPUT_NONE) ||
  674. ret != 0 || !regex.find(out)) {
  675. cmSystemTools::ReplaceString(out, "\n", "\n ");
  676. std::ostringstream e;
  677. /* clang-format off */
  678. e <<
  679. "Failed to run MSBuild command:\n"
  680. " " << cmd[0] << "\n"
  681. "to get the value of VCTargetsPath:\n"
  682. " " << out << "\n"
  683. ;
  684. /* clang-format on */
  685. if (ret != 0) {
  686. e << "Exit code: " << ret << "\n";
  687. }
  688. mf->IssueMessage(cmake::FATAL_ERROR, e.str().c_str());
  689. cmSystemTools::SetFatalErrorOccured();
  690. return false;
  691. }
  692. this->VCTargetsPath = regex.match(1);
  693. cmSystemTools::ConvertToUnixSlashes(this->VCTargetsPath);
  694. {
  695. cmsys::ofstream fout(txt.c_str());
  696. fout << this->VCTargetsPath << "\n";
  697. }
  698. return true;
  699. }
  700. void cmGlobalVisualStudio10Generator::GenerateBuildCommand(
  701. std::vector<std::string>& makeCommand, const std::string& makeProgram,
  702. const std::string& projectName, const std::string& projectDir,
  703. const std::string& targetName, const std::string& config, bool fast,
  704. int jobs, bool verbose, std::vector<std::string> const& makeOptions)
  705. {
  706. // Select the caller- or user-preferred make program, else MSBuild.
  707. std::string makeProgramSelected =
  708. this->SelectMakeProgram(makeProgram, this->GetMSBuildCommand());
  709. // Check if the caller explicitly requested a devenv tool.
  710. std::string makeProgramLower = makeProgramSelected;
  711. cmSystemTools::LowerCase(makeProgramLower);
  712. bool useDevEnv = (makeProgramLower.find("devenv") != std::string::npos ||
  713. makeProgramLower.find("vcexpress") != std::string::npos);
  714. // MSBuild is preferred (and required for VS Express), but if the .sln has
  715. // an Intel Fortran .vfproj then we have to use devenv. Parse it to find out.
  716. cmSlnData slnData;
  717. {
  718. std::string slnFile;
  719. if (!projectDir.empty()) {
  720. slnFile = projectDir;
  721. slnFile += "/";
  722. }
  723. slnFile += projectName;
  724. slnFile += ".sln";
  725. cmVisualStudioSlnParser parser;
  726. if (parser.ParseFile(slnFile, slnData,
  727. cmVisualStudioSlnParser::DataGroupProjects)) {
  728. std::vector<cmSlnProjectEntry> slnProjects = slnData.GetProjects();
  729. for (std::vector<cmSlnProjectEntry>::const_iterator i =
  730. slnProjects.cbegin();
  731. !useDevEnv && i != slnProjects.cend(); ++i) {
  732. std::string proj = i->GetRelativePath();
  733. if (proj.size() > 7 && proj.substr(proj.size() - 7) == ".vfproj") {
  734. useDevEnv = true;
  735. }
  736. }
  737. }
  738. }
  739. if (useDevEnv) {
  740. // Use devenv to build solutions containing Intel Fortran projects.
  741. cmGlobalVisualStudio7Generator::GenerateBuildCommand(
  742. makeCommand, makeProgram, projectName, projectDir, targetName, config,
  743. fast, jobs, verbose, makeOptions);
  744. return;
  745. }
  746. makeCommand.push_back(makeProgramSelected);
  747. std::string realTarget = targetName;
  748. // msbuild.exe CxxOnly.sln /t:Build /p:Configuration=Debug /target:ALL_BUILD
  749. // /m
  750. if (realTarget.empty()) {
  751. realTarget = "ALL_BUILD";
  752. }
  753. if (realTarget == "clean") {
  754. makeCommand.push_back(std::string(projectName) + ".sln");
  755. makeCommand.push_back("/t:Clean");
  756. } else {
  757. std::string targetProject(realTarget);
  758. targetProject += ".vcxproj";
  759. if (targetProject.find('/') == std::string::npos) {
  760. // it might be in a subdir
  761. if (cmSlnProjectEntry const* proj =
  762. slnData.GetProjectByName(realTarget)) {
  763. targetProject = proj->GetRelativePath();
  764. cmSystemTools::ConvertToUnixSlashes(targetProject);
  765. }
  766. }
  767. makeCommand.push_back(targetProject);
  768. }
  769. std::string configArg = "/p:Configuration=";
  770. if (!config.empty()) {
  771. configArg += config;
  772. } else {
  773. configArg += "Debug";
  774. }
  775. makeCommand.push_back(configArg);
  776. makeCommand.push_back(std::string("/p:VisualStudioVersion=") +
  777. this->GetIDEVersion());
  778. if (jobs != cmake::NO_BUILD_PARALLEL_LEVEL) {
  779. if (jobs == cmake::DEFAULT_BUILD_PARALLEL_LEVEL) {
  780. makeCommand.push_back("/m");
  781. } else {
  782. makeCommand.push_back(std::string("/m:") + std::to_string(jobs));
  783. }
  784. // Having msbuild.exe and cl.exe using multiple jobs is discouraged
  785. makeCommand.push_back("/p:CL_MPCount=1");
  786. }
  787. makeCommand.insert(makeCommand.end(), makeOptions.begin(),
  788. makeOptions.end());
  789. }
  790. bool cmGlobalVisualStudio10Generator::Find64BitTools(cmMakefile* mf)
  791. {
  792. if (this->DefaultPlatformToolset == "v100") {
  793. // The v100 64-bit toolset does not exist in the express edition.
  794. this->DefaultPlatformToolset.clear();
  795. }
  796. if (this->GetPlatformToolset()) {
  797. return true;
  798. }
  799. // This edition does not come with 64-bit tools. Look for them.
  800. //
  801. // TODO: Detect available tools? x64\v100 exists but does not work?
  802. // HKLM\\SOFTWARE\\Microsoft\\MSBuild\\ToolsVersions\\4.0;VCTargetsPath
  803. // c:/Program Files (x86)/MSBuild/Microsoft.Cpp/v4.0/Platforms/
  804. // {Itanium,Win32,x64}/PlatformToolsets/{v100,v90,Windows7.1SDK}
  805. std::string winSDK_7_1;
  806. if (cmSystemTools::ReadRegistryValue(
  807. "HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Microsoft SDKs\\"
  808. "Windows\\v7.1;InstallationFolder",
  809. winSDK_7_1)) {
  810. std::ostringstream m;
  811. m << "Found Windows SDK v7.1: " << winSDK_7_1;
  812. mf->DisplayStatus(m.str().c_str(), -1);
  813. this->DefaultPlatformToolset = "Windows7.1SDK";
  814. return true;
  815. } else {
  816. std::ostringstream e;
  817. /* clang-format off */
  818. e << "Cannot enable 64-bit tools with Visual Studio 2010 Express.\n"
  819. << "Install the Microsoft Windows SDK v7.1 to get 64-bit tools:\n"
  820. << " http://msdn.microsoft.com/en-us/windows/bb980924.aspx";
  821. /* clang-format on */
  822. mf->IssueMessage(cmake::FATAL_ERROR, e.str().c_str());
  823. cmSystemTools::SetFatalErrorOccured();
  824. return false;
  825. }
  826. }
  827. std::string cmGlobalVisualStudio10Generator::GenerateRuleFile(
  828. std::string const& output) const
  829. {
  830. // The VS 10 generator needs to create the .rule files on disk.
  831. // Hide them away under the CMakeFiles directory.
  832. std::string ruleDir = this->GetCMakeInstance()->GetHomeOutputDirectory();
  833. ruleDir += cmake::GetCMakeFilesDirectory();
  834. ruleDir += "/";
  835. ruleDir += cmSystemTools::ComputeStringMD5(
  836. cmSystemTools::GetFilenamePath(output).c_str());
  837. std::string ruleFile = ruleDir + "/";
  838. ruleFile += cmSystemTools::GetFilenameName(output);
  839. ruleFile += ".rule";
  840. return ruleFile;
  841. }
  842. void cmGlobalVisualStudio10Generator::PathTooLong(cmGeneratorTarget* target,
  843. cmSourceFile const* sf,
  844. std::string const& sfRel)
  845. {
  846. size_t len =
  847. (strlen(target->GetLocalGenerator()->GetCurrentBinaryDirectory()) + 1 +
  848. sfRel.length());
  849. if (len > this->LongestSource.Length) {
  850. this->LongestSource.Length = len;
  851. this->LongestSource.Target = target;
  852. this->LongestSource.SourceFile = sf;
  853. this->LongestSource.SourceRel = sfRel;
  854. }
  855. }
  856. std::string cmGlobalVisualStudio10Generator::Encoding()
  857. {
  858. return "utf-8";
  859. }
  860. bool cmGlobalVisualStudio10Generator::IsNsightTegra() const
  861. {
  862. return !this->NsightTegraVersion.empty();
  863. }
  864. std::string cmGlobalVisualStudio10Generator::GetNsightTegraVersion() const
  865. {
  866. return this->NsightTegraVersion;
  867. }
  868. std::string cmGlobalVisualStudio10Generator::GetInstalledNsightTegraVersion()
  869. {
  870. std::string version;
  871. cmSystemTools::ReadRegistryValue(
  872. "HKEY_LOCAL_MACHINE\\SOFTWARE\\NVIDIA Corporation\\Nsight Tegra;"
  873. "Version",
  874. version, cmSystemTools::KeyWOW64_32);
  875. return version;
  876. }
  877. cmIDEFlagTable const* cmGlobalVisualStudio10Generator::GetClFlagTable() const
  878. {
  879. cmIDEFlagTable const* table = this->ToolsetOptions.GetClFlagTable(
  880. this->GetPlatformName(), this->GetPlatformToolsetString());
  881. return (table != nullptr) ? table : this->DefaultClFlagTable;
  882. }
  883. cmIDEFlagTable const* cmGlobalVisualStudio10Generator::GetCSharpFlagTable()
  884. const
  885. {
  886. cmIDEFlagTable const* table = this->ToolsetOptions.GetCSharpFlagTable(
  887. this->GetPlatformName(), this->GetPlatformToolsetString());
  888. return (table != nullptr) ? table : this->DefaultCSharpFlagTable;
  889. }
  890. cmIDEFlagTable const* cmGlobalVisualStudio10Generator::GetRcFlagTable() const
  891. {
  892. cmIDEFlagTable const* table = this->ToolsetOptions.GetRcFlagTable(
  893. this->GetPlatformName(), this->GetPlatformToolsetString());
  894. return (table != nullptr) ? table : this->DefaultRcFlagTable;
  895. }
  896. cmIDEFlagTable const* cmGlobalVisualStudio10Generator::GetLibFlagTable() const
  897. {
  898. cmIDEFlagTable const* table = this->ToolsetOptions.GetLibFlagTable(
  899. this->GetPlatformName(), this->GetPlatformToolsetString());
  900. return (table != nullptr) ? table : this->DefaultLibFlagTable;
  901. }
  902. cmIDEFlagTable const* cmGlobalVisualStudio10Generator::GetLinkFlagTable() const
  903. {
  904. cmIDEFlagTable const* table = this->ToolsetOptions.GetLinkFlagTable(
  905. this->GetPlatformName(), this->GetPlatformToolsetString());
  906. return (table != nullptr) ? table : this->DefaultLinkFlagTable;
  907. }
  908. cmIDEFlagTable const* cmGlobalVisualStudio10Generator::GetCudaFlagTable() const
  909. {
  910. return this->DefaultCudaFlagTable;
  911. }
  912. cmIDEFlagTable const* cmGlobalVisualStudio10Generator::GetCudaHostFlagTable()
  913. const
  914. {
  915. return this->DefaultCudaHostFlagTable;
  916. }
  917. cmIDEFlagTable const* cmGlobalVisualStudio10Generator::GetMasmFlagTable() const
  918. {
  919. cmIDEFlagTable const* table = this->ToolsetOptions.GetMasmFlagTable(
  920. this->GetPlatformName(), this->GetPlatformToolsetString());
  921. return (table != nullptr) ? table : this->DefaultMasmFlagTable;
  922. }
  923. cmIDEFlagTable const* cmGlobalVisualStudio10Generator::GetNasmFlagTable() const
  924. {
  925. return this->DefaultNasmFlagTable;
  926. }