cmVisualStudioGeneratorOptions.cxx 13 KB

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