cmLocalVisualStudio7Generator.cxx 63 KB

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