cmLocalVisualStudio7Generator.cxx 67 KB

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