cmVisualStudioGeneratorOptions.cxx 12 KB

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