cmVisualStudioGeneratorOptions.cxx 14 KB

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