cmVisualStudioGeneratorOptions.cxx 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457
  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. bool cmVisualStudioGeneratorOptions::IsWinRt() const
  112. {
  113. return this->FlagMap.find("CompileAsWinRT") != this->FlagMap.end();
  114. }
  115. bool cmVisualStudioGeneratorOptions::IsManaged() const
  116. {
  117. return this->FlagMap.find("CompileAsManaged") != this->FlagMap.end();
  118. }
  119. bool cmVisualStudioGeneratorOptions::UsingUnicode() const
  120. {
  121. // Look for a _UNICODE definition.
  122. return std::any_of(
  123. this->Defines.begin(), this->Defines.end(), [](std::string const& di) {
  124. return di == "_UNICODE"_s || cmHasLiteralPrefix(di, "_UNICODE=");
  125. });
  126. }
  127. bool cmVisualStudioGeneratorOptions::UsingSBCS() const
  128. {
  129. // Look for a _SBCS definition.
  130. return std::any_of(
  131. this->Defines.begin(), this->Defines.end(), [](std::string const& di) {
  132. return di == "_SBCS"_s || cmHasLiteralPrefix(di, "_SBCS=");
  133. });
  134. }
  135. void cmVisualStudioGeneratorOptions::FixCudaCodeGeneration()
  136. {
  137. // Create an empty CodeGeneration field, and pass the the actual
  138. // compile flags via additional options so that we have consistent
  139. // behavior and avoid issues with MSBuild extensions injecting
  140. // virtual code when we request real only.
  141. FlagValue& code_gen_flag = this->FlagMap["CodeGeneration"];
  142. code_gen_flag = "";
  143. }
  144. void cmVisualStudioGeneratorOptions::FixManifestUACFlags()
  145. {
  146. static std::string const ENABLE_UAC = "EnableUAC";
  147. if (!HasFlag(ENABLE_UAC)) {
  148. return;
  149. }
  150. const std::string uacFlag = GetFlag(ENABLE_UAC);
  151. std::vector<std::string> subOptions;
  152. cmsys::SystemTools::Split(uacFlag, subOptions, ' ');
  153. if (subOptions.empty()) {
  154. AddFlag(ENABLE_UAC, "true");
  155. return;
  156. }
  157. if (subOptions.size() == 1 && subOptions[0] == "NO"_s) {
  158. AddFlag(ENABLE_UAC, "false");
  159. return;
  160. }
  161. std::map<std::string, std::string> uacMap;
  162. uacMap["level"] = "UACExecutionLevel";
  163. uacMap["uiAccess"] = "UACUIAccess";
  164. std::map<std::string, std::string> uacExecuteLevelMap;
  165. uacExecuteLevelMap["asInvoker"] = "AsInvoker";
  166. uacExecuteLevelMap["highestAvailable"] = "HighestAvailable";
  167. uacExecuteLevelMap["requireAdministrator"] = "RequireAdministrator";
  168. for (std::string const& subopt : subOptions) {
  169. std::vector<std::string> keyValue;
  170. cmsys::SystemTools::Split(subopt, keyValue, '=');
  171. if (keyValue.size() != 2 || (uacMap.find(keyValue[0]) == uacMap.end())) {
  172. // ignore none key=value option or unknown flags
  173. continue;
  174. }
  175. if (keyValue[1].front() == '\'' && keyValue[1].back() == '\'') {
  176. keyValue[1] = keyValue[1].substr(
  177. 1, std::max(std::string::size_type(0), keyValue[1].length() - 2));
  178. }
  179. if (keyValue[0] == "level"_s) {
  180. if (uacExecuteLevelMap.find(keyValue[1]) == uacExecuteLevelMap.end()) {
  181. // unknown level value
  182. continue;
  183. }
  184. AddFlag(uacMap[keyValue[0]], uacExecuteLevelMap[keyValue[1]]);
  185. continue;
  186. }
  187. if (keyValue[0] == "uiAccess"_s) {
  188. if (keyValue[1] != "true"_s && keyValue[1] != "false"_s) {
  189. // unknown uiAccess value
  190. continue;
  191. }
  192. AddFlag(uacMap[keyValue[0]], keyValue[1]);
  193. continue;
  194. }
  195. // unknown sub option
  196. }
  197. AddFlag(ENABLE_UAC, "true");
  198. }
  199. void cmVisualStudioGeneratorOptions::Parse(const std::string& flags)
  200. {
  201. // Parse the input string as a windows command line since the string
  202. // is intended for writing directly into the build files.
  203. std::vector<std::string> args;
  204. cmSystemTools::ParseWindowsCommandLine(flags.c_str(), args);
  205. // Process flags that need to be represented specially in the IDE
  206. // project file.
  207. for (std::string const& ai : args) {
  208. this->HandleFlag(ai);
  209. }
  210. }
  211. void cmVisualStudioGeneratorOptions::ParseFinish()
  212. {
  213. if (this->CurrentTool == FortranCompiler) {
  214. // "RuntimeLibrary" attribute values:
  215. // "rtMultiThreaded", "0", /threads /libs:static
  216. // "rtMultiThreadedDLL", "2", /threads /libs:dll
  217. // "rtMultiThreadedDebug", "1", /threads /dbglibs /libs:static
  218. // "rtMultiThreadedDebugDLL", "3", /threads /dbglibs /libs:dll
  219. // These seem unimplemented by the IDE:
  220. // "rtSingleThreaded", "4", /libs:static
  221. // "rtSingleThreadedDLL", "10", /libs:dll
  222. // "rtSingleThreadedDebug", "5", /dbglibs /libs:static
  223. // "rtSingleThreadedDebugDLL", "11", /dbglibs /libs:dll
  224. std::string rl =
  225. cmStrCat("rtMultiThreaded", this->FortranRuntimeDebug ? "Debug" : "",
  226. this->FortranRuntimeDLL ? "DLL" : "");
  227. this->FlagMap["RuntimeLibrary"] = rl;
  228. }
  229. if (this->CurrentTool == CudaCompiler) {
  230. auto i = this->FlagMap.find("CudaRuntime");
  231. if (i != this->FlagMap.end() && i->second.size() == 1) {
  232. std::string& cudaRuntime = i->second[0];
  233. if (cudaRuntime == "static"_s) {
  234. cudaRuntime = "Static";
  235. } else if (cudaRuntime == "shared"_s) {
  236. cudaRuntime = "Shared";
  237. } else if (cudaRuntime == "none"_s) {
  238. cudaRuntime = "None";
  239. }
  240. }
  241. }
  242. }
  243. void cmVisualStudioGeneratorOptions::PrependInheritedString(
  244. std::string const& key)
  245. {
  246. auto i = this->FlagMap.find(key);
  247. if (i == this->FlagMap.end() || i->second.size() != 1) {
  248. return;
  249. }
  250. std::string& value = i->second[0];
  251. value = cmStrCat("%(", key, ") ", value);
  252. }
  253. void cmVisualStudioGeneratorOptions::Reparse(std::string const& key)
  254. {
  255. auto i = this->FlagMap.find(key);
  256. if (i == this->FlagMap.end() || i->second.size() != 1) {
  257. return;
  258. }
  259. std::string const original = i->second[0];
  260. i->second[0] = "";
  261. this->UnknownFlagField = key;
  262. this->Parse(original);
  263. }
  264. void cmVisualStudioGeneratorOptions::StoreUnknownFlag(std::string const& flag)
  265. {
  266. // Look for Intel Fortran flags that do not map well in the flag table.
  267. if (this->CurrentTool == FortranCompiler) {
  268. if (flag == "/dbglibs"_s || flag == "-dbglibs"_s) {
  269. this->FortranRuntimeDebug = true;
  270. return;
  271. }
  272. if (flag == "/threads"_s || flag == "-threads"_s) {
  273. this->FortranRuntimeMT = true;
  274. return;
  275. }
  276. if (flag == "/libs:dll"_s || flag == "-libs:dll"_s) {
  277. this->FortranRuntimeDLL = true;
  278. return;
  279. }
  280. if (flag == "/libs:static"_s || flag == "-libs:static"_s) {
  281. this->FortranRuntimeDLL = false;
  282. return;
  283. }
  284. }
  285. // This option is not known. Store it in the output flags.
  286. std::string const opts = cmOutputConverter::EscapeWindowsShellArgument(
  287. flag.c_str(),
  288. cmOutputConverter::Shell_Flag_AllowMakeVariables |
  289. cmOutputConverter::Shell_Flag_VSIDE);
  290. this->AppendFlagString(this->UnknownFlagField, opts);
  291. }
  292. cmIDEOptions::FlagValue cmVisualStudioGeneratorOptions::TakeFlag(
  293. std::string const& key)
  294. {
  295. FlagValue value;
  296. auto i = this->FlagMap.find(key);
  297. if (i != this->FlagMap.end()) {
  298. value = i->second;
  299. this->FlagMap.erase(i);
  300. }
  301. return value;
  302. }
  303. void cmVisualStudioGeneratorOptions::SetConfiguration(
  304. const std::string& config)
  305. {
  306. this->Configuration = config;
  307. }
  308. const std::string& cmVisualStudioGeneratorOptions::GetConfiguration() const
  309. {
  310. return this->Configuration;
  311. }
  312. void cmVisualStudioGeneratorOptions::OutputPreprocessorDefinitions(
  313. std::ostream& fout, int indent, const std::string& lang)
  314. {
  315. if (this->Defines.empty()) {
  316. return;
  317. }
  318. std::string tag = "PreprocessorDefinitions";
  319. if (lang == "CUDA"_s) {
  320. tag = "Defines";
  321. }
  322. std::ostringstream oss;
  323. if (this->Version != cmGlobalVisualStudioGenerator::VSVersion::VS9) {
  324. oss << "%(" << tag << ')';
  325. }
  326. auto de = cmRemoveDuplicates(this->Defines);
  327. for (std::string const& di : cmMakeRange(this->Defines.cbegin(), de)) {
  328. // Escape the definition for the compiler.
  329. std::string define;
  330. if (this->Version == cmGlobalVisualStudioGenerator::VSVersion::VS9) {
  331. define = this->LocalGenerator->EscapeForShell(di, true);
  332. } else {
  333. define = di;
  334. }
  335. // Escape this flag for the MSBuild.
  336. if (this->Version != cmGlobalVisualStudioGenerator::VSVersion::VS9) {
  337. cmVS10EscapeForMSBuild(define);
  338. if (lang == "RC"_s) {
  339. cmSystemTools::ReplaceString(define, "\"", "\\\"");
  340. }
  341. }
  342. // Store the flag in the project file.
  343. oss << ';' << define;
  344. }
  345. this->OutputFlag(fout, indent, tag, oss.str());
  346. }
  347. void cmVisualStudioGeneratorOptions::OutputAdditionalIncludeDirectories(
  348. std::ostream& fout, int indent, const std::string& lang)
  349. {
  350. if (this->Includes.empty()) {
  351. return;
  352. }
  353. std::string tag = "AdditionalIncludeDirectories";
  354. if (lang == "CUDA"_s) {
  355. tag = "Include";
  356. } else if (lang == "ASM_MASM"_s || lang == "ASM_NASM"_s) {
  357. tag = "IncludePaths";
  358. }
  359. std::ostringstream oss;
  360. const char* sep = "";
  361. for (std::string include : this->Includes) {
  362. // first convert all of the slashes
  363. std::string::size_type pos = 0;
  364. while ((pos = include.find('/', pos)) != std::string::npos) {
  365. include[pos] = '\\';
  366. pos++;
  367. }
  368. if (lang == "ASM_NASM"_s) {
  369. include += '\\';
  370. }
  371. // Escape this include for the MSBuild.
  372. if (this->Version != cmGlobalVisualStudioGenerator::VSVersion::VS9) {
  373. cmVS10EscapeForMSBuild(include);
  374. }
  375. oss << sep << include;
  376. sep = ";";
  377. if (lang == "Fortran"_s) {
  378. include += "/$(ConfigurationName)";
  379. oss << sep << include;
  380. }
  381. }
  382. if (this->Version != cmGlobalVisualStudioGenerator::VSVersion::VS9) {
  383. oss << sep << "%(" << tag << ')';
  384. }
  385. this->OutputFlag(fout, indent, tag, oss.str());
  386. }
  387. void cmVisualStudioGeneratorOptions::OutputFlagMap(std::ostream& fout,
  388. int indent)
  389. {
  390. for (auto const& m : this->FlagMap) {
  391. std::ostringstream oss;
  392. const char* sep = "";
  393. for (std::string i : m.second) {
  394. if (this->Version != cmGlobalVisualStudioGenerator::VSVersion::VS9) {
  395. cmVS10EscapeForMSBuild(i);
  396. }
  397. oss << sep << i;
  398. sep = ";";
  399. }
  400. this->OutputFlag(fout, indent, m.first, oss.str());
  401. }
  402. }