cmVisualStudioGeneratorOptions.cxx 16 KB

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