cmVisualStudioGeneratorOptions.cxx 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548
  1. #include "cmVisualStudioGeneratorOptions.h"
  2. #include "cmAlgorithms.h"
  3. #include "cmLocalVisualStudioGenerator.h"
  4. #include "cmOutputConverter.h"
  5. #include "cmSystemTools.h"
  6. #include "cmVisualStudio10TargetGenerator.h"
  7. static std::string cmVisualStudio10GeneratorOptionsEscapeForXML(
  8. std::string ret)
  9. {
  10. cmSystemTools::ReplaceString(ret, ";", "%3B");
  11. cmSystemTools::ReplaceString(ret, "&", "&");
  12. cmSystemTools::ReplaceString(ret, "<", "&lt;");
  13. cmSystemTools::ReplaceString(ret, ">", "&gt;");
  14. return ret;
  15. }
  16. static std::string cmVisualStudioGeneratorOptionsEscapeForXML(std::string ret)
  17. {
  18. cmSystemTools::ReplaceString(ret, "&", "&amp;");
  19. cmSystemTools::ReplaceString(ret, "\"", "&quot;");
  20. cmSystemTools::ReplaceString(ret, "<", "&lt;");
  21. cmSystemTools::ReplaceString(ret, ">", "&gt;");
  22. cmSystemTools::ReplaceString(ret, "\n", "&#x0D;&#x0A;");
  23. return ret;
  24. }
  25. cmVisualStudioGeneratorOptions::cmVisualStudioGeneratorOptions(
  26. cmLocalVisualStudioGenerator* lg, Tool tool,
  27. cmVisualStudio10TargetGenerator* g)
  28. : cmIDEOptions()
  29. , LocalGenerator(lg)
  30. , Version(lg->GetVersion())
  31. , CurrentTool(tool)
  32. , TargetGenerator(g)
  33. {
  34. // Preprocessor definitions are not allowed for linker tools.
  35. this->AllowDefine = (tool != Linker);
  36. // Slash options are allowed for VS.
  37. this->AllowSlash = true;
  38. this->FortranRuntimeDebug = false;
  39. this->FortranRuntimeDLL = false;
  40. this->FortranRuntimeMT = false;
  41. this->UnknownFlagField = "AdditionalOptions";
  42. }
  43. cmVisualStudioGeneratorOptions::cmVisualStudioGeneratorOptions(
  44. cmLocalVisualStudioGenerator* lg, Tool tool, cmVS7FlagTable const* table,
  45. cmVS7FlagTable const* extraTable, cmVisualStudio10TargetGenerator* g)
  46. : cmIDEOptions()
  47. , LocalGenerator(lg)
  48. , Version(lg->GetVersion())
  49. , CurrentTool(tool)
  50. , TargetGenerator(g)
  51. {
  52. // Store the given flag tables.
  53. this->AddTable(table);
  54. this->AddTable(extraTable);
  55. // Preprocessor definitions are not allowed for linker tools.
  56. this->AllowDefine = (tool != Linker);
  57. // Slash options are allowed for VS.
  58. this->AllowSlash = true;
  59. this->FortranRuntimeDebug = false;
  60. this->FortranRuntimeDLL = false;
  61. this->FortranRuntimeMT = false;
  62. this->UnknownFlagField = "AdditionalOptions";
  63. }
  64. void cmVisualStudioGeneratorOptions::AddTable(cmVS7FlagTable const* table)
  65. {
  66. if (table) {
  67. for (int i = 0; i < FlagTableCount; ++i) {
  68. if (!this->FlagTable[i]) {
  69. this->FlagTable[i] = table;
  70. break;
  71. }
  72. }
  73. }
  74. }
  75. void cmVisualStudioGeneratorOptions::ClearTables()
  76. {
  77. for (int i = 0; i < FlagTableCount; ++i) {
  78. this->FlagTable[i] = nullptr;
  79. }
  80. }
  81. void cmVisualStudioGeneratorOptions::FixExceptionHandlingDefault()
  82. {
  83. // Exception handling is on by default because the platform file has
  84. // "/EHsc" in the flags. Normally, that will override this
  85. // initialization to off, but the user has the option of removing
  86. // the flag to disable exception handling. When the user does
  87. // remove the flag we need to override the IDE default of on.
  88. switch (this->Version) {
  89. case cmGlobalVisualStudioGenerator::VS10:
  90. case cmGlobalVisualStudioGenerator::VS11:
  91. case cmGlobalVisualStudioGenerator::VS12:
  92. case cmGlobalVisualStudioGenerator::VS14:
  93. case cmGlobalVisualStudioGenerator::VS15:
  94. // by default VS puts <ExceptionHandling></ExceptionHandling> empty
  95. // for a project, to make our projects look the same put a new line
  96. // and space over for the closing </ExceptionHandling> as the default
  97. // value
  98. this->FlagMap["ExceptionHandling"] = "\n ";
  99. break;
  100. default:
  101. this->FlagMap["ExceptionHandling"] = "0";
  102. break;
  103. }
  104. }
  105. void cmVisualStudioGeneratorOptions::SetVerboseMakefile(bool verbose)
  106. {
  107. // If verbose makefiles have been requested and the /nologo option
  108. // was not given explicitly in the flags we want to add an attribute
  109. // to the generated project to disable logo suppression. Otherwise
  110. // the GUI default is to enable suppression.
  111. //
  112. // On Visual Studio 10 (and later!), the value of this attribute should be
  113. // an empty string, instead of "FALSE", in order to avoid a warning:
  114. // "cl ... warning D9035: option 'nologo-' has been deprecated"
  115. //
  116. if (verbose &&
  117. this->FlagMap.find("SuppressStartupBanner") == this->FlagMap.end()) {
  118. this->FlagMap["SuppressStartupBanner"] =
  119. this->Version < cmGlobalVisualStudioGenerator::VS10 ? "FALSE" : "";
  120. }
  121. }
  122. bool cmVisualStudioGeneratorOptions::IsDebug() const
  123. {
  124. if (this->CurrentTool != CSharpCompiler) {
  125. return this->FlagMap.find("DebugInformationFormat") != this->FlagMap.end();
  126. }
  127. std::map<std::string, FlagValue>::const_iterator i =
  128. this->FlagMap.find("DebugType");
  129. if (i != this->FlagMap.end()) {
  130. if (i->second.size() == 1) {
  131. return i->second[0] != "none";
  132. }
  133. }
  134. return false;
  135. }
  136. bool cmVisualStudioGeneratorOptions::IsWinRt() const
  137. {
  138. return this->FlagMap.find("CompileAsWinRT") != this->FlagMap.end();
  139. }
  140. bool cmVisualStudioGeneratorOptions::IsManaged() const
  141. {
  142. return this->FlagMap.find("CompileAsManaged") != this->FlagMap.end();
  143. }
  144. bool cmVisualStudioGeneratorOptions::UsingUnicode() const
  145. {
  146. // Look for the a _UNICODE definition.
  147. for (std::vector<std::string>::const_iterator di = this->Defines.begin();
  148. di != this->Defines.end(); ++di) {
  149. if (*di == "_UNICODE") {
  150. return true;
  151. }
  152. }
  153. return false;
  154. }
  155. bool cmVisualStudioGeneratorOptions::UsingSBCS() const
  156. {
  157. // Look for the a _SBCS definition.
  158. for (std::vector<std::string>::const_iterator di = this->Defines.begin();
  159. di != this->Defines.end(); ++di) {
  160. if (*di == "_SBCS") {
  161. return true;
  162. }
  163. }
  164. return false;
  165. }
  166. cmVisualStudioGeneratorOptions::CudaRuntime
  167. cmVisualStudioGeneratorOptions::GetCudaRuntime() const
  168. {
  169. std::map<std::string, FlagValue>::const_iterator i =
  170. this->FlagMap.find("CudaRuntime");
  171. if (i != this->FlagMap.end() && i->second.size() == 1) {
  172. std::string const& cudaRuntime = i->second[0];
  173. if (cudaRuntime == "Static") {
  174. return CudaRuntimeStatic;
  175. }
  176. if (cudaRuntime == "Shared") {
  177. return CudaRuntimeShared;
  178. }
  179. if (cudaRuntime == "None") {
  180. return CudaRuntimeNone;
  181. }
  182. }
  183. // nvcc default is static
  184. return CudaRuntimeStatic;
  185. }
  186. void cmVisualStudioGeneratorOptions::FixCudaCodeGeneration()
  187. {
  188. // Extract temporary values stored by our flag table.
  189. FlagValue arch = this->TakeFlag("cmake-temp-arch");
  190. FlagValue code = this->TakeFlag("cmake-temp-code");
  191. FlagValue gencode = this->TakeFlag("cmake-temp-gencode");
  192. // No -code allowed without -arch.
  193. if (arch.empty()) {
  194. code.clear();
  195. }
  196. if (arch.empty() && gencode.empty()) {
  197. return;
  198. }
  199. // Create a CodeGeneration field with [arch],[code] syntax in each entry.
  200. // CUDA will convert it to `-gencode=arch=[arch],code="[code],[arch]"`.
  201. FlagValue& result = this->FlagMap["CodeGeneration"];
  202. // First entries for the -arch=<arch> [-code=<code>,...] pair.
  203. if (!arch.empty()) {
  204. std::string arch_name = arch[0];
  205. std::vector<std::string> codes;
  206. if (!code.empty()) {
  207. codes = cmSystemTools::tokenize(code[0], ",");
  208. }
  209. if (codes.empty()) {
  210. codes.push_back(arch_name);
  211. // nvcc -arch=<arch> has a special case that allows a real
  212. // architecture to be specified instead of a virtual arch.
  213. // It translates to -arch=<virtual> -code=<real>.
  214. cmSystemTools::ReplaceString(arch_name, "sm_", "compute_");
  215. }
  216. for (auto const& c : codes) {
  217. std::string entry = arch_name + "," + c;
  218. result.push_back(entry);
  219. }
  220. }
  221. // Now add entries for the following signatures:
  222. // -gencode=<arch>,<code>
  223. // -gencode=<arch>,[<code1>,<code2>]
  224. // -gencode=<arch>,"<code1>,<code2>"
  225. for (auto const& e : gencode) {
  226. std::string entry = e;
  227. cmSystemTools::ReplaceString(entry, "arch=", "");
  228. cmSystemTools::ReplaceString(entry, "code=", "");
  229. cmSystemTools::ReplaceString(entry, "[", "");
  230. cmSystemTools::ReplaceString(entry, "]", "");
  231. cmSystemTools::ReplaceString(entry, "\"", "");
  232. std::vector<std::string> codes = cmSystemTools::tokenize(entry, ",");
  233. if (codes.size() >= 2) {
  234. auto gencode_arch = cm::cbegin(codes);
  235. for (auto ci = gencode_arch + 1; ci != cm::cend(codes); ++ci) {
  236. std::string code_entry = *gencode_arch + "," + *ci;
  237. result.push_back(code_entry);
  238. }
  239. }
  240. }
  241. }
  242. void cmVisualStudioGeneratorOptions::FixManifestUACFlags()
  243. {
  244. static std::string const ENABLE_UAC = "EnableUAC";
  245. if (!HasFlag(ENABLE_UAC)) {
  246. return;
  247. }
  248. const std::string uacFlag = GetFlag(ENABLE_UAC);
  249. std::vector<std::string> subOptions;
  250. cmsys::SystemTools::Split(uacFlag, subOptions, ' ');
  251. if (subOptions.empty()) {
  252. AddFlag(ENABLE_UAC, "true");
  253. return;
  254. }
  255. if (subOptions.size() == 1 && subOptions[0] == "NO") {
  256. AddFlag(ENABLE_UAC, "false");
  257. return;
  258. }
  259. std::map<std::string, std::string> uacMap;
  260. uacMap["level"] = "UACExecutionLevel";
  261. uacMap["uiAccess"] = "UACUIAccess";
  262. std::map<std::string, std::string> uacExecuteLevelMap;
  263. uacExecuteLevelMap["asInvoker"] = "AsInvoker";
  264. uacExecuteLevelMap["highestAvailable"] = "HighestAvailable";
  265. uacExecuteLevelMap["requireAdministrator"] = "RequireAdministrator";
  266. for (auto const& subopt : subOptions) {
  267. std::vector<std::string> keyValue;
  268. cmsys::SystemTools::Split(subopt, keyValue, '=');
  269. if (keyValue.size() != 2 || (uacMap.find(keyValue[0]) == uacMap.end())) {
  270. // ignore none key=value option or unknown flags
  271. continue;
  272. }
  273. if (keyValue[1].front() == '\'' && keyValue[1].back() == '\'') {
  274. keyValue[1] =
  275. keyValue[1].substr(1, std::max<int>(0, keyValue[1].size() - 2));
  276. }
  277. if (keyValue[0] == "level") {
  278. if (uacExecuteLevelMap.find(keyValue[1]) == uacExecuteLevelMap.end()) {
  279. // unknown level value
  280. continue;
  281. }
  282. AddFlag(uacMap[keyValue[0]], uacExecuteLevelMap[keyValue[1]]);
  283. continue;
  284. }
  285. if (keyValue[0] == "uiAccess") {
  286. if (keyValue[1] != "true" && keyValue[1] != "false") {
  287. // unknown uiAccess value
  288. continue;
  289. }
  290. AddFlag(uacMap[keyValue[0]], keyValue[1]);
  291. continue;
  292. }
  293. // unknown sub option
  294. }
  295. AddFlag(ENABLE_UAC, "true");
  296. }
  297. void cmVisualStudioGeneratorOptions::Parse(const char* flags)
  298. {
  299. // Parse the input string as a windows command line since the string
  300. // is intended for writing directly into the build files.
  301. std::vector<std::string> args;
  302. cmSystemTools::ParseWindowsCommandLine(flags, args);
  303. // Process flags that need to be represented specially in the IDE
  304. // project file.
  305. for (std::vector<std::string>::iterator ai = args.begin(); ai != args.end();
  306. ++ai) {
  307. this->HandleFlag(ai->c_str());
  308. }
  309. }
  310. void cmVisualStudioGeneratorOptions::ParseFinish()
  311. {
  312. if (this->CurrentTool == FortranCompiler) {
  313. // "RuntimeLibrary" attribute values:
  314. // "rtMultiThreaded", "0", /threads /libs:static
  315. // "rtMultiThreadedDLL", "2", /threads /libs:dll
  316. // "rtMultiThreadedDebug", "1", /threads /dbglibs /libs:static
  317. // "rtMultiThreadedDebugDLL", "3", /threads /dbglibs /libs:dll
  318. // These seem unimplemented by the IDE:
  319. // "rtSingleThreaded", "4", /libs:static
  320. // "rtSingleThreadedDLL", "10", /libs:dll
  321. // "rtSingleThreadedDebug", "5", /dbglibs /libs:static
  322. // "rtSingleThreadedDebugDLL", "11", /dbglibs /libs:dll
  323. std::string rl = "rtMultiThreaded";
  324. rl += this->FortranRuntimeDebug ? "Debug" : "";
  325. rl += this->FortranRuntimeDLL ? "DLL" : "";
  326. this->FlagMap["RuntimeLibrary"] = rl;
  327. }
  328. if (this->CurrentTool == CudaCompiler) {
  329. std::map<std::string, FlagValue>::iterator i =
  330. this->FlagMap.find("CudaRuntime");
  331. if (i != this->FlagMap.end() && i->second.size() == 1) {
  332. std::string& cudaRuntime = i->second[0];
  333. if (cudaRuntime == "static") {
  334. cudaRuntime = "Static";
  335. } else if (cudaRuntime == "shared") {
  336. cudaRuntime = "Shared";
  337. } else if (cudaRuntime == "none") {
  338. cudaRuntime = "None";
  339. }
  340. }
  341. }
  342. }
  343. void cmVisualStudioGeneratorOptions::PrependInheritedString(
  344. std::string const& key)
  345. {
  346. std::map<std::string, FlagValue>::iterator i = this->FlagMap.find(key);
  347. if (i == this->FlagMap.end() || i->second.size() != 1) {
  348. return;
  349. }
  350. std::string& value = i->second[0];
  351. value = "%(" + key + ") " + value;
  352. }
  353. void cmVisualStudioGeneratorOptions::Reparse(std::string const& key)
  354. {
  355. std::map<std::string, FlagValue>::iterator i = this->FlagMap.find(key);
  356. if (i == this->FlagMap.end() || i->second.size() != 1) {
  357. return;
  358. }
  359. std::string const original = i->second[0];
  360. i->second[0] = "";
  361. this->UnknownFlagField = key;
  362. this->Parse(original.c_str());
  363. }
  364. void cmVisualStudioGeneratorOptions::StoreUnknownFlag(const char* flag)
  365. {
  366. // Look for Intel Fortran flags that do not map well in the flag table.
  367. if (this->CurrentTool == FortranCompiler) {
  368. if (strcmp(flag, "/dbglibs") == 0) {
  369. this->FortranRuntimeDebug = true;
  370. return;
  371. }
  372. if (strcmp(flag, "/threads") == 0) {
  373. this->FortranRuntimeMT = true;
  374. return;
  375. }
  376. if (strcmp(flag, "/libs:dll") == 0) {
  377. this->FortranRuntimeDLL = true;
  378. return;
  379. }
  380. if (strcmp(flag, "/libs:static") == 0) {
  381. this->FortranRuntimeDLL = false;
  382. return;
  383. }
  384. }
  385. // This option is not known. Store it in the output flags.
  386. std::string const opts = cmOutputConverter::EscapeWindowsShellArgument(
  387. flag, cmOutputConverter::Shell_Flag_AllowMakeVariables |
  388. cmOutputConverter::Shell_Flag_VSIDE);
  389. this->AppendFlagString(this->UnknownFlagField, opts);
  390. }
  391. cmIDEOptions::FlagValue cmVisualStudioGeneratorOptions::TakeFlag(
  392. std::string const& key)
  393. {
  394. FlagValue value;
  395. std::map<std::string, FlagValue>::iterator i = this->FlagMap.find(key);
  396. if (i != this->FlagMap.end()) {
  397. value = i->second;
  398. this->FlagMap.erase(i);
  399. }
  400. return value;
  401. }
  402. void cmVisualStudioGeneratorOptions::SetConfiguration(const char* config)
  403. {
  404. this->Configuration = config;
  405. }
  406. void cmVisualStudioGeneratorOptions::OutputPreprocessorDefinitions(
  407. std::ostream& fout, const char* prefix, const char* suffix,
  408. const std::string& lang)
  409. {
  410. if (this->Defines.empty()) {
  411. return;
  412. }
  413. const char* tag = "PreprocessorDefinitions";
  414. if (lang == "CUDA") {
  415. tag = "Defines";
  416. }
  417. if (this->Version >= cmGlobalVisualStudioGenerator::VS10) {
  418. // if there are configuration specific flags, then
  419. // use the configuration specific tag for PreprocessorDefinitions
  420. if (!this->Configuration.empty()) {
  421. fout << prefix;
  422. this->TargetGenerator->WritePlatformConfigTag(
  423. tag, this->Configuration.c_str(), 0, 0, 0, &fout);
  424. } else {
  425. fout << prefix << "<" << tag << ">";
  426. }
  427. } else {
  428. fout << prefix << tag << "=\"";
  429. }
  430. const char* sep = "";
  431. std::vector<std::string>::const_iterator de =
  432. cmRemoveDuplicates(this->Defines);
  433. for (std::vector<std::string>::const_iterator di = this->Defines.begin();
  434. di != de; ++di) {
  435. // Escape the definition for the compiler.
  436. std::string define;
  437. if (this->Version < cmGlobalVisualStudioGenerator::VS10) {
  438. define = this->LocalGenerator->EscapeForShell(di->c_str(), true);
  439. } else {
  440. define = *di;
  441. }
  442. // Escape this flag for the IDE.
  443. if (this->Version >= cmGlobalVisualStudioGenerator::VS10) {
  444. define = cmVisualStudio10GeneratorOptionsEscapeForXML(define);
  445. if (lang == "RC") {
  446. cmSystemTools::ReplaceString(define, "\"", "\\\"");
  447. }
  448. } else {
  449. define = cmVisualStudioGeneratorOptionsEscapeForXML(define);
  450. }
  451. // Store the flag in the project file.
  452. fout << sep << define;
  453. sep = ";";
  454. }
  455. if (this->Version >= cmGlobalVisualStudioGenerator::VS10) {
  456. fout << ";%(" << tag << ")</" << tag << ">" << suffix;
  457. } else {
  458. fout << "\"" << suffix;
  459. }
  460. }
  461. void cmVisualStudioGeneratorOptions::OutputFlagMap(std::ostream& fout,
  462. const char* indent)
  463. {
  464. if (this->Version >= cmGlobalVisualStudioGenerator::VS10) {
  465. for (std::map<std::string, FlagValue>::iterator m = this->FlagMap.begin();
  466. m != this->FlagMap.end(); ++m) {
  467. fout << indent;
  468. if (!this->Configuration.empty()) {
  469. this->TargetGenerator->WritePlatformConfigTag(
  470. m->first.c_str(), this->Configuration.c_str(), 0, 0, 0, &fout);
  471. } else {
  472. fout << "<" << m->first << ">";
  473. }
  474. const char* sep = "";
  475. for (std::vector<std::string>::iterator i = m->second.begin();
  476. i != m->second.end(); ++i) {
  477. fout << sep << cmVisualStudio10GeneratorOptionsEscapeForXML(*i);
  478. sep = ";";
  479. }
  480. fout << "</" << m->first << ">\n";
  481. }
  482. } else {
  483. for (std::map<std::string, FlagValue>::iterator m = this->FlagMap.begin();
  484. m != this->FlagMap.end(); ++m) {
  485. fout << indent << m->first << "=\"";
  486. const char* sep = "";
  487. for (std::vector<std::string>::iterator i = m->second.begin();
  488. i != m->second.end(); ++i) {
  489. fout << sep << cmVisualStudioGeneratorOptionsEscapeForXML(*i);
  490. sep = ";";
  491. }
  492. fout << "\"\n";
  493. }
  494. }
  495. }