cmVisualStudioGeneratorOptions.cxx 15 KB

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