cmGlobalVisualStudio10Generator.cxx 49 KB

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