cmVisualStudioGeneratorOptions.cxx 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  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. // Avoid this on Visual Studio 10 (and later!) because it results in:
  82. // "cl ... warning D9035: option 'nologo-' has been deprecated"
  83. //
  84. if(verbose &&
  85. this->Version != 10 &&
  86. this->FlagMap.find("SuppressStartupBanner") == this->FlagMap.end())
  87. {
  88. this->FlagMap["SuppressStartupBanner"] = "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. {
  192. if(this->Defines.empty())
  193. {
  194. return;
  195. }
  196. if(this->Version == 10)
  197. {
  198. // if there are configuration specifc flags, then
  199. // use the configuration specific tag for PreprocessorDefinitions
  200. if(this->Configuration.size())
  201. {
  202. fout << prefix;
  203. this->TargetGenerator->WritePlatformConfigTag(
  204. "PreprocessorDefinitions",
  205. this->Configuration.c_str(),
  206. 0,
  207. 0, 0, &fout);
  208. }
  209. else
  210. {
  211. fout << prefix << "<PreprocessorDefinitions>";
  212. }
  213. }
  214. else
  215. {
  216. fout << prefix << "PreprocessorDefinitions=\"";
  217. }
  218. const char* sep = "";
  219. for(std::vector<std::string>::const_iterator di = this->Defines.begin();
  220. di != this->Defines.end(); ++di)
  221. {
  222. // Escape the definition for the compiler.
  223. std::string define;
  224. if(this->Version != 10)
  225. {
  226. define =
  227. this->LocalGenerator->EscapeForShell(di->c_str(), true);
  228. }
  229. else
  230. {
  231. define = *di;
  232. }
  233. // Escape this flag for the IDE.
  234. if(this->Version == 10)
  235. {
  236. define = cmVisualStudio10GeneratorOptionsEscapeForXML(define.c_str());
  237. }
  238. else
  239. {
  240. define = cmVisualStudioGeneratorOptionsEscapeForXML(define.c_str());
  241. }
  242. // Store the flag in the project file.
  243. fout << sep << define;
  244. sep = ";";
  245. }
  246. if(this->Version == 10)
  247. {
  248. fout << ";%(PreprocessorDefinitions)</PreprocessorDefinitions>" << suffix;
  249. }
  250. else
  251. {
  252. fout << "\"" << suffix;
  253. }
  254. }
  255. //----------------------------------------------------------------------------
  256. void
  257. cmVisualStudioGeneratorOptions
  258. ::OutputFlagMap(std::ostream& fout, const char* indent)
  259. {
  260. if(this->Version == 10)
  261. {
  262. for(std::map<cmStdString, cmStdString>::iterator m = this->FlagMap.begin();
  263. m != this->FlagMap.end(); ++m)
  264. {
  265. fout << indent;
  266. if(this->Configuration.size())
  267. {
  268. this->TargetGenerator->WritePlatformConfigTag(
  269. m->first.c_str(),
  270. this->Configuration.c_str(),
  271. 0,
  272. 0, 0, &fout);
  273. }
  274. else
  275. {
  276. fout << "<" << m->first << ">";
  277. }
  278. fout << m->second << "</" << m->first << ">\n";
  279. }
  280. }
  281. else
  282. {
  283. for(std::map<cmStdString, cmStdString>::iterator m = this->FlagMap.begin();
  284. m != this->FlagMap.end(); ++m)
  285. {
  286. fout << indent << m->first << "=\"" << m->second << "\"\n";
  287. }
  288. }
  289. }
  290. //----------------------------------------------------------------------------
  291. void
  292. cmVisualStudioGeneratorOptions
  293. ::OutputAdditionalOptions(std::ostream& fout,
  294. const char* prefix,
  295. const char* suffix)
  296. {
  297. if(!this->FlagString.empty())
  298. {
  299. if(this->Version == 10)
  300. {
  301. fout << prefix;
  302. if(this->Configuration.size())
  303. {
  304. this->TargetGenerator->WritePlatformConfigTag(
  305. "AdditionalOptions",
  306. this->Configuration.c_str(),
  307. 0,
  308. 0, 0, &fout);
  309. }
  310. else
  311. {
  312. fout << "<AdditionalOptions>";
  313. }
  314. fout << this->FlagString.c_str()
  315. << " %(AdditionalOptions)</AdditionalOptions>\n";
  316. }
  317. else
  318. {
  319. fout << prefix << "AdditionalOptions=\"";
  320. fout <<
  321. cmVisualStudioGeneratorOptionsEscapeForXML(this->FlagString.c_str());
  322. fout << "\"" << suffix;
  323. }
  324. }
  325. }