cmVisualStudioGeneratorOptions.cxx 16 KB

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