cmGlobalVisualStudio10Generator.cxx 54 KB

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