cmVisualStudioGeneratorOptions.cxx 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525
  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. AppendFlagString("AdditionalOptions", "-arch=" + arch_name);
  165. return;
  166. }
  167. std::vector<std::string> codes;
  168. if (!code.empty()) {
  169. codes = cmTokenize(code[0], ",");
  170. }
  171. if (codes.empty()) {
  172. codes.push_back(arch_name);
  173. // nvcc -arch=<arch> has a special case that allows a real
  174. // architecture to be specified instead of a virtual arch.
  175. // It translates to -arch=<virtual> -code=<real>.
  176. cmSystemTools::ReplaceString(arch_name, "sm_", "compute_");
  177. }
  178. for (std::string const& c : codes) {
  179. std::string entry = arch_name + "," + c;
  180. result.push_back(entry);
  181. }
  182. }
  183. // Now add entries for the following signatures:
  184. // -gencode=<arch>,<code>
  185. // -gencode=<arch>,[<code1>,<code2>]
  186. // -gencode=<arch>,"<code1>,<code2>"
  187. for (std::string const& e : gencode) {
  188. std::string entry = e;
  189. cmSystemTools::ReplaceString(entry, "arch=", "");
  190. cmSystemTools::ReplaceString(entry, "code=", "");
  191. cmSystemTools::ReplaceString(entry, "[", "");
  192. cmSystemTools::ReplaceString(entry, "]", "");
  193. cmSystemTools::ReplaceString(entry, "\"", "");
  194. std::vector<std::string> codes = cmTokenize(entry, ",");
  195. if (codes.size() >= 2) {
  196. auto gencode_arch = cm::cbegin(codes);
  197. for (auto ci = gencode_arch + 1; ci != cm::cend(codes); ++ci) {
  198. std::string code_entry = *gencode_arch + "," + *ci;
  199. result.push_back(code_entry);
  200. }
  201. }
  202. }
  203. }
  204. void cmVisualStudioGeneratorOptions::FixManifestUACFlags()
  205. {
  206. static std::string const ENABLE_UAC = "EnableUAC";
  207. if (!HasFlag(ENABLE_UAC)) {
  208. return;
  209. }
  210. const std::string uacFlag = GetFlag(ENABLE_UAC);
  211. std::vector<std::string> subOptions;
  212. cmsys::SystemTools::Split(uacFlag, subOptions, ' ');
  213. if (subOptions.empty()) {
  214. AddFlag(ENABLE_UAC, "true");
  215. return;
  216. }
  217. if (subOptions.size() == 1 && subOptions[0] == "NO") {
  218. AddFlag(ENABLE_UAC, "false");
  219. return;
  220. }
  221. std::map<std::string, std::string> uacMap;
  222. uacMap["level"] = "UACExecutionLevel";
  223. uacMap["uiAccess"] = "UACUIAccess";
  224. std::map<std::string, std::string> uacExecuteLevelMap;
  225. uacExecuteLevelMap["asInvoker"] = "AsInvoker";
  226. uacExecuteLevelMap["highestAvailable"] = "HighestAvailable";
  227. uacExecuteLevelMap["requireAdministrator"] = "RequireAdministrator";
  228. for (std::string const& subopt : subOptions) {
  229. std::vector<std::string> keyValue;
  230. cmsys::SystemTools::Split(subopt, keyValue, '=');
  231. if (keyValue.size() != 2 || (uacMap.find(keyValue[0]) == uacMap.end())) {
  232. // ignore none key=value option or unknown flags
  233. continue;
  234. }
  235. if (keyValue[1].front() == '\'' && keyValue[1].back() == '\'') {
  236. keyValue[1] = keyValue[1].substr(
  237. 1, std::max(std::string::size_type(0), keyValue[1].length() - 2));
  238. }
  239. if (keyValue[0] == "level") {
  240. if (uacExecuteLevelMap.find(keyValue[1]) == uacExecuteLevelMap.end()) {
  241. // unknown level value
  242. continue;
  243. }
  244. AddFlag(uacMap[keyValue[0]], uacExecuteLevelMap[keyValue[1]]);
  245. continue;
  246. }
  247. if (keyValue[0] == "uiAccess") {
  248. if (keyValue[1] != "true" && keyValue[1] != "false") {
  249. // unknown uiAccess value
  250. continue;
  251. }
  252. AddFlag(uacMap[keyValue[0]], keyValue[1]);
  253. continue;
  254. }
  255. // unknown sub option
  256. }
  257. AddFlag(ENABLE_UAC, "true");
  258. }
  259. void cmVisualStudioGeneratorOptions::Parse(const std::string& flags)
  260. {
  261. // Parse the input string as a windows command line since the string
  262. // is intended for writing directly into the build files.
  263. std::vector<std::string> args;
  264. cmSystemTools::ParseWindowsCommandLine(flags.c_str(), args);
  265. // Process flags that need to be represented specially in the IDE
  266. // project file.
  267. for (std::string const& ai : args) {
  268. this->HandleFlag(ai);
  269. }
  270. }
  271. void cmVisualStudioGeneratorOptions::ParseFinish()
  272. {
  273. if (this->CurrentTool == FortranCompiler) {
  274. // "RuntimeLibrary" attribute values:
  275. // "rtMultiThreaded", "0", /threads /libs:static
  276. // "rtMultiThreadedDLL", "2", /threads /libs:dll
  277. // "rtMultiThreadedDebug", "1", /threads /dbglibs /libs:static
  278. // "rtMultiThreadedDebugDLL", "3", /threads /dbglibs /libs:dll
  279. // These seem unimplemented by the IDE:
  280. // "rtSingleThreaded", "4", /libs:static
  281. // "rtSingleThreadedDLL", "10", /libs:dll
  282. // "rtSingleThreadedDebug", "5", /dbglibs /libs:static
  283. // "rtSingleThreadedDebugDLL", "11", /dbglibs /libs:dll
  284. std::string rl =
  285. cmStrCat("rtMultiThreaded", this->FortranRuntimeDebug ? "Debug" : "",
  286. this->FortranRuntimeDLL ? "DLL" : "");
  287. this->FlagMap["RuntimeLibrary"] = rl;
  288. }
  289. if (this->CurrentTool == CudaCompiler) {
  290. std::map<std::string, FlagValue>::iterator i =
  291. this->FlagMap.find("CudaRuntime");
  292. if (i != this->FlagMap.end() && i->second.size() == 1) {
  293. std::string& cudaRuntime = i->second[0];
  294. if (cudaRuntime == "static") {
  295. cudaRuntime = "Static";
  296. } else if (cudaRuntime == "shared") {
  297. cudaRuntime = "Shared";
  298. } else if (cudaRuntime == "none") {
  299. cudaRuntime = "None";
  300. }
  301. }
  302. }
  303. }
  304. void cmVisualStudioGeneratorOptions::PrependInheritedString(
  305. std::string const& key)
  306. {
  307. std::map<std::string, FlagValue>::iterator i = this->FlagMap.find(key);
  308. if (i == this->FlagMap.end() || i->second.size() != 1) {
  309. return;
  310. }
  311. std::string& value = i->second[0];
  312. value = "%(" + key + ") " + value;
  313. }
  314. void cmVisualStudioGeneratorOptions::Reparse(std::string const& key)
  315. {
  316. std::map<std::string, FlagValue>::iterator i = this->FlagMap.find(key);
  317. if (i == this->FlagMap.end() || i->second.size() != 1) {
  318. return;
  319. }
  320. std::string const original = i->second[0];
  321. i->second[0] = "";
  322. this->UnknownFlagField = key;
  323. this->Parse(original);
  324. }
  325. void cmVisualStudioGeneratorOptions::StoreUnknownFlag(std::string const& flag)
  326. {
  327. // Look for Intel Fortran flags that do not map well in the flag table.
  328. if (this->CurrentTool == FortranCompiler) {
  329. if (flag == "/dbglibs" || flag == "-dbglibs") {
  330. this->FortranRuntimeDebug = true;
  331. return;
  332. }
  333. if (flag == "/threads" || flag == "-threads") {
  334. this->FortranRuntimeMT = true;
  335. return;
  336. }
  337. if (flag == "/libs:dll" || flag == "-libs:dll") {
  338. this->FortranRuntimeDLL = true;
  339. return;
  340. }
  341. if (flag == "/libs:static" || flag == "-libs:static") {
  342. this->FortranRuntimeDLL = false;
  343. return;
  344. }
  345. }
  346. // This option is not known. Store it in the output flags.
  347. std::string const opts = cmOutputConverter::EscapeWindowsShellArgument(
  348. flag.c_str(),
  349. cmOutputConverter::Shell_Flag_AllowMakeVariables |
  350. cmOutputConverter::Shell_Flag_VSIDE);
  351. this->AppendFlagString(this->UnknownFlagField, opts);
  352. }
  353. cmIDEOptions::FlagValue cmVisualStudioGeneratorOptions::TakeFlag(
  354. std::string const& key)
  355. {
  356. FlagValue value;
  357. std::map<std::string, FlagValue>::iterator i = this->FlagMap.find(key);
  358. if (i != this->FlagMap.end()) {
  359. value = i->second;
  360. this->FlagMap.erase(i);
  361. }
  362. return value;
  363. }
  364. void cmVisualStudioGeneratorOptions::SetConfiguration(
  365. const std::string& config)
  366. {
  367. this->Configuration = config;
  368. }
  369. const std::string& cmVisualStudioGeneratorOptions::GetConfiguration() const
  370. {
  371. return this->Configuration;
  372. }
  373. void cmVisualStudioGeneratorOptions::OutputPreprocessorDefinitions(
  374. std::ostream& fout, int indent, const std::string& lang)
  375. {
  376. if (this->Defines.empty()) {
  377. return;
  378. }
  379. std::string tag = "PreprocessorDefinitions";
  380. if (lang == "CUDA") {
  381. tag = "Defines";
  382. }
  383. std::ostringstream oss;
  384. if (this->Version >= cmGlobalVisualStudioGenerator::VSVersion::VS10) {
  385. oss << "%(" << tag << ")";
  386. }
  387. std::vector<std::string>::const_iterator de =
  388. cmRemoveDuplicates(this->Defines);
  389. for (std::string const& di : cmMakeRange(this->Defines.cbegin(), de)) {
  390. // Escape the definition for the compiler.
  391. std::string define;
  392. if (this->Version < cmGlobalVisualStudioGenerator::VSVersion::VS10) {
  393. define = this->LocalGenerator->EscapeForShell(di, true);
  394. } else {
  395. define = di;
  396. }
  397. // Escape this flag for the MSBuild.
  398. if (this->Version >= cmGlobalVisualStudioGenerator::VSVersion::VS10) {
  399. cmVS10EscapeForMSBuild(define);
  400. if (lang == "RC") {
  401. cmSystemTools::ReplaceString(define, "\"", "\\\"");
  402. }
  403. }
  404. // Store the flag in the project file.
  405. oss << ';' << define;
  406. }
  407. this->OutputFlag(fout, indent, tag, oss.str());
  408. }
  409. void cmVisualStudioGeneratorOptions::OutputAdditionalIncludeDirectories(
  410. std::ostream& fout, int indent, const std::string& lang)
  411. {
  412. if (this->Includes.empty()) {
  413. return;
  414. }
  415. std::string tag = "AdditionalIncludeDirectories";
  416. if (lang == "CUDA") {
  417. tag = "Include";
  418. } else if (lang == "ASM_MASM" || lang == "ASM_NASM") {
  419. tag = "IncludePaths";
  420. }
  421. std::ostringstream oss;
  422. const char* sep = "";
  423. for (std::string include : this->Includes) {
  424. // first convert all of the slashes
  425. std::string::size_type pos = 0;
  426. while ((pos = include.find('/', pos)) != std::string::npos) {
  427. include[pos] = '\\';
  428. pos++;
  429. }
  430. if (lang == "ASM_NASM") {
  431. include += "\\";
  432. }
  433. // Escape this include for the MSBuild.
  434. if (this->Version >= cmGlobalVisualStudioGenerator::VSVersion::VS10) {
  435. cmVS10EscapeForMSBuild(include);
  436. }
  437. oss << sep << include;
  438. sep = ";";
  439. if (lang == "Fortran") {
  440. include += "/$(ConfigurationName)";
  441. oss << sep << include;
  442. }
  443. }
  444. if (this->Version >= cmGlobalVisualStudioGenerator::VSVersion::VS10) {
  445. oss << sep << "%(" << tag << ")";
  446. }
  447. this->OutputFlag(fout, indent, tag, oss.str());
  448. }
  449. void cmVisualStudioGeneratorOptions::OutputFlagMap(std::ostream& fout,
  450. int indent)
  451. {
  452. for (auto const& m : this->FlagMap) {
  453. std::ostringstream oss;
  454. const char* sep = "";
  455. for (std::string i : m.second) {
  456. if (this->Version >= cmGlobalVisualStudioGenerator::VSVersion::VS10) {
  457. cmVS10EscapeForMSBuild(i);
  458. }
  459. oss << sep << i;
  460. sep = ";";
  461. }
  462. this->OutputFlag(fout, indent, m.first, oss.str());
  463. }
  464. }