cmVisualStudioGeneratorOptions.cxx 15 KB

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