cmGlobalVisualStudio10Generator.cxx 53 KB

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