cmVisualStudioGeneratorOptions.cxx 13 KB

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