cmVisualStudioGeneratorOptions.cxx 13 KB

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