cmVisualStudioGeneratorOptions.cxx 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  1. #include "cmVisualStudioGeneratorOptions.h"
  2. #include "cmSystemTools.h"
  3. #include <cmsys/System.h>
  4. #include "cmVisualStudio10TargetGenerator.h"
  5. inline std::string cmVisualStudio10GeneratorOptionsEscapeForXML(const char* s)
  6. {
  7. std::string ret = s;
  8. cmSystemTools::ReplaceString(ret, ";", "%3B");
  9. cmSystemTools::ReplaceString(ret, "&", "&amp;");
  10. cmSystemTools::ReplaceString(ret, "<", "&lt;");
  11. cmSystemTools::ReplaceString(ret, ">", "&gt;");
  12. return ret;
  13. }
  14. inline std::string cmVisualStudioGeneratorOptionsEscapeForXML(const char* s)
  15. {
  16. std::string ret = s;
  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. cmVS7FlagTable const* table,
  29. cmVS7FlagTable const* extraTable,
  30. cmVisualStudio10TargetGenerator* g):
  31. cmIDEOptions(),
  32. LocalGenerator(lg), Version(lg->GetVersion()), CurrentTool(tool),
  33. TargetGenerator(g)
  34. {
  35. // Store the given flag tables.
  36. cmIDEFlagTable const** ft = this->FlagTable;
  37. if(table) { *ft++ = table; }
  38. if(extraTable) { *ft++ = extraTable; }
  39. // Preprocessor definitions are not allowed for linker tools.
  40. this->AllowDefine = (tool != Linker);
  41. // Slash options are allowed for VS.
  42. this->AllowSlash = true;
  43. this->FortranRuntimeDebug = false;
  44. this->FortranRuntimeDLL = false;
  45. this->FortranRuntimeMT = false;
  46. }
  47. //----------------------------------------------------------------------------
  48. void cmVisualStudioGeneratorOptions::FixExceptionHandlingDefault()
  49. {
  50. // Exception handling is on by default because the platform file has
  51. // "/EHsc" in the flags. Normally, that will override this
  52. // initialization to off, but the user has the option of removing
  53. // the flag to disable exception handling. When the user does
  54. // remove the flag we need to override the IDE default of on.
  55. switch (this->Version)
  56. {
  57. case cmLocalVisualStudioGenerator::VS7:
  58. case cmLocalVisualStudioGenerator::VS71:
  59. this->FlagMap["ExceptionHandling"] = "FALSE";
  60. break;
  61. case cmLocalVisualStudioGenerator::VS10:
  62. case cmLocalVisualStudioGenerator::VS11:
  63. case cmLocalVisualStudioGenerator::VS12:
  64. // by default VS puts <ExceptionHandling></ExceptionHandling> empty
  65. // for a project, to make our projects look the same put a new line
  66. // and space over for the closing </ExceptionHandling> as the default
  67. // value
  68. this->FlagMap["ExceptionHandling"] = "\n ";
  69. break;
  70. default:
  71. this->FlagMap["ExceptionHandling"] = "0";
  72. break;
  73. }
  74. }
  75. //----------------------------------------------------------------------------
  76. void cmVisualStudioGeneratorOptions::SetVerboseMakefile(bool verbose)
  77. {
  78. // If verbose makefiles have been requested and the /nologo option
  79. // was not given explicitly in the flags we want to add an attribute
  80. // to the generated project to disable logo suppression. Otherwise
  81. // the GUI default is to enable suppression.
  82. //
  83. // On Visual Studio 10 (and later!), the value of this attribute should be
  84. // an empty string, instead of "FALSE", in order to avoid a warning:
  85. // "cl ... warning D9035: option 'nologo-' has been deprecated"
  86. //
  87. if(verbose &&
  88. this->FlagMap.find("SuppressStartupBanner") == this->FlagMap.end())
  89. {
  90. this->FlagMap["SuppressStartupBanner"] =
  91. this->Version < cmLocalVisualStudioGenerator::VS10 ? "FALSE" : "";
  92. }
  93. }
  94. bool cmVisualStudioGeneratorOptions::IsDebug()
  95. {
  96. return this->FlagMap.find("DebugInformationFormat") != this->FlagMap.end();
  97. }
  98. //----------------------------------------------------------------------------
  99. bool cmVisualStudioGeneratorOptions::UsingUnicode()
  100. {
  101. // Look for the a _UNICODE definition.
  102. for(std::vector<std::string>::const_iterator di = this->Defines.begin();
  103. di != this->Defines.end(); ++di)
  104. {
  105. if(*di == "_UNICODE")
  106. {
  107. return true;
  108. }
  109. }
  110. return false;
  111. }
  112. //----------------------------------------------------------------------------
  113. bool cmVisualStudioGeneratorOptions::UsingSBCS()
  114. {
  115. // Look for the a _SBCS definition.
  116. for(std::vector<std::string>::const_iterator di = this->Defines.begin();
  117. di != this->Defines.end(); ++di)
  118. {
  119. if(*di == "_SBCS")
  120. {
  121. return true;
  122. }
  123. }
  124. return false;
  125. }
  126. //----------------------------------------------------------------------------
  127. void cmVisualStudioGeneratorOptions::Parse(const char* flags)
  128. {
  129. // Parse the input string as a windows command line since the string
  130. // is intended for writing directly into the build files.
  131. std::vector<std::string> args;
  132. cmSystemTools::ParseWindowsCommandLine(flags, args);
  133. // Process flags that need to be represented specially in the IDE
  134. // project file.
  135. for(std::vector<std::string>::iterator ai = args.begin();
  136. ai != args.end(); ++ai)
  137. {
  138. this->HandleFlag(ai->c_str());
  139. }
  140. }
  141. //----------------------------------------------------------------------------
  142. void cmVisualStudioGeneratorOptions::ParseFinish()
  143. {
  144. if(this->CurrentTool == FortranCompiler)
  145. {
  146. // "RuntimeLibrary" attribute values:
  147. // "rtMultiThreaded", "0", /threads /libs:static
  148. // "rtMultiThreadedDLL", "2", /threads /libs:dll
  149. // "rtMultiThreadedDebug", "1", /threads /dbglibs /libs:static
  150. // "rtMultiThreadedDebugDLL", "3", /threads /dbglibs /libs:dll
  151. // These seem unimplemented by the IDE:
  152. // "rtSingleThreaded", "4", /libs:static
  153. // "rtSingleThreadedDLL", "10", /libs:dll
  154. // "rtSingleThreadedDebug", "5", /dbglibs /libs:static
  155. // "rtSingleThreadedDebugDLL", "11", /dbglibs /libs:dll
  156. std::string rl = "rtMultiThreaded";
  157. rl += this->FortranRuntimeDebug? "Debug" : "";
  158. rl += this->FortranRuntimeDLL? "DLL" : "";
  159. this->FlagMap["RuntimeLibrary"] = rl;
  160. }
  161. }
  162. //----------------------------------------------------------------------------
  163. void cmVisualStudioGeneratorOptions::StoreUnknownFlag(const char* flag)
  164. {
  165. // Look for Intel Fortran flags that do not map well in the flag table.
  166. if(this->CurrentTool == FortranCompiler)
  167. {
  168. if(strcmp(flag, "/dbglibs") == 0)
  169. {
  170. this->FortranRuntimeDebug = true;
  171. return;
  172. }
  173. if(strcmp(flag, "/threads") == 0)
  174. {
  175. this->FortranRuntimeMT = true;
  176. return;
  177. }
  178. if(strcmp(flag, "/libs:dll") == 0)
  179. {
  180. this->FortranRuntimeDLL = true;
  181. return;
  182. }
  183. if(strcmp(flag, "/libs:static") == 0)
  184. {
  185. this->FortranRuntimeDLL = false;
  186. return;
  187. }
  188. }
  189. // This option is not known. Store it in the output flags.
  190. this->FlagString += " ";
  191. this->FlagString +=
  192. cmSystemTools::EscapeWindowsShellArgument(
  193. flag,
  194. cmsysSystem_Shell_Flag_AllowMakeVariables |
  195. cmsysSystem_Shell_Flag_VSIDE);
  196. }
  197. //----------------------------------------------------------------------------
  198. void cmVisualStudioGeneratorOptions::SetConfiguration(const char* config)
  199. {
  200. this->Configuration = config;
  201. }
  202. //----------------------------------------------------------------------------
  203. void
  204. cmVisualStudioGeneratorOptions
  205. ::OutputPreprocessorDefinitions(std::ostream& fout,
  206. const char* prefix,
  207. const char* suffix,
  208. const char* lang)
  209. {
  210. if(this->Defines.empty())
  211. {
  212. return;
  213. }
  214. if(this->Version >= cmLocalVisualStudioGenerator::VS10)
  215. {
  216. // if there are configuration specific flags, then
  217. // use the configuration specific tag for PreprocessorDefinitions
  218. if(this->Configuration.size())
  219. {
  220. fout << prefix;
  221. this->TargetGenerator->WritePlatformConfigTag(
  222. "PreprocessorDefinitions",
  223. this->Configuration.c_str(),
  224. 0,
  225. 0, 0, &fout);
  226. }
  227. else
  228. {
  229. fout << prefix << "<PreprocessorDefinitions>";
  230. }
  231. }
  232. else
  233. {
  234. fout << prefix << "PreprocessorDefinitions=\"";
  235. }
  236. const char* sep = "";
  237. for(std::vector<std::string>::const_iterator di = this->Defines.begin();
  238. di != this->Defines.end(); ++di)
  239. {
  240. // Escape the definition for the compiler.
  241. std::string define;
  242. if(this->Version < cmLocalVisualStudioGenerator::VS10)
  243. {
  244. define =
  245. this->LocalGenerator->EscapeForShell(di->c_str(), true);
  246. }
  247. else
  248. {
  249. define = *di;
  250. }
  251. // Escape this flag for the IDE.
  252. if(this->Version >= cmLocalVisualStudioGenerator::VS10)
  253. {
  254. define = cmVisualStudio10GeneratorOptionsEscapeForXML(define.c_str());
  255. if(0 == strcmp(lang, "RC"))
  256. {
  257. cmSystemTools::ReplaceString(define, "\"", "\\\"");
  258. }
  259. }
  260. else
  261. {
  262. define = cmVisualStudioGeneratorOptionsEscapeForXML(define.c_str());
  263. }
  264. // Store the flag in the project file.
  265. fout << sep << define;
  266. sep = ";";
  267. }
  268. if(this->Version >= cmLocalVisualStudioGenerator::VS10)
  269. {
  270. fout << ";%(PreprocessorDefinitions)</PreprocessorDefinitions>" << suffix;
  271. }
  272. else
  273. {
  274. fout << "\"" << suffix;
  275. }
  276. }
  277. //----------------------------------------------------------------------------
  278. void
  279. cmVisualStudioGeneratorOptions
  280. ::OutputFlagMap(std::ostream& fout, const char* indent)
  281. {
  282. if(this->Version >= cmLocalVisualStudioGenerator::VS10)
  283. {
  284. for(std::map<cmStdString, cmStdString>::iterator m = this->FlagMap.begin();
  285. m != this->FlagMap.end(); ++m)
  286. {
  287. fout << indent;
  288. if(this->Configuration.size())
  289. {
  290. this->TargetGenerator->WritePlatformConfigTag(
  291. m->first.c_str(),
  292. this->Configuration.c_str(),
  293. 0,
  294. 0, 0, &fout);
  295. }
  296. else
  297. {
  298. fout << "<" << m->first << ">";
  299. }
  300. fout << m->second;
  301. if (m->first == "AdditionalIncludeDirectories")
  302. {
  303. fout << ";%(AdditionalIncludeDirectories)";
  304. }
  305. fout << "</" << m->first << ">\n";
  306. }
  307. }
  308. else
  309. {
  310. for(std::map<cmStdString, cmStdString>::iterator m = this->FlagMap.begin();
  311. m != this->FlagMap.end(); ++m)
  312. {
  313. fout << indent << m->first << "=\"" << m->second << "\"\n";
  314. }
  315. }
  316. }
  317. //----------------------------------------------------------------------------
  318. void
  319. cmVisualStudioGeneratorOptions
  320. ::OutputAdditionalOptions(std::ostream& fout,
  321. const char* prefix,
  322. const char* suffix)
  323. {
  324. if(!this->FlagString.empty())
  325. {
  326. if(this->Version >= cmLocalVisualStudioGenerator::VS10)
  327. {
  328. fout << prefix;
  329. if(this->Configuration.size())
  330. {
  331. this->TargetGenerator->WritePlatformConfigTag(
  332. "AdditionalOptions",
  333. this->Configuration.c_str(),
  334. 0,
  335. 0, 0, &fout);
  336. }
  337. else
  338. {
  339. fout << "<AdditionalOptions>";
  340. }
  341. fout << this->FlagString.c_str()
  342. << " %(AdditionalOptions)</AdditionalOptions>\n";
  343. }
  344. else
  345. {
  346. fout << prefix << "AdditionalOptions=\"";
  347. fout <<
  348. cmVisualStudioGeneratorOptionsEscapeForXML(this->FlagString.c_str());
  349. fout << "\"" << suffix;
  350. }
  351. }
  352. }