cmVisualStudioGeneratorOptions.cxx 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526
  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::VS10:
  66. case cmGlobalVisualStudioGenerator::VSVersion::VS11:
  67. case cmGlobalVisualStudioGenerator::VSVersion::VS12:
  68. case cmGlobalVisualStudioGenerator::VSVersion::VS14:
  69. case cmGlobalVisualStudioGenerator::VSVersion::VS15:
  70. case cmGlobalVisualStudioGenerator::VSVersion::VS16:
  71. case cmGlobalVisualStudioGenerator::VSVersion::VS17:
  72. // by default VS puts <ExceptionHandling></ExceptionHandling> empty
  73. // for a project, to make our projects look the same put a new line
  74. // and space over for the closing </ExceptionHandling> as the default
  75. // value
  76. this->FlagMap["ExceptionHandling"] = "\n ";
  77. break;
  78. default:
  79. this->FlagMap["ExceptionHandling"] = "0";
  80. break;
  81. }
  82. }
  83. void cmVisualStudioGeneratorOptions::SetVerboseMakefile(bool verbose)
  84. {
  85. // If verbose makefiles have been requested and the /nologo option
  86. // was not given explicitly in the flags we want to add an attribute
  87. // to the generated project to disable logo suppression. Otherwise
  88. // the GUI default is to enable suppression.
  89. //
  90. // On Visual Studio 10 (and later!), the value of this attribute should be
  91. // an empty string, instead of "FALSE", in order to avoid a warning:
  92. // "cl ... warning D9035: option 'nologo-' has been deprecated"
  93. //
  94. if (verbose &&
  95. this->FlagMap.find("SuppressStartupBanner") == this->FlagMap.end()) {
  96. this->FlagMap["SuppressStartupBanner"] =
  97. this->Version < cmGlobalVisualStudioGenerator::VSVersion::VS10 ? "FALSE"
  98. : "";
  99. }
  100. }
  101. bool cmVisualStudioGeneratorOptions::IsDebug() const
  102. {
  103. if (this->CurrentTool != CSharpCompiler) {
  104. return this->FlagMap.find("DebugInformationFormat") != this->FlagMap.end();
  105. }
  106. std::map<std::string, FlagValue>::const_iterator i =
  107. this->FlagMap.find("DebugType");
  108. if (i != this->FlagMap.end()) {
  109. if (i->second.size() == 1) {
  110. return i->second[0] != "none";
  111. }
  112. }
  113. return false;
  114. }
  115. bool cmVisualStudioGeneratorOptions::IsWinRt() const
  116. {
  117. return this->FlagMap.find("CompileAsWinRT") != this->FlagMap.end();
  118. }
  119. bool cmVisualStudioGeneratorOptions::IsManaged() const
  120. {
  121. return this->FlagMap.find("CompileAsManaged") != this->FlagMap.end();
  122. }
  123. bool cmVisualStudioGeneratorOptions::UsingUnicode() const
  124. {
  125. // Look for a _UNICODE definition.
  126. for (std::string const& di : this->Defines) {
  127. if (di == "_UNICODE") {
  128. return true;
  129. }
  130. }
  131. return false;
  132. }
  133. bool cmVisualStudioGeneratorOptions::UsingSBCS() const
  134. {
  135. // Look for a _SBCS definition.
  136. for (std::string const& di : this->Defines) {
  137. if (di == "_SBCS") {
  138. return true;
  139. }
  140. }
  141. return false;
  142. }
  143. void cmVisualStudioGeneratorOptions::FixCudaCodeGeneration()
  144. {
  145. // Extract temporary values stored by our flag table.
  146. FlagValue arch = this->TakeFlag("cmake-temp-arch");
  147. FlagValue code = this->TakeFlag("cmake-temp-code");
  148. FlagValue gencode = this->TakeFlag("cmake-temp-gencode");
  149. // No -code allowed without -arch.
  150. if (arch.empty()) {
  151. code.clear();
  152. }
  153. // Create a CodeGeneration field with [arch],[code] syntax in each entry.
  154. // CUDA will convert it to `-gencode=arch=[arch],code="[code],[arch]"`.
  155. FlagValue& result = this->FlagMap["CodeGeneration"];
  156. // If there are no flags, leave the CodeGeneration field empty.
  157. if (arch.empty() && gencode.empty()) {
  158. return;
  159. }
  160. // First entries for the -arch=<arch> [-code=<code>,...] pair.
  161. if (!arch.empty()) {
  162. std::string arch_name = arch[0];
  163. if (arch_name == "all" || arch_name == "all-major" ||
  164. arch_name == "native") {
  165. AppendFlagString("AdditionalOptions", "-arch=" + arch_name);
  166. return;
  167. }
  168. std::vector<std::string> codes;
  169. if (!code.empty()) {
  170. codes = cmTokenize(code[0], ",");
  171. }
  172. if (codes.empty()) {
  173. codes.push_back(arch_name);
  174. // nvcc -arch=<arch> has a special case that allows a real
  175. // architecture to be specified instead of a virtual arch.
  176. // It translates to -arch=<virtual> -code=<real>.
  177. cmSystemTools::ReplaceString(arch_name, "sm_", "compute_");
  178. }
  179. for (std::string const& c : codes) {
  180. std::string entry = arch_name + "," + c;
  181. result.push_back(entry);
  182. }
  183. }
  184. // Now add entries for the following signatures:
  185. // -gencode=<arch>,<code>
  186. // -gencode=<arch>,[<code1>,<code2>]
  187. // -gencode=<arch>,"<code1>,<code2>"
  188. for (std::string const& e : gencode) {
  189. std::string entry = e;
  190. cmSystemTools::ReplaceString(entry, "arch=", "");
  191. cmSystemTools::ReplaceString(entry, "code=", "");
  192. cmSystemTools::ReplaceString(entry, "[", "");
  193. cmSystemTools::ReplaceString(entry, "]", "");
  194. cmSystemTools::ReplaceString(entry, "\"", "");
  195. std::vector<std::string> codes = cmTokenize(entry, ",");
  196. if (codes.size() >= 2) {
  197. auto gencode_arch = cm::cbegin(codes);
  198. for (auto ci = gencode_arch + 1; ci != cm::cend(codes); ++ci) {
  199. std::string code_entry = *gencode_arch + "," + *ci;
  200. result.push_back(code_entry);
  201. }
  202. }
  203. }
  204. }
  205. void cmVisualStudioGeneratorOptions::FixManifestUACFlags()
  206. {
  207. static std::string const ENABLE_UAC = "EnableUAC";
  208. if (!HasFlag(ENABLE_UAC)) {
  209. return;
  210. }
  211. const std::string uacFlag = GetFlag(ENABLE_UAC);
  212. std::vector<std::string> subOptions;
  213. cmsys::SystemTools::Split(uacFlag, subOptions, ' ');
  214. if (subOptions.empty()) {
  215. AddFlag(ENABLE_UAC, "true");
  216. return;
  217. }
  218. if (subOptions.size() == 1 && subOptions[0] == "NO") {
  219. AddFlag(ENABLE_UAC, "false");
  220. return;
  221. }
  222. std::map<std::string, std::string> uacMap;
  223. uacMap["level"] = "UACExecutionLevel";
  224. uacMap["uiAccess"] = "UACUIAccess";
  225. std::map<std::string, std::string> uacExecuteLevelMap;
  226. uacExecuteLevelMap["asInvoker"] = "AsInvoker";
  227. uacExecuteLevelMap["highestAvailable"] = "HighestAvailable";
  228. uacExecuteLevelMap["requireAdministrator"] = "RequireAdministrator";
  229. for (std::string const& subopt : subOptions) {
  230. std::vector<std::string> keyValue;
  231. cmsys::SystemTools::Split(subopt, keyValue, '=');
  232. if (keyValue.size() != 2 || (uacMap.find(keyValue[0]) == uacMap.end())) {
  233. // ignore none key=value option or unknown flags
  234. continue;
  235. }
  236. if (keyValue[1].front() == '\'' && keyValue[1].back() == '\'') {
  237. keyValue[1] = keyValue[1].substr(
  238. 1, std::max(std::string::size_type(0), keyValue[1].length() - 2));
  239. }
  240. if (keyValue[0] == "level") {
  241. if (uacExecuteLevelMap.find(keyValue[1]) == uacExecuteLevelMap.end()) {
  242. // unknown level value
  243. continue;
  244. }
  245. AddFlag(uacMap[keyValue[0]], uacExecuteLevelMap[keyValue[1]]);
  246. continue;
  247. }
  248. if (keyValue[0] == "uiAccess") {
  249. if (keyValue[1] != "true" && keyValue[1] != "false") {
  250. // unknown uiAccess value
  251. continue;
  252. }
  253. AddFlag(uacMap[keyValue[0]], keyValue[1]);
  254. continue;
  255. }
  256. // unknown sub option
  257. }
  258. AddFlag(ENABLE_UAC, "true");
  259. }
  260. void cmVisualStudioGeneratorOptions::Parse(const std::string& flags)
  261. {
  262. // Parse the input string as a windows command line since the string
  263. // is intended for writing directly into the build files.
  264. std::vector<std::string> args;
  265. cmSystemTools::ParseWindowsCommandLine(flags.c_str(), args);
  266. // Process flags that need to be represented specially in the IDE
  267. // project file.
  268. for (std::string const& ai : args) {
  269. this->HandleFlag(ai);
  270. }
  271. }
  272. void cmVisualStudioGeneratorOptions::ParseFinish()
  273. {
  274. if (this->CurrentTool == FortranCompiler) {
  275. // "RuntimeLibrary" attribute values:
  276. // "rtMultiThreaded", "0", /threads /libs:static
  277. // "rtMultiThreadedDLL", "2", /threads /libs:dll
  278. // "rtMultiThreadedDebug", "1", /threads /dbglibs /libs:static
  279. // "rtMultiThreadedDebugDLL", "3", /threads /dbglibs /libs:dll
  280. // These seem unimplemented by the IDE:
  281. // "rtSingleThreaded", "4", /libs:static
  282. // "rtSingleThreadedDLL", "10", /libs:dll
  283. // "rtSingleThreadedDebug", "5", /dbglibs /libs:static
  284. // "rtSingleThreadedDebugDLL", "11", /dbglibs /libs:dll
  285. std::string rl =
  286. cmStrCat("rtMultiThreaded", this->FortranRuntimeDebug ? "Debug" : "",
  287. this->FortranRuntimeDLL ? "DLL" : "");
  288. this->FlagMap["RuntimeLibrary"] = rl;
  289. }
  290. if (this->CurrentTool == CudaCompiler) {
  291. std::map<std::string, FlagValue>::iterator i =
  292. this->FlagMap.find("CudaRuntime");
  293. if (i != this->FlagMap.end() && i->second.size() == 1) {
  294. std::string& cudaRuntime = i->second[0];
  295. if (cudaRuntime == "static") {
  296. cudaRuntime = "Static";
  297. } else if (cudaRuntime == "shared") {
  298. cudaRuntime = "Shared";
  299. } else if (cudaRuntime == "none") {
  300. cudaRuntime = "None";
  301. }
  302. }
  303. }
  304. }
  305. void cmVisualStudioGeneratorOptions::PrependInheritedString(
  306. std::string const& key)
  307. {
  308. std::map<std::string, FlagValue>::iterator i = this->FlagMap.find(key);
  309. if (i == this->FlagMap.end() || i->second.size() != 1) {
  310. return;
  311. }
  312. std::string& value = i->second[0];
  313. value = "%(" + key + ") " + value;
  314. }
  315. void cmVisualStudioGeneratorOptions::Reparse(std::string const& key)
  316. {
  317. std::map<std::string, FlagValue>::iterator i = this->FlagMap.find(key);
  318. if (i == this->FlagMap.end() || i->second.size() != 1) {
  319. return;
  320. }
  321. std::string const original = i->second[0];
  322. i->second[0] = "";
  323. this->UnknownFlagField = key;
  324. this->Parse(original);
  325. }
  326. void cmVisualStudioGeneratorOptions::StoreUnknownFlag(std::string const& flag)
  327. {
  328. // Look for Intel Fortran flags that do not map well in the flag table.
  329. if (this->CurrentTool == FortranCompiler) {
  330. if (flag == "/dbglibs" || flag == "-dbglibs") {
  331. this->FortranRuntimeDebug = true;
  332. return;
  333. }
  334. if (flag == "/threads" || flag == "-threads") {
  335. this->FortranRuntimeMT = true;
  336. return;
  337. }
  338. if (flag == "/libs:dll" || flag == "-libs:dll") {
  339. this->FortranRuntimeDLL = true;
  340. return;
  341. }
  342. if (flag == "/libs:static" || flag == "-libs:static") {
  343. this->FortranRuntimeDLL = false;
  344. return;
  345. }
  346. }
  347. // This option is not known. Store it in the output flags.
  348. std::string const opts = cmOutputConverter::EscapeWindowsShellArgument(
  349. flag.c_str(),
  350. cmOutputConverter::Shell_Flag_AllowMakeVariables |
  351. cmOutputConverter::Shell_Flag_VSIDE);
  352. this->AppendFlagString(this->UnknownFlagField, opts);
  353. }
  354. cmIDEOptions::FlagValue cmVisualStudioGeneratorOptions::TakeFlag(
  355. std::string const& key)
  356. {
  357. FlagValue value;
  358. std::map<std::string, FlagValue>::iterator i = this->FlagMap.find(key);
  359. if (i != this->FlagMap.end()) {
  360. value = i->second;
  361. this->FlagMap.erase(i);
  362. }
  363. return value;
  364. }
  365. void cmVisualStudioGeneratorOptions::SetConfiguration(
  366. const std::string& config)
  367. {
  368. this->Configuration = config;
  369. }
  370. const std::string& cmVisualStudioGeneratorOptions::GetConfiguration() const
  371. {
  372. return this->Configuration;
  373. }
  374. void cmVisualStudioGeneratorOptions::OutputPreprocessorDefinitions(
  375. std::ostream& fout, int indent, const std::string& lang)
  376. {
  377. if (this->Defines.empty()) {
  378. return;
  379. }
  380. std::string tag = "PreprocessorDefinitions";
  381. if (lang == "CUDA") {
  382. tag = "Defines";
  383. }
  384. std::ostringstream oss;
  385. if (this->Version >= cmGlobalVisualStudioGenerator::VSVersion::VS10) {
  386. oss << "%(" << tag << ")";
  387. }
  388. std::vector<std::string>::const_iterator de =
  389. cmRemoveDuplicates(this->Defines);
  390. for (std::string const& di : cmMakeRange(this->Defines.cbegin(), de)) {
  391. // Escape the definition for the compiler.
  392. std::string define;
  393. if (this->Version < cmGlobalVisualStudioGenerator::VSVersion::VS10) {
  394. define = this->LocalGenerator->EscapeForShell(di, true);
  395. } else {
  396. define = di;
  397. }
  398. // Escape this flag for the MSBuild.
  399. if (this->Version >= cmGlobalVisualStudioGenerator::VSVersion::VS10) {
  400. cmVS10EscapeForMSBuild(define);
  401. if (lang == "RC") {
  402. cmSystemTools::ReplaceString(define, "\"", "\\\"");
  403. }
  404. }
  405. // Store the flag in the project file.
  406. oss << ';' << define;
  407. }
  408. this->OutputFlag(fout, indent, tag, oss.str());
  409. }
  410. void cmVisualStudioGeneratorOptions::OutputAdditionalIncludeDirectories(
  411. std::ostream& fout, int indent, const std::string& lang)
  412. {
  413. if (this->Includes.empty()) {
  414. return;
  415. }
  416. std::string tag = "AdditionalIncludeDirectories";
  417. if (lang == "CUDA") {
  418. tag = "Include";
  419. } else if (lang == "ASM_MASM" || lang == "ASM_NASM") {
  420. tag = "IncludePaths";
  421. }
  422. std::ostringstream oss;
  423. const char* sep = "";
  424. for (std::string include : this->Includes) {
  425. // first convert all of the slashes
  426. std::string::size_type pos = 0;
  427. while ((pos = include.find('/', pos)) != std::string::npos) {
  428. include[pos] = '\\';
  429. pos++;
  430. }
  431. if (lang == "ASM_NASM") {
  432. include += "\\";
  433. }
  434. // Escape this include for the MSBuild.
  435. if (this->Version >= cmGlobalVisualStudioGenerator::VSVersion::VS10) {
  436. cmVS10EscapeForMSBuild(include);
  437. }
  438. oss << sep << include;
  439. sep = ";";
  440. if (lang == "Fortran") {
  441. include += "/$(ConfigurationName)";
  442. oss << sep << include;
  443. }
  444. }
  445. if (this->Version >= cmGlobalVisualStudioGenerator::VSVersion::VS10) {
  446. oss << sep << "%(" << tag << ")";
  447. }
  448. this->OutputFlag(fout, indent, tag, oss.str());
  449. }
  450. void cmVisualStudioGeneratorOptions::OutputFlagMap(std::ostream& fout,
  451. int indent)
  452. {
  453. for (auto const& m : this->FlagMap) {
  454. std::ostringstream oss;
  455. const char* sep = "";
  456. for (std::string i : m.second) {
  457. if (this->Version >= cmGlobalVisualStudioGenerator::VSVersion::VS10) {
  458. cmVS10EscapeForMSBuild(i);
  459. }
  460. oss << sep << i;
  461. sep = ";";
  462. }
  463. this->OutputFlag(fout, indent, m.first, oss.str());
  464. }
  465. }