cmVisualStudioGeneratorOptions.cxx 11 KB

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