1
0

cmVisualStudioGeneratorOptions.cxx 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  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. LocalGenerator(lg), Version(version), CurrentTool(tool),
  32. DoingDefine(false), FlagTable(table), ExtraFlagTable(extraTable),
  33. TargetGenerator(g)
  34. {
  35. }
  36. //----------------------------------------------------------------------------
  37. void cmVisualStudioGeneratorOptions::FixExceptionHandlingDefault()
  38. {
  39. // Exception handling is on by default because the platform file has
  40. // "/EHsc" in the flags. Normally, that will override this
  41. // initialization to off, but the user has the option of removing
  42. // the flag to disable exception handling. When the user does
  43. // remove the flag we need to override the IDE default of on.
  44. switch (this->Version)
  45. {
  46. case 7:
  47. case 71:
  48. this->FlagMap["ExceptionHandling"] = "FALSE";
  49. break;
  50. case 10:
  51. // by default VS puts <ExceptionHandling></ExceptionHandling> empty
  52. // for a project, to make our projects look the same put a new line
  53. // and space over for the closing </ExceptionHandling> as the default
  54. // value
  55. this->FlagMap["ExceptionHandling"] = "\n ";
  56. break;
  57. default:
  58. this->FlagMap["ExceptionHandling"] = "0";
  59. break;
  60. }
  61. }
  62. //----------------------------------------------------------------------------
  63. void cmVisualStudioGeneratorOptions::SetVerboseMakefile(bool verbose)
  64. {
  65. // If verbose makefiles have been requested and the /nologo option
  66. // was not given explicitly in the flags we want to add an attribute
  67. // to the generated project to disable logo suppression. Otherwise
  68. // the GUI default is to enable suppression.
  69. if(verbose &&
  70. this->FlagMap.find("SuppressStartupBanner") == this->FlagMap.end())
  71. {
  72. if(this->Version == 10)
  73. {
  74. this->FlagMap["SuppressStartupBanner"] = "false";
  75. }
  76. else
  77. {
  78. this->FlagMap["SuppressStartupBanner"] = "FALSE";
  79. }
  80. }
  81. }
  82. //----------------------------------------------------------------------------
  83. void cmVisualStudioGeneratorOptions::AddDefine(const std::string& def)
  84. {
  85. this->Defines.push_back(def);
  86. }
  87. //----------------------------------------------------------------------------
  88. void cmVisualStudioGeneratorOptions::AddDefines(const char* defines)
  89. {
  90. if(defines)
  91. {
  92. // Expand the list of definitions.
  93. cmSystemTools::ExpandListArgument(defines, this->Defines);
  94. }
  95. }
  96. //----------------------------------------------------------------------------
  97. void cmVisualStudioGeneratorOptions::AddFlag(const char* flag,
  98. const char* value)
  99. {
  100. this->FlagMap[flag] = value;
  101. }
  102. bool cmVisualStudioGeneratorOptions::IsDebug()
  103. {
  104. return this->FlagMap.find("DebugInformationFormat") != this->FlagMap.end();
  105. }
  106. //----------------------------------------------------------------------------
  107. bool cmVisualStudioGeneratorOptions::UsingUnicode()
  108. {
  109. // Look for the a _UNICODE definition.
  110. for(std::vector<std::string>::const_iterator di = this->Defines.begin();
  111. di != this->Defines.end(); ++di)
  112. {
  113. if(*di == "_UNICODE")
  114. {
  115. return true;
  116. }
  117. }
  118. return false;
  119. }
  120. //----------------------------------------------------------------------------
  121. void cmVisualStudioGeneratorOptions::Parse(const char* flags)
  122. {
  123. // Parse the input string as a windows command line since the string
  124. // is intended for writing directly into the build files.
  125. std::vector<std::string> args;
  126. cmSystemTools::ParseWindowsCommandLine(flags, args);
  127. // Process flags that need to be represented specially in the IDE
  128. // project file.
  129. for(std::vector<std::string>::iterator ai = args.begin();
  130. ai != args.end(); ++ai)
  131. {
  132. this->HandleFlag(ai->c_str());
  133. }
  134. }
  135. //----------------------------------------------------------------------------
  136. void cmVisualStudioGeneratorOptions::HandleFlag(const char* flag)
  137. {
  138. // If the last option was -D then this option is the definition.
  139. if(this->DoingDefine)
  140. {
  141. this->DoingDefine = false;
  142. this->Defines.push_back(flag);
  143. return;
  144. }
  145. // Look for known arguments.
  146. if(flag[0] == '-' || flag[0] == '/')
  147. {
  148. // Look for preprocessor definitions.
  149. if(this->CurrentTool == Compiler && flag[1] == 'D')
  150. {
  151. if(flag[2] == '\0')
  152. {
  153. // The next argument will have the definition.
  154. this->DoingDefine = true;
  155. }
  156. else
  157. {
  158. // Store this definition.
  159. this->Defines.push_back(flag+2);
  160. }
  161. return;
  162. }
  163. // Look through the available flag tables.
  164. bool flag_handled = false;
  165. if(this->FlagTable &&
  166. this->CheckFlagTable(this->FlagTable, flag, flag_handled))
  167. {
  168. return;
  169. }
  170. if(this->ExtraFlagTable &&
  171. this->CheckFlagTable(this->ExtraFlagTable, flag, flag_handled))
  172. {
  173. return;
  174. }
  175. // If any map entry handled the flag we are done.
  176. if(flag_handled)
  177. {
  178. return;
  179. }
  180. }
  181. // This option is not known. Store it in the output flags.
  182. this->FlagString += " ";
  183. this->FlagString +=
  184. cmSystemTools::EscapeWindowsShellArgument(
  185. flag,
  186. cmsysSystem_Shell_Flag_AllowMakeVariables |
  187. cmsysSystem_Shell_Flag_VSIDE);
  188. }
  189. //----------------------------------------------------------------------------
  190. bool
  191. cmVisualStudioGeneratorOptions
  192. ::CheckFlagTable(cmVS7FlagTable const* table, const char* flag,
  193. bool& flag_handled)
  194. {
  195. // Look for an entry in the flag table matching this flag.
  196. for(cmVS7FlagTable const* entry = table; entry->IDEName; ++entry)
  197. {
  198. bool entry_found = false;
  199. if(entry->special & cmVS7FlagTable::UserValue)
  200. {
  201. // This flag table entry accepts a user-specified value. If
  202. // the entry specifies UserRequired we must match only if a
  203. // non-empty value is given.
  204. int n = static_cast<int>(strlen(entry->commandFlag));
  205. if(strncmp(flag+1, entry->commandFlag, n) == 0 &&
  206. (!(entry->special & cmVS7FlagTable::UserRequired) ||
  207. static_cast<int>(strlen(flag+1)) > n))
  208. {
  209. if(entry->special & cmVS7FlagTable::UserIgnored)
  210. {
  211. // Ignore the user-specified value.
  212. this->FlagMap[entry->IDEName] = entry->value;
  213. }
  214. else if(entry->special & cmVS7FlagTable::SemicolonAppendable)
  215. {
  216. const char *new_value = flag+1+n;
  217. std::map<cmStdString,cmStdString>::iterator itr;
  218. itr = this->FlagMap.find(entry->IDEName);
  219. if(itr != this->FlagMap.end())
  220. {
  221. // Append to old value (if present) with semicolons;
  222. itr->second += ";";
  223. itr->second += new_value;
  224. }
  225. else
  226. {
  227. this->FlagMap[entry->IDEName] = new_value;
  228. }
  229. }
  230. else
  231. {
  232. // Use the user-specified value.
  233. this->FlagMap[entry->IDEName] = flag+1+n;
  234. }
  235. entry_found = true;
  236. }
  237. }
  238. else if(strcmp(flag+1, entry->commandFlag) == 0)
  239. {
  240. // This flag table entry provides a fixed value.
  241. this->FlagMap[entry->IDEName] = entry->value;
  242. entry_found = true;
  243. }
  244. // If the flag has been handled by an entry not requesting a
  245. // search continuation we are done.
  246. if(entry_found && !(entry->special & cmVS7FlagTable::Continue))
  247. {
  248. return true;
  249. }
  250. // If the entry was found the flag has been handled.
  251. flag_handled = flag_handled || entry_found;
  252. }
  253. return false;
  254. }
  255. void cmVisualStudioGeneratorOptions::SetConfiguration(const char* config)
  256. {
  257. this->Configuration = config;
  258. }
  259. //----------------------------------------------------------------------------
  260. void
  261. cmVisualStudioGeneratorOptions
  262. ::OutputPreprocessorDefinitions(std::ostream& fout,
  263. const char* prefix,
  264. const char* suffix)
  265. {
  266. if(this->Defines.empty())
  267. {
  268. return;
  269. }
  270. if(this->Version == 10)
  271. {
  272. // if there are configuration specifc flags, then
  273. // use the configuration specific tag for PreprocessorDefinitions
  274. if(this->Configuration.size())
  275. {
  276. fout << prefix;
  277. this->TargetGenerator->WritePlatformConfigTag(
  278. "PreprocessorDefinitions",
  279. this->Configuration.c_str(),
  280. 0,
  281. 0, 0, &fout);
  282. }
  283. else
  284. {
  285. fout << prefix << "<PreprocessorDefinitions>";
  286. }
  287. }
  288. else
  289. {
  290. fout << prefix << "PreprocessorDefinitions=\"";
  291. }
  292. const char* comma = "";
  293. for(std::vector<std::string>::const_iterator di = this->Defines.begin();
  294. di != this->Defines.end(); ++di)
  295. {
  296. // Escape the definition for the compiler.
  297. std::string define;
  298. if(this->Version != 10)
  299. {
  300. define =
  301. this->LocalGenerator->EscapeForShell(di->c_str(), true);
  302. }
  303. else
  304. {
  305. define = *di;
  306. }
  307. // Escape this flag for the IDE.
  308. if(this->Version == 10)
  309. {
  310. define = cmVisualStudio10GeneratorOptionsEscapeForXML(define.c_str());
  311. }
  312. else
  313. {
  314. define = cmVisualStudioGeneratorOptionsEscapeForXML(define.c_str());
  315. }
  316. // Store the flag in the project file.
  317. fout << comma << define;
  318. if(this->Version == 10)
  319. {
  320. comma = ";";
  321. }
  322. else
  323. {
  324. comma = ",";
  325. }
  326. }
  327. if(this->Version == 10)
  328. {
  329. fout << ";%(PreprocessorDefinitions)</PreprocessorDefinitions>" << suffix;
  330. }
  331. else
  332. {
  333. fout << "\"" << suffix;
  334. }
  335. }
  336. //----------------------------------------------------------------------------
  337. void
  338. cmVisualStudioGeneratorOptions
  339. ::OutputFlagMap(std::ostream& fout, const char* indent)
  340. {
  341. if(this->Version == 10)
  342. {
  343. for(std::map<cmStdString, cmStdString>::iterator m = this->FlagMap.begin();
  344. m != this->FlagMap.end(); ++m)
  345. {
  346. fout << indent;
  347. if(this->Configuration.size())
  348. {
  349. this->TargetGenerator->WritePlatformConfigTag(
  350. m->first.c_str(),
  351. this->Configuration.c_str(),
  352. 0,
  353. 0, 0, &fout);
  354. }
  355. else
  356. {
  357. fout << "<" << m->first << ">";
  358. }
  359. fout << m->second << "</" << m->first << ">\n";
  360. }
  361. }
  362. else
  363. {
  364. for(std::map<cmStdString, cmStdString>::iterator m = this->FlagMap.begin();
  365. m != this->FlagMap.end(); ++m)
  366. {
  367. fout << indent << m->first << "=\"" << m->second << "\"\n";
  368. }
  369. }
  370. }
  371. //----------------------------------------------------------------------------
  372. void
  373. cmVisualStudioGeneratorOptions
  374. ::OutputAdditionalOptions(std::ostream& fout,
  375. const char* prefix,
  376. const char* suffix)
  377. {
  378. if(!this->FlagString.empty())
  379. {
  380. if(this->Version == 10)
  381. {
  382. fout << prefix;
  383. if(this->Configuration.size())
  384. {
  385. this->TargetGenerator->WritePlatformConfigTag(
  386. "AdditionalOptions",
  387. this->Configuration.c_str(),
  388. 0,
  389. 0, 0, &fout);
  390. }
  391. else
  392. {
  393. fout << "<AdditionalOptions>";
  394. }
  395. fout << this->FlagString.c_str()
  396. << " %(AdditionalOptions)</AdditionalOptions>\n";
  397. }
  398. else
  399. {
  400. fout << prefix << "AdditionalOptions=\"";
  401. fout <<
  402. cmVisualStudioGeneratorOptionsEscapeForXML(this->FlagString.c_str());
  403. fout << "\"" << suffix;
  404. }
  405. }
  406. }