cmGlobalVisualStudio10Generator.cxx 49 KB

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