cmLocalVisualStudio7Generator.cxx 62 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908
  1. /*=========================================================================
  2. Program: CMake - Cross-Platform Makefile Generator
  3. Module: $RCSfile$
  4. Language: C++
  5. Date: $Date$
  6. Version: $Revision$
  7. Copyright (c) 2002 Kitware, Inc., Insight Consortium. All rights reserved.
  8. See Copyright.txt or http://www.cmake.org/HTML/Copyright.html for details.
  9. This software is distributed WITHOUT ANY WARRANTY; without even
  10. the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  11. PURPOSE. See the above copyright notices for more information.
  12. =========================================================================*/
  13. #include "cmGlobalVisualStudio7Generator.h"
  14. #include "cmLocalVisualStudio7Generator.h"
  15. #include "cmXMLParser.h"
  16. #include <cm_expat.h>
  17. #include "cmMakefile.h"
  18. #include "cmSystemTools.h"
  19. #include "cmSourceFile.h"
  20. #include "cmCacheManager.h"
  21. #include "cmake.h"
  22. #include <cmsys/System.h>
  23. #include <ctype.h> // for isspace
  24. extern cmVS7FlagTable cmLocalVisualStudio7GeneratorFlagTable[];
  25. //----------------------------------------------------------------------------
  26. cmLocalVisualStudio7Generator::cmLocalVisualStudio7Generator()
  27. {
  28. this->Version = 7;
  29. this->PlatformName = "Win32";
  30. this->ExtraFlagTable = 0;
  31. }
  32. cmLocalVisualStudio7Generator::~cmLocalVisualStudio7Generator()
  33. {
  34. }
  35. void cmLocalVisualStudio7Generator::AddHelperCommands()
  36. {
  37. std::set<cmStdString> lang;
  38. lang.insert("C");
  39. lang.insert("CXX");
  40. lang.insert("RC");
  41. lang.insert("IDL");
  42. lang.insert("DEF");
  43. this->CreateCustomTargetsAndCommands(lang);
  44. this->FixGlobalTargets();
  45. }
  46. void cmLocalVisualStudio7Generator::Generate()
  47. {
  48. this->OutputVCProjFile();
  49. }
  50. void cmLocalVisualStudio7Generator::FixGlobalTargets()
  51. {
  52. // Visual Studio .NET 2003 Service Pack 1 will not run post-build
  53. // commands for targets in which no sources are built. Add dummy
  54. // rules to force these targets to build.
  55. cmTargets &tgts = this->Makefile->GetTargets();
  56. for(cmTargets::iterator l = tgts.begin();
  57. l != tgts.end(); l++)
  58. {
  59. cmTarget& tgt = l->second;
  60. if(tgt.GetType() == cmTarget::GLOBAL_TARGET)
  61. {
  62. std::vector<std::string> no_depends;
  63. cmCustomCommandLine force_command;
  64. force_command.push_back(";");
  65. cmCustomCommandLines force_commands;
  66. force_commands.push_back(force_command);
  67. const char* no_main_dependency = 0;
  68. std::string force = this->Makefile->GetStartOutputDirectory();
  69. force += cmake::GetCMakeFilesDirectory();
  70. force += "/";
  71. force += tgt.GetName();
  72. force += "_force";
  73. this->Makefile->AddCustomCommandToOutput(force.c_str(), no_depends,
  74. no_main_dependency,
  75. force_commands, " ", 0, true);
  76. if(cmSourceFile* file =
  77. this->Makefile->GetSourceFileWithOutput(force.c_str()))
  78. {
  79. tgt.AddSourceFile(file);
  80. }
  81. }
  82. }
  83. }
  84. // TODO
  85. // for CommandLine= need to repleace quotes with &quot
  86. // write out configurations
  87. void cmLocalVisualStudio7Generator::OutputVCProjFile()
  88. {
  89. // If not an in source build, then create the output directory
  90. if(strcmp(this->Makefile->GetStartOutputDirectory(),
  91. this->Makefile->GetHomeDirectory()) != 0)
  92. {
  93. if(!cmSystemTools::MakeDirectory
  94. (this->Makefile->GetStartOutputDirectory()))
  95. {
  96. cmSystemTools::Error("Error creating directory ",
  97. this->Makefile->GetStartOutputDirectory());
  98. }
  99. }
  100. // Create the VCProj or set of VCProj's for libraries and executables
  101. cmTargets &tgts = this->Makefile->GetTargets();
  102. for(cmTargets::iterator l = tgts.begin();
  103. l != tgts.end(); l++)
  104. {
  105. // Add a rule to regenerate the build system when the target
  106. // specification source changes.
  107. const char* suppRegenRule =
  108. this->Makefile->GetDefinition("CMAKE_SUPPRESS_REGENERATION");
  109. if (!cmSystemTools::IsOn(suppRegenRule) &&
  110. (strcmp(l->first.c_str(), CMAKE_CHECK_BUILD_SYSTEM_TARGET) != 0))
  111. {
  112. this->AddVCProjBuildRule(l->second);
  113. }
  114. }
  115. for(cmTargets::iterator l = tgts.begin();
  116. l != tgts.end(); l++)
  117. {
  118. // INCLUDE_EXTERNAL_MSPROJECT command only affects the workspace
  119. // so don't build a projectfile for it
  120. if (strncmp(l->first.c_str(), "INCLUDE_EXTERNAL_MSPROJECT", 26) != 0)
  121. {
  122. this->CreateSingleVCProj(l->first.c_str(),l->second);
  123. }
  124. }
  125. }
  126. void cmLocalVisualStudio7Generator
  127. ::CreateSingleVCProj(const char *lname, cmTarget &target)
  128. {
  129. // add to the list of projects
  130. std::string pname = lname;
  131. target.SetProperty("GENERATOR_FILE_NAME",lname);
  132. // create the dsp.cmake file
  133. std::string fname;
  134. fname = this->Makefile->GetStartOutputDirectory();
  135. fname += "/";
  136. fname += lname;
  137. fname += ".vcproj";
  138. // save the name of the real dsp file
  139. std::string realVCProj = fname;
  140. fname += ".cmake";
  141. std::ofstream fout(fname.c_str());
  142. if(!fout)
  143. {
  144. cmSystemTools::Error("Error Writing ", fname.c_str());
  145. }
  146. this->WriteVCProjFile(fout,lname,target);
  147. fout.close();
  148. // if the dsp file has changed, then write it.
  149. cmSystemTools::CopyFileIfDifferent(fname.c_str(), realVCProj.c_str());
  150. }
  151. void cmLocalVisualStudio7Generator::AddVCProjBuildRule(cmTarget& tgt)
  152. {
  153. std::string dspname = tgt.GetName();
  154. dspname += ".vcproj.cmake";
  155. const char* dsprule =
  156. this->Makefile->GetRequiredDefinition("CMAKE_COMMAND");
  157. cmCustomCommandLine commandLine;
  158. commandLine.push_back(dsprule);
  159. std::string makefileIn = this->Makefile->GetStartDirectory();
  160. makefileIn += "/";
  161. makefileIn += "CMakeLists.txt";
  162. makefileIn = cmSystemTools::CollapseFullPath(makefileIn.c_str());
  163. std::string comment = "Building Custom Rule ";
  164. comment += makefileIn;
  165. std::string args;
  166. args = "-H";
  167. args += this->Convert(this->Makefile->GetHomeDirectory(),
  168. START_OUTPUT, UNCHANGED, true);
  169. commandLine.push_back(args);
  170. args = "-B";
  171. args +=
  172. this->Convert(this->Makefile->GetHomeOutputDirectory(),
  173. START_OUTPUT, UNCHANGED, true);
  174. commandLine.push_back(args);
  175. std::string configFile =
  176. this->Makefile->GetRequiredDefinition("CMAKE_ROOT");
  177. configFile += "/Templates/CMakeWindowsSystemConfig.cmake";
  178. std::vector<std::string> listFiles = this->Makefile->GetListFiles();
  179. bool found = false;
  180. for(std::vector<std::string>::iterator i = listFiles.begin();
  181. i != listFiles.end(); ++i)
  182. {
  183. if(*i == configFile)
  184. {
  185. found = true;
  186. }
  187. }
  188. if(!found)
  189. {
  190. listFiles.push_back(configFile);
  191. }
  192. cmCustomCommandLines commandLines;
  193. commandLines.push_back(commandLine);
  194. const char* no_working_directory = 0;
  195. this->Makefile->AddCustomCommandToOutput(dspname.c_str(), listFiles,
  196. makefileIn.c_str(), commandLines,
  197. comment.c_str(),
  198. no_working_directory, true);
  199. if(cmSourceFile* file = this->Makefile->GetSource(makefileIn.c_str()))
  200. {
  201. tgt.AddSourceFile(file);
  202. }
  203. else
  204. {
  205. cmSystemTools::Error("Error adding rule for ", makefileIn.c_str());
  206. }
  207. }
  208. void cmLocalVisualStudio7Generator::WriteConfigurations(std::ostream& fout,
  209. const char *libName,
  210. cmTarget &target)
  211. {
  212. std::vector<std::string> *configs =
  213. static_cast<cmGlobalVisualStudio7Generator *>
  214. (this->GlobalGenerator)->GetConfigurations();
  215. fout << "\t<Configurations>\n";
  216. for( std::vector<std::string>::iterator i = configs->begin();
  217. i != configs->end(); ++i)
  218. {
  219. this->WriteConfiguration(fout, i->c_str(), libName, target);
  220. }
  221. fout << "\t</Configurations>\n";
  222. }
  223. // fill the table here currently the comment field is not used for
  224. // anything other than documentation NOTE: Make sure the longer
  225. // commandFlag comes FIRST!
  226. cmVS7FlagTable cmLocalVisualStudio7GeneratorFlagTable[] =
  227. {
  228. // option flags (some flags map to the same option)
  229. {"BasicRuntimeChecks", "GZ", "Stack frame checks", "1", 0},
  230. {"BasicRuntimeChecks", "RTCsu",
  231. "Both stack and uninitialized checks", "3", 0},
  232. {"BasicRuntimeChecks", "RTCs", "Stack frame checks", "1", 0},
  233. {"BasicRuntimeChecks", "RTCu", "Uninitialized Variables ", "2", 0},
  234. {"BasicRuntimeChecks", "RTC1",
  235. "Both stack and uninitialized checks", "3", 0},
  236. {"DebugInformationFormat", "Z7", "debug format", "1", 0},
  237. {"DebugInformationFormat", "Zd", "debug format", "2", 0},
  238. {"DebugInformationFormat", "Zi", "debug format", "3", 0},
  239. {"DebugInformationFormat", "ZI", "debug format", "4", 0},
  240. {"EnableEnhancedInstructionSet", "arch:SSE2",
  241. "Use sse2 instructions", "2", 0},
  242. {"EnableEnhancedInstructionSet", "arch:SSE",
  243. "Use sse instructions", "1", 0},
  244. {"FavorSizeOrSpeed", "Ot", "Favor fast code", "1", 0},
  245. {"FavorSizeOrSpeed", "Os", "Favor small code", "2", 0},
  246. {"CompileAs", "TC", "Compile as c code", "1", 0},
  247. {"CompileAs", "TP", "Compile as c++ code", "2", 0},
  248. {"Optimization", "Od", "Non Debug", "0", 0},
  249. {"Optimization", "O1", "Min Size", "1", 0},
  250. {"Optimization", "O2", "Max Speed", "2", 0},
  251. {"Optimization", "Ox", "Max Optimization", "3", 0},
  252. {"OptimizeForProcessor", "GB", "Blended processor mode", "0", 0},
  253. {"OptimizeForProcessor", "G5", "Pentium", "1", 0},
  254. {"OptimizeForProcessor", "G6", "PPro PII PIII", "2", 0},
  255. {"OptimizeForProcessor", "G7", "Pentium 4 or Athlon", "3", 0},
  256. {"InlineFunctionExpansion", "Ob0", "no inlines", "0", 0},
  257. {"InlineFunctionExpansion", "Ob1", "when inline keyword", "1", 0},
  258. {"InlineFunctionExpansion", "Ob2", "any time you can inline", "2", 0},
  259. {"RuntimeLibrary", "MTd", "Multithreded debug", "1", 0},
  260. {"RuntimeLibrary", "MT", "Multithreded", "0", 0},
  261. {"RuntimeLibrary", "MDd", "Multithreded dll debug", "3", 0},
  262. {"RuntimeLibrary", "MD", "Multithreded dll", "2", 0},
  263. {"RuntimeLibrary", "MLd", "Sinble Thread debug", "5", 0},
  264. {"RuntimeLibrary", "ML", "Sinble Thread", "4", 0},
  265. {"StructMemberAlignment", "Zp16", "struct align 16 byte ", "5", 0},
  266. {"StructMemberAlignment", "Zp1", "struct align 1 byte ", "1", 0},
  267. {"StructMemberAlignment", "Zp2", "struct align 2 byte ", "2", 0},
  268. {"StructMemberAlignment", "Zp4", "struct align 4 byte ", "3", 0},
  269. {"StructMemberAlignment", "Zp8", "struct align 8 byte ", "4", 0},
  270. {"WarningLevel", "W1", "Warning level", "1", 0},
  271. {"WarningLevel", "W2", "Warning level", "2", 0},
  272. {"WarningLevel", "W3", "Warning level", "3", 0},
  273. {"WarningLevel", "W4", "Warning level", "4", 0},
  274. // Precompiled header and related options. Note that the
  275. // UsePrecompiledHeader entries are marked as "Continue" so that the
  276. // corresponding PrecompiledHeaderThrough entry can be found.
  277. {"UsePrecompiledHeader", "Yc", "Create Precompiled Header", "1",
  278. cmVS7FlagTable::UserValueIgnored | cmVS7FlagTable::Continue},
  279. {"PrecompiledHeaderThrough", "Yc", "Precompiled Header Name", "",
  280. cmVS7FlagTable::UserValueRequired},
  281. {"PrecompiledHeaderFile", "Fp", "Generated Precompiled Header", "",
  282. cmVS7FlagTable::UserValue},
  283. // The YX and Yu options are in a per-global-generator table because
  284. // their values differ based on the VS IDE version.
  285. {"ForcedIncludeFiles", "FI", "Forced include files", "",
  286. cmVS7FlagTable::UserValueRequired},
  287. // boolean flags
  288. {"BufferSecurityCheck", "GS", "Buffer security check", "TRUE", 0},
  289. {"EnableFibreSafeOptimization", "GT", "OmitFramePointers", "TRUE", 0},
  290. {"EnableFunctionLevelLinking", "Gy",
  291. "EnableFunctionLevelLinking", "TRUE", 0},
  292. {"EnableIntrinsicFunctions", "Oi", "EnableIntrinsicFunctions", "TRUE", 0},
  293. {"ExceptionHandling", "EHsc", "enable c++ exceptions", "TRUE", 0},
  294. {"ExceptionHandling", "EHa", "enable c++ exceptions", "2", 0},
  295. {"ExceptionHandling", "GX", "enable c++ exceptions", "TRUE", 0},
  296. {"GlobalOptimizations", "Og", "Global Optimize", "TRUE", 0},
  297. {"ImproveFloatingPointConsistency", "Op",
  298. "ImproveFloatingPointConsistency", "TRUE", 0},
  299. {"MinimalRebuild", "Gm", "minimal rebuild", "TRUE", 0},
  300. {"OmitFramePointers", "Oy", "OmitFramePointers", "TRUE", 0},
  301. {"OptimizeForWindowsApplication", "GA", "Optimize for windows", "TRUE", 0},
  302. {"RuntimeTypeInfo", "GR",
  303. "Turn on Run time type information for c++", "TRUE", 0},
  304. {"SmallerTypeCheck", "RTCc", "smaller type check", "TRUE", 0},
  305. {"SuppressStartupBanner", "nologo", "SuppressStartupBanner", "TRUE", 0},
  306. {"WarnAsError", "WX", "Treat warnings as errors", "TRUE", 0},
  307. {0,0,0,0,0}
  308. };
  309. cmVS7FlagTable cmLocalVisualStudio7GeneratorLinkFlagTable[] =
  310. {
  311. // option flags (some flags map to the same option)
  312. {"GenerateManifest", "MANIFEST:NO",
  313. "disable manifest generation", "FALSE", 0},
  314. {"GenerateManifest", "MANIFEST", "enable manifest generation", "TRUE", 0},
  315. {"LinkIncremental", "INCREMENTAL:NO", "link incremental", "1", 0},
  316. {"LinkIncremental", "INCREMENTAL:YES", "link incremental", "2", 0},
  317. {"IgnoreDefaultLibraryNames", "NODEFAULTLIB:", "default libs to ignore", "",
  318. cmVS7FlagTable::UserValue},
  319. {"IgnoreAllDefaultLibraries", "NODEFAULTLIB", "ignore all default libs",
  320. "TRUE", 0},
  321. {"ModuleDefinitionFile", "DEF:", "add an export def file", "",
  322. cmVS7FlagTable::UserValue},
  323. {0,0,0,0,0}
  324. };
  325. //----------------------------------------------------------------------------
  326. class cmLocalVisualStudio7GeneratorOptions
  327. {
  328. public:
  329. // Construct an options table for a given tool.
  330. enum Tool
  331. {
  332. Compiler,
  333. Linker
  334. };
  335. cmLocalVisualStudio7GeneratorOptions(Tool tool,
  336. cmVS7FlagTable const* extraTable = 0);
  337. // Store options from command line flags.
  338. void Parse(const char* flags);
  339. // Fix the ExceptionHandling option to default to off.
  340. void FixExceptionHandlingDefault();
  341. // Store options for verbose builds.
  342. void SetVerboseMakefile(bool verbose);
  343. // Store definitions and flags.
  344. void AddDefine(const std::string& define);
  345. void AddFlag(const char* flag, const char* value);
  346. // Check for specific options.
  347. bool UsingUnicode();
  348. // Write options to output.
  349. void OutputPreprocessorDefinitions(std::ostream& fout,
  350. const char* prefix,
  351. const char* suffix);
  352. void OutputFlagMap(std::ostream& fout, const char* indent);
  353. void OutputAdditionalOptions(std::ostream& fout,
  354. const char* prefix,
  355. const char* suffix);
  356. private:
  357. // create a map of xml tags to the values they should have in the output
  358. // for example, "BufferSecurityCheck" = "TRUE"
  359. // first fill this table with the values for the configuration
  360. // Debug, Release, etc,
  361. // Then parse the command line flags specified in CMAKE_CXX_FLAGS
  362. // and CMAKE_C_FLAGS
  363. // and overwrite or add new values to this map
  364. std::map<cmStdString, cmStdString> FlagMap;
  365. // Preprocessor definitions.
  366. std::vector<cmStdString> Defines;
  367. // Unrecognized flags that get no special handling.
  368. cmStdString FlagString;
  369. Tool CurrentTool;
  370. bool DoingDefine;
  371. cmVS7FlagTable const* FlagTable;
  372. cmVS7FlagTable const* ExtraFlagTable;
  373. void HandleFlag(const char* flag);
  374. bool CheckFlagTable(cmVS7FlagTable const* table, const char* flag,
  375. bool& flag_handled);
  376. };
  377. void cmLocalVisualStudio7Generator::WriteConfiguration(std::ostream& fout,
  378. const char* configName,
  379. const char *libName,
  380. cmTarget &target)
  381. {
  382. const char* mfcFlag = this->Makefile->GetDefinition("CMAKE_MFC_FLAG");
  383. if(!mfcFlag)
  384. {
  385. mfcFlag = "0";
  386. }
  387. fout << "\t\t<Configuration\n"
  388. << "\t\t\tName=\"" << configName << "|" << this->PlatformName << "\"\n"
  389. << "\t\t\tOutputDirectory=\"" << configName << "\"\n";
  390. // This is an internal type to Visual Studio, it seems that:
  391. // 4 == static library
  392. // 2 == dll
  393. // 1 == executable
  394. // 10 == utility
  395. const char* configType = "10";
  396. switch(target.GetType())
  397. {
  398. case cmTarget::STATIC_LIBRARY:
  399. configType = "4";
  400. break;
  401. case cmTarget::SHARED_LIBRARY:
  402. case cmTarget::MODULE_LIBRARY:
  403. configType = "2";
  404. break;
  405. case cmTarget::EXECUTABLE:
  406. configType = "1";
  407. break;
  408. case cmTarget::UTILITY:
  409. case cmTarget::GLOBAL_TARGET:
  410. configType = "10";
  411. default:
  412. break;
  413. }
  414. std::string flags;
  415. if(strcmp(configType, "10") != 0)
  416. {
  417. const char* linkLanguage =
  418. target.GetLinkerLanguage(this->GetGlobalGenerator());
  419. if(!linkLanguage)
  420. {
  421. cmSystemTools::Error
  422. ("CMake can not determine linker language for target:",
  423. target.GetName());
  424. return;
  425. }
  426. if(strcmp(linkLanguage, "C") == 0 || strcmp(linkLanguage, "CXX") == 0)
  427. {
  428. std::string baseFlagVar = "CMAKE_";
  429. baseFlagVar += linkLanguage;
  430. baseFlagVar += "_FLAGS";
  431. flags = this->Makefile->GetRequiredDefinition(baseFlagVar.c_str());
  432. std::string flagVar = baseFlagVar + std::string("_") +
  433. cmSystemTools::UpperCase(configName);
  434. flags += " ";
  435. flags += this->Makefile->GetRequiredDefinition(flagVar.c_str());
  436. }
  437. // set the correct language
  438. if(strcmp(linkLanguage, "C") == 0)
  439. {
  440. flags += " /TC ";
  441. }
  442. if(strcmp(linkLanguage, "CXX") == 0)
  443. {
  444. flags += " /TP ";
  445. }
  446. }
  447. // Add the target-specific flags.
  448. if(const char* targetFlags = target.GetProperty("COMPILE_FLAGS"))
  449. {
  450. flags += " ";
  451. flags += targetFlags;
  452. }
  453. // Get preprocessor definitions for this directory.
  454. std::string defineFlags = this->Makefile->GetDefineFlags();
  455. // Construct a set of build options for this target.
  456. Options targetOptions(Options::Compiler, this->ExtraFlagTable);
  457. targetOptions.FixExceptionHandlingDefault();
  458. targetOptions.Parse(flags.c_str());
  459. targetOptions.Parse(defineFlags.c_str());
  460. targetOptions.SetVerboseMakefile(
  461. this->Makefile->IsOn("CMAKE_VERBOSE_MAKEFILE"));
  462. // Add a definition for the configuration name.
  463. std::string configDefine = "CMAKE_INTDIR=\"";
  464. configDefine += configName;
  465. configDefine += "\"";
  466. targetOptions.AddDefine(configDefine);
  467. // Add the export symbol definition for shared library objects.
  468. if(const char* exportMacro = target.GetExportMacro())
  469. {
  470. targetOptions.AddDefine(exportMacro);
  471. }
  472. // The intermediate directory name consists of a directory for the
  473. // target and a subdirectory for the configuration name.
  474. std::string intermediateDir = this->GetTargetDirectory(target);
  475. intermediateDir += "/";
  476. intermediateDir += configName;
  477. fout << "\t\t\tIntermediateDirectory=\""
  478. << this->ConvertToXMLOutputPath(intermediateDir.c_str())
  479. << "\"\n"
  480. << "\t\t\tConfigurationType=\"" << configType << "\"\n"
  481. << "\t\t\tUseOfMFC=\"" << mfcFlag << "\"\n"
  482. << "\t\t\tATLMinimizesCRunTimeLibraryUsage=\"FALSE\"\n";
  483. // If unicode is enabled change the character set to unicode, if not
  484. // then default to MBCS.
  485. if(targetOptions.UsingUnicode())
  486. {
  487. fout << "\t\t\tCharacterSet=\"1\">\n";
  488. }
  489. else
  490. {
  491. fout << "\t\t\tCharacterSet=\"2\">\n";
  492. }
  493. fout << "\t\t\t<Tool\n"
  494. << "\t\t\t\tName=\"VCCLCompilerTool\"\n";
  495. targetOptions.OutputAdditionalOptions(fout, "\t\t\t\t", "\n");
  496. fout << "\t\t\t\tAdditionalIncludeDirectories=\"";
  497. std::vector<std::string> includes;
  498. this->GetIncludeDirectories(includes);
  499. std::vector<std::string>::iterator i = includes.begin();
  500. for(;i != includes.end(); ++i)
  501. {
  502. std::string ipath = this->ConvertToXMLOutputPath(i->c_str());
  503. fout << ipath << ";";
  504. }
  505. fout << "\"\n";
  506. targetOptions.OutputFlagMap(fout, "\t\t\t\t");
  507. targetOptions.OutputPreprocessorDefinitions(fout, "\t\t\t\t", "\n");
  508. fout << "\t\t\t\tAssemblerListingLocation=\"" << configName << "\"\n";
  509. fout << "\t\t\t\tObjectFile=\"$(IntDir)\\\"\n";
  510. if(target.GetType() == cmTarget::EXECUTABLE ||
  511. target.GetType() == cmTarget::STATIC_LIBRARY ||
  512. target.GetType() == cmTarget::SHARED_LIBRARY ||
  513. target.GetType() == cmTarget::MODULE_LIBRARY)
  514. {
  515. // We need to specify a program database file name even for
  516. // non-debug configurations because VS still creates .idb files.
  517. fout << "\t\t\t\tProgramDataBaseFileName=\""
  518. << target.GetDirectory(configName) << "/"
  519. << target.GetPDBName(configName) << "\"\n";
  520. }
  521. fout << "/>\n"; // end of <Tool Name=VCCLCompilerTool
  522. fout << "\t\t\t<Tool\n\t\t\t\tName=\"VCCustomBuildTool\"/>\n";
  523. fout << "\t\t\t<Tool\n\t\t\t\tName=\"VCResourceCompilerTool\"\n"
  524. << "\t\t\t\tAdditionalIncludeDirectories=\"";
  525. for(i = includes.begin();i != includes.end(); ++i)
  526. {
  527. std::string ipath = this->ConvertToXMLOutputPath(i->c_str());
  528. fout << ipath << ";";
  529. }
  530. // add the -D flags to the RC tool
  531. fout << "\"";
  532. targetOptions.OutputPreprocessorDefinitions(fout, "\n\t\t\t\t", "");
  533. fout << "/>\n";
  534. fout << "\t\t\t<Tool\n\t\t\t\tName=\"VCMIDLTool\"\n";
  535. targetOptions.OutputPreprocessorDefinitions(fout, "\t\t\t\t", "\n");
  536. fout << "\t\t\t\tMkTypLibCompatible=\"FALSE\"\n";
  537. if( this->PlatformName == "x64" )
  538. {
  539. fout << "\t\t\t\tTargetEnvironment=\"3\"\n";
  540. }
  541. else if( this->PlatformName == "ia64" )
  542. {
  543. fout << "\t\t\t\tTargetEnvironment=\"2\"\n";
  544. }
  545. else
  546. {
  547. fout << "\t\t\t\tTargetEnvironment=\"1\"\n";
  548. }
  549. fout << "\t\t\t\tGenerateStublessProxies=\"TRUE\"\n";
  550. fout << "\t\t\t\tTypeLibraryName=\"$(InputName).tlb\"\n";
  551. fout << "\t\t\t\tOutputDirectory=\"$(IntDir)\"\n";
  552. fout << "\t\t\t\tHeaderFileName=\"$(InputName).h\"\n";
  553. fout << "\t\t\t\tDLLDataFileName=\"\"\n";
  554. fout << "\t\t\t\tInterfaceIdentifierFileName=\"$(InputName)_i.c\"\n";
  555. fout << "\t\t\t\tProxyFileName=\"$(InputName)_p.c\"/>\n";
  556. // end of <Tool Name=VCMIDLTool
  557. // If we are building a version 8 project file, add a flag telling the
  558. // manifest tool to use a workaround for FAT32 file systems, which can cause
  559. // an empty manifest to be embedded into the resulting executable.
  560. // See CMake bug #2617.
  561. if ( this->Version == 8 )
  562. {
  563. fout << "\t\t\t<Tool\n\t\t\t\tName=\"VCManifestTool\"\n"
  564. << "\t\t\t\tUseFAT32Workaround=\"true\"\n"
  565. << "\t\t\t/>\n";
  566. }
  567. this->OutputTargetRules(fout, configName, target, libName);
  568. this->OutputBuildTool(fout, configName, target);
  569. fout << "\t\t</Configuration>\n";
  570. }
  571. //----------------------------------------------------------------------------
  572. std::string
  573. cmLocalVisualStudio7Generator
  574. ::GetBuildTypeLinkerFlags(std::string rootLinkerFlags, const char* configName)
  575. {
  576. std::string configTypeUpper = cmSystemTools::UpperCase(configName);
  577. std::string extraLinkOptionsBuildTypeDef =
  578. rootLinkerFlags + "_" + configTypeUpper;
  579. std::string extraLinkOptionsBuildType =
  580. this->Makefile->GetRequiredDefinition
  581. (extraLinkOptionsBuildTypeDef.c_str());
  582. return extraLinkOptionsBuildType;
  583. }
  584. void cmLocalVisualStudio7Generator::OutputBuildTool(std::ostream& fout,
  585. const char* configName,
  586. cmTarget &target)
  587. {
  588. std::string temp;
  589. std::string extraLinkOptions;
  590. if(target.GetType() == cmTarget::EXECUTABLE)
  591. {
  592. extraLinkOptions =
  593. this->Makefile->GetRequiredDefinition("CMAKE_EXE_LINKER_FLAGS")
  594. + std::string(" ")
  595. + GetBuildTypeLinkerFlags("CMAKE_EXE_LINKER_FLAGS", configName);
  596. }
  597. if(target.GetType() == cmTarget::SHARED_LIBRARY)
  598. {
  599. extraLinkOptions =
  600. this->Makefile->GetRequiredDefinition("CMAKE_SHARED_LINKER_FLAGS")
  601. + std::string(" ")
  602. + GetBuildTypeLinkerFlags("CMAKE_SHARED_LINKER_FLAGS", configName);
  603. }
  604. if(target.GetType() == cmTarget::MODULE_LIBRARY)
  605. {
  606. extraLinkOptions =
  607. this->Makefile->GetRequiredDefinition("CMAKE_MODULE_LINKER_FLAGS")
  608. + std::string(" ")
  609. + GetBuildTypeLinkerFlags("CMAKE_MODULE_LINKER_FLAGS", configName);
  610. }
  611. const char* targetLinkFlags = target.GetProperty("LINK_FLAGS");
  612. if(targetLinkFlags)
  613. {
  614. extraLinkOptions += " ";
  615. extraLinkOptions += targetLinkFlags;
  616. }
  617. std::string configTypeUpper = cmSystemTools::UpperCase(configName);
  618. std::string linkFlagsConfig = "LINK_FLAGS_";
  619. linkFlagsConfig += configTypeUpper;
  620. targetLinkFlags = target.GetProperty(linkFlagsConfig.c_str());
  621. if(targetLinkFlags)
  622. {
  623. extraLinkOptions += " ";
  624. extraLinkOptions += targetLinkFlags;
  625. }
  626. Options linkOptions(Options::Linker);
  627. linkOptions.Parse(extraLinkOptions.c_str());
  628. switch(target.GetType())
  629. {
  630. case cmTarget::STATIC_LIBRARY:
  631. {
  632. std::string targetNameFull = target.GetFullName(configName);
  633. std::string libpath = target.GetDirectory(configName);
  634. libpath += "/";
  635. libpath += targetNameFull;
  636. fout << "\t\t\t<Tool\n"
  637. << "\t\t\t\tName=\"VCLibrarianTool\"\n";
  638. if(const char* libflags = target.GetProperty("STATIC_LIBRARY_FLAGS"))
  639. {
  640. fout << "\t\t\t\tAdditionalOptions=\"" << libflags << "\"\n";
  641. }
  642. fout << "\t\t\t\tOutputFile=\""
  643. << this->ConvertToXMLOutputPathSingle(libpath.c_str()) << "\"/>\n";
  644. break;
  645. }
  646. case cmTarget::SHARED_LIBRARY:
  647. case cmTarget::MODULE_LIBRARY:
  648. {
  649. std::string targetName;
  650. std::string targetNameSO;
  651. std::string targetNameFull;
  652. std::string targetNameImport;
  653. std::string targetNamePDB;
  654. target.GetLibraryNames(targetName, targetNameSO, targetNameFull,
  655. targetNameImport, targetNamePDB, configName);
  656. // Compute the link library and directory information.
  657. std::vector<cmStdString> linkLibs;
  658. std::vector<cmStdString> linkDirs;
  659. this->ComputeLinkInformation(target, configName, linkLibs, linkDirs);
  660. // Get the language to use for linking.
  661. const char* linkLanguage =
  662. target.GetLinkerLanguage(this->GetGlobalGenerator());
  663. if(!linkLanguage)
  664. {
  665. cmSystemTools::Error
  666. ("CMake can not determine linker language for target:",
  667. target.GetName());
  668. return;
  669. }
  670. // Compute the variable name to lookup standard libraries for this
  671. // language.
  672. std::string standardLibsVar = "CMAKE_";
  673. standardLibsVar += linkLanguage;
  674. standardLibsVar += "_STANDARD_LIBRARIES";
  675. fout << "\t\t\t<Tool\n"
  676. << "\t\t\t\tName=\"VCLinkerTool\"\n";
  677. linkOptions.OutputAdditionalOptions(fout, "\t\t\t\t", "\n");
  678. // Use the NOINHERIT macro to avoid getting VS project default
  679. // libraries which may be set by the user to something bad.
  680. fout << "\t\t\t\tAdditionalDependencies=\"$(NOINHERIT) "
  681. << this->Makefile->GetSafeDefinition(standardLibsVar.c_str())
  682. << " ";
  683. this->OutputLibraries(fout, linkLibs);
  684. fout << "\"\n";
  685. temp = target.GetDirectory(configName);
  686. temp += "/";
  687. temp += targetNameFull;
  688. fout << "\t\t\t\tOutputFile=\""
  689. << this->ConvertToXMLOutputPathSingle(temp.c_str()) << "\"\n";
  690. this->WriteTargetVersionAttribute(fout, target);
  691. linkOptions.OutputFlagMap(fout, "\t\t\t\t");
  692. fout << "\t\t\t\tAdditionalLibraryDirectories=\"";
  693. this->OutputLibraryDirectories(fout, linkDirs);
  694. fout << "\"\n";
  695. this->OutputModuleDefinitionFile(fout, target);
  696. temp = target.GetDirectory(configName);
  697. temp += "/";
  698. temp += targetNamePDB;
  699. fout << "\t\t\t\tProgramDataBaseFile=\"" <<
  700. this->ConvertToXMLOutputPathSingle(temp.c_str()) << "\"\n";
  701. if(strcmp(configName, "Debug") == 0
  702. || strcmp(configName, "RelWithDebInfo") == 0)
  703. {
  704. fout << "\t\t\t\tGenerateDebugInformation=\"TRUE\"\n";
  705. }
  706. std::string stackVar = "CMAKE_";
  707. stackVar += linkLanguage;
  708. stackVar += "_STACK_SIZE";
  709. const char* stackVal = this->Makefile->GetDefinition(stackVar.c_str());
  710. if(stackVal)
  711. {
  712. fout << "\t\t\t\tStackReserveSize=\"" << stackVal << "\"\n";
  713. }
  714. temp = target.GetDirectory(configName, true);
  715. temp += "/";
  716. temp += targetNameImport;
  717. fout << "\t\t\t\tImportLibrary=\""
  718. << this->ConvertToXMLOutputPathSingle(temp.c_str()) << "\"/>\n";
  719. }
  720. break;
  721. case cmTarget::EXECUTABLE:
  722. {
  723. std::string targetName;
  724. std::string targetNameFull;
  725. std::string targetNameImport;
  726. std::string targetNamePDB;
  727. target.GetExecutableNames(targetName, targetNameFull,
  728. targetNameImport, targetNamePDB, configName);
  729. // Compute the link library and directory information.
  730. std::vector<cmStdString> linkLibs;
  731. std::vector<cmStdString> linkDirs;
  732. this->ComputeLinkInformation(target, configName, linkLibs, linkDirs);
  733. // Get the language to use for linking.
  734. const char* linkLanguage =
  735. target.GetLinkerLanguage(this->GetGlobalGenerator());
  736. if(!linkLanguage)
  737. {
  738. cmSystemTools::Error
  739. ("CMake can not determine linker language for target:",
  740. target.GetName());
  741. return;
  742. }
  743. // Compute the variable name to lookup standard libraries for this
  744. // language.
  745. std::string standardLibsVar = "CMAKE_";
  746. standardLibsVar += linkLanguage;
  747. standardLibsVar += "_STANDARD_LIBRARIES";
  748. fout << "\t\t\t<Tool\n"
  749. << "\t\t\t\tName=\"VCLinkerTool\"\n";
  750. linkOptions.OutputAdditionalOptions(fout, "\t\t\t\t", "\n");
  751. // Use the NOINHERIT macro to avoid getting VS project default
  752. // libraries which may be set by the user to something bad.
  753. fout << "\t\t\t\tAdditionalDependencies=\"$(NOINHERIT) "
  754. << this->Makefile->GetSafeDefinition(standardLibsVar.c_str())
  755. << " ";
  756. this->OutputLibraries(fout, linkLibs);
  757. fout << "\"\n";
  758. temp = target.GetDirectory(configName);
  759. temp += "/";
  760. temp += targetNameFull;
  761. fout << "\t\t\t\tOutputFile=\""
  762. << this->ConvertToXMLOutputPathSingle(temp.c_str()) << "\"\n";
  763. this->WriteTargetVersionAttribute(fout, target);
  764. linkOptions.OutputFlagMap(fout, "\t\t\t\t");
  765. fout << "\t\t\t\tAdditionalLibraryDirectories=\"";
  766. this->OutputLibraryDirectories(fout, linkDirs);
  767. fout << "\"\n";
  768. fout << "\t\t\t\tProgramDataBaseFile=\""
  769. << target.GetDirectory(configName) << "/" << targetNamePDB
  770. << "\"\n";
  771. if(strcmp(configName, "Debug") == 0
  772. || strcmp(configName, "RelWithDebInfo") == 0)
  773. {
  774. fout << "\t\t\t\tGenerateDebugInformation=\"TRUE\"\n";
  775. }
  776. if ( target.GetPropertyAsBool("WIN32_EXECUTABLE") )
  777. {
  778. fout << "\t\t\t\tSubSystem=\"2\"\n";
  779. }
  780. else
  781. {
  782. fout << "\t\t\t\tSubSystem=\"1\"\n";
  783. }
  784. std::string stackVar = "CMAKE_";
  785. stackVar += linkLanguage;
  786. stackVar += "_STACK_SIZE";
  787. const char* stackVal = this->Makefile->GetDefinition(stackVar.c_str());
  788. if(stackVal)
  789. {
  790. fout << "\t\t\t\tStackReserveSize=\"" << stackVal << "\"";
  791. }
  792. temp = target.GetDirectory(configName, true);
  793. temp += "/";
  794. temp += targetNameImport;
  795. fout << "\t\t\t\tImportLibrary=\""
  796. << this->ConvertToXMLOutputPathSingle(temp.c_str()) << "\"/>\n";
  797. break;
  798. }
  799. case cmTarget::UTILITY:
  800. case cmTarget::GLOBAL_TARGET:
  801. break;
  802. }
  803. }
  804. //----------------------------------------------------------------------------
  805. void
  806. cmLocalVisualStudio7Generator
  807. ::WriteTargetVersionAttribute(std::ostream& fout, cmTarget& target)
  808. {
  809. int major;
  810. int minor;
  811. target.GetTargetVersion(major, minor);
  812. fout << "\t\t\t\tVersion=\"" << major << "." << minor << "\"\n";
  813. }
  814. void cmLocalVisualStudio7Generator
  815. ::OutputModuleDefinitionFile(std::ostream& fout,
  816. cmTarget &target)
  817. {
  818. std::vector<cmSourceFile*> const& classes = target.GetSourceFiles();
  819. for(std::vector<cmSourceFile*>::const_iterator i = classes.begin();
  820. i != classes.end(); i++)
  821. {
  822. cmSourceFile* sf = *i;
  823. if(cmSystemTools::UpperCase(sf->GetExtension()) == "DEF")
  824. {
  825. fout << "\t\t\t\tModuleDefinitionFile=\""
  826. << this->ConvertToXMLOutputPath(sf->GetFullPath().c_str())
  827. << "\"\n";
  828. return;
  829. }
  830. }
  831. }
  832. //----------------------------------------------------------------------------
  833. void
  834. cmLocalVisualStudio7Generator
  835. ::OutputLibraries(std::ostream& fout,
  836. std::vector<cmStdString> const& libs)
  837. {
  838. for(std::vector<cmStdString>::const_iterator l = libs.begin();
  839. l != libs.end(); ++l)
  840. {
  841. fout << this->ConvertToXMLOutputPath(l->c_str()) << " ";
  842. }
  843. }
  844. //----------------------------------------------------------------------------
  845. void
  846. cmLocalVisualStudio7Generator
  847. ::OutputLibraryDirectories(std::ostream& fout,
  848. std::vector<cmStdString> const& dirs)
  849. {
  850. const char* comma = "";
  851. for(std::vector<cmStdString>::const_iterator d = dirs.begin();
  852. d != dirs.end(); ++d)
  853. {
  854. // Remove any trailing slash and skip empty paths.
  855. std::string dir = *d;
  856. if(dir[dir.size()-1] == '/')
  857. {
  858. dir = dir.substr(0, dir.size()-1);
  859. }
  860. if(dir.empty())
  861. {
  862. continue;
  863. }
  864. // Switch to a relative path specification if it is shorter.
  865. if(cmSystemTools::FileIsFullPath(dir.c_str()))
  866. {
  867. std::string rel = this->Convert(dir.c_str(), START_OUTPUT, UNCHANGED);
  868. if(rel.size() < dir.size())
  869. {
  870. dir = rel;
  871. }
  872. }
  873. // First search a configuration-specific subdirectory and then the
  874. // original directory.
  875. fout << comma << this->ConvertToXMLOutputPath((dir+"/$(OutDir)").c_str())
  876. << "," << this->ConvertToXMLOutputPath(dir.c_str());
  877. comma = ",";
  878. }
  879. }
  880. void cmLocalVisualStudio7Generator::WriteVCProjFile(std::ostream& fout,
  881. const char *libName,
  882. cmTarget &target)
  883. {
  884. // get the configurations
  885. std::vector<std::string> *configs =
  886. static_cast<cmGlobalVisualStudio7Generator *>
  887. (this->GlobalGenerator)->GetConfigurations();
  888. // trace the visual studio dependencies
  889. std::string name = libName;
  890. name += ".vcproj.cmake";
  891. // We may be modifying the source groups temporarily, so make a copy.
  892. std::vector<cmSourceGroup> sourceGroups = this->Makefile->GetSourceGroups();
  893. // get the classes from the source lists then add them to the groups
  894. std::vector<cmSourceFile*>const & classes = target.GetSourceFiles();
  895. for(std::vector<cmSourceFile*>::const_iterator i = classes.begin();
  896. i != classes.end(); i++)
  897. {
  898. // Add the file to the list of sources.
  899. std::string source = (*i)->GetFullPath();
  900. if(cmSystemTools::UpperCase((*i)->GetExtension()) == "DEF")
  901. {
  902. this->ModuleDefinitionFile = (*i)->GetFullPath();
  903. }
  904. cmSourceGroup& sourceGroup =
  905. this->Makefile->FindSourceGroup(source.c_str(), sourceGroups);
  906. sourceGroup.AssignSource(*i);
  907. }
  908. // Compute which sources need unique object computation.
  909. this->ComputeObjectNameRequirements(sourceGroups);
  910. // open the project
  911. this->WriteProjectStart(fout, libName, target, sourceGroups);
  912. // write the configuration information
  913. this->WriteConfigurations(fout, libName, target);
  914. fout << "\t<Files>\n";
  915. // Loop through every source group.
  916. for(unsigned int i = 0; i < sourceGroups.size(); ++i)
  917. {
  918. cmSourceGroup sg = sourceGroups[i];
  919. this->WriteGroup(&sg, target, fout, libName, configs);
  920. }
  921. //}
  922. fout << "\t</Files>\n";
  923. // Write the VCProj file's footer.
  924. this->WriteVCProjFooter(fout);
  925. }
  926. void cmLocalVisualStudio7Generator
  927. ::WriteGroup(const cmSourceGroup *sg, cmTarget target,
  928. std::ostream &fout, const char *libName,
  929. std::vector<std::string> *configs)
  930. {
  931. const std::vector<const cmSourceFile *> &sourceFiles =
  932. sg->GetSourceFiles();
  933. // If the group is empty, don't write it at all.
  934. if(sourceFiles.empty() && sg->GetGroupChildren().empty())
  935. {
  936. return;
  937. }
  938. // If the group has a name, write the header.
  939. std::string name = sg->GetName();
  940. if(name != "")
  941. {
  942. this->WriteVCProjBeginGroup(fout, name.c_str(), "");
  943. }
  944. // Compute the maximum length of a configuration name.
  945. std::string::size_type config_len_max = 0;
  946. for(std::vector<std::string>::iterator i = configs->begin();
  947. i != configs->end(); ++i)
  948. {
  949. if(i->size() > config_len_max)
  950. {
  951. config_len_max = i->size();
  952. }
  953. }
  954. // Compute the maximum length of the full path to the intermediate
  955. // files directory for any configuration. This is used to construct
  956. // object file names that do not produce paths that are too long.
  957. std::string::size_type dir_len = 0;
  958. dir_len += strlen(this->Makefile->GetCurrentOutputDirectory());
  959. dir_len += 1;
  960. dir_len += this->GetTargetDirectory(target).size();
  961. dir_len += 1;
  962. dir_len += config_len_max;
  963. dir_len += 1;
  964. // Loop through each source in the source group.
  965. std::string objectName;
  966. for(std::vector<const cmSourceFile *>::const_iterator sf =
  967. sourceFiles.begin(); sf != sourceFiles.end(); ++sf)
  968. {
  969. std::string source = (*sf)->GetFullPath();
  970. const cmCustomCommand *command = (*sf)->GetCustomCommand();
  971. std::string compileFlags;
  972. std::string additionalDeps;
  973. if(this->NeedObjectName.find(*sf) != this->NeedObjectName.end())
  974. {
  975. objectName = this->GetObjectFileNameWithoutTarget(*(*sf), dir_len);
  976. }
  977. else
  978. {
  979. objectName = "";
  980. }
  981. // Add per-source flags.
  982. const char* cflags = (*sf)->GetProperty("COMPILE_FLAGS");
  983. if(cflags)
  984. {
  985. compileFlags += " ";
  986. compileFlags += cflags;
  987. }
  988. const char* lang =
  989. this->GlobalGenerator->GetLanguageFromExtension
  990. ((*sf)->GetExtension().c_str());
  991. const char* sourceLang = this->GetSourceFileLanguage(*(*sf));
  992. const char* linkLanguage = target.GetLinkerLanguage
  993. (this->GetGlobalGenerator());
  994. bool needForceLang = false;
  995. // source file does not match its extension language
  996. if(lang && sourceLang && strcmp(lang, sourceLang) != 0)
  997. {
  998. needForceLang = true;
  999. lang = sourceLang;
  1000. }
  1001. // If lang is set, the compiler will generate code automatically.
  1002. // If HEADER_FILE_ONLY is set, we must suppress this generation in
  1003. // the project file
  1004. bool excludedFromBuild =
  1005. (lang && (*sf)->GetPropertyAsBool("HEADER_FILE_ONLY"));
  1006. // if the source file does not match the linker language
  1007. // then force c or c++
  1008. if(needForceLang || (linkLanguage && lang
  1009. && strcmp(lang, linkLanguage) != 0))
  1010. {
  1011. if(strcmp(lang, "CXX") == 0)
  1012. {
  1013. // force a C++ file type
  1014. compileFlags += " /TP ";
  1015. }
  1016. else if(strcmp(lang, "C") == 0)
  1017. {
  1018. // force to c
  1019. compileFlags += " /TC ";
  1020. }
  1021. }
  1022. // Check for extra object-file dependencies.
  1023. const char* deps = (*sf)->GetProperty("OBJECT_DEPENDS");
  1024. if(deps)
  1025. {
  1026. std::vector<std::string> depends;
  1027. cmSystemTools::ExpandListArgument(deps, depends);
  1028. if(!depends.empty())
  1029. {
  1030. std::vector<std::string>::iterator i = depends.begin();
  1031. additionalDeps = this->ConvertToXMLOutputPath(i->c_str());
  1032. for(++i;i != depends.end(); ++i)
  1033. {
  1034. additionalDeps += ";";
  1035. additionalDeps += this->ConvertToXMLOutputPath(i->c_str());
  1036. }
  1037. }
  1038. }
  1039. if (source != libName || target.GetType() == cmTarget::UTILITY ||
  1040. target.GetType() == cmTarget::GLOBAL_TARGET )
  1041. {
  1042. fout << "\t\t\t<File\n";
  1043. std::string d = this->ConvertToXMLOutputPathSingle(source.c_str());
  1044. // Tell MS-Dev what the source is. If the compiler knows how to
  1045. // build it, then it will.
  1046. fout << "\t\t\t\tRelativePath=\"" << d << "\">\n";
  1047. if (command)
  1048. {
  1049. const char* flags = compileFlags.size() ? compileFlags.c_str(): 0;
  1050. this->WriteCustomRule(fout, source.c_str(), *command, flags);
  1051. }
  1052. else if(compileFlags.size() || additionalDeps.length()
  1053. || objectName.size() || excludedFromBuild)
  1054. {
  1055. const char* aCompilerTool = "VCCLCompilerTool";
  1056. std::string ext = (*sf)->GetExtension();
  1057. ext = cmSystemTools::LowerCase(ext);
  1058. if(ext == "idl")
  1059. {
  1060. aCompilerTool = "VCMIDLTool";
  1061. }
  1062. if(ext == "rc")
  1063. {
  1064. aCompilerTool = "VCResourceCompilerTool";
  1065. }
  1066. if(ext == "def")
  1067. {
  1068. aCompilerTool = "VCCustomBuildTool";
  1069. }
  1070. for(std::vector<std::string>::iterator i = configs->begin();
  1071. i != configs->end(); ++i)
  1072. {
  1073. fout << "\t\t\t\t<FileConfiguration\n"
  1074. << "\t\t\t\t\tName=\"" << *i
  1075. << "|" << this->PlatformName << "\"";
  1076. if(excludedFromBuild)
  1077. {
  1078. fout << " ExcludedFromBuild=\"true\"";
  1079. }
  1080. fout << ">\n";
  1081. fout << "\t\t\t\t\t<Tool\n"
  1082. << "\t\t\t\t\tName=\"" << aCompilerTool << "\"\n";
  1083. if(!compileFlags.empty())
  1084. {
  1085. Options fileOptions(Options::Compiler, this->ExtraFlagTable);
  1086. fileOptions.Parse(compileFlags.c_str());
  1087. fileOptions.OutputAdditionalOptions(fout, "\t\t\t\t\t", "\n");
  1088. fileOptions.OutputFlagMap(fout, "\t\t\t\t\t");
  1089. fileOptions.OutputPreprocessorDefinitions(fout,
  1090. "\t\t\t\t\t", "\n");
  1091. }
  1092. if(additionalDeps.length())
  1093. {
  1094. fout << "\t\t\t\t\tAdditionalDependencies=\""
  1095. << additionalDeps.c_str() << "\"\n";
  1096. }
  1097. if(objectName.size())
  1098. {
  1099. fout << "\t\t\t\t\tObjectFile=\"$(IntDir)/"
  1100. << objectName.c_str() << "\"\n";
  1101. }
  1102. fout << "\t\t\t\t\t/>\n"
  1103. << "\t\t\t\t</FileConfiguration>\n";
  1104. }
  1105. }
  1106. fout << "\t\t\t</File>\n";
  1107. }
  1108. }
  1109. std::vector<cmSourceGroup> children = sg->GetGroupChildren();
  1110. for(unsigned int i=0;i<children.size();++i)
  1111. {
  1112. this->WriteGroup(&children[i], target, fout, libName, configs);
  1113. }
  1114. // If the group has a name, write the footer.
  1115. if(name != "")
  1116. {
  1117. this->WriteVCProjEndGroup(fout);
  1118. }
  1119. }
  1120. void cmLocalVisualStudio7Generator::
  1121. WriteCustomRule(std::ostream& fout,
  1122. const char* source,
  1123. const cmCustomCommand& command,
  1124. const char* compileFlags)
  1125. {
  1126. std::string comment = this->ConstructComment(command);
  1127. // Write the rule for each configuration.
  1128. std::vector<std::string>::iterator i;
  1129. std::vector<std::string> *configs =
  1130. static_cast<cmGlobalVisualStudio7Generator *>
  1131. (this->GlobalGenerator)->GetConfigurations();
  1132. for(i = configs->begin(); i != configs->end(); ++i)
  1133. {
  1134. fout << "\t\t\t\t<FileConfiguration\n";
  1135. fout << "\t\t\t\t\tName=\"" << *i << "|" << this->PlatformName << "\">\n";
  1136. if(compileFlags)
  1137. {
  1138. fout << "\t\t\t\t\t<Tool\n"
  1139. << "\t\t\t\t\tName=\"VCCLCompilerTool\"\n"
  1140. << "\t\t\t\t\tAdditionalOptions=\""
  1141. << this->EscapeForXML(compileFlags) << "\"/>\n";
  1142. }
  1143. std::string script =
  1144. this->ConstructScript(command.GetCommandLines(),
  1145. command.GetWorkingDirectory(),
  1146. i->c_str(),
  1147. command.GetEscapeOldStyle(),
  1148. command.GetEscapeAllowMakeVars());
  1149. fout << "\t\t\t\t\t<Tool\n"
  1150. << "\t\t\t\t\tName=\"VCCustomBuildTool\"\n"
  1151. << "\t\t\t\t\tDescription=\""
  1152. << this->EscapeForXML(comment.c_str()) << "\"\n"
  1153. << "\t\t\t\t\tCommandLine=\""
  1154. << this->EscapeForXML(script.c_str()) << "\"\n"
  1155. << "\t\t\t\t\tAdditionalDependencies=\"";
  1156. if(command.GetDepends().empty())
  1157. {
  1158. // There are no real dependencies. Produce an artificial one to
  1159. // make sure the rule runs reliably.
  1160. if(!cmSystemTools::FileExists(source))
  1161. {
  1162. std::ofstream depout(source);
  1163. depout << "Artificial dependency for a custom command.\n";
  1164. }
  1165. fout << this->ConvertToXMLOutputPath(source);
  1166. }
  1167. else
  1168. {
  1169. // Write out the dependencies for the rule.
  1170. for(std::vector<std::string>::const_iterator d =
  1171. command.GetDepends().begin();
  1172. d != command.GetDepends().end();
  1173. ++d)
  1174. {
  1175. // Get the real name of the dependency in case it is a CMake target.
  1176. std::string dep = this->GetRealDependency(d->c_str(), i->c_str());
  1177. fout << this->ConvertToXMLOutputPath(dep.c_str())
  1178. << ";";
  1179. }
  1180. }
  1181. fout << "\"\n";
  1182. fout << "\t\t\t\t\tOutputs=\"";
  1183. if(command.GetOutputs().empty())
  1184. {
  1185. fout << source << "_force";
  1186. }
  1187. else
  1188. {
  1189. // Write a rule for the output generated by this command.
  1190. const char* sep = "";
  1191. for(std::vector<std::string>::const_iterator o =
  1192. command.GetOutputs().begin();
  1193. o != command.GetOutputs().end();
  1194. ++o)
  1195. {
  1196. fout << sep << this->ConvertToXMLOutputPathSingle(o->c_str());
  1197. sep = ";";
  1198. }
  1199. }
  1200. fout << "\"/>\n";
  1201. fout << "\t\t\t\t</FileConfiguration>\n";
  1202. }
  1203. }
  1204. void cmLocalVisualStudio7Generator::WriteVCProjBeginGroup(std::ostream& fout,
  1205. const char* group,
  1206. const char* )
  1207. {
  1208. fout << "\t\t<Filter\n"
  1209. << "\t\t\tName=\"" << group << "\"\n"
  1210. << "\t\t\tFilter=\"\">\n";
  1211. }
  1212. void cmLocalVisualStudio7Generator::WriteVCProjEndGroup(std::ostream& fout)
  1213. {
  1214. fout << "\t\t</Filter>\n";
  1215. }
  1216. // look for custom rules on a target and collect them together
  1217. void cmLocalVisualStudio7Generator
  1218. ::OutputTargetRules(std::ostream& fout,
  1219. const char* configName,
  1220. cmTarget &target,
  1221. const char * /*libName*/)
  1222. {
  1223. if (target.GetType() > cmTarget::GLOBAL_TARGET)
  1224. {
  1225. return;
  1226. }
  1227. // add the pre build rules
  1228. fout << "\t\t\t<Tool\n\t\t\t\tName=\"VCPreBuildEventTool\"";
  1229. bool init = false;
  1230. for (std::vector<cmCustomCommand>::const_iterator cr =
  1231. target.GetPreBuildCommands().begin();
  1232. cr != target.GetPreBuildCommands().end(); ++cr)
  1233. {
  1234. if(!init)
  1235. {
  1236. fout << "\nCommandLine=\"";
  1237. init = true;
  1238. }
  1239. else
  1240. {
  1241. fout << this->EscapeForXML("\n");
  1242. }
  1243. std::string script =
  1244. this->ConstructScript(cr->GetCommandLines(),
  1245. cr->GetWorkingDirectory(),
  1246. configName,
  1247. cr->GetEscapeOldStyle(),
  1248. cr->GetEscapeAllowMakeVars());
  1249. fout << this->EscapeForXML(script.c_str()).c_str();
  1250. }
  1251. if (init)
  1252. {
  1253. fout << "\"";
  1254. }
  1255. fout << "/>\n";
  1256. // add the pre Link rules
  1257. fout << "\t\t\t<Tool\n\t\t\t\tName=\"VCPreLinkEventTool\"";
  1258. init = false;
  1259. for (std::vector<cmCustomCommand>::const_iterator cr =
  1260. target.GetPreLinkCommands().begin();
  1261. cr != target.GetPreLinkCommands().end(); ++cr)
  1262. {
  1263. if(!init)
  1264. {
  1265. fout << "\nCommandLine=\"";
  1266. init = true;
  1267. }
  1268. else
  1269. {
  1270. fout << this->EscapeForXML("\n");
  1271. }
  1272. std::string script =
  1273. this->ConstructScript(cr->GetCommandLines(),
  1274. cr->GetWorkingDirectory(),
  1275. configName,
  1276. cr->GetEscapeOldStyle(),
  1277. cr->GetEscapeAllowMakeVars());
  1278. fout << this->EscapeForXML(script.c_str()).c_str();
  1279. }
  1280. if (init)
  1281. {
  1282. fout << "\"";
  1283. }
  1284. fout << "/>\n";
  1285. // add the PostBuild rules
  1286. fout << "\t\t\t<Tool\n\t\t\t\tName=\"VCPostBuildEventTool\"";
  1287. init = false;
  1288. for (std::vector<cmCustomCommand>::const_iterator cr =
  1289. target.GetPostBuildCommands().begin();
  1290. cr != target.GetPostBuildCommands().end(); ++cr)
  1291. {
  1292. if(!init)
  1293. {
  1294. fout << "\nCommandLine=\"";
  1295. init = true;
  1296. }
  1297. else
  1298. {
  1299. fout << this->EscapeForXML("\n");
  1300. }
  1301. std::string script =
  1302. this->ConstructScript(cr->GetCommandLines(),
  1303. cr->GetWorkingDirectory(),
  1304. configName,
  1305. cr->GetEscapeOldStyle(),
  1306. cr->GetEscapeAllowMakeVars());
  1307. fout << this->EscapeForXML(script.c_str()).c_str();
  1308. }
  1309. if (init)
  1310. {
  1311. fout << "\"";
  1312. }
  1313. fout << "/>\n";
  1314. }
  1315. void
  1316. cmLocalVisualStudio7Generator::WriteProjectStart(std::ostream& fout,
  1317. const char *libName,
  1318. cmTarget & target,
  1319. std::vector<cmSourceGroup> &)
  1320. {
  1321. fout << "<?xml version=\"1.0\" encoding = \"Windows-1252\"?>\n"
  1322. << "<VisualStudioProject\n"
  1323. << "\tProjectType=\"Visual C++\"\n";
  1324. if(this->Version == 71)
  1325. {
  1326. fout << "\tVersion=\"7.10\"\n";
  1327. }
  1328. else
  1329. {
  1330. if (this->Version == 8)
  1331. {
  1332. fout << "\tVersion=\"8.00\"\n";
  1333. }
  1334. else
  1335. {
  1336. fout << "\tVersion=\"7.00\"\n";
  1337. }
  1338. }
  1339. const char* projLabel = target.GetProperty("PROJECT_LABEL");
  1340. if(!projLabel)
  1341. {
  1342. projLabel = libName;
  1343. }
  1344. const char* keyword = target.GetProperty("VS_KEYWORD");
  1345. if(!keyword)
  1346. {
  1347. keyword = "Win32Proj";
  1348. }
  1349. cmGlobalVisualStudio7Generator* gg =
  1350. static_cast<cmGlobalVisualStudio7Generator *>(this->GlobalGenerator);
  1351. fout << "\tName=\"" << projLabel << "\"\n";
  1352. if(this->Version == 8)
  1353. {
  1354. fout << "\tProjectGUID=\"{" << gg->GetGUID(libName) << "}\"\n";
  1355. }
  1356. fout << "\tSccProjectName=\"\"\n"
  1357. << "\tSccLocalPath=\"\"\n"
  1358. << "\tKeyword=\"" << keyword << "\">\n"
  1359. << "\t<Platforms>\n"
  1360. << "\t\t<Platform\n\t\t\tName=\"" << this->PlatformName << "\"/>\n"
  1361. << "\t</Platforms>\n";
  1362. }
  1363. void cmLocalVisualStudio7Generator::WriteVCProjFooter(std::ostream& fout)
  1364. {
  1365. fout << "\t<Globals>\n"
  1366. << "\t</Globals>\n"
  1367. << "</VisualStudioProject>\n";
  1368. }
  1369. std::string cmLocalVisualStudio7GeneratorEscapeForXML(const char* s)
  1370. {
  1371. std::string ret = s;
  1372. cmSystemTools::ReplaceString(ret, "&", "&amp;");
  1373. cmSystemTools::ReplaceString(ret, "\"", "&quot;");
  1374. cmSystemTools::ReplaceString(ret, "<", "&lt;");
  1375. cmSystemTools::ReplaceString(ret, ">", "&gt;");
  1376. cmSystemTools::ReplaceString(ret, "\n", "&#x0D;&#x0A;");
  1377. return ret;
  1378. }
  1379. std::string cmLocalVisualStudio7Generator::EscapeForXML(const char* s)
  1380. {
  1381. return cmLocalVisualStudio7GeneratorEscapeForXML(s);
  1382. }
  1383. std::string cmLocalVisualStudio7Generator
  1384. ::ConvertToXMLOutputPath(const char* path)
  1385. {
  1386. std::string ret = this->ConvertToOptionallyRelativeOutputPath(path);
  1387. cmSystemTools::ReplaceString(ret, "&", "&amp;");
  1388. cmSystemTools::ReplaceString(ret, "\"", "&quot;");
  1389. cmSystemTools::ReplaceString(ret, "<", "&lt;");
  1390. cmSystemTools::ReplaceString(ret, ">", "&gt;");
  1391. return ret;
  1392. }
  1393. std::string cmLocalVisualStudio7Generator
  1394. ::ConvertToXMLOutputPathSingle(const char* path)
  1395. {
  1396. std::string ret = this->ConvertToOptionallyRelativeOutputPath(path);
  1397. cmSystemTools::ReplaceString(ret, "\"", "");
  1398. cmSystemTools::ReplaceString(ret, "&", "&amp;");
  1399. cmSystemTools::ReplaceString(ret, "<", "&lt;");
  1400. cmSystemTools::ReplaceString(ret, ">", "&gt;");
  1401. return ret;
  1402. }
  1403. // This class is used to parse an existing vs 7 project
  1404. // and extract the GUID
  1405. class cmVS7XMLParser : public cmXMLParser
  1406. {
  1407. public:
  1408. virtual void EndElement(const char* /* name */)
  1409. {
  1410. }
  1411. virtual void StartElement(const char* name, const char** atts)
  1412. {
  1413. // once the GUID is found do nothing
  1414. if(this->GUID.size())
  1415. {
  1416. return;
  1417. }
  1418. int i =0;
  1419. if(strcmp("VisualStudioProject", name) == 0)
  1420. {
  1421. while(atts[i])
  1422. {
  1423. if(strcmp(atts[i], "ProjectGUID") == 0)
  1424. {
  1425. if(atts[i+1])
  1426. {
  1427. this->GUID = atts[i+1];
  1428. this->GUID = this->GUID.substr(1, this->GUID.size()-2);
  1429. }
  1430. else
  1431. {
  1432. this->GUID = "";
  1433. }
  1434. return;
  1435. }
  1436. ++i;
  1437. }
  1438. }
  1439. }
  1440. int InitializeParser()
  1441. {
  1442. int ret = cmXMLParser::InitializeParser();
  1443. if(ret == 0)
  1444. {
  1445. return ret;
  1446. }
  1447. // visual studio projects have a strange encoding, but it is
  1448. // really utf-8
  1449. XML_SetEncoding(static_cast<XML_Parser>(this->Parser), "utf-8");
  1450. return 1;
  1451. }
  1452. std::string GUID;
  1453. };
  1454. void cmLocalVisualStudio7Generator::ReadAndStoreExternalGUID(
  1455. const char* name,
  1456. const char* path)
  1457. {
  1458. cmVS7XMLParser parser;
  1459. parser.ParseFile(path);
  1460. // if we can not find a GUID then create one
  1461. if(parser.GUID.size() == 0)
  1462. {
  1463. cmGlobalVisualStudio7Generator* gg =
  1464. static_cast<cmGlobalVisualStudio7Generator *>(this->GlobalGenerator);
  1465. gg->CreateGUID(name);
  1466. return;
  1467. }
  1468. std::string guidStoreName = name;
  1469. guidStoreName += "_GUID_CMAKE";
  1470. // save the GUID in the cache
  1471. this->GlobalGenerator->GetCMakeInstance()->
  1472. AddCacheEntry(guidStoreName.c_str(),
  1473. parser.GUID.c_str(),
  1474. "Stored GUID",
  1475. cmCacheManager::INTERNAL);
  1476. }
  1477. void cmLocalVisualStudio7Generator::ConfigureFinalPass()
  1478. {
  1479. cmLocalGenerator::ConfigureFinalPass();
  1480. cmTargets &tgts = this->Makefile->GetTargets();
  1481. cmGlobalVisualStudio7Generator* gg =
  1482. static_cast<cmGlobalVisualStudio7Generator *>(this->GlobalGenerator);
  1483. for(cmTargets::iterator l = tgts.begin(); l != tgts.end(); l++)
  1484. {
  1485. if (strncmp(l->first.c_str(), "INCLUDE_EXTERNAL_MSPROJECT", 26) == 0)
  1486. {
  1487. cmCustomCommand cc = l->second.GetPostBuildCommands()[0];
  1488. const cmCustomCommandLines& cmds = cc.GetCommandLines();
  1489. std::string project_name = cmds[0][0];
  1490. this->ReadAndStoreExternalGUID(project_name.c_str(),
  1491. cmds[0][1].c_str());
  1492. }
  1493. else
  1494. {
  1495. gg->CreateGUID(l->first.c_str());
  1496. }
  1497. }
  1498. }
  1499. //----------------------------------------------------------------------------
  1500. std::string cmLocalVisualStudio7Generator
  1501. ::GetTargetDirectory(cmTarget const& target) const
  1502. {
  1503. std::string dir;
  1504. dir += target.GetName();
  1505. dir += ".dir";
  1506. return dir;
  1507. }
  1508. //----------------------------------------------------------------------------
  1509. cmLocalVisualStudio7GeneratorOptions
  1510. ::cmLocalVisualStudio7GeneratorOptions(Tool tool,
  1511. cmVS7FlagTable const* extraTable):
  1512. CurrentTool(tool),
  1513. DoingDefine(false), FlagTable(0), ExtraFlagTable(extraTable)
  1514. {
  1515. // Choose the flag table for the requested tool.
  1516. switch(tool)
  1517. {
  1518. case Compiler:
  1519. this->FlagTable = cmLocalVisualStudio7GeneratorFlagTable; break;
  1520. case Linker:
  1521. this->FlagTable = cmLocalVisualStudio7GeneratorLinkFlagTable; break;
  1522. default: break;
  1523. }
  1524. }
  1525. //----------------------------------------------------------------------------
  1526. void cmLocalVisualStudio7GeneratorOptions::FixExceptionHandlingDefault()
  1527. {
  1528. // Exception handling is on by default because the platform file has
  1529. // "/EHsc" in the flags. Normally, that will override this
  1530. // initialization to off, but the user has the option of removing
  1531. // the flag to disable exception handling. When the user does
  1532. // remove the flag we need to override the IDE default of on.
  1533. this->FlagMap["ExceptionHandling"] = "FALSE";
  1534. }
  1535. //----------------------------------------------------------------------------
  1536. void cmLocalVisualStudio7GeneratorOptions::SetVerboseMakefile(bool verbose)
  1537. {
  1538. // If verbose makefiles have been requested and the /nologo option
  1539. // was not given explicitly in the flags we want to add an attribute
  1540. // to the generated project to disable logo suppression. Otherwise
  1541. // the GUI default is to enable suppression.
  1542. if(verbose &&
  1543. this->FlagMap.find("SuppressStartupBanner") == this->FlagMap.end())
  1544. {
  1545. this->FlagMap["SuppressStartupBanner"] = "FALSE";
  1546. }
  1547. }
  1548. //----------------------------------------------------------------------------
  1549. void cmLocalVisualStudio7GeneratorOptions::AddDefine(const std::string& def)
  1550. {
  1551. this->Defines.push_back(def);
  1552. }
  1553. //----------------------------------------------------------------------------
  1554. void cmLocalVisualStudio7GeneratorOptions::AddFlag(const char* flag,
  1555. const char* value)
  1556. {
  1557. this->FlagMap[flag] = value;
  1558. }
  1559. //----------------------------------------------------------------------------
  1560. bool cmLocalVisualStudio7GeneratorOptions::UsingUnicode()
  1561. {
  1562. // Look for the a _UNICODE definition.
  1563. for(std::vector<cmStdString>::const_iterator di = this->Defines.begin();
  1564. di != this->Defines.end(); ++di)
  1565. {
  1566. if(*di == "_UNICODE")
  1567. {
  1568. return true;
  1569. }
  1570. }
  1571. return false;
  1572. }
  1573. //----------------------------------------------------------------------------
  1574. void cmLocalVisualStudio7GeneratorOptions::Parse(const char* flags)
  1575. {
  1576. // Parse the input string as a windows command line since the string
  1577. // is intended for writing directly into the build files.
  1578. std::vector<std::string> args;
  1579. cmSystemTools::ParseWindowsCommandLine(flags, args);
  1580. // Process flags that need to be represented specially in the IDE
  1581. // project file.
  1582. for(std::vector<std::string>::iterator ai = args.begin();
  1583. ai != args.end(); ++ai)
  1584. {
  1585. this->HandleFlag(ai->c_str());
  1586. }
  1587. }
  1588. //----------------------------------------------------------------------------
  1589. void cmLocalVisualStudio7GeneratorOptions::HandleFlag(const char* flag)
  1590. {
  1591. // If the last option was -D then this option is the definition.
  1592. if(this->DoingDefine)
  1593. {
  1594. this->DoingDefine = false;
  1595. this->Defines.push_back(flag);
  1596. return;
  1597. }
  1598. // Look for known arguments.
  1599. if(flag[0] == '-' || flag[0] == '/')
  1600. {
  1601. // Look for preprocessor definitions.
  1602. if(this->CurrentTool == Compiler && flag[1] == 'D')
  1603. {
  1604. if(flag[2] == '\0')
  1605. {
  1606. // The next argument will have the definition.
  1607. this->DoingDefine = true;
  1608. }
  1609. else
  1610. {
  1611. // Store this definition.
  1612. this->Defines.push_back(flag+2);
  1613. }
  1614. return;
  1615. }
  1616. // Look through the available flag tables.
  1617. bool flag_handled = false;
  1618. if(this->FlagTable &&
  1619. this->CheckFlagTable(this->FlagTable, flag, flag_handled))
  1620. {
  1621. return;
  1622. }
  1623. if(this->ExtraFlagTable &&
  1624. this->CheckFlagTable(this->ExtraFlagTable, flag, flag_handled))
  1625. {
  1626. return;
  1627. }
  1628. // If any map entry handled the flag we are done.
  1629. if(flag_handled)
  1630. {
  1631. return;
  1632. }
  1633. }
  1634. // This option is not known. Store it in the output flags.
  1635. this->FlagString += " ";
  1636. this->FlagString +=
  1637. cmSystemTools::EscapeWindowsShellArgument(flag,
  1638. cmsysSystem_Shell_Flag_VSIDE);
  1639. }
  1640. //----------------------------------------------------------------------------
  1641. bool
  1642. cmLocalVisualStudio7GeneratorOptions
  1643. ::CheckFlagTable(cmVS7FlagTable const* table, const char* flag,
  1644. bool& flag_handled)
  1645. {
  1646. // Look for an entry in the flag table matching this flag.
  1647. for(cmVS7FlagTable const* entry = table; entry->IDEName; ++entry)
  1648. {
  1649. bool entry_found = false;
  1650. if(entry->special & cmVS7FlagTable::UserValue)
  1651. {
  1652. // This flag table entry accepts a user-specified value. If
  1653. // the entry specifies UserRequired we must match only if a
  1654. // non-empty value is given.
  1655. int n = static_cast<int>(strlen(entry->commandFlag));
  1656. if(strncmp(flag+1, entry->commandFlag, n) == 0 &&
  1657. (!(entry->special & cmVS7FlagTable::UserRequired) ||
  1658. static_cast<int>(strlen(flag+1)) > n))
  1659. {
  1660. if(entry->special & cmVS7FlagTable::UserIgnored)
  1661. {
  1662. // Ignore the user-specified value.
  1663. this->FlagMap[entry->IDEName] = entry->value;
  1664. }
  1665. else
  1666. {
  1667. // Use the user-specified value.
  1668. this->FlagMap[entry->IDEName] = flag+1+n;
  1669. }
  1670. entry_found = true;
  1671. }
  1672. }
  1673. else if(strcmp(flag+1, entry->commandFlag) == 0)
  1674. {
  1675. // This flag table entry provides a fixed value.
  1676. this->FlagMap[entry->IDEName] = entry->value;
  1677. entry_found = true;
  1678. }
  1679. // If the flag has been handled by an entry not requesting a
  1680. // search continuation we are done.
  1681. if(entry_found && !(entry->special & cmVS7FlagTable::Continue))
  1682. {
  1683. return true;
  1684. }
  1685. // If the entry was found the flag has been handled.
  1686. flag_handled = flag_handled || entry_found;
  1687. }
  1688. return false;
  1689. }
  1690. //----------------------------------------------------------------------------
  1691. void
  1692. cmLocalVisualStudio7GeneratorOptions
  1693. ::OutputPreprocessorDefinitions(std::ostream& fout,
  1694. const char* prefix,
  1695. const char* suffix)
  1696. {
  1697. if(this->Defines.empty())
  1698. {
  1699. return;
  1700. }
  1701. fout << prefix << "PreprocessorDefinitions=\"";
  1702. const char* comma = "";
  1703. for(std::vector<cmStdString>::const_iterator di = this->Defines.begin();
  1704. di != this->Defines.end(); ++di)
  1705. {
  1706. // Double-quotes in the value of the definition must be escaped
  1707. // with a backslash.
  1708. std::string define = di->c_str();
  1709. cmSystemTools::ReplaceString(define, "\"", "\\\"");
  1710. // Escape this flag for the IDE.
  1711. define = cmLocalVisualStudio7GeneratorEscapeForXML(define.c_str());
  1712. // Write this flag. Quote it if the definition is not
  1713. // alphanumeric.
  1714. if(define.find_first_not_of(
  1715. "-_abcdefghigklmnopqrstuvwxyz1234567890ABCDEFGHIGKLMNOPQRSTUVWXYZ")
  1716. != define.npos)
  1717. {
  1718. fout << comma << "&quot;" << define << "&quot;";
  1719. }
  1720. else
  1721. {
  1722. fout << comma << define;
  1723. }
  1724. comma = ",";
  1725. }
  1726. fout << "\"" << suffix;
  1727. }
  1728. //----------------------------------------------------------------------------
  1729. void
  1730. cmLocalVisualStudio7GeneratorOptions
  1731. ::OutputFlagMap(std::ostream& fout, const char* indent)
  1732. {
  1733. for(std::map<cmStdString, cmStdString>::iterator m = this->FlagMap.begin();
  1734. m != this->FlagMap.end(); ++m)
  1735. {
  1736. fout << indent << m->first << "=\"" << m->second << "\"\n";
  1737. }
  1738. }
  1739. //----------------------------------------------------------------------------
  1740. void
  1741. cmLocalVisualStudio7GeneratorOptions
  1742. ::OutputAdditionalOptions(std::ostream& fout,
  1743. const char* prefix,
  1744. const char* suffix)
  1745. {
  1746. if(!this->FlagString.empty())
  1747. {
  1748. fout << prefix << "AdditionalOptions=\"";
  1749. fout <<
  1750. cmLocalVisualStudio7GeneratorEscapeForXML(this->FlagString.c_str());
  1751. fout << "\"" << suffix;
  1752. }
  1753. }
  1754. void cmLocalVisualStudio7Generator::
  1755. GetTargetObjectFileDirectories(cmTarget* target,
  1756. std::vector<std::string>&
  1757. dirs)
  1758. {
  1759. std::string dir = this->Makefile->GetCurrentOutputDirectory();
  1760. dir += "/";
  1761. dir += this->GetTargetDirectory(*target);
  1762. dir += "/";
  1763. dir += this->GetGlobalGenerator()->GetCMakeCFGInitDirectory();
  1764. dirs.push_back(dir);
  1765. }