cmVisualStudioGeneratorOptions.cxx 11 KB

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