cmVisualStudioGeneratorOptions.cxx 15 KB

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