cmLocalVisualStudio7Generator.cxx 67 KB

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