cmVisualStudioGeneratorOptions.cxx 15 KB

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