cmVisualStudioGeneratorOptions.cxx 11 KB

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