cmVisualStudioGeneratorOptions.cxx 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  1. #include "cmVisualStudioGeneratorOptions.h"
  2. #include "cmOutputConverter.h"
  3. #include "cmSystemTools.h"
  4. #include "cmVisualStudio10TargetGenerator.h"
  5. static
  6. std::string cmVisualStudio10GeneratorOptionsEscapeForXML(std::string ret)
  7. {
  8. cmSystemTools::ReplaceString(ret, ";", "%3B");
  9. cmSystemTools::ReplaceString(ret, "&", "&");
  10. cmSystemTools::ReplaceString(ret, "<", "&lt;");
  11. cmSystemTools::ReplaceString(ret, ">", "&gt;");
  12. return ret;
  13. }
  14. static
  15. 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. //----------------------------------------------------------------------------
  25. cmVisualStudioGeneratorOptions
  26. ::cmVisualStudioGeneratorOptions(cmLocalVisualStudioGenerator* lg,
  27. Tool tool,
  28. cmVisualStudio10TargetGenerator* g):
  29. cmIDEOptions(),
  30. LocalGenerator(lg), Version(lg->GetVersion()), 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. }
  41. //----------------------------------------------------------------------------
  42. cmVisualStudioGeneratorOptions
  43. ::cmVisualStudioGeneratorOptions(cmLocalVisualStudioGenerator* lg,
  44. Tool tool,
  45. cmVS7FlagTable const* table,
  46. cmVS7FlagTable const* extraTable,
  47. cmVisualStudio10TargetGenerator* g):
  48. cmIDEOptions(),
  49. LocalGenerator(lg), Version(lg->GetVersion()), 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. }
  63. //----------------------------------------------------------------------------
  64. void cmVisualStudioGeneratorOptions::AddTable(cmVS7FlagTable const* table)
  65. {
  66. if(table)
  67. {
  68. for(int i=0; i < FlagTableCount; ++i)
  69. {
  70. if (!this->FlagTable[i])
  71. {
  72. this->FlagTable[i] = table;
  73. break;
  74. }
  75. }
  76. }
  77. }
  78. //----------------------------------------------------------------------------
  79. void cmVisualStudioGeneratorOptions::FixExceptionHandlingDefault()
  80. {
  81. // Exception handling is on by default because the platform file has
  82. // "/EHsc" in the flags. Normally, that will override this
  83. // initialization to off, but the user has the option of removing
  84. // the flag to disable exception handling. When the user does
  85. // remove the flag we need to override the IDE default of on.
  86. switch (this->Version)
  87. {
  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. // by default VS puts <ExceptionHandling></ExceptionHandling> empty
  97. // for a project, to make our projects look the same put a new line
  98. // and space over for the closing </ExceptionHandling> as the default
  99. // value
  100. this->FlagMap["ExceptionHandling"] = "\n ";
  101. break;
  102. default:
  103. this->FlagMap["ExceptionHandling"] = "0";
  104. break;
  105. }
  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. {
  122. this->FlagMap["SuppressStartupBanner"] =
  123. this->Version < cmGlobalVisualStudioGenerator::VS10 ? "FALSE" : "";
  124. }
  125. }
  126. bool cmVisualStudioGeneratorOptions::IsDebug() const
  127. {
  128. return this->FlagMap.find("DebugInformationFormat") != this->FlagMap.end();
  129. }
  130. //----------------------------------------------------------------------------
  131. bool cmVisualStudioGeneratorOptions::IsWinRt() const
  132. {
  133. return this->FlagMap.find("CompileAsWinRT") != this->FlagMap.end();
  134. }
  135. //----------------------------------------------------------------------------
  136. bool cmVisualStudioGeneratorOptions::UsingUnicode() const
  137. {
  138. // Look for the a _UNICODE definition.
  139. for(std::vector<std::string>::const_iterator di = this->Defines.begin();
  140. di != this->Defines.end(); ++di)
  141. {
  142. if(*di == "_UNICODE")
  143. {
  144. return true;
  145. }
  146. }
  147. return false;
  148. }
  149. //----------------------------------------------------------------------------
  150. bool cmVisualStudioGeneratorOptions::UsingSBCS() const
  151. {
  152. // Look for the a _SBCS definition.
  153. for(std::vector<std::string>::const_iterator di = this->Defines.begin();
  154. di != this->Defines.end(); ++di)
  155. {
  156. if(*di == "_SBCS")
  157. {
  158. return true;
  159. }
  160. }
  161. return false;
  162. }
  163. //----------------------------------------------------------------------------
  164. void cmVisualStudioGeneratorOptions::Parse(const char* flags)
  165. {
  166. // Parse the input string as a windows command line since the string
  167. // is intended for writing directly into the build files.
  168. std::vector<std::string> args;
  169. cmSystemTools::ParseWindowsCommandLine(flags, args);
  170. // Process flags that need to be represented specially in the IDE
  171. // project file.
  172. for(std::vector<std::string>::iterator ai = args.begin();
  173. ai != args.end(); ++ai)
  174. {
  175. this->HandleFlag(ai->c_str());
  176. }
  177. }
  178. //----------------------------------------------------------------------------
  179. void cmVisualStudioGeneratorOptions::ParseFinish()
  180. {
  181. if(this->CurrentTool == FortranCompiler)
  182. {
  183. // "RuntimeLibrary" attribute values:
  184. // "rtMultiThreaded", "0", /threads /libs:static
  185. // "rtMultiThreadedDLL", "2", /threads /libs:dll
  186. // "rtMultiThreadedDebug", "1", /threads /dbglibs /libs:static
  187. // "rtMultiThreadedDebugDLL", "3", /threads /dbglibs /libs:dll
  188. // These seem unimplemented by the IDE:
  189. // "rtSingleThreaded", "4", /libs:static
  190. // "rtSingleThreadedDLL", "10", /libs:dll
  191. // "rtSingleThreadedDebug", "5", /dbglibs /libs:static
  192. // "rtSingleThreadedDebugDLL", "11", /dbglibs /libs:dll
  193. std::string rl = "rtMultiThreaded";
  194. rl += this->FortranRuntimeDebug? "Debug" : "";
  195. rl += this->FortranRuntimeDLL? "DLL" : "";
  196. this->FlagMap["RuntimeLibrary"] = rl;
  197. }
  198. }
  199. //----------------------------------------------------------------------------
  200. void cmVisualStudioGeneratorOptions::StoreUnknownFlag(const char* flag)
  201. {
  202. // Look for Intel Fortran flags that do not map well in the flag table.
  203. if(this->CurrentTool == FortranCompiler)
  204. {
  205. if(strcmp(flag, "/dbglibs") == 0)
  206. {
  207. this->FortranRuntimeDebug = true;
  208. return;
  209. }
  210. if(strcmp(flag, "/threads") == 0)
  211. {
  212. this->FortranRuntimeMT = true;
  213. return;
  214. }
  215. if(strcmp(flag, "/libs:dll") == 0)
  216. {
  217. this->FortranRuntimeDLL = true;
  218. return;
  219. }
  220. if(strcmp(flag, "/libs:static") == 0)
  221. {
  222. this->FortranRuntimeDLL = false;
  223. return;
  224. }
  225. }
  226. // This option is not known. Store it in the output flags.
  227. this->FlagString += " ";
  228. this->FlagString +=
  229. cmOutputConverter::EscapeWindowsShellArgument(
  230. flag,
  231. cmOutputConverter::Shell_Flag_AllowMakeVariables |
  232. cmOutputConverter::Shell_Flag_VSIDE);
  233. }
  234. //----------------------------------------------------------------------------
  235. void cmVisualStudioGeneratorOptions::SetConfiguration(const char* config)
  236. {
  237. this->Configuration = config;
  238. }
  239. //----------------------------------------------------------------------------
  240. void
  241. cmVisualStudioGeneratorOptions
  242. ::OutputPreprocessorDefinitions(std::ostream& fout,
  243. const char* prefix,
  244. const char* suffix,
  245. const std::string& lang)
  246. {
  247. if(this->Defines.empty())
  248. {
  249. return;
  250. }
  251. if(this->Version >= cmGlobalVisualStudioGenerator::VS10)
  252. {
  253. // if there are configuration specific flags, then
  254. // use the configuration specific tag for PreprocessorDefinitions
  255. if(!this->Configuration.empty())
  256. {
  257. fout << prefix;
  258. this->TargetGenerator->WritePlatformConfigTag(
  259. "PreprocessorDefinitions",
  260. this->Configuration.c_str(),
  261. 0,
  262. 0, 0, &fout);
  263. }
  264. else
  265. {
  266. fout << prefix << "<PreprocessorDefinitions>";
  267. }
  268. }
  269. else
  270. {
  271. fout << prefix << "PreprocessorDefinitions=\"";
  272. }
  273. const char* sep = "";
  274. for(std::vector<std::string>::const_iterator di = this->Defines.begin();
  275. di != this->Defines.end(); ++di)
  276. {
  277. // Escape the definition for the compiler.
  278. std::string define;
  279. if(this->Version < cmGlobalVisualStudioGenerator::VS10)
  280. {
  281. define =
  282. this->LocalGenerator->EscapeForShell(di->c_str(), true);
  283. }
  284. else
  285. {
  286. define = *di;
  287. }
  288. // Escape this flag for the IDE.
  289. if(this->Version >= cmGlobalVisualStudioGenerator::VS10)
  290. {
  291. define = cmVisualStudio10GeneratorOptionsEscapeForXML(define);
  292. if(lang == "RC")
  293. {
  294. cmSystemTools::ReplaceString(define, "\"", "\\\"");
  295. }
  296. }
  297. else
  298. {
  299. define = cmVisualStudioGeneratorOptionsEscapeForXML(define);
  300. }
  301. // Store the flag in the project file.
  302. fout << sep << define;
  303. sep = ";";
  304. }
  305. if(this->Version >= cmGlobalVisualStudioGenerator::VS10)
  306. {
  307. fout << ";%(PreprocessorDefinitions)</PreprocessorDefinitions>" << suffix;
  308. }
  309. else
  310. {
  311. fout << "\"" << suffix;
  312. }
  313. }
  314. //----------------------------------------------------------------------------
  315. void
  316. cmVisualStudioGeneratorOptions
  317. ::OutputFlagMap(std::ostream& fout, const char* indent)
  318. {
  319. if(this->Version >= cmGlobalVisualStudioGenerator::VS10)
  320. {
  321. for(std::map<std::string, FlagValue>::iterator m = this->FlagMap.begin();
  322. m != this->FlagMap.end(); ++m)
  323. {
  324. fout << indent;
  325. if(!this->Configuration.empty())
  326. {
  327. this->TargetGenerator->WritePlatformConfigTag(
  328. m->first.c_str(),
  329. this->Configuration.c_str(),
  330. 0,
  331. 0, 0, &fout);
  332. }
  333. else
  334. {
  335. fout << "<" << m->first << ">";
  336. }
  337. const char* sep = "";
  338. for(std::vector<std::string>::iterator i = m->second.begin();
  339. i != m->second.end(); ++i)
  340. {
  341. fout << sep << cmVisualStudio10GeneratorOptionsEscapeForXML(*i);
  342. sep = ";";
  343. }
  344. fout << "</" << m->first << ">\n";
  345. }
  346. }
  347. else
  348. {
  349. for(std::map<std::string, FlagValue>::iterator m = this->FlagMap.begin();
  350. m != this->FlagMap.end(); ++m)
  351. {
  352. fout << indent << m->first << "=\"";
  353. const char* sep = "";
  354. for(std::vector<std::string>::iterator i = m->second.begin();
  355. i != m->second.end(); ++i)
  356. {
  357. fout << sep << cmVisualStudioGeneratorOptionsEscapeForXML(*i);
  358. sep = ";";
  359. }
  360. fout << "\"\n";
  361. }
  362. }
  363. }
  364. //----------------------------------------------------------------------------
  365. void
  366. cmVisualStudioGeneratorOptions
  367. ::OutputAdditionalOptions(std::ostream& fout,
  368. const char* prefix,
  369. const char* suffix)
  370. {
  371. if(!this->FlagString.empty())
  372. {
  373. if(this->Version >= cmGlobalVisualStudioGenerator::VS10)
  374. {
  375. fout << prefix;
  376. if(!this->Configuration.empty())
  377. {
  378. this->TargetGenerator->WritePlatformConfigTag(
  379. "AdditionalOptions",
  380. this->Configuration.c_str(),
  381. 0,
  382. 0, 0, &fout);
  383. }
  384. else
  385. {
  386. fout << "<AdditionalOptions>";
  387. }
  388. fout << cmVisualStudio10GeneratorOptionsEscapeForXML(this->FlagString)
  389. << " %(AdditionalOptions)</AdditionalOptions>\n";
  390. }
  391. else
  392. {
  393. fout << prefix << "AdditionalOptions=\"";
  394. fout << cmVisualStudioGeneratorOptionsEscapeForXML(this->FlagString);
  395. fout << "\"" << suffix;
  396. }
  397. }
  398. }