cmVisualStudioGeneratorOptions.cxx 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531
  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::string const& di : cmMakeRange(this->Defines.cbegin(), de)) {
  393. // Escape the definition for the compiler.
  394. std::string define;
  395. if (this->Version < cmGlobalVisualStudioGenerator::VS10) {
  396. define = this->LocalGenerator->EscapeForShell(di, true);
  397. } else {
  398. define = di;
  399. }
  400. // Escape this flag for the MSBuild.
  401. if (this->Version >= cmGlobalVisualStudioGenerator::VS10) {
  402. cmVS10EscapeForMSBuild(define);
  403. if (lang == "RC") {
  404. cmSystemTools::ReplaceString(define, "\"", "\\\"");
  405. }
  406. }
  407. // Store the flag in the project file.
  408. oss << sep << define;
  409. sep = ";";
  410. }
  411. if (this->Version >= cmGlobalVisualStudioGenerator::VS10) {
  412. oss << ";%(" << tag << ")";
  413. }
  414. this->OutputFlag(fout, indent, tag, oss.str());
  415. }
  416. void cmVisualStudioGeneratorOptions::OutputAdditionalIncludeDirectories(
  417. std::ostream& fout, int indent, const std::string& lang)
  418. {
  419. if (this->Includes.empty()) {
  420. return;
  421. }
  422. const char* tag = "AdditionalIncludeDirectories";
  423. if (lang == "CUDA") {
  424. tag = "Include";
  425. } else if (lang == "ASM_MASM" || lang == "ASM_NASM") {
  426. tag = "IncludePaths";
  427. }
  428. std::ostringstream oss;
  429. const char* sep = "";
  430. for (std::string include : this->Includes) {
  431. // first convert all of the slashes
  432. std::string::size_type pos = 0;
  433. while ((pos = include.find('/', pos)) != std::string::npos) {
  434. include[pos] = '\\';
  435. pos++;
  436. }
  437. if (lang == "ASM_NASM") {
  438. include += "\\";
  439. }
  440. // Escape this include for the MSBuild.
  441. if (this->Version >= cmGlobalVisualStudioGenerator::VS10) {
  442. cmVS10EscapeForMSBuild(include);
  443. }
  444. oss << sep << include;
  445. sep = ";";
  446. if (lang == "Fortran") {
  447. include += "/$(ConfigurationName)";
  448. oss << sep << include;
  449. }
  450. }
  451. if (this->Version >= cmGlobalVisualStudioGenerator::VS10) {
  452. oss << sep << "%(" << tag << ")";
  453. }
  454. this->OutputFlag(fout, indent, tag, oss.str());
  455. }
  456. void cmVisualStudioGeneratorOptions::OutputFlagMap(std::ostream& fout,
  457. int indent)
  458. {
  459. for (auto const& m : this->FlagMap) {
  460. std::ostringstream oss;
  461. const char* sep = "";
  462. for (std::string i : m.second) {
  463. if (this->Version >= cmGlobalVisualStudioGenerator::VS10) {
  464. cmVS10EscapeForMSBuild(i);
  465. }
  466. oss << sep << i;
  467. sep = ";";
  468. }
  469. this->OutputFlag(fout, indent, m.first.c_str(), oss.str());
  470. }
  471. }