1
0

cmVisualStudioGeneratorOptions.cxx 11 KB

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