cmGlobalVisualStudio10Generator.cxx 53 KB

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