cmGlobalVisualStudio10Generator.cxx 53 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672
  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. case cmGlobalVisualStudioGenerator::VS17:
  1177. return "17.0";
  1178. }
  1179. return "";
  1180. }
  1181. bool cmGlobalVisualStudio10Generator::IsNsightTegra() const
  1182. {
  1183. return !this->NsightTegraVersion.empty();
  1184. }
  1185. std::string cmGlobalVisualStudio10Generator::GetNsightTegraVersion() const
  1186. {
  1187. return this->NsightTegraVersion;
  1188. }
  1189. std::string cmGlobalVisualStudio10Generator::GetInstalledNsightTegraVersion()
  1190. {
  1191. std::string version;
  1192. cmSystemTools::ReadRegistryValue(
  1193. "HKEY_LOCAL_MACHINE\\SOFTWARE\\NVIDIA Corporation\\Nsight Tegra;"
  1194. "Version",
  1195. version, cmSystemTools::KeyWOW64_32);
  1196. return version;
  1197. }
  1198. std::string cmGlobalVisualStudio10Generator::GetApplicationTypeRevision() const
  1199. {
  1200. if (this->GetSystemName() == "Android") {
  1201. return this->GetAndroidApplicationTypeRevision();
  1202. }
  1203. // Return the first two '.'-separated components of the Windows version.
  1204. std::string::size_type end1 = this->SystemVersion.find('.');
  1205. std::string::size_type end2 =
  1206. end1 == std::string::npos ? end1 : this->SystemVersion.find('.', end1 + 1);
  1207. return this->SystemVersion.substr(0, end2);
  1208. }
  1209. static std::string cmLoadFlagTableString(Json::Value entry, const char* field)
  1210. {
  1211. if (entry.isMember(field)) {
  1212. auto string = entry[field];
  1213. if (string.isConvertibleTo(Json::ValueType::stringValue)) {
  1214. return string.asString();
  1215. }
  1216. }
  1217. return "";
  1218. }
  1219. static unsigned int cmLoadFlagTableSpecial(Json::Value entry,
  1220. const char* field)
  1221. {
  1222. unsigned int value = 0;
  1223. if (entry.isMember(field)) {
  1224. auto specials = entry[field];
  1225. if (specials.isArray()) {
  1226. for (auto const& special : specials) {
  1227. std::string s = special.asString();
  1228. if (s == "UserValue") {
  1229. value |= cmIDEFlagTable::UserValue;
  1230. } else if (s == "UserIgnored") {
  1231. value |= cmIDEFlagTable::UserIgnored;
  1232. } else if (s == "UserRequired") {
  1233. value |= cmIDEFlagTable::UserRequired;
  1234. } else if (s == "Continue") {
  1235. value |= cmIDEFlagTable::Continue;
  1236. } else if (s == "SemicolonAppendable") {
  1237. value |= cmIDEFlagTable::SemicolonAppendable;
  1238. } else if (s == "UserFollowing") {
  1239. value |= cmIDEFlagTable::UserFollowing;
  1240. } else if (s == "CaseInsensitive") {
  1241. value |= cmIDEFlagTable::CaseInsensitive;
  1242. } else if (s == "SpaceAppendable") {
  1243. value |= cmIDEFlagTable::SpaceAppendable;
  1244. } else if (s == "CommaAppendable") {
  1245. value |= cmIDEFlagTable::CommaAppendable;
  1246. }
  1247. }
  1248. }
  1249. }
  1250. return value;
  1251. }
  1252. namespace {
  1253. cmIDEFlagTable const* cmLoadFlagTableJson(std::string const& flagJsonPath,
  1254. cm::optional<std::string> vsVer)
  1255. {
  1256. cmIDEFlagTable* ret = nullptr;
  1257. auto savedFlagIterator = loadedFlagJsonFiles.find(flagJsonPath);
  1258. if (savedFlagIterator != loadedFlagJsonFiles.end()) {
  1259. ret = savedFlagIterator->second.data();
  1260. } else {
  1261. Json::Reader reader;
  1262. cmsys::ifstream stream;
  1263. stream.open(flagJsonPath.c_str(), std::ios_base::in);
  1264. if (stream) {
  1265. Json::Value flags;
  1266. if (reader.parse(stream, flags, false) && flags.isArray()) {
  1267. std::vector<cmIDEFlagTable> flagTable;
  1268. for (auto const& flag : flags) {
  1269. Json::Value const& vsminJson = flag["vsmin"];
  1270. if (vsminJson.isString()) {
  1271. std::string const& vsmin = vsminJson.asString();
  1272. if (!vsmin.empty()) {
  1273. if (!vsVer ||
  1274. cmSystemTools::VersionCompareGreater(vsmin, *vsVer)) {
  1275. continue;
  1276. }
  1277. }
  1278. }
  1279. cmIDEFlagTable flagEntry;
  1280. flagEntry.IDEName = cmLoadFlagTableString(flag, "name");
  1281. flagEntry.commandFlag = cmLoadFlagTableString(flag, "switch");
  1282. flagEntry.comment = cmLoadFlagTableString(flag, "comment");
  1283. flagEntry.value = cmLoadFlagTableString(flag, "value");
  1284. flagEntry.special = cmLoadFlagTableSpecial(flag, "flags");
  1285. flagTable.push_back(flagEntry);
  1286. }
  1287. cmIDEFlagTable endFlag{ "", "", "", "", 0 };
  1288. flagTable.push_back(endFlag);
  1289. loadedFlagJsonFiles[flagJsonPath] = flagTable;
  1290. ret = loadedFlagJsonFiles[flagJsonPath].data();
  1291. }
  1292. }
  1293. }
  1294. return ret;
  1295. }
  1296. }
  1297. cm::optional<std::string> cmGlobalVisualStudio10Generator::FindFlagTable(
  1298. cm::string_view toolsetName, cm::string_view table) const
  1299. {
  1300. if (!this->CustomFlagTableDir.empty()) {
  1301. std::string customFlagTableFile =
  1302. cmStrCat(this->CustomFlagTableDir, '/', this->GetPlatformName(), '_',
  1303. toolsetName, '_', table, ".json");
  1304. if (cmSystemTools::FileExists(customFlagTableFile)) {
  1305. return customFlagTableFile;
  1306. }
  1307. customFlagTableFile =
  1308. cmStrCat(this->CustomFlagTableDir, '/', this->GetPlatformName(), '_',
  1309. table, ".json");
  1310. if (cmSystemTools::FileExists(customFlagTableFile)) {
  1311. return customFlagTableFile;
  1312. }
  1313. }
  1314. std::string fullPath =
  1315. cmStrCat(cmSystemTools::GetCMakeRoot(), "/Templates/MSBuild/FlagTables/",
  1316. toolsetName, '_', table, ".json");
  1317. if (cmSystemTools::FileExists(fullPath)) {
  1318. return fullPath;
  1319. }
  1320. return {};
  1321. }
  1322. cmIDEFlagTable const* cmGlobalVisualStudio10Generator::LoadFlagTable(
  1323. std::string const& toolSpecificName, std::string const& defaultName,
  1324. std::string const& table) const
  1325. {
  1326. cmMakefile* mf = this->GetCurrentMakefile();
  1327. std::string filename;
  1328. if (!toolSpecificName.empty()) {
  1329. if (cm::optional<std::string> found =
  1330. this->FindFlagTable(toolSpecificName, table)) {
  1331. filename = std::move(*found);
  1332. } else {
  1333. mf->IssueMessage(MessageType::FATAL_ERROR,
  1334. cmStrCat("JSON flag table for ", table,
  1335. " not found for toolset ", toolSpecificName));
  1336. return nullptr;
  1337. }
  1338. } else {
  1339. std::string const& genericName =
  1340. this->CanonicalToolsetName(this->GetPlatformToolsetString());
  1341. cm::optional<std::string> found = this->FindFlagTable(genericName, table);
  1342. if (!found) {
  1343. found = this->FindFlagTable(defaultName, table);
  1344. }
  1345. if (found) {
  1346. filename = std::move(*found);
  1347. } else {
  1348. mf->IssueMessage(MessageType::FATAL_ERROR,
  1349. cmStrCat("JSON flag table for ", table,
  1350. " not found for toolset ", genericName, " ",
  1351. defaultName));
  1352. return nullptr;
  1353. }
  1354. }
  1355. cm::optional<std::string> vsVer = this->GetVSInstanceVersion();
  1356. if (cmIDEFlagTable const* ret = cmLoadFlagTableJson(filename, vsVer)) {
  1357. return ret;
  1358. }
  1359. mf->IssueMessage(
  1360. MessageType::FATAL_ERROR,
  1361. cmStrCat("JSON flag table could not be loaded:\n ", filename));
  1362. return nullptr;
  1363. }
  1364. cmIDEFlagTable const* cmGlobalVisualStudio10Generator::GetClFlagTable() const
  1365. {
  1366. return LoadFlagTable(this->GetClFlagTableName(),
  1367. this->DefaultCLFlagTableName, "CL");
  1368. }
  1369. cmIDEFlagTable const* cmGlobalVisualStudio10Generator::GetCSharpFlagTable()
  1370. const
  1371. {
  1372. return LoadFlagTable(this->GetCSharpFlagTableName(),
  1373. this->DefaultCSharpFlagTableName, "CSharp");
  1374. }
  1375. cmIDEFlagTable const* cmGlobalVisualStudio10Generator::GetRcFlagTable() const
  1376. {
  1377. return LoadFlagTable(this->GetRcFlagTableName(),
  1378. this->DefaultRCFlagTableName, "RC");
  1379. }
  1380. cmIDEFlagTable const* cmGlobalVisualStudio10Generator::GetLibFlagTable() const
  1381. {
  1382. return LoadFlagTable(this->GetLibFlagTableName(),
  1383. this->DefaultLibFlagTableName, "LIB");
  1384. }
  1385. cmIDEFlagTable const* cmGlobalVisualStudio10Generator::GetLinkFlagTable() const
  1386. {
  1387. return LoadFlagTable(this->GetLinkFlagTableName(),
  1388. this->DefaultLinkFlagTableName, "Link");
  1389. }
  1390. cmIDEFlagTable const* cmGlobalVisualStudio10Generator::GetCudaFlagTable() const
  1391. {
  1392. return LoadFlagTable(std::string(), this->DefaultCudaFlagTableName, "Cuda");
  1393. }
  1394. cmIDEFlagTable const* cmGlobalVisualStudio10Generator::GetCudaHostFlagTable()
  1395. const
  1396. {
  1397. return LoadFlagTable(std::string(), this->DefaultCudaHostFlagTableName,
  1398. "CudaHost");
  1399. }
  1400. cmIDEFlagTable const* cmGlobalVisualStudio10Generator::GetMasmFlagTable() const
  1401. {
  1402. return LoadFlagTable(this->GetMasmFlagTableName(),
  1403. this->DefaultMasmFlagTableName, "MASM");
  1404. }
  1405. cmIDEFlagTable const* cmGlobalVisualStudio10Generator::GetNasmFlagTable() const
  1406. {
  1407. return LoadFlagTable(std::string(), this->DefaultNasmFlagTableName, "NASM");
  1408. }
  1409. std::string cmGlobalVisualStudio10Generator::GetClFlagTableName() const
  1410. {
  1411. std::string const& toolset = this->GetPlatformToolsetString();
  1412. std::string const useToolset = this->CanonicalToolsetName(toolset);
  1413. if (toolset == "v142") {
  1414. return "v142";
  1415. } else if (toolset == "v141") {
  1416. return "v141";
  1417. } else if (useToolset == "v140") {
  1418. return "v140";
  1419. } else if (useToolset == "v120") {
  1420. return "v12";
  1421. } else if (useToolset == "v110") {
  1422. return "v11";
  1423. } else if (useToolset == "v100") {
  1424. return "v10";
  1425. } else {
  1426. return "";
  1427. }
  1428. }
  1429. std::string cmGlobalVisualStudio10Generator::GetCSharpFlagTableName() const
  1430. {
  1431. std::string const& toolset = this->GetPlatformToolsetString();
  1432. std::string const useToolset = this->CanonicalToolsetName(toolset);
  1433. if (useToolset == "v142") {
  1434. return "v142";
  1435. } else if (useToolset == "v141") {
  1436. return "v141";
  1437. } else if (useToolset == "v140") {
  1438. return "v140";
  1439. } else if (useToolset == "v120") {
  1440. return "v12";
  1441. } else if (useToolset == "v110") {
  1442. return "v11";
  1443. } else if (useToolset == "v100") {
  1444. return "v10";
  1445. } else {
  1446. return "";
  1447. }
  1448. }
  1449. std::string cmGlobalVisualStudio10Generator::GetRcFlagTableName() const
  1450. {
  1451. std::string const& toolset = this->GetPlatformToolsetString();
  1452. std::string const useToolset = this->CanonicalToolsetName(toolset);
  1453. if ((useToolset == "v140") || (useToolset == "v141") ||
  1454. (useToolset == "v142")) {
  1455. return "v14";
  1456. } else if (useToolset == "v120") {
  1457. return "v12";
  1458. } else if (useToolset == "v110") {
  1459. return "v11";
  1460. } else if (useToolset == "v100") {
  1461. return "v10";
  1462. } else {
  1463. return "";
  1464. }
  1465. }
  1466. std::string cmGlobalVisualStudio10Generator::GetLibFlagTableName() const
  1467. {
  1468. std::string const& toolset = this->GetPlatformToolsetString();
  1469. std::string const useToolset = this->CanonicalToolsetName(toolset);
  1470. if ((useToolset == "v140") || (useToolset == "v141") ||
  1471. (useToolset == "v142")) {
  1472. return "v14";
  1473. } else if (useToolset == "v120") {
  1474. return "v12";
  1475. } else if (useToolset == "v110") {
  1476. return "v11";
  1477. } else if (useToolset == "v100") {
  1478. return "v10";
  1479. } else {
  1480. return "";
  1481. }
  1482. }
  1483. std::string cmGlobalVisualStudio10Generator::GetLinkFlagTableName() const
  1484. {
  1485. std::string const& toolset = this->GetPlatformToolsetString();
  1486. std::string const useToolset = this->CanonicalToolsetName(toolset);
  1487. if (useToolset == "v142") {
  1488. return "v142";
  1489. } else if (useToolset == "v141") {
  1490. return "v141";
  1491. } else if (useToolset == "v140") {
  1492. return "v140";
  1493. } else if (useToolset == "v120") {
  1494. return "v12";
  1495. } else if (useToolset == "v110") {
  1496. return "v11";
  1497. } else if (useToolset == "v100") {
  1498. return "v10";
  1499. } else {
  1500. return "";
  1501. }
  1502. }
  1503. std::string cmGlobalVisualStudio10Generator::GetMasmFlagTableName() const
  1504. {
  1505. std::string const& toolset = this->GetPlatformToolsetString();
  1506. std::string const useToolset = this->CanonicalToolsetName(toolset);
  1507. if ((useToolset == "v140") || (useToolset == "v141") ||
  1508. (useToolset == "v142")) {
  1509. return "v14";
  1510. } else if (useToolset == "v120") {
  1511. return "v12";
  1512. } else if (useToolset == "v110") {
  1513. return "v11";
  1514. } else if (useToolset == "v100") {
  1515. return "v10";
  1516. } else {
  1517. return "";
  1518. }
  1519. }
  1520. std::string cmGlobalVisualStudio10Generator::CanonicalToolsetName(
  1521. std::string const& toolset) const
  1522. {
  1523. std::size_t length = toolset.length();
  1524. if (cmHasLiteralSuffix(toolset, "_xp")) {
  1525. length -= 3;
  1526. }
  1527. return toolset.substr(0, length);
  1528. }