cmVisualStudioGeneratorOptions.cxx 15 KB

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