cmVisualStudioGeneratorOptions.cxx 13 KB

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