cmVisualStudioGeneratorOptions.cxx 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536
  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 (std::vector<std::string>::iterator ci = codes.begin();
  217. ci != codes.end(); ++ci) {
  218. std::string entry = arch_name + "," + *ci;
  219. result.push_back(entry);
  220. }
  221. }
  222. // Now add entries for the -gencode=<arch>,<code> pairs.
  223. for (std::vector<std::string>::iterator ei = gencode.begin();
  224. ei != gencode.end(); ++ei) {
  225. std::string entry = *ei;
  226. cmSystemTools::ReplaceString(entry, "arch=", "");
  227. cmSystemTools::ReplaceString(entry, "code=", "");
  228. result.push_back(entry);
  229. }
  230. }
  231. void cmVisualStudioGeneratorOptions::FixManifestUACFlags()
  232. {
  233. static std::string const ENABLE_UAC = "EnableUAC";
  234. if (!HasFlag(ENABLE_UAC)) {
  235. return;
  236. }
  237. const std::string uacFlag = GetFlag(ENABLE_UAC);
  238. std::vector<std::string> subOptions;
  239. cmsys::SystemTools::Split(uacFlag, subOptions, ' ');
  240. if (subOptions.empty()) {
  241. AddFlag(ENABLE_UAC, "true");
  242. return;
  243. }
  244. if (subOptions.size() == 1 && subOptions[0] == "NO") {
  245. AddFlag(ENABLE_UAC, "false");
  246. return;
  247. }
  248. std::map<std::string, std::string> uacMap;
  249. uacMap["level"] = "UACExecutionLevel";
  250. uacMap["uiAccess"] = "UACUIAccess";
  251. std::map<std::string, std::string> uacExecuteLevelMap;
  252. uacExecuteLevelMap["asInvoker"] = "AsInvoker";
  253. uacExecuteLevelMap["highestAvailable"] = "HighestAvailable";
  254. uacExecuteLevelMap["requireAdministrator"] = "RequireAdministrator";
  255. for (auto const& subopt : subOptions) {
  256. std::vector<std::string> keyValue;
  257. cmsys::SystemTools::Split(subopt, keyValue, '=');
  258. if (keyValue.size() != 2 || (uacMap.find(keyValue[0]) == uacMap.end())) {
  259. // ignore none key=value option or unknown flags
  260. continue;
  261. }
  262. if (keyValue[1].front() == '\'' && keyValue[1].back() == '\'') {
  263. keyValue[1] =
  264. keyValue[1].substr(1, std::max<int>(0, keyValue[1].size() - 2));
  265. }
  266. if (keyValue[0] == "level") {
  267. if (uacExecuteLevelMap.find(keyValue[1]) == uacExecuteLevelMap.end()) {
  268. // unknown level value
  269. continue;
  270. }
  271. AddFlag(uacMap[keyValue[0]], uacExecuteLevelMap[keyValue[1]]);
  272. continue;
  273. }
  274. if (keyValue[0] == "uiAccess") {
  275. if (keyValue[1] != "true" && keyValue[1] != "false") {
  276. // unknown uiAccess value
  277. continue;
  278. }
  279. AddFlag(uacMap[keyValue[0]], keyValue[1]);
  280. continue;
  281. }
  282. // unknown sub option
  283. }
  284. AddFlag(ENABLE_UAC, "true");
  285. }
  286. void cmVisualStudioGeneratorOptions::Parse(const char* flags)
  287. {
  288. // Parse the input string as a windows command line since the string
  289. // is intended for writing directly into the build files.
  290. std::vector<std::string> args;
  291. cmSystemTools::ParseWindowsCommandLine(flags, args);
  292. // Process flags that need to be represented specially in the IDE
  293. // project file.
  294. for (std::vector<std::string>::iterator ai = args.begin(); ai != args.end();
  295. ++ai) {
  296. this->HandleFlag(ai->c_str());
  297. }
  298. }
  299. void cmVisualStudioGeneratorOptions::ParseFinish()
  300. {
  301. if (this->CurrentTool == FortranCompiler) {
  302. // "RuntimeLibrary" attribute values:
  303. // "rtMultiThreaded", "0", /threads /libs:static
  304. // "rtMultiThreadedDLL", "2", /threads /libs:dll
  305. // "rtMultiThreadedDebug", "1", /threads /dbglibs /libs:static
  306. // "rtMultiThreadedDebugDLL", "3", /threads /dbglibs /libs:dll
  307. // These seem unimplemented by the IDE:
  308. // "rtSingleThreaded", "4", /libs:static
  309. // "rtSingleThreadedDLL", "10", /libs:dll
  310. // "rtSingleThreadedDebug", "5", /dbglibs /libs:static
  311. // "rtSingleThreadedDebugDLL", "11", /dbglibs /libs:dll
  312. std::string rl = "rtMultiThreaded";
  313. rl += this->FortranRuntimeDebug ? "Debug" : "";
  314. rl += this->FortranRuntimeDLL ? "DLL" : "";
  315. this->FlagMap["RuntimeLibrary"] = rl;
  316. }
  317. if (this->CurrentTool == CudaCompiler) {
  318. std::map<std::string, FlagValue>::iterator i =
  319. this->FlagMap.find("CudaRuntime");
  320. if (i != this->FlagMap.end() && i->second.size() == 1) {
  321. std::string& cudaRuntime = i->second[0];
  322. if (cudaRuntime == "static") {
  323. cudaRuntime = "Static";
  324. } else if (cudaRuntime == "shared") {
  325. cudaRuntime = "Shared";
  326. } else if (cudaRuntime == "none") {
  327. cudaRuntime = "None";
  328. }
  329. }
  330. }
  331. }
  332. void cmVisualStudioGeneratorOptions::PrependInheritedString(
  333. std::string const& key)
  334. {
  335. std::map<std::string, FlagValue>::iterator i = this->FlagMap.find(key);
  336. if (i == this->FlagMap.end() || i->second.size() != 1) {
  337. return;
  338. }
  339. std::string& value = i->second[0];
  340. value = "%(" + key + ") " + value;
  341. }
  342. void cmVisualStudioGeneratorOptions::Reparse(std::string const& key)
  343. {
  344. std::map<std::string, FlagValue>::iterator i = this->FlagMap.find(key);
  345. if (i == this->FlagMap.end() || i->second.size() != 1) {
  346. return;
  347. }
  348. std::string const original = i->second[0];
  349. i->second[0] = "";
  350. this->UnknownFlagField = key;
  351. this->Parse(original.c_str());
  352. }
  353. void cmVisualStudioGeneratorOptions::StoreUnknownFlag(const char* flag)
  354. {
  355. // Look for Intel Fortran flags that do not map well in the flag table.
  356. if (this->CurrentTool == FortranCompiler) {
  357. if (strcmp(flag, "/dbglibs") == 0) {
  358. this->FortranRuntimeDebug = true;
  359. return;
  360. }
  361. if (strcmp(flag, "/threads") == 0) {
  362. this->FortranRuntimeMT = true;
  363. return;
  364. }
  365. if (strcmp(flag, "/libs:dll") == 0) {
  366. this->FortranRuntimeDLL = true;
  367. return;
  368. }
  369. if (strcmp(flag, "/libs:static") == 0) {
  370. this->FortranRuntimeDLL = false;
  371. return;
  372. }
  373. }
  374. // This option is not known. Store it in the output flags.
  375. std::string const opts = cmOutputConverter::EscapeWindowsShellArgument(
  376. flag, cmOutputConverter::Shell_Flag_AllowMakeVariables |
  377. cmOutputConverter::Shell_Flag_VSIDE);
  378. this->AppendFlagString(this->UnknownFlagField, opts);
  379. }
  380. cmIDEOptions::FlagValue cmVisualStudioGeneratorOptions::TakeFlag(
  381. std::string const& key)
  382. {
  383. FlagValue value;
  384. std::map<std::string, FlagValue>::iterator i = this->FlagMap.find(key);
  385. if (i != this->FlagMap.end()) {
  386. value = i->second;
  387. this->FlagMap.erase(i);
  388. }
  389. return value;
  390. }
  391. void cmVisualStudioGeneratorOptions::SetConfiguration(const char* config)
  392. {
  393. this->Configuration = config;
  394. }
  395. void cmVisualStudioGeneratorOptions::OutputPreprocessorDefinitions(
  396. std::ostream& fout, const char* prefix, const char* suffix,
  397. const std::string& lang)
  398. {
  399. if (this->Defines.empty()) {
  400. return;
  401. }
  402. const char* tag = "PreprocessorDefinitions";
  403. if (lang == "CUDA") {
  404. tag = "Defines";
  405. }
  406. if (this->Version >= cmGlobalVisualStudioGenerator::VS10) {
  407. // if there are configuration specific flags, then
  408. // use the configuration specific tag for PreprocessorDefinitions
  409. if (!this->Configuration.empty()) {
  410. fout << prefix;
  411. this->TargetGenerator->WritePlatformConfigTag(
  412. tag, this->Configuration.c_str(), 0, 0, 0, &fout);
  413. } else {
  414. fout << prefix << "<" << tag << ">";
  415. }
  416. } else {
  417. fout << prefix << tag << "=\"";
  418. }
  419. const char* sep = "";
  420. std::vector<std::string>::const_iterator de =
  421. cmRemoveDuplicates(this->Defines);
  422. for (std::vector<std::string>::const_iterator di = this->Defines.begin();
  423. di != de; ++di) {
  424. // Escape the definition for the compiler.
  425. std::string define;
  426. if (this->Version < cmGlobalVisualStudioGenerator::VS10) {
  427. define = this->LocalGenerator->EscapeForShell(di->c_str(), true);
  428. } else {
  429. define = *di;
  430. }
  431. // Escape this flag for the IDE.
  432. if (this->Version >= cmGlobalVisualStudioGenerator::VS10) {
  433. define = cmVisualStudio10GeneratorOptionsEscapeForXML(define);
  434. if (lang == "RC") {
  435. cmSystemTools::ReplaceString(define, "\"", "\\\"");
  436. }
  437. } else {
  438. define = cmVisualStudioGeneratorOptionsEscapeForXML(define);
  439. }
  440. // Store the flag in the project file.
  441. fout << sep << define;
  442. sep = ";";
  443. }
  444. if (this->Version >= cmGlobalVisualStudioGenerator::VS10) {
  445. fout << ";%(" << tag << ")</" << tag << ">" << suffix;
  446. } else {
  447. fout << "\"" << suffix;
  448. }
  449. }
  450. void cmVisualStudioGeneratorOptions::OutputFlagMap(std::ostream& fout,
  451. const char* indent)
  452. {
  453. if (this->Version >= cmGlobalVisualStudioGenerator::VS10) {
  454. for (std::map<std::string, FlagValue>::iterator m = this->FlagMap.begin();
  455. m != this->FlagMap.end(); ++m) {
  456. fout << indent;
  457. if (!this->Configuration.empty()) {
  458. this->TargetGenerator->WritePlatformConfigTag(
  459. m->first.c_str(), this->Configuration.c_str(), 0, 0, 0, &fout);
  460. } else {
  461. fout << "<" << m->first << ">";
  462. }
  463. const char* sep = "";
  464. for (std::vector<std::string>::iterator i = m->second.begin();
  465. i != m->second.end(); ++i) {
  466. fout << sep << cmVisualStudio10GeneratorOptionsEscapeForXML(*i);
  467. sep = ";";
  468. }
  469. fout << "</" << m->first << ">\n";
  470. }
  471. } else {
  472. for (std::map<std::string, FlagValue>::iterator m = this->FlagMap.begin();
  473. m != this->FlagMap.end(); ++m) {
  474. fout << indent << m->first << "=\"";
  475. const char* sep = "";
  476. for (std::vector<std::string>::iterator i = m->second.begin();
  477. i != m->second.end(); ++i) {
  478. fout << sep << cmVisualStudioGeneratorOptionsEscapeForXML(*i);
  479. sep = ";";
  480. }
  481. fout << "\"\n";
  482. }
  483. }
  484. }