cmLocalVisualStudio7Generator.cxx 63 KB

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