cmGlobalVisualStudio10Generator.cxx 49 KB

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