cmVisualStudioGeneratorOptions.cxx 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  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. return this->FlagMap.find("DebugInformationFormat") != this->FlagMap.end();
  120. }
  121. bool cmVisualStudioGeneratorOptions::IsWinRt() const
  122. {
  123. return this->FlagMap.find("CompileAsWinRT") != this->FlagMap.end();
  124. }
  125. bool cmVisualStudioGeneratorOptions::IsManaged() const
  126. {
  127. return this->FlagMap.find("CompileAsManaged") != this->FlagMap.end();
  128. }
  129. bool cmVisualStudioGeneratorOptions::UsingUnicode() const
  130. {
  131. // Look for the a _UNICODE definition.
  132. for (std::vector<std::string>::const_iterator di = this->Defines.begin();
  133. di != this->Defines.end(); ++di) {
  134. if (*di == "_UNICODE") {
  135. return true;
  136. }
  137. }
  138. return false;
  139. }
  140. bool cmVisualStudioGeneratorOptions::UsingSBCS() const
  141. {
  142. // Look for the a _SBCS definition.
  143. for (std::vector<std::string>::const_iterator di = this->Defines.begin();
  144. di != this->Defines.end(); ++di) {
  145. if (*di == "_SBCS") {
  146. return true;
  147. }
  148. }
  149. return false;
  150. }
  151. void cmVisualStudioGeneratorOptions::Parse(const char* flags)
  152. {
  153. // Parse the input string as a windows command line since the string
  154. // is intended for writing directly into the build files.
  155. std::vector<std::string> args;
  156. cmSystemTools::ParseWindowsCommandLine(flags, args);
  157. // Process flags that need to be represented specially in the IDE
  158. // project file.
  159. for (std::vector<std::string>::iterator ai = args.begin(); ai != args.end();
  160. ++ai) {
  161. this->HandleFlag(ai->c_str());
  162. }
  163. }
  164. void cmVisualStudioGeneratorOptions::ParseFinish()
  165. {
  166. if (this->CurrentTool == FortranCompiler) {
  167. // "RuntimeLibrary" attribute values:
  168. // "rtMultiThreaded", "0", /threads /libs:static
  169. // "rtMultiThreadedDLL", "2", /threads /libs:dll
  170. // "rtMultiThreadedDebug", "1", /threads /dbglibs /libs:static
  171. // "rtMultiThreadedDebugDLL", "3", /threads /dbglibs /libs:dll
  172. // These seem unimplemented by the IDE:
  173. // "rtSingleThreaded", "4", /libs:static
  174. // "rtSingleThreadedDLL", "10", /libs:dll
  175. // "rtSingleThreadedDebug", "5", /dbglibs /libs:static
  176. // "rtSingleThreadedDebugDLL", "11", /dbglibs /libs:dll
  177. std::string rl = "rtMultiThreaded";
  178. rl += this->FortranRuntimeDebug ? "Debug" : "";
  179. rl += this->FortranRuntimeDLL ? "DLL" : "";
  180. this->FlagMap["RuntimeLibrary"] = rl;
  181. }
  182. }
  183. void cmVisualStudioGeneratorOptions::StoreUnknownFlag(const char* flag)
  184. {
  185. // Look for Intel Fortran flags that do not map well in the flag table.
  186. if (this->CurrentTool == FortranCompiler) {
  187. if (strcmp(flag, "/dbglibs") == 0) {
  188. this->FortranRuntimeDebug = true;
  189. return;
  190. }
  191. if (strcmp(flag, "/threads") == 0) {
  192. this->FortranRuntimeMT = true;
  193. return;
  194. }
  195. if (strcmp(flag, "/libs:dll") == 0) {
  196. this->FortranRuntimeDLL = true;
  197. return;
  198. }
  199. if (strcmp(flag, "/libs:static") == 0) {
  200. this->FortranRuntimeDLL = false;
  201. return;
  202. }
  203. }
  204. // This option is not known. Store it in the output flags.
  205. this->FlagString += " ";
  206. this->FlagString += cmOutputConverter::EscapeWindowsShellArgument(
  207. flag, cmOutputConverter::Shell_Flag_AllowMakeVariables |
  208. cmOutputConverter::Shell_Flag_VSIDE);
  209. }
  210. void cmVisualStudioGeneratorOptions::SetConfiguration(const char* config)
  211. {
  212. this->Configuration = config;
  213. }
  214. void cmVisualStudioGeneratorOptions::OutputPreprocessorDefinitions(
  215. std::ostream& fout, const char* prefix, const char* suffix,
  216. const std::string& lang)
  217. {
  218. if (this->Defines.empty()) {
  219. return;
  220. }
  221. if (this->Version >= cmGlobalVisualStudioGenerator::VS10) {
  222. // if there are configuration specific flags, then
  223. // use the configuration specific tag for PreprocessorDefinitions
  224. if (!this->Configuration.empty()) {
  225. fout << prefix;
  226. this->TargetGenerator->WritePlatformConfigTag(
  227. "PreprocessorDefinitions", this->Configuration.c_str(), 0, 0, 0,
  228. &fout);
  229. } else {
  230. fout << prefix << "<PreprocessorDefinitions>";
  231. }
  232. } else {
  233. fout << prefix << "PreprocessorDefinitions=\"";
  234. }
  235. const char* sep = "";
  236. for (std::vector<std::string>::const_iterator di = this->Defines.begin();
  237. di != this->Defines.end(); ++di) {
  238. // Escape the definition for the compiler.
  239. std::string define;
  240. if (this->Version < cmGlobalVisualStudioGenerator::VS10) {
  241. define = this->LocalGenerator->EscapeForShell(di->c_str(), true);
  242. } else {
  243. define = *di;
  244. }
  245. // Escape this flag for the IDE.
  246. if (this->Version >= cmGlobalVisualStudioGenerator::VS10) {
  247. define = cmVisualStudio10GeneratorOptionsEscapeForXML(define);
  248. if (lang == "RC") {
  249. cmSystemTools::ReplaceString(define, "\"", "\\\"");
  250. }
  251. } else {
  252. define = cmVisualStudioGeneratorOptionsEscapeForXML(define);
  253. }
  254. // Store the flag in the project file.
  255. fout << sep << define;
  256. sep = ";";
  257. }
  258. if (this->Version >= cmGlobalVisualStudioGenerator::VS10) {
  259. fout << ";%(PreprocessorDefinitions)</PreprocessorDefinitions>" << suffix;
  260. } else {
  261. fout << "\"" << suffix;
  262. }
  263. }
  264. void cmVisualStudioGeneratorOptions::OutputFlagMap(std::ostream& fout,
  265. const char* indent)
  266. {
  267. if (this->Version >= cmGlobalVisualStudioGenerator::VS10) {
  268. for (std::map<std::string, FlagValue>::iterator m = this->FlagMap.begin();
  269. m != this->FlagMap.end(); ++m) {
  270. fout << indent;
  271. if (!this->Configuration.empty()) {
  272. this->TargetGenerator->WritePlatformConfigTag(
  273. m->first.c_str(), this->Configuration.c_str(), 0, 0, 0, &fout);
  274. } else {
  275. fout << "<" << m->first << ">";
  276. }
  277. const char* sep = "";
  278. for (std::vector<std::string>::iterator i = m->second.begin();
  279. i != m->second.end(); ++i) {
  280. fout << sep << cmVisualStudio10GeneratorOptionsEscapeForXML(*i);
  281. sep = ";";
  282. }
  283. fout << "</" << m->first << ">\n";
  284. }
  285. } else {
  286. for (std::map<std::string, FlagValue>::iterator m = this->FlagMap.begin();
  287. m != this->FlagMap.end(); ++m) {
  288. fout << indent << m->first << "=\"";
  289. const char* sep = "";
  290. for (std::vector<std::string>::iterator i = m->second.begin();
  291. i != m->second.end(); ++i) {
  292. fout << sep << cmVisualStudioGeneratorOptionsEscapeForXML(*i);
  293. sep = ";";
  294. }
  295. fout << "\"\n";
  296. }
  297. }
  298. }
  299. void cmVisualStudioGeneratorOptions::OutputAdditionalOptions(
  300. std::ostream& fout, const char* prefix, const char* suffix)
  301. {
  302. if (!this->FlagString.empty()) {
  303. if (this->Version >= cmGlobalVisualStudioGenerator::VS10) {
  304. fout << prefix;
  305. if (!this->Configuration.empty()) {
  306. this->TargetGenerator->WritePlatformConfigTag(
  307. "AdditionalOptions", this->Configuration.c_str(), 0, 0, 0, &fout);
  308. } else {
  309. fout << "<AdditionalOptions>";
  310. }
  311. fout << "%(AdditionalOptions) "
  312. << cmVisualStudio10GeneratorOptionsEscapeForXML(this->FlagString)
  313. << "</AdditionalOptions>\n";
  314. } else {
  315. fout << prefix << "AdditionalOptions=\"";
  316. fout << cmVisualStudioGeneratorOptionsEscapeForXML(this->FlagString);
  317. fout << "\"" << suffix;
  318. }
  319. }
  320. }