cmVisualStudioGeneratorOptions.cxx 15 KB

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