cmLocalVisualStudio7Generator.cxx 63 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941
  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 a definition for the export macro.
  476. if(target.GetType() == cmTarget::SHARED_LIBRARY ||
  477. target.GetType() == cmTarget::MODULE_LIBRARY)
  478. {
  479. std::string exportSymbol;
  480. if(const char* custom_export_name = target.GetProperty("DEFINE_SYMBOL"))
  481. {
  482. exportSymbol = custom_export_name;
  483. }
  484. else
  485. {
  486. std::string id = libName;
  487. id += "_EXPORTS";
  488. exportSymbol = cmSystemTools::MakeCindentifier(id.c_str());
  489. }
  490. targetOptions.AddDefine(exportSymbol);
  491. }
  492. // The intermediate directory name consists of a directory for the
  493. // target and a subdirectory for the configuration name.
  494. std::string intermediateDir = this->GetTargetDirectory(target);
  495. intermediateDir += "/";
  496. intermediateDir += configName;
  497. fout << "\t\t\tIntermediateDirectory=\""
  498. << this->ConvertToXMLOutputPath(intermediateDir.c_str())
  499. << "\"\n"
  500. << "\t\t\tConfigurationType=\"" << configType << "\"\n"
  501. << "\t\t\tUseOfMFC=\"" << mfcFlag << "\"\n"
  502. << "\t\t\tATLMinimizesCRunTimeLibraryUsage=\"FALSE\"\n";
  503. // If unicode is enabled change the character set to unicode, if not
  504. // then default to MBCS.
  505. if(targetOptions.UsingUnicode())
  506. {
  507. fout << "\t\t\tCharacterSet=\"1\">\n";
  508. }
  509. else
  510. {
  511. fout << "\t\t\tCharacterSet=\"2\">\n";
  512. }
  513. fout << "\t\t\t<Tool\n"
  514. << "\t\t\t\tName=\"VCCLCompilerTool\"\n";
  515. targetOptions.OutputAdditionalOptions(fout, "\t\t\t\t", "\n");
  516. fout << "\t\t\t\tAdditionalIncludeDirectories=\"";
  517. std::vector<std::string> includes;
  518. this->GetIncludeDirectories(includes);
  519. std::vector<std::string>::iterator i = includes.begin();
  520. for(;i != includes.end(); ++i)
  521. {
  522. std::string ipath = this->ConvertToXMLOutputPath(i->c_str());
  523. fout << ipath << ";";
  524. }
  525. fout << "\"\n";
  526. targetOptions.OutputFlagMap(fout, "\t\t\t\t");
  527. targetOptions.OutputPreprocessorDefinitions(fout, "\t\t\t\t", "\n");
  528. fout << "\t\t\t\tAssemblerListingLocation=\"" << configName << "\"\n";
  529. fout << "\t\t\t\tObjectFile=\"$(IntDir)\\\"\n";
  530. if(target.GetType() == cmTarget::EXECUTABLE ||
  531. target.GetType() == cmTarget::STATIC_LIBRARY ||
  532. target.GetType() == cmTarget::SHARED_LIBRARY ||
  533. target.GetType() == cmTarget::MODULE_LIBRARY)
  534. {
  535. // We need to specify a program database file name even for
  536. // non-debug configurations because VS still creates .idb files.
  537. fout << "\t\t\t\tProgramDataBaseFileName=\""
  538. << target.GetDirectory(configName) << "/"
  539. << target.GetPDBName(configName) << "\"\n";
  540. }
  541. fout << "/>\n"; // end of <Tool Name=VCCLCompilerTool
  542. fout << "\t\t\t<Tool\n\t\t\t\tName=\"VCCustomBuildTool\"/>\n";
  543. fout << "\t\t\t<Tool\n\t\t\t\tName=\"VCResourceCompilerTool\"\n"
  544. << "\t\t\t\tAdditionalIncludeDirectories=\"";
  545. for(i = includes.begin();i != includes.end(); ++i)
  546. {
  547. std::string ipath = this->ConvertToXMLOutputPath(i->c_str());
  548. fout << ipath << ";";
  549. }
  550. // add the -D flags to the RC tool
  551. fout << "\"";
  552. targetOptions.OutputPreprocessorDefinitions(fout, "\n\t\t\t\t", "");
  553. fout << "/>\n";
  554. fout << "\t\t\t<Tool\n\t\t\t\tName=\"VCMIDLTool\"\n";
  555. targetOptions.OutputPreprocessorDefinitions(fout, "\t\t\t\t", "\n");
  556. fout << "\t\t\t\tMkTypLibCompatible=\"FALSE\"\n";
  557. if( this->PlatformName == "x64" )
  558. {
  559. fout << "\t\t\t\tTargetEnvironment=\"3\"\n";
  560. }
  561. else if( this->PlatformName == "ia64" )
  562. {
  563. fout << "\t\t\t\tTargetEnvironment=\"2\"\n";
  564. }
  565. else
  566. {
  567. fout << "\t\t\t\tTargetEnvironment=\"1\"\n";
  568. }
  569. fout << "\t\t\t\tGenerateStublessProxies=\"TRUE\"\n";
  570. fout << "\t\t\t\tTypeLibraryName=\"$(InputName).tlb\"\n";
  571. fout << "\t\t\t\tOutputDirectory=\"$(IntDir)\"\n";
  572. fout << "\t\t\t\tHeaderFileName=\"$(InputName).h\"\n";
  573. fout << "\t\t\t\tDLLDataFileName=\"\"\n";
  574. fout << "\t\t\t\tInterfaceIdentifierFileName=\"$(InputName)_i.c\"\n";
  575. fout << "\t\t\t\tProxyFileName=\"$(InputName)_p.c\"/>\n";
  576. // end of <Tool Name=VCMIDLTool
  577. // If we are building a version 8 project file, add a flag telling the
  578. // manifest tool to use a workaround for FAT32 file systems, which can cause
  579. // an empty manifest to be embedded into the resulting executable.
  580. // See CMake bug #2617.
  581. if ( this->Version == 8 )
  582. {
  583. fout << "\t\t\t<Tool\n\t\t\t\tName=\"VCManifestTool\"\n"
  584. << "\t\t\t\tUseFAT32Workaround=\"true\"\n"
  585. << "\t\t\t/>\n";
  586. }
  587. this->OutputTargetRules(fout, target, libName);
  588. this->OutputBuildTool(fout, configName, target);
  589. fout << "\t\t</Configuration>\n";
  590. }
  591. //----------------------------------------------------------------------------
  592. std::string
  593. cmLocalVisualStudio7Generator
  594. ::GetBuildTypeLinkerFlags(std::string rootLinkerFlags, const char* configName)
  595. {
  596. std::string configTypeUpper = cmSystemTools::UpperCase(configName);
  597. std::string extraLinkOptionsBuildTypeDef =
  598. rootLinkerFlags + "_" + configTypeUpper;
  599. std::string extraLinkOptionsBuildType =
  600. this->Makefile->GetRequiredDefinition
  601. (extraLinkOptionsBuildTypeDef.c_str());
  602. return extraLinkOptionsBuildType;
  603. }
  604. void cmLocalVisualStudio7Generator::OutputBuildTool(std::ostream& fout,
  605. const char* configName,
  606. cmTarget &target)
  607. {
  608. std::string temp;
  609. std::string extraLinkOptions;
  610. if(target.GetType() == cmTarget::EXECUTABLE)
  611. {
  612. extraLinkOptions =
  613. this->Makefile->GetRequiredDefinition("CMAKE_EXE_LINKER_FLAGS")
  614. + std::string(" ")
  615. + GetBuildTypeLinkerFlags("CMAKE_EXE_LINKER_FLAGS", configName);
  616. }
  617. if(target.GetType() == cmTarget::SHARED_LIBRARY)
  618. {
  619. extraLinkOptions =
  620. this->Makefile->GetRequiredDefinition("CMAKE_SHARED_LINKER_FLAGS")
  621. + std::string(" ")
  622. + GetBuildTypeLinkerFlags("CMAKE_SHARED_LINKER_FLAGS", configName);
  623. }
  624. if(target.GetType() == cmTarget::MODULE_LIBRARY)
  625. {
  626. extraLinkOptions =
  627. this->Makefile->GetRequiredDefinition("CMAKE_MODULE_LINKER_FLAGS")
  628. + std::string(" ")
  629. + GetBuildTypeLinkerFlags("CMAKE_MODULE_LINKER_FLAGS", configName);
  630. }
  631. const char* targetLinkFlags = target.GetProperty("LINK_FLAGS");
  632. if(targetLinkFlags)
  633. {
  634. extraLinkOptions += " ";
  635. extraLinkOptions += targetLinkFlags;
  636. }
  637. std::string configTypeUpper = cmSystemTools::UpperCase(configName);
  638. std::string linkFlagsConfig = "LINK_FLAGS_";
  639. linkFlagsConfig += configTypeUpper;
  640. targetLinkFlags = target.GetProperty(linkFlagsConfig.c_str());
  641. if(targetLinkFlags)
  642. {
  643. extraLinkOptions += " ";
  644. extraLinkOptions += targetLinkFlags;
  645. }
  646. Options linkOptions(Options::Linker);
  647. linkOptions.Parse(extraLinkOptions.c_str());
  648. switch(target.GetType())
  649. {
  650. case cmTarget::STATIC_LIBRARY:
  651. {
  652. std::string targetNameFull = target.GetFullName(configName);
  653. std::string libpath = target.GetDirectory(configName);
  654. libpath += "/";
  655. libpath += targetNameFull;
  656. fout << "\t\t\t<Tool\n"
  657. << "\t\t\t\tName=\"VCLibrarianTool\"\n";
  658. if(const char* libflags = target.GetProperty("STATIC_LIBRARY_FLAGS"))
  659. {
  660. fout << "\t\t\t\tAdditionalOptions=\"" << libflags << "\"\n";
  661. }
  662. fout << "\t\t\t\tOutputFile=\""
  663. << this->ConvertToXMLOutputPathSingle(libpath.c_str()) << "\"/>\n";
  664. break;
  665. }
  666. case cmTarget::SHARED_LIBRARY:
  667. case cmTarget::MODULE_LIBRARY:
  668. {
  669. std::string targetName;
  670. std::string targetNameSO;
  671. std::string targetNameFull;
  672. std::string targetNameImport;
  673. std::string targetNamePDB;
  674. target.GetLibraryNames(targetName, targetNameSO, targetNameFull,
  675. targetNameImport, targetNamePDB, configName);
  676. // Compute the link library and directory information.
  677. std::vector<cmStdString> linkLibs;
  678. std::vector<cmStdString> linkDirs;
  679. this->ComputeLinkInformation(target, configName, linkLibs, linkDirs);
  680. // Get the language to use for linking.
  681. const char* linkLanguage =
  682. target.GetLinkerLanguage(this->GetGlobalGenerator());
  683. if(!linkLanguage)
  684. {
  685. cmSystemTools::Error
  686. ("CMake can not determine linker language for target:",
  687. target.GetName());
  688. return;
  689. }
  690. // Compute the variable name to lookup standard libraries for this
  691. // language.
  692. std::string standardLibsVar = "CMAKE_";
  693. standardLibsVar += linkLanguage;
  694. standardLibsVar += "_STANDARD_LIBRARIES";
  695. fout << "\t\t\t<Tool\n"
  696. << "\t\t\t\tName=\"VCLinkerTool\"\n";
  697. linkOptions.OutputAdditionalOptions(fout, "\t\t\t\t", "\n");
  698. // Use the NOINHERIT macro to avoid getting VS project default
  699. // libraries which may be set by the user to something bad.
  700. fout << "\t\t\t\tAdditionalDependencies=\"$(NOINHERIT) "
  701. << this->Makefile->GetSafeDefinition(standardLibsVar.c_str())
  702. << " ";
  703. this->OutputLibraries(fout, linkLibs);
  704. fout << "\"\n";
  705. temp = target.GetDirectory(configName);
  706. temp += "/";
  707. temp += targetNameFull;
  708. fout << "\t\t\t\tOutputFile=\""
  709. << this->ConvertToXMLOutputPathSingle(temp.c_str()) << "\"\n";
  710. this->WriteTargetVersionAttribute(fout, target);
  711. linkOptions.OutputFlagMap(fout, "\t\t\t\t");
  712. fout << "\t\t\t\tAdditionalLibraryDirectories=\"";
  713. this->OutputLibraryDirectories(fout, linkDirs);
  714. fout << "\"\n";
  715. this->OutputModuleDefinitionFile(fout, target);
  716. temp = target.GetDirectory(configName);
  717. temp += "/";
  718. temp += targetNamePDB;
  719. fout << "\t\t\t\tProgramDataBaseFile=\"" <<
  720. this->ConvertToXMLOutputPathSingle(temp.c_str()) << "\"\n";
  721. if(strcmp(configName, "Debug") == 0
  722. || strcmp(configName, "RelWithDebInfo") == 0)
  723. {
  724. fout << "\t\t\t\tGenerateDebugInformation=\"TRUE\"\n";
  725. }
  726. std::string stackVar = "CMAKE_";
  727. stackVar += linkLanguage;
  728. stackVar += "_STACK_SIZE";
  729. const char* stackVal = this->Makefile->GetDefinition(stackVar.c_str());
  730. if(stackVal)
  731. {
  732. fout << "\t\t\t\tStackReserveSize=\"" << stackVal << "\"\n";
  733. }
  734. temp = target.GetDirectory(configName, true);
  735. temp += "/";
  736. temp += targetNameImport;
  737. fout << "\t\t\t\tImportLibrary=\""
  738. << this->ConvertToXMLOutputPathSingle(temp.c_str()) << "\"/>\n";
  739. }
  740. break;
  741. case cmTarget::EXECUTABLE:
  742. {
  743. std::string targetName;
  744. std::string targetNameFull;
  745. std::string targetNameImport;
  746. std::string targetNamePDB;
  747. target.GetExecutableNames(targetName, targetNameFull,
  748. targetNameImport, targetNamePDB, configName);
  749. // Compute the link library and directory information.
  750. std::vector<cmStdString> linkLibs;
  751. std::vector<cmStdString> linkDirs;
  752. this->ComputeLinkInformation(target, configName, linkLibs, linkDirs);
  753. // Get the language to use for linking.
  754. const char* linkLanguage =
  755. target.GetLinkerLanguage(this->GetGlobalGenerator());
  756. if(!linkLanguage)
  757. {
  758. cmSystemTools::Error
  759. ("CMake can not determine linker language for target:",
  760. target.GetName());
  761. return;
  762. }
  763. // Compute the variable name to lookup standard libraries for this
  764. // language.
  765. std::string standardLibsVar = "CMAKE_";
  766. standardLibsVar += linkLanguage;
  767. standardLibsVar += "_STANDARD_LIBRARIES";
  768. fout << "\t\t\t<Tool\n"
  769. << "\t\t\t\tName=\"VCLinkerTool\"\n";
  770. linkOptions.OutputAdditionalOptions(fout, "\t\t\t\t", "\n");
  771. // Use the NOINHERIT macro to avoid getting VS project default
  772. // libraries which may be set by the user to something bad.
  773. fout << "\t\t\t\tAdditionalDependencies=\"$(NOINHERIT) "
  774. << this->Makefile->GetSafeDefinition(standardLibsVar.c_str())
  775. << " ";
  776. this->OutputLibraries(fout, linkLibs);
  777. fout << "\"\n";
  778. temp = target.GetDirectory(configName);
  779. temp += "/";
  780. temp += targetNameFull;
  781. fout << "\t\t\t\tOutputFile=\""
  782. << this->ConvertToXMLOutputPathSingle(temp.c_str()) << "\"\n";
  783. this->WriteTargetVersionAttribute(fout, target);
  784. linkOptions.OutputFlagMap(fout, "\t\t\t\t");
  785. fout << "\t\t\t\tAdditionalLibraryDirectories=\"";
  786. this->OutputLibraryDirectories(fout, linkDirs);
  787. fout << "\"\n";
  788. fout << "\t\t\t\tProgramDataBaseFile=\""
  789. << target.GetDirectory(configName) << "/" << targetNamePDB
  790. << "\"\n";
  791. if(strcmp(configName, "Debug") == 0
  792. || strcmp(configName, "RelWithDebInfo") == 0)
  793. {
  794. fout << "\t\t\t\tGenerateDebugInformation=\"TRUE\"\n";
  795. }
  796. if ( target.GetPropertyAsBool("WIN32_EXECUTABLE") )
  797. {
  798. fout << "\t\t\t\tSubSystem=\"2\"\n";
  799. }
  800. else
  801. {
  802. fout << "\t\t\t\tSubSystem=\"1\"\n";
  803. }
  804. std::string stackVar = "CMAKE_";
  805. stackVar += linkLanguage;
  806. stackVar += "_STACK_SIZE";
  807. const char* stackVal = this->Makefile->GetDefinition(stackVar.c_str());
  808. if(stackVal)
  809. {
  810. fout << "\t\t\t\tStackReserveSize=\"" << stackVal << "\"";
  811. }
  812. temp = target.GetDirectory(configName, true);
  813. temp += "/";
  814. temp += targetNameImport;
  815. fout << "\t\t\t\tImportLibrary=\""
  816. << this->ConvertToXMLOutputPathSingle(temp.c_str()) << "\"/>\n";
  817. break;
  818. }
  819. case cmTarget::UTILITY:
  820. case cmTarget::GLOBAL_TARGET:
  821. break;
  822. }
  823. }
  824. //----------------------------------------------------------------------------
  825. void
  826. cmLocalVisualStudio7Generator
  827. ::WriteTargetVersionAttribute(std::ostream& fout, cmTarget& target)
  828. {
  829. int major;
  830. int minor;
  831. target.GetTargetVersion(major, minor);
  832. fout << "\t\t\t\tVersion=\"" << major << "." << minor << "\"\n";
  833. }
  834. void cmLocalVisualStudio7Generator
  835. ::OutputModuleDefinitionFile(std::ostream& fout,
  836. cmTarget &target)
  837. {
  838. std::vector<cmSourceFile*> const& classes = target.GetSourceFiles();
  839. for(std::vector<cmSourceFile*>::const_iterator i = classes.begin();
  840. i != classes.end(); i++)
  841. {
  842. if(cmSystemTools::UpperCase((*i)->GetSourceExtension()) == "DEF")
  843. {
  844. fout << "\t\t\t\tModuleDefinitionFile=\""
  845. << this->ConvertToXMLOutputPath((*i)->GetFullPath().c_str())
  846. << "\"\n";
  847. return;
  848. }
  849. }
  850. }
  851. //----------------------------------------------------------------------------
  852. void
  853. cmLocalVisualStudio7Generator
  854. ::OutputLibraries(std::ostream& fout,
  855. std::vector<cmStdString> const& libs)
  856. {
  857. for(std::vector<cmStdString>::const_iterator l = libs.begin();
  858. l != libs.end(); ++l)
  859. {
  860. fout << this->ConvertToXMLOutputPath(l->c_str()) << " ";
  861. }
  862. }
  863. //----------------------------------------------------------------------------
  864. void
  865. cmLocalVisualStudio7Generator
  866. ::OutputLibraryDirectories(std::ostream& fout,
  867. std::vector<cmStdString> const& dirs)
  868. {
  869. const char* comma = "";
  870. for(std::vector<cmStdString>::const_iterator d = dirs.begin();
  871. d != dirs.end(); ++d)
  872. {
  873. // Remove any trailing slash and skip empty paths.
  874. std::string dir = *d;
  875. if(dir[dir.size()-1] == '/')
  876. {
  877. dir = dir.substr(0, dir.size()-1);
  878. }
  879. if(dir.empty())
  880. {
  881. continue;
  882. }
  883. // Switch to a relative path specification if it is shorter.
  884. if(cmSystemTools::FileIsFullPath(dir.c_str()))
  885. {
  886. std::string rel = this->Convert(dir.c_str(), START_OUTPUT, UNCHANGED);
  887. if(rel.size() < dir.size())
  888. {
  889. dir = rel;
  890. }
  891. }
  892. // First search a configuration-specific subdirectory and then the
  893. // original directory.
  894. fout << comma << this->ConvertToXMLOutputPath((dir+"/$(OutDir)").c_str())
  895. << "," << this->ConvertToXMLOutputPath(dir.c_str());
  896. comma = ",";
  897. }
  898. }
  899. void cmLocalVisualStudio7Generator::WriteVCProjFile(std::ostream& fout,
  900. const char *libName,
  901. cmTarget &target)
  902. {
  903. // get the configurations
  904. std::vector<std::string> *configs =
  905. static_cast<cmGlobalVisualStudio7Generator *>
  906. (this->GlobalGenerator)->GetConfigurations();
  907. // trace the visual studio dependencies
  908. std::string name = libName;
  909. name += ".vcproj.cmake";
  910. // We may be modifying the source groups temporarily, so make a copy.
  911. std::vector<cmSourceGroup> sourceGroups = this->Makefile->GetSourceGroups();
  912. // get the classes from the source lists then add them to the groups
  913. std::vector<cmSourceFile*> & classes = target.GetSourceFiles();
  914. for(std::vector<cmSourceFile*>::const_iterator i = classes.begin();
  915. i != classes.end(); i++)
  916. {
  917. // Add the file to the list of sources.
  918. std::string source = (*i)->GetFullPath();
  919. if(cmSystemTools::UpperCase((*i)->GetSourceExtension()) == "DEF")
  920. {
  921. this->ModuleDefinitionFile = (*i)->GetFullPath();
  922. }
  923. cmSourceGroup& sourceGroup =
  924. this->Makefile->FindSourceGroup(source.c_str(), sourceGroups);
  925. sourceGroup.AssignSource(*i);
  926. }
  927. // Compute which sources need unique object computation.
  928. this->ComputeObjectNameRequirements(sourceGroups);
  929. // open the project
  930. this->WriteProjectStart(fout, libName, target, sourceGroups);
  931. // write the configuration information
  932. this->WriteConfigurations(fout, libName, target);
  933. fout << "\t<Files>\n";
  934. // Loop through every source group.
  935. for(unsigned int i = 0; i < sourceGroups.size(); ++i)
  936. {
  937. cmSourceGroup sg = sourceGroups[i];
  938. this->WriteGroup(&sg, target, fout, libName, configs);
  939. }
  940. //}
  941. fout << "\t</Files>\n";
  942. // Write the VCProj file's footer.
  943. this->WriteVCProjFooter(fout);
  944. }
  945. void cmLocalVisualStudio7Generator
  946. ::WriteGroup(const cmSourceGroup *sg, cmTarget target,
  947. std::ostream &fout, const char *libName,
  948. std::vector<std::string> *configs)
  949. {
  950. const std::vector<const cmSourceFile *> &sourceFiles =
  951. sg->GetSourceFiles();
  952. // If the group is empty, don't write it at all.
  953. if(sourceFiles.empty() && sg->GetGroupChildren().empty())
  954. {
  955. return;
  956. }
  957. // If the group has a name, write the header.
  958. std::string name = sg->GetName();
  959. if(name != "")
  960. {
  961. this->WriteVCProjBeginGroup(fout, name.c_str(), "");
  962. }
  963. // Compute the maximum length of a configuration name.
  964. std::string::size_type config_len_max = 0;
  965. for(std::vector<std::string>::iterator i = configs->begin();
  966. i != configs->end(); ++i)
  967. {
  968. if(i->size() > config_len_max)
  969. {
  970. config_len_max = i->size();
  971. }
  972. }
  973. // Compute the maximum length of the full path to the intermediate
  974. // files directory for any configuration. This is used to construct
  975. // object file names that do not produce paths that are too long.
  976. std::string::size_type dir_len = 0;
  977. dir_len += strlen(this->Makefile->GetCurrentOutputDirectory());
  978. dir_len += 1;
  979. dir_len += this->GetTargetDirectory(target).size();
  980. dir_len += 1;
  981. dir_len += config_len_max;
  982. dir_len += 1;
  983. // Loop through each source in the source group.
  984. std::string objectName;
  985. for(std::vector<const cmSourceFile *>::const_iterator sf =
  986. sourceFiles.begin(); sf != sourceFiles.end(); ++sf)
  987. {
  988. std::string source = (*sf)->GetFullPath();
  989. const cmCustomCommand *command = (*sf)->GetCustomCommand();
  990. std::string compileFlags;
  991. std::string additionalDeps;
  992. if(this->NeedObjectName.find(*sf) != this->NeedObjectName.end())
  993. {
  994. objectName = this->GetObjectFileNameWithoutTarget(*(*sf), dir_len);
  995. }
  996. else
  997. {
  998. objectName = "";
  999. }
  1000. // Add per-source flags.
  1001. const char* cflags = (*sf)->GetProperty("COMPILE_FLAGS");
  1002. if(cflags)
  1003. {
  1004. compileFlags += " ";
  1005. compileFlags += cflags;
  1006. }
  1007. const char* lang = this->GlobalGenerator->GetLanguageFromExtension
  1008. ((*sf)->GetSourceExtension().c_str());
  1009. const char* sourceLang = this->GetSourceFileLanguage(*(*sf));
  1010. const char* linkLanguage = target.GetLinkerLanguage
  1011. (this->GetGlobalGenerator());
  1012. bool needForceLang = false;
  1013. // source file does not match its extension language
  1014. if(lang && sourceLang && strcmp(lang, sourceLang) != 0)
  1015. {
  1016. needForceLang = true;
  1017. lang = sourceLang;
  1018. }
  1019. // If lang is set, the compiler will generate code automatically.
  1020. // If HEADER_FILE_ONLY is set, we must suppress this generation in
  1021. // the project file
  1022. bool excludedFromBuild =
  1023. (lang && (*sf)->GetPropertyAsBool("HEADER_FILE_ONLY"));
  1024. // if the source file does not match the linker language
  1025. // then force c or c++
  1026. if(needForceLang || (linkLanguage && lang
  1027. && strcmp(lang, linkLanguage) != 0))
  1028. {
  1029. if(strcmp(lang, "CXX") == 0)
  1030. {
  1031. // force a C++ file type
  1032. compileFlags += " /TP ";
  1033. }
  1034. else if(strcmp(lang, "C") == 0)
  1035. {
  1036. // force to c
  1037. compileFlags += " /TC ";
  1038. }
  1039. }
  1040. // Check for extra object-file dependencies.
  1041. const char* deps = (*sf)->GetProperty("OBJECT_DEPENDS");
  1042. if(deps)
  1043. {
  1044. std::vector<std::string> depends;
  1045. cmSystemTools::ExpandListArgument(deps, depends);
  1046. if(!depends.empty())
  1047. {
  1048. std::vector<std::string>::iterator i = depends.begin();
  1049. additionalDeps = this->ConvertToXMLOutputPath(i->c_str());
  1050. for(++i;i != depends.end(); ++i)
  1051. {
  1052. additionalDeps += ";";
  1053. additionalDeps += this->ConvertToXMLOutputPath(i->c_str());
  1054. }
  1055. }
  1056. }
  1057. if (source != libName || target.GetType() == cmTarget::UTILITY ||
  1058. target.GetType() == cmTarget::GLOBAL_TARGET )
  1059. {
  1060. fout << "\t\t\t<File\n";
  1061. std::string d = this->ConvertToXMLOutputPathSingle(source.c_str());
  1062. // Tell MS-Dev what the source is. If the compiler knows how to
  1063. // build it, then it will.
  1064. fout << "\t\t\t\tRelativePath=\"" << d << "\">\n";
  1065. if (command)
  1066. {
  1067. // Construct the entire set of commands in one string.
  1068. std::string script =
  1069. this->ConstructScript(command->GetCommandLines(),
  1070. command->GetWorkingDirectory(),
  1071. command->GetEscapeOldStyle(),
  1072. command->GetEscapeAllowMakeVars());
  1073. std::string comment = this->ConstructComment(*command);
  1074. const char* flags = compileFlags.size() ? compileFlags.c_str(): 0;
  1075. this->WriteCustomRule(fout, source.c_str(), script.c_str(),
  1076. comment.c_str(), command->GetDepends(),
  1077. command->GetOutputs(), flags);
  1078. }
  1079. else if(compileFlags.size() || additionalDeps.length()
  1080. || objectName.size() || excludedFromBuild)
  1081. {
  1082. const char* aCompilerTool = "VCCLCompilerTool";
  1083. std::string ext = (*sf)->GetSourceExtension();
  1084. ext = cmSystemTools::LowerCase(ext);
  1085. if(ext == "idl")
  1086. {
  1087. aCompilerTool = "VCMIDLTool";
  1088. }
  1089. if(ext == "rc")
  1090. {
  1091. aCompilerTool = "VCResourceCompilerTool";
  1092. }
  1093. if(ext == "def")
  1094. {
  1095. aCompilerTool = "VCCustomBuildTool";
  1096. }
  1097. for(std::vector<std::string>::iterator i = configs->begin();
  1098. i != configs->end(); ++i)
  1099. {
  1100. fout << "\t\t\t\t<FileConfiguration\n"
  1101. << "\t\t\t\t\tName=\"" << *i
  1102. << "|" << this->PlatformName << "\"";
  1103. if(excludedFromBuild)
  1104. {
  1105. fout << " ExcludedFromBuild=\"true\"";
  1106. }
  1107. fout << ">\n";
  1108. fout << "\t\t\t\t\t<Tool\n"
  1109. << "\t\t\t\t\tName=\"" << aCompilerTool << "\"\n";
  1110. if(!compileFlags.empty())
  1111. {
  1112. Options fileOptions(Options::Compiler, this->ExtraFlagTable);
  1113. fileOptions.Parse(compileFlags.c_str());
  1114. fileOptions.OutputAdditionalOptions(fout, "\t\t\t\t\t", "\n");
  1115. fileOptions.OutputFlagMap(fout, "\t\t\t\t\t");
  1116. fileOptions.OutputPreprocessorDefinitions(fout,
  1117. "\t\t\t\t\t", "\n");
  1118. }
  1119. if(additionalDeps.length())
  1120. {
  1121. fout << "\t\t\t\t\tAdditionalDependencies=\""
  1122. << additionalDeps.c_str() << "\"\n";
  1123. }
  1124. if(objectName.size())
  1125. {
  1126. fout << "\t\t\t\t\tObjectFile=\"$(IntDir)/"
  1127. << objectName.c_str() << "\"\n";
  1128. }
  1129. fout << "\t\t\t\t\t/>\n"
  1130. << "\t\t\t\t</FileConfiguration>\n";
  1131. }
  1132. }
  1133. fout << "\t\t\t</File>\n";
  1134. }
  1135. }
  1136. std::vector<cmSourceGroup> children = sg->GetGroupChildren();
  1137. for(unsigned int i=0;i<children.size();++i)
  1138. {
  1139. this->WriteGroup(&children[i], target, fout, libName, configs);
  1140. }
  1141. // If the group has a name, write the footer.
  1142. if(name != "")
  1143. {
  1144. this->WriteVCProjEndGroup(fout);
  1145. }
  1146. }
  1147. void cmLocalVisualStudio7Generator::
  1148. WriteCustomRule(std::ostream& fout,
  1149. const char* source,
  1150. const char* command,
  1151. const char* comment,
  1152. const std::vector<std::string>& depends,
  1153. const std::vector<std::string>& outputs,
  1154. const char* compileFlags)
  1155. {
  1156. // Write the rule for each configuration.
  1157. std::vector<std::string>::iterator i;
  1158. std::vector<std::string> *configs =
  1159. static_cast<cmGlobalVisualStudio7Generator *>
  1160. (this->GlobalGenerator)->GetConfigurations();
  1161. for(i = configs->begin(); i != configs->end(); ++i)
  1162. {
  1163. fout << "\t\t\t\t<FileConfiguration\n";
  1164. fout << "\t\t\t\t\tName=\"" << *i << "|" << this->PlatformName << "\">\n";
  1165. if(compileFlags)
  1166. {
  1167. fout << "\t\t\t\t\t<Tool\n"
  1168. << "\t\t\t\t\tName=\"VCCLCompilerTool\"\n"
  1169. << "\t\t\t\t\tAdditionalOptions=\""
  1170. << this->EscapeForXML(compileFlags) << "\"/>\n";
  1171. }
  1172. fout << "\t\t\t\t\t<Tool\n"
  1173. << "\t\t\t\t\tName=\"VCCustomBuildTool\"\n"
  1174. << "\t\t\t\t\tDescription=\""
  1175. << this->EscapeForXML(comment) << "\"\n"
  1176. << "\t\t\t\t\tCommandLine=\""
  1177. << this->EscapeForXML(command) << "\"\n"
  1178. << "\t\t\t\t\tAdditionalDependencies=\"";
  1179. if(depends.empty())
  1180. {
  1181. // There are no real dependencies. Produce an artificial one to
  1182. // make sure the rule runs reliably.
  1183. if(!cmSystemTools::FileExists(source))
  1184. {
  1185. std::ofstream depout(source);
  1186. depout << "Artificial dependency for a custom command.\n";
  1187. }
  1188. fout << this->ConvertToXMLOutputPath(source);
  1189. }
  1190. else
  1191. {
  1192. // Write out the dependencies for the rule.
  1193. std::string temp;
  1194. for(std::vector<std::string>::const_iterator d = depends.begin();
  1195. d != depends.end(); ++d)
  1196. {
  1197. // Get the real name of the dependency in case it is a CMake target.
  1198. std::string dep = this->GetRealDependency(d->c_str(), i->c_str());
  1199. fout << this->ConvertToXMLOutputPath(dep.c_str())
  1200. << ";";
  1201. }
  1202. }
  1203. fout << "\"\n";
  1204. fout << "\t\t\t\t\tOutputs=\"";
  1205. if(outputs.empty())
  1206. {
  1207. fout << source << "_force";
  1208. }
  1209. else
  1210. {
  1211. // Write a rule for the output generated by this command.
  1212. const char* sep = "";
  1213. for(std::vector<std::string>::const_iterator o = outputs.begin();
  1214. o != outputs.end(); ++o)
  1215. {
  1216. fout << sep << this->ConvertToXMLOutputPathSingle(o->c_str());
  1217. sep = ";";
  1218. }
  1219. }
  1220. fout << "\"/>\n";
  1221. fout << "\t\t\t\t</FileConfiguration>\n";
  1222. }
  1223. }
  1224. void cmLocalVisualStudio7Generator::WriteVCProjBeginGroup(std::ostream& fout,
  1225. const char* group,
  1226. const char* )
  1227. {
  1228. fout << "\t\t<Filter\n"
  1229. << "\t\t\tName=\"" << group << "\"\n"
  1230. << "\t\t\tFilter=\"\">\n";
  1231. }
  1232. void cmLocalVisualStudio7Generator::WriteVCProjEndGroup(std::ostream& fout)
  1233. {
  1234. fout << "\t\t</Filter>\n";
  1235. }
  1236. // look for custom rules on a target and collect them together
  1237. void cmLocalVisualStudio7Generator
  1238. ::OutputTargetRules(std::ostream& fout,
  1239. cmTarget &target,
  1240. const char * /*libName*/)
  1241. {
  1242. if (target.GetType() > cmTarget::GLOBAL_TARGET)
  1243. {
  1244. return;
  1245. }
  1246. // add the pre build rules
  1247. fout << "\t\t\t<Tool\n\t\t\t\tName=\"VCPreBuildEventTool\"";
  1248. bool init = false;
  1249. for (std::vector<cmCustomCommand>::const_iterator cr =
  1250. target.GetPreBuildCommands().begin();
  1251. cr != target.GetPreBuildCommands().end(); ++cr)
  1252. {
  1253. if(!init)
  1254. {
  1255. fout << "\nCommandLine=\"";
  1256. init = true;
  1257. }
  1258. else
  1259. {
  1260. fout << this->EscapeForXML("\n");
  1261. }
  1262. std::string script =
  1263. this->ConstructScript(cr->GetCommandLines(),
  1264. cr->GetWorkingDirectory(),
  1265. cr->GetEscapeOldStyle(),
  1266. cr->GetEscapeAllowMakeVars());
  1267. fout << this->EscapeForXML(script.c_str()).c_str();
  1268. }
  1269. if (init)
  1270. {
  1271. fout << "\"";
  1272. }
  1273. fout << "/>\n";
  1274. // add the pre Link rules
  1275. fout << "\t\t\t<Tool\n\t\t\t\tName=\"VCPreLinkEventTool\"";
  1276. init = false;
  1277. for (std::vector<cmCustomCommand>::const_iterator cr =
  1278. target.GetPreLinkCommands().begin();
  1279. cr != target.GetPreLinkCommands().end(); ++cr)
  1280. {
  1281. if(!init)
  1282. {
  1283. fout << "\nCommandLine=\"";
  1284. init = true;
  1285. }
  1286. else
  1287. {
  1288. fout << this->EscapeForXML("\n");
  1289. }
  1290. std::string script =
  1291. this->ConstructScript(cr->GetCommandLines(),
  1292. cr->GetWorkingDirectory(),
  1293. cr->GetEscapeOldStyle(),
  1294. cr->GetEscapeAllowMakeVars());
  1295. fout << this->EscapeForXML(script.c_str()).c_str();
  1296. }
  1297. if (init)
  1298. {
  1299. fout << "\"";
  1300. }
  1301. fout << "/>\n";
  1302. // add the PostBuild rules
  1303. fout << "\t\t\t<Tool\n\t\t\t\tName=\"VCPostBuildEventTool\"";
  1304. init = false;
  1305. for (std::vector<cmCustomCommand>::const_iterator cr =
  1306. target.GetPostBuildCommands().begin();
  1307. cr != target.GetPostBuildCommands().end(); ++cr)
  1308. {
  1309. if(!init)
  1310. {
  1311. fout << "\nCommandLine=\"";
  1312. init = true;
  1313. }
  1314. else
  1315. {
  1316. fout << this->EscapeForXML("\n");
  1317. }
  1318. std::string script =
  1319. this->ConstructScript(cr->GetCommandLines(),
  1320. cr->GetWorkingDirectory(),
  1321. cr->GetEscapeOldStyle(),
  1322. cr->GetEscapeAllowMakeVars());
  1323. fout << this->EscapeForXML(script.c_str()).c_str();
  1324. }
  1325. if (init)
  1326. {
  1327. fout << "\"";
  1328. }
  1329. fout << "/>\n";
  1330. }
  1331. void
  1332. cmLocalVisualStudio7Generator::WriteProjectStart(std::ostream& fout,
  1333. const char *libName,
  1334. cmTarget & target,
  1335. std::vector<cmSourceGroup> &)
  1336. {
  1337. fout << "<?xml version=\"1.0\" encoding = \"Windows-1252\"?>\n"
  1338. << "<VisualStudioProject\n"
  1339. << "\tProjectType=\"Visual C++\"\n";
  1340. if(this->Version == 71)
  1341. {
  1342. fout << "\tVersion=\"7.10\"\n";
  1343. }
  1344. else
  1345. {
  1346. if (this->Version == 8)
  1347. {
  1348. fout << "\tVersion=\"8.00\"\n";
  1349. }
  1350. else
  1351. {
  1352. fout << "\tVersion=\"7.00\"\n";
  1353. }
  1354. }
  1355. const char* projLabel = target.GetProperty("PROJECT_LABEL");
  1356. if(!projLabel)
  1357. {
  1358. projLabel = libName;
  1359. }
  1360. const char* keyword = target.GetProperty("VS_KEYWORD");
  1361. if(!keyword)
  1362. {
  1363. keyword = "Win32Proj";
  1364. }
  1365. cmGlobalVisualStudio7Generator* gg =
  1366. static_cast<cmGlobalVisualStudio7Generator *>(this->GlobalGenerator);
  1367. fout << "\tName=\"" << projLabel << "\"\n";
  1368. if(this->Version == 8)
  1369. {
  1370. fout << "\tProjectGUID=\"{" << gg->GetGUID(libName) << "}\"\n";
  1371. }
  1372. fout << "\tSccProjectName=\"\"\n"
  1373. << "\tSccLocalPath=\"\"\n"
  1374. << "\tKeyword=\"" << keyword << "\">\n"
  1375. << "\t<Platforms>\n"
  1376. << "\t\t<Platform\n\t\t\tName=\"" << this->PlatformName << "\"/>\n"
  1377. << "\t</Platforms>\n";
  1378. }
  1379. void cmLocalVisualStudio7Generator::WriteVCProjFooter(std::ostream& fout)
  1380. {
  1381. fout << "\t<Globals>\n"
  1382. << "\t</Globals>\n"
  1383. << "</VisualStudioProject>\n";
  1384. }
  1385. std::string cmLocalVisualStudio7GeneratorEscapeForXML(const char* s)
  1386. {
  1387. std::string ret = s;
  1388. cmSystemTools::ReplaceString(ret, "&", "&amp;");
  1389. cmSystemTools::ReplaceString(ret, "\"", "&quot;");
  1390. cmSystemTools::ReplaceString(ret, "<", "&lt;");
  1391. cmSystemTools::ReplaceString(ret, ">", "&gt;");
  1392. cmSystemTools::ReplaceString(ret, "\n", "&#x0D;&#x0A;");
  1393. return ret;
  1394. }
  1395. std::string cmLocalVisualStudio7Generator::EscapeForXML(const char* s)
  1396. {
  1397. return cmLocalVisualStudio7GeneratorEscapeForXML(s);
  1398. }
  1399. std::string cmLocalVisualStudio7Generator
  1400. ::ConvertToXMLOutputPath(const char* path)
  1401. {
  1402. std::string ret = this->ConvertToOptionallyRelativeOutputPath(path);
  1403. cmSystemTools::ReplaceString(ret, "&", "&amp;");
  1404. cmSystemTools::ReplaceString(ret, "\"", "&quot;");
  1405. cmSystemTools::ReplaceString(ret, "<", "&lt;");
  1406. cmSystemTools::ReplaceString(ret, ">", "&gt;");
  1407. return ret;
  1408. }
  1409. std::string cmLocalVisualStudio7Generator
  1410. ::ConvertToXMLOutputPathSingle(const char* path)
  1411. {
  1412. std::string ret = this->ConvertToOptionallyRelativeOutputPath(path);
  1413. cmSystemTools::ReplaceString(ret, "\"", "");
  1414. cmSystemTools::ReplaceString(ret, "&", "&amp;");
  1415. cmSystemTools::ReplaceString(ret, "<", "&lt;");
  1416. cmSystemTools::ReplaceString(ret, ">", "&gt;");
  1417. return ret;
  1418. }
  1419. // This class is used to parse an existing vs 7 project
  1420. // and extract the GUID
  1421. class cmVS7XMLParser : public cmXMLParser
  1422. {
  1423. public:
  1424. virtual void EndElement(const char* /* name */)
  1425. {
  1426. }
  1427. virtual void StartElement(const char* name, const char** atts)
  1428. {
  1429. // once the GUID is found do nothing
  1430. if(this->GUID.size())
  1431. {
  1432. return;
  1433. }
  1434. int i =0;
  1435. if(strcmp("VisualStudioProject", name) == 0)
  1436. {
  1437. while(atts[i])
  1438. {
  1439. if(strcmp(atts[i], "ProjectGUID") == 0)
  1440. {
  1441. if(atts[i+1])
  1442. {
  1443. this->GUID = atts[i+1];
  1444. this->GUID = this->GUID.substr(1, this->GUID.size()-2);
  1445. }
  1446. else
  1447. {
  1448. this->GUID = "";
  1449. }
  1450. return;
  1451. }
  1452. ++i;
  1453. }
  1454. }
  1455. }
  1456. int InitializeParser()
  1457. {
  1458. int ret = cmXMLParser::InitializeParser();
  1459. if(ret == 0)
  1460. {
  1461. return ret;
  1462. }
  1463. // visual studio projects have a strange encoding, but it is
  1464. // really utf-8
  1465. XML_SetEncoding(static_cast<XML_Parser>(this->Parser), "utf-8");
  1466. return 1;
  1467. }
  1468. std::string GUID;
  1469. };
  1470. void cmLocalVisualStudio7Generator::ReadAndStoreExternalGUID(
  1471. const char* name,
  1472. const char* path)
  1473. {
  1474. cmVS7XMLParser parser;
  1475. parser.ParseFile(path);
  1476. // if we can not find a GUID then create one
  1477. if(parser.GUID.size() == 0)
  1478. {
  1479. cmGlobalVisualStudio7Generator* gg =
  1480. static_cast<cmGlobalVisualStudio7Generator *>(this->GlobalGenerator);
  1481. gg->CreateGUID(name);
  1482. return;
  1483. }
  1484. std::string guidStoreName = name;
  1485. guidStoreName += "_GUID_CMAKE";
  1486. // save the GUID in the cache
  1487. this->GlobalGenerator->GetCMakeInstance()->
  1488. AddCacheEntry(guidStoreName.c_str(),
  1489. parser.GUID.c_str(),
  1490. "Stored GUID",
  1491. cmCacheManager::INTERNAL);
  1492. }
  1493. void cmLocalVisualStudio7Generator::ConfigureFinalPass()
  1494. {
  1495. cmLocalGenerator::ConfigureFinalPass();
  1496. cmTargets &tgts = this->Makefile->GetTargets();
  1497. cmGlobalVisualStudio7Generator* gg =
  1498. static_cast<cmGlobalVisualStudio7Generator *>(this->GlobalGenerator);
  1499. for(cmTargets::iterator l = tgts.begin(); l != tgts.end(); l++)
  1500. {
  1501. if (strncmp(l->first.c_str(), "INCLUDE_EXTERNAL_MSPROJECT", 26) == 0)
  1502. {
  1503. cmCustomCommand cc = l->second.GetPostBuildCommands()[0];
  1504. const cmCustomCommandLines& cmds = cc.GetCommandLines();
  1505. std::string project_name = cmds[0][0];
  1506. this->ReadAndStoreExternalGUID(project_name.c_str(),
  1507. cmds[0][1].c_str());
  1508. }
  1509. else
  1510. {
  1511. gg->CreateGUID(l->first.c_str());
  1512. }
  1513. }
  1514. }
  1515. //----------------------------------------------------------------------------
  1516. std::string cmLocalVisualStudio7Generator
  1517. ::GetTargetDirectory(cmTarget& target)
  1518. {
  1519. std::string dir;
  1520. dir += target.GetName();
  1521. dir += ".dir";
  1522. return dir;
  1523. }
  1524. //----------------------------------------------------------------------------
  1525. cmLocalVisualStudio7GeneratorOptions
  1526. ::cmLocalVisualStudio7GeneratorOptions(Tool tool,
  1527. cmVS7FlagTable const* extraTable):
  1528. DoingDefine(false), FlagTable(0), ExtraFlagTable(extraTable)
  1529. {
  1530. // Choose the flag table for the requested tool.
  1531. switch(tool)
  1532. {
  1533. case Compiler:
  1534. this->FlagTable = cmLocalVisualStudio7GeneratorFlagTable; break;
  1535. case Linker:
  1536. this->FlagTable = cmLocalVisualStudio7GeneratorLinkFlagTable; break;
  1537. default: break;
  1538. }
  1539. }
  1540. //----------------------------------------------------------------------------
  1541. void cmLocalVisualStudio7GeneratorOptions::FixExceptionHandlingDefault()
  1542. {
  1543. // Exception handling is on by default because the platform file has
  1544. // "/EHsc" in the flags. Normally, that will override this
  1545. // initialization to off, but the user has the option of removing
  1546. // the flag to disable exception handling. When the user does
  1547. // remove the flag we need to override the IDE default of on.
  1548. this->FlagMap["ExceptionHandling"] = "FALSE";
  1549. }
  1550. //----------------------------------------------------------------------------
  1551. void cmLocalVisualStudio7GeneratorOptions::SetVerboseMakefile(bool verbose)
  1552. {
  1553. // If verbose makefiles have been requested and the /nologo option
  1554. // was not given explicitly in the flags we want to add an attribute
  1555. // to the generated project to disable logo suppression. Otherwise
  1556. // the GUI default is to enable suppression.
  1557. if(verbose &&
  1558. this->FlagMap.find("SuppressStartupBanner") == this->FlagMap.end())
  1559. {
  1560. this->FlagMap["SuppressStartupBanner"] = "FALSE";
  1561. }
  1562. }
  1563. //----------------------------------------------------------------------------
  1564. void cmLocalVisualStudio7GeneratorOptions::AddDefine(const std::string& def)
  1565. {
  1566. this->Defines.push_back(def);
  1567. }
  1568. //----------------------------------------------------------------------------
  1569. void cmLocalVisualStudio7GeneratorOptions::AddFlag(const char* flag,
  1570. const char* value)
  1571. {
  1572. this->FlagMap[flag] = value;
  1573. }
  1574. //----------------------------------------------------------------------------
  1575. bool cmLocalVisualStudio7GeneratorOptions::UsingUnicode()
  1576. {
  1577. // Look for the a _UNICODE definition.
  1578. for(std::vector<cmStdString>::const_iterator di = this->Defines.begin();
  1579. di != this->Defines.end(); ++di)
  1580. {
  1581. if(*di == "_UNICODE")
  1582. {
  1583. return true;
  1584. }
  1585. }
  1586. return false;
  1587. }
  1588. //----------------------------------------------------------------------------
  1589. void cmLocalVisualStudio7GeneratorOptions::Parse(const char* flags)
  1590. {
  1591. // Parse the input string as a windows command line since the string
  1592. // is intended for writing directly into the build files.
  1593. std::vector<std::string> args;
  1594. cmSystemTools::ParseWindowsCommandLine(flags, args);
  1595. // Process flags that need to be represented specially in the IDE
  1596. // project file.
  1597. for(std::vector<std::string>::iterator ai = args.begin();
  1598. ai != args.end(); ++ai)
  1599. {
  1600. this->HandleFlag(ai->c_str());
  1601. }
  1602. }
  1603. //----------------------------------------------------------------------------
  1604. void cmLocalVisualStudio7GeneratorOptions::HandleFlag(const char* flag)
  1605. {
  1606. // If the last option was -D then this option is the definition.
  1607. if(this->DoingDefine)
  1608. {
  1609. this->DoingDefine = false;
  1610. this->Defines.push_back(flag);
  1611. return;
  1612. }
  1613. // Look for known arguments.
  1614. if(flag[0] == '-' || flag[0] == '/')
  1615. {
  1616. // Look for preprocessor definitions.
  1617. if(flag[1] == 'D')
  1618. {
  1619. if(flag[2] == '\0')
  1620. {
  1621. // The next argument will have the definition.
  1622. this->DoingDefine = true;
  1623. }
  1624. else
  1625. {
  1626. // Store this definition.
  1627. this->Defines.push_back(flag+2);
  1628. }
  1629. return;
  1630. }
  1631. // Look through the available flag tables.
  1632. bool flag_handled = false;
  1633. if(this->FlagTable &&
  1634. this->CheckFlagTable(this->FlagTable, flag, flag_handled))
  1635. {
  1636. return;
  1637. }
  1638. if(this->ExtraFlagTable &&
  1639. this->CheckFlagTable(this->ExtraFlagTable, flag, flag_handled))
  1640. {
  1641. return;
  1642. }
  1643. // If any map entry handled the flag we are done.
  1644. if(flag_handled)
  1645. {
  1646. return;
  1647. }
  1648. }
  1649. // This option is not known. Store it in the output flags.
  1650. this->FlagString += " ";
  1651. this->FlagString +=
  1652. cmSystemTools::EscapeWindowsShellArgument(flag,
  1653. cmsysSystem_Shell_Flag_VSIDE);
  1654. }
  1655. //----------------------------------------------------------------------------
  1656. bool
  1657. cmLocalVisualStudio7GeneratorOptions
  1658. ::CheckFlagTable(cmVS7FlagTable const* table, const char* flag,
  1659. bool& flag_handled)
  1660. {
  1661. // Look for an entry in the flag table matching this flag.
  1662. for(cmVS7FlagTable const* entry = table; entry->IDEName; ++entry)
  1663. {
  1664. bool entry_found = false;
  1665. if(entry->special & cmVS7FlagTable::UserValue)
  1666. {
  1667. // This flag table entry accepts a user-specified value. If
  1668. // the entry specifies UserRequired we must match only if a
  1669. // non-empty value is given.
  1670. int n = static_cast<int>(strlen(entry->commandFlag));
  1671. if(strncmp(flag+1, entry->commandFlag, n) == 0 &&
  1672. (!(entry->special & cmVS7FlagTable::UserRequired) ||
  1673. static_cast<int>(strlen(flag+1)) > n))
  1674. {
  1675. if(entry->special & cmVS7FlagTable::UserIgnored)
  1676. {
  1677. // Ignore the user-specified value.
  1678. this->FlagMap[entry->IDEName] = entry->value;
  1679. }
  1680. else
  1681. {
  1682. // Use the user-specified value.
  1683. this->FlagMap[entry->IDEName] = flag+1+n;
  1684. }
  1685. entry_found = true;
  1686. }
  1687. }
  1688. else if(strcmp(flag+1, entry->commandFlag) == 0)
  1689. {
  1690. // This flag table entry provides a fixed value.
  1691. this->FlagMap[entry->IDEName] = entry->value;
  1692. entry_found = true;
  1693. }
  1694. // If the flag has been handled by an entry not requesting a
  1695. // search continuation we are done.
  1696. if(entry_found && !(entry->special & cmVS7FlagTable::Continue))
  1697. {
  1698. return true;
  1699. }
  1700. // If the entry was found the flag has been handled.
  1701. flag_handled = flag_handled || entry_found;
  1702. }
  1703. return false;
  1704. }
  1705. //----------------------------------------------------------------------------
  1706. void
  1707. cmLocalVisualStudio7GeneratorOptions
  1708. ::OutputPreprocessorDefinitions(std::ostream& fout,
  1709. const char* prefix,
  1710. const char* suffix)
  1711. {
  1712. if(this->Defines.empty())
  1713. {
  1714. return;
  1715. }
  1716. fout << prefix << "PreprocessorDefinitions=\"";
  1717. const char* comma = "";
  1718. for(std::vector<cmStdString>::const_iterator di = this->Defines.begin();
  1719. di != this->Defines.end(); ++di)
  1720. {
  1721. // Escape this flag for the IDE.
  1722. std::string define =
  1723. cmLocalVisualStudio7GeneratorEscapeForXML(di->c_str());
  1724. // Old comment:
  1725. // Double-quotes in the value of the definition must be escaped
  1726. // with a backslash. The entire definition should be quoted in
  1727. // the generated xml attribute to avoid confusing the VS parser.
  1728. // Write this flag. Quote it if the definition is not
  1729. // alphanumeric.
  1730. if(define.find_first_not_of(
  1731. "-_abcdefghigklmnopqrstuvwxyz1234567890ABCDEFGHIGKLMNOPQRSTUVWXYZ")
  1732. != define.npos)
  1733. {
  1734. fout << comma << "&quot;" << define << "&quot;";
  1735. }
  1736. else
  1737. {
  1738. fout << comma << define;
  1739. }
  1740. comma = ",";
  1741. }
  1742. fout << "\"" << suffix;
  1743. }
  1744. //----------------------------------------------------------------------------
  1745. void
  1746. cmLocalVisualStudio7GeneratorOptions
  1747. ::OutputFlagMap(std::ostream& fout, const char* indent)
  1748. {
  1749. for(std::map<cmStdString, cmStdString>::iterator m = this->FlagMap.begin();
  1750. m != this->FlagMap.end(); ++m)
  1751. {
  1752. fout << indent << m->first << "=\"" << m->second << "\"\n";
  1753. }
  1754. }
  1755. //----------------------------------------------------------------------------
  1756. void
  1757. cmLocalVisualStudio7GeneratorOptions
  1758. ::OutputAdditionalOptions(std::ostream& fout,
  1759. const char* prefix,
  1760. const char* suffix)
  1761. {
  1762. if(!this->FlagString.empty())
  1763. {
  1764. fout << prefix << "AdditionalOptions=\"";
  1765. fout <<
  1766. cmLocalVisualStudio7GeneratorEscapeForXML(this->FlagString.c_str());
  1767. fout << "\"" << suffix;
  1768. }
  1769. }
  1770. void cmLocalVisualStudio7Generator::
  1771. GetTargetObjectFileDirectories(cmTarget* target,
  1772. std::vector<std::string>&
  1773. dirs)
  1774. {
  1775. std::string dir = this->Makefile->GetCurrentOutputDirectory();
  1776. dir += "/";
  1777. dir += this->GetTargetDirectory(*target);
  1778. dir += "/";
  1779. dir += this->GetGlobalGenerator()->GetCMakeCFGInitDirectory();
  1780. std::cerr << dir << "\n";
  1781. dirs.push_back(dir);
  1782. }
  1783. // return the source name for the object file
  1784. std::string
  1785. cmLocalVisualStudio7Generator::GetSourceObjectName(cmSourceFile& sf )
  1786. {
  1787. std::string ret = sf.GetSourceName();
  1788. std::string::size_type pos = ret.find("/");
  1789. if(pos == ret.npos)
  1790. {
  1791. return ret;
  1792. }
  1793. return ret.substr(pos+1);
  1794. }