cmVisualStudioGeneratorOptions.cxx 18 KB

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