cmLocalVisualStudio7Generator.cxx 61 KB

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