cmVisualStudioGeneratorOptions.cxx 13 KB

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