cmVisualStudioGeneratorOptions.cxx 16 KB

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