cmVisualStudioGeneratorOptions.cxx 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  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(cmLocalGenerator* lg,
  26. int version,
  27. Tool tool,
  28. cmVS7FlagTable const* table,
  29. cmVS7FlagTable const* extraTable,
  30. cmVisualStudio10TargetGenerator* g):
  31. cmIDEOptions(),
  32. LocalGenerator(lg), Version(version), 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 7:
  58. case 71:
  59. this->FlagMap["ExceptionHandling"] = "FALSE";
  60. break;
  61. case 10:
  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"] = this->Version < 10 ? "FALSE" : "";
  89. }
  90. }
  91. bool cmVisualStudioGeneratorOptions::IsDebug()
  92. {
  93. return this->FlagMap.find("DebugInformationFormat") != this->FlagMap.end();
  94. }
  95. //----------------------------------------------------------------------------
  96. bool cmVisualStudioGeneratorOptions::UsingUnicode()
  97. {
  98. // Look for the a _UNICODE definition.
  99. for(std::vector<std::string>::const_iterator di = this->Defines.begin();
  100. di != this->Defines.end(); ++di)
  101. {
  102. if(*di == "_UNICODE")
  103. {
  104. return true;
  105. }
  106. }
  107. return false;
  108. }
  109. //----------------------------------------------------------------------------
  110. void cmVisualStudioGeneratorOptions::Parse(const char* flags)
  111. {
  112. // Parse the input string as a windows command line since the string
  113. // is intended for writing directly into the build files.
  114. std::vector<std::string> args;
  115. cmSystemTools::ParseWindowsCommandLine(flags, args);
  116. // Process flags that need to be represented specially in the IDE
  117. // project file.
  118. for(std::vector<std::string>::iterator ai = args.begin();
  119. ai != args.end(); ++ai)
  120. {
  121. this->HandleFlag(ai->c_str());
  122. }
  123. }
  124. //----------------------------------------------------------------------------
  125. void cmVisualStudioGeneratorOptions::ParseFinish()
  126. {
  127. if(this->CurrentTool == FortranCompiler)
  128. {
  129. // "RuntimeLibrary" attribute values:
  130. // "rtMultiThreaded", "0", /threads /libs:static
  131. // "rtMultiThreadedDLL", "2", /threads /libs:dll
  132. // "rtMultiThreadedDebug", "1", /threads /dbglibs /libs:static
  133. // "rtMultiThreadedDebugDLL", "3", /threads /dbglibs /libs:dll
  134. // These seem unimplemented by the IDE:
  135. // "rtSingleThreaded", "4", /libs:static
  136. // "rtSingleThreadedDLL", "10", /libs:dll
  137. // "rtSingleThreadedDebug", "5", /dbglibs /libs:static
  138. // "rtSingleThreadedDebugDLL", "11", /dbglibs /libs:dll
  139. std::string rl = "rtMultiThreaded";
  140. rl += this->FortranRuntimeDebug? "Debug" : "";
  141. rl += this->FortranRuntimeDLL? "DLL" : "";
  142. this->FlagMap["RuntimeLibrary"] = rl;
  143. }
  144. }
  145. //----------------------------------------------------------------------------
  146. void cmVisualStudioGeneratorOptions::StoreUnknownFlag(const char* flag)
  147. {
  148. // Look for Intel Fortran flags that do not map well in the flag table.
  149. if(this->CurrentTool == FortranCompiler)
  150. {
  151. if(strcmp(flag, "/dbglibs") == 0)
  152. {
  153. this->FortranRuntimeDebug = true;
  154. return;
  155. }
  156. if(strcmp(flag, "/threads") == 0)
  157. {
  158. this->FortranRuntimeMT = true;
  159. return;
  160. }
  161. if(strcmp(flag, "/libs:dll") == 0)
  162. {
  163. this->FortranRuntimeDLL = true;
  164. return;
  165. }
  166. if(strcmp(flag, "/libs:static") == 0)
  167. {
  168. this->FortranRuntimeDLL = false;
  169. return;
  170. }
  171. }
  172. // This option is not known. Store it in the output flags.
  173. this->FlagString += " ";
  174. this->FlagString +=
  175. cmSystemTools::EscapeWindowsShellArgument(
  176. flag,
  177. cmsysSystem_Shell_Flag_AllowMakeVariables |
  178. cmsysSystem_Shell_Flag_VSIDE);
  179. }
  180. //----------------------------------------------------------------------------
  181. void cmVisualStudioGeneratorOptions::SetConfiguration(const char* config)
  182. {
  183. this->Configuration = config;
  184. }
  185. //----------------------------------------------------------------------------
  186. void
  187. cmVisualStudioGeneratorOptions
  188. ::OutputPreprocessorDefinitions(std::ostream& fout,
  189. const char* prefix,
  190. const char* suffix,
  191. const char* lang)
  192. {
  193. if(this->Defines.empty())
  194. {
  195. return;
  196. }
  197. if(this->Version == 10)
  198. {
  199. // if there are configuration specifc flags, then
  200. // use the configuration specific tag for PreprocessorDefinitions
  201. if(this->Configuration.size())
  202. {
  203. fout << prefix;
  204. this->TargetGenerator->WritePlatformConfigTag(
  205. "PreprocessorDefinitions",
  206. this->Configuration.c_str(),
  207. 0,
  208. 0, 0, &fout);
  209. }
  210. else
  211. {
  212. fout << prefix << "<PreprocessorDefinitions>";
  213. }
  214. }
  215. else
  216. {
  217. fout << prefix << "PreprocessorDefinitions=\"";
  218. }
  219. const char* sep = "";
  220. for(std::vector<std::string>::const_iterator di = this->Defines.begin();
  221. di != this->Defines.end(); ++di)
  222. {
  223. // Escape the definition for the compiler.
  224. std::string define;
  225. if(this->Version != 10)
  226. {
  227. define =
  228. this->LocalGenerator->EscapeForShell(di->c_str(), true);
  229. }
  230. else
  231. {
  232. define = *di;
  233. }
  234. // Escape this flag for the IDE.
  235. if(this->Version == 10)
  236. {
  237. define = cmVisualStudio10GeneratorOptionsEscapeForXML(define.c_str());
  238. if(0 == strcmp(lang, "RC"))
  239. {
  240. cmSystemTools::ReplaceString(define, "\"", "\\\"");
  241. }
  242. }
  243. else
  244. {
  245. define = cmVisualStudioGeneratorOptionsEscapeForXML(define.c_str());
  246. }
  247. // Store the flag in the project file.
  248. fout << sep << define;
  249. sep = ";";
  250. }
  251. if(this->Version == 10)
  252. {
  253. fout << ";%(PreprocessorDefinitions)</PreprocessorDefinitions>" << suffix;
  254. }
  255. else
  256. {
  257. fout << "\"" << suffix;
  258. }
  259. }
  260. //----------------------------------------------------------------------------
  261. void
  262. cmVisualStudioGeneratorOptions
  263. ::OutputFlagMap(std::ostream& fout, const char* indent)
  264. {
  265. if(this->Version == 10)
  266. {
  267. for(std::map<cmStdString, cmStdString>::iterator m = this->FlagMap.begin();
  268. m != this->FlagMap.end(); ++m)
  269. {
  270. fout << indent;
  271. if(this->Configuration.size())
  272. {
  273. this->TargetGenerator->WritePlatformConfigTag(
  274. m->first.c_str(),
  275. this->Configuration.c_str(),
  276. 0,
  277. 0, 0, &fout);
  278. }
  279. else
  280. {
  281. fout << "<" << m->first << ">";
  282. }
  283. fout << m->second;
  284. if (m->first == "AdditionalIncludeDirectories")
  285. {
  286. fout << ";%(AdditionalIncludeDirectories)";
  287. }
  288. fout << "</" << m->first << ">\n";
  289. }
  290. }
  291. else
  292. {
  293. for(std::map<cmStdString, cmStdString>::iterator m = this->FlagMap.begin();
  294. m != this->FlagMap.end(); ++m)
  295. {
  296. fout << indent << m->first << "=\"" << m->second << "\"\n";
  297. }
  298. }
  299. }
  300. //----------------------------------------------------------------------------
  301. void
  302. cmVisualStudioGeneratorOptions
  303. ::OutputAdditionalOptions(std::ostream& fout,
  304. const char* prefix,
  305. const char* suffix)
  306. {
  307. if(!this->FlagString.empty())
  308. {
  309. if(this->Version == 10)
  310. {
  311. fout << prefix;
  312. if(this->Configuration.size())
  313. {
  314. this->TargetGenerator->WritePlatformConfigTag(
  315. "AdditionalOptions",
  316. this->Configuration.c_str(),
  317. 0,
  318. 0, 0, &fout);
  319. }
  320. else
  321. {
  322. fout << "<AdditionalOptions>";
  323. }
  324. fout << this->FlagString.c_str()
  325. << " %(AdditionalOptions)</AdditionalOptions>\n";
  326. }
  327. else
  328. {
  329. fout << prefix << "AdditionalOptions=\"";
  330. fout <<
  331. cmVisualStudioGeneratorOptionsEscapeForXML(this->FlagString.c_str());
  332. fout << "\"" << suffix;
  333. }
  334. }
  335. }