cmVisualStudioGeneratorOptions.cxx 11 KB

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