cmLocalVisualStudio7Generator.cxx 63 KB

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