cmLocalVisualStudio7Generator.cxx 67 KB

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