cmLocalVisualStudio7Generator.cxx 68 KB

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