cmVisualStudioGeneratorOptions.cxx 15 KB

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