cmVisualStudioGeneratorOptions.cxx 15 KB

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