cmVisualStudioGeneratorOptions.cxx 12 KB

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