Imported Upstream version 2.8.10.2
[platform/upstream/cmake.git] / Source / cmGlobalVisualStudio8Generator.cxx
1 /*============================================================================
2   CMake - Cross Platform Makefile Generator
3   Copyright 2000-2009 Kitware, Inc., Insight Software Consortium
4
5   Distributed under the OSI-approved BSD License (the "License");
6   see accompanying file Copyright.txt for details.
7
8   This software is distributed WITHOUT ANY WARRANTY; without even the
9   implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10   See the License for more information.
11 ============================================================================*/
12 #include "windows.h" // this must be first to define GetCurrentDirectory
13 #include "cmGlobalVisualStudio8Generator.h"
14 #include "cmLocalVisualStudio7Generator.h"
15 #include "cmMakefile.h"
16 #include "cmake.h"
17 #include "cmGeneratedFileStream.h"
18
19 //----------------------------------------------------------------------------
20 cmGlobalVisualStudio8Generator::cmGlobalVisualStudio8Generator()
21 {
22   this->FindMakeProgramFile = "CMakeVS8FindMake.cmake";
23   this->ProjectConfigurationSectionName = "ProjectConfigurationPlatforms";
24 }
25
26 //----------------------------------------------------------------------------
27 ///! Create a local generator appropriate to this Global Generator
28 cmLocalGenerator *cmGlobalVisualStudio8Generator::CreateLocalGenerator()
29 {
30   cmLocalVisualStudio7Generator *lg =
31     new cmLocalVisualStudio7Generator(cmLocalVisualStudioGenerator::VS8);
32   lg->SetPlatformName(this->GetPlatformName());
33   lg->SetExtraFlagTable(this->GetExtraFlagTableVS8());
34   lg->SetGlobalGenerator(this);
35   return lg;
36 }
37
38 //----------------------------------------------------------------------------
39 // ouput standard header for dsw file
40 void cmGlobalVisualStudio8Generator::WriteSLNHeader(std::ostream& fout)
41 {
42   fout << "Microsoft Visual Studio Solution File, Format Version 9.00\n";
43   fout << "# Visual Studio 2005\n";
44 }
45
46 //----------------------------------------------------------------------------
47 void cmGlobalVisualStudio8Generator
48 ::GetDocumentation(cmDocumentationEntry& entry) const
49 {
50   entry.Name = this->GetName();
51   entry.Brief = "Generates Visual Studio 8 2005 project files.";
52   entry.Full = "";
53 }
54
55 //----------------------------------------------------------------------------
56 void cmGlobalVisualStudio8Generator::Configure()
57 {
58   this->cmGlobalVisualStudio7Generator::Configure();
59   this->CreateGUID(CMAKE_CHECK_BUILD_SYSTEM_TARGET);
60 }
61
62 //----------------------------------------------------------------------------
63 std::string cmGlobalVisualStudio8Generator::GetUserMacrosDirectory()
64 {
65   // Some VS8 sp0 versions cannot run macros.
66   // See http://support.microsoft.com/kb/928209
67   const char* vc8sp1Registry =
68     "HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\VisualStudio\\8.0\\"
69     "InstalledProducts\\KB926601;";
70   const char* vc8exSP1Registry =
71     "HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\VisualStudio\\8.0\\"
72     "InstalledProducts\\KB926748;";
73   std::string vc8sp1;
74   if (!cmSystemTools::ReadRegistryValue(vc8sp1Registry, vc8sp1) &&
75       !cmSystemTools::ReadRegistryValue(vc8exSP1Registry, vc8sp1))
76     {
77     return "";
78     }
79
80   std::string base;
81   std::string path;
82
83   // base begins with the VisualStudioProjectsLocation reg value...
84   if (cmSystemTools::ReadRegistryValue(
85     "HKEY_CURRENT_USER\\Software\\Microsoft\\VisualStudio\\8.0;"
86     "VisualStudioProjectsLocation",
87     base))
88     {
89     cmSystemTools::ConvertToUnixSlashes(base);
90
91     // 8.0 macros folder:
92     path = base + "/VSMacros80";
93     }
94
95   // path is (correctly) still empty if we did not read the base value from
96   // the Registry value
97   return path;
98 }
99
100 //----------------------------------------------------------------------------
101 std::string cmGlobalVisualStudio8Generator::GetUserMacrosRegKeyBase()
102 {
103   return "Software\\Microsoft\\VisualStudio\\8.0\\vsmacros";
104 }
105
106 //----------------------------------------------------------------------------
107 void cmGlobalVisualStudio8Generator::AddCheckTarget()
108 {
109   // Add a special target on which all other targets depend that
110   // checks the build system and optionally re-runs CMake.
111   const char* no_working_directory = 0;
112   std::vector<std::string> no_depends;
113   std::vector<cmLocalGenerator*> const& generators = this->LocalGenerators;
114   cmLocalVisualStudio7Generator* lg =
115     static_cast<cmLocalVisualStudio7Generator*>(generators[0]);
116   cmMakefile* mf = lg->GetMakefile();
117
118   // Skip the target if no regeneration is to be done.
119   if(mf->IsOn("CMAKE_SUPPRESS_REGENERATION"))
120     {
121     return;
122     }
123
124   std::string cmake_command = mf->GetRequiredDefinition("CMAKE_COMMAND");
125   cmCustomCommandLines noCommandLines;
126   cmTarget* tgt =
127     mf->AddUtilityCommand(CMAKE_CHECK_BUILD_SYSTEM_TARGET, false,
128                           no_working_directory, no_depends,
129                           noCommandLines);
130
131   // Organize in the "predefined targets" folder:
132   //
133   if (this->UseFolderProperty())
134     {
135     tgt->SetProperty("FOLDER", this->GetPredefinedTargetsFolder());
136     }
137
138   // Create a list of all stamp files for this project.
139   std::vector<std::string> stamps;
140   std::string stampList = cmake::GetCMakeFilesDirectoryPostSlash();
141   stampList += "generate.stamp.list";
142   {
143   std::string stampListFile =
144     generators[0]->GetMakefile()->GetCurrentOutputDirectory();
145   stampListFile += "/";
146   stampListFile += stampList;
147   std::string stampFile;
148   cmGeneratedFileStream fout(stampListFile.c_str());
149   for(std::vector<cmLocalGenerator*>::const_iterator
150         gi = generators.begin(); gi != generators.end(); ++gi)
151     {
152     stampFile = (*gi)->GetMakefile()->GetCurrentOutputDirectory();
153     stampFile += "/";
154     stampFile += cmake::GetCMakeFilesDirectoryPostSlash();
155     stampFile += "generate.stamp";
156     fout << stampFile << "\n";
157     stamps.push_back(stampFile);
158     }
159   }
160
161   // Add a custom rule to re-run CMake if any input files changed.
162   {
163   // Collect the input files used to generate all targets in this
164   // project.
165   std::vector<std::string> listFiles;
166   for(unsigned int j = 0; j < generators.size(); ++j)
167     {
168     cmMakefile* lmf = generators[j]->GetMakefile();
169     listFiles.insert(listFiles.end(), lmf->GetListFiles().begin(),
170                      lmf->GetListFiles().end());
171     }
172   // Sort the list of input files and remove duplicates.
173   std::sort(listFiles.begin(), listFiles.end(),
174             std::less<std::string>());
175   std::vector<std::string>::iterator new_end =
176     std::unique(listFiles.begin(), listFiles.end());
177   listFiles.erase(new_end, listFiles.end());
178
179   // Create a rule to re-run CMake.
180   std::string stampName = cmake::GetCMakeFilesDirectoryPostSlash();
181   stampName += "generate.stamp";
182   const char* dsprule = mf->GetRequiredDefinition("CMAKE_COMMAND");
183   cmCustomCommandLine commandLine;
184   commandLine.push_back(dsprule);
185   std::string argH = "-H";
186   argH += lg->Convert(mf->GetHomeDirectory(),
187                       cmLocalGenerator::START_OUTPUT,
188                       cmLocalGenerator::UNCHANGED, true);
189   commandLine.push_back(argH);
190   std::string argB = "-B";
191   argB += lg->Convert(mf->GetHomeOutputDirectory(),
192                       cmLocalGenerator::START_OUTPUT,
193                       cmLocalGenerator::UNCHANGED, true);
194   commandLine.push_back(argB);
195   commandLine.push_back("--check-stamp-list");
196   commandLine.push_back(stampList.c_str());
197   commandLine.push_back("--vs-solution-file");
198   commandLine.push_back("\"$(SolutionPath)\"");
199   cmCustomCommandLines commandLines;
200   commandLines.push_back(commandLine);
201
202   // Add the rule.  Note that we cannot use the CMakeLists.txt
203   // file as the main dependency because it would get
204   // overwritten by the CreateVCProjBuildRule.
205   // (this could be avoided with per-target source files)
206   const char* no_main_dependency = 0;
207   const char* no_working_directory = 0;
208   if(cmSourceFile* file =
209      mf->AddCustomCommandToOutput(
210        stamps, listFiles,
211        no_main_dependency, commandLines, "Checking Build System",
212        no_working_directory, true))
213     {
214     tgt->AddSourceFile(file);
215     }
216   else
217     {
218     cmSystemTools::Error("Error adding rule for ", stamps[0].c_str());
219     }
220   }
221 }
222
223 //----------------------------------------------------------------------------
224 void cmGlobalVisualStudio8Generator::Generate()
225 {
226   this->AddCheckTarget();
227
228   // All targets depend on the build-system check target.
229   for(std::map<cmStdString,cmTarget *>::const_iterator
230         ti = this->TotalTargets.begin();
231       ti != this->TotalTargets.end(); ++ti)
232     {
233     if(ti->first != CMAKE_CHECK_BUILD_SYSTEM_TARGET)
234       {
235       ti->second->AddUtility(CMAKE_CHECK_BUILD_SYSTEM_TARGET);
236       }
237     }
238
239   // Now perform the main generation.
240   this->cmGlobalVisualStudio7Generator::Generate();
241 }
242
243 //----------------------------------------------------------------------------
244 void
245 cmGlobalVisualStudio8Generator
246 ::WriteSolutionConfigurations(std::ostream& fout)
247 {
248   fout << "\tGlobalSection(SolutionConfigurationPlatforms) = preSolution\n";
249   for(std::vector<std::string>::iterator i = this->Configurations.begin();
250       i != this->Configurations.end(); ++i)
251     {
252     fout << "\t\t" << *i << "|" << this->GetPlatformName()
253          << " = "  << *i << "|" << this->GetPlatformName() << "\n";
254     }
255   fout << "\tEndGlobalSection\n";
256 }
257
258 //----------------------------------------------------------------------------
259 void
260 cmGlobalVisualStudio8Generator
261 ::WriteProjectConfigurations(std::ostream& fout, const char* name,
262                              bool partOfDefaultBuild,
263                              const char* platformMapping)
264 {
265   std::string guid = this->GetGUID(name);
266   for(std::vector<std::string>::iterator i = this->Configurations.begin();
267       i != this->Configurations.end(); ++i)
268     {
269     fout << "\t\t{" << guid << "}." << *i
270          << "|" << this->GetPlatformName() << ".ActiveCfg = " << *i << "|"
271          << (platformMapping ? platformMapping : this->GetPlatformName())
272          << "\n";
273     if(partOfDefaultBuild)
274       {
275       fout << "\t\t{" << guid << "}." << *i
276            << "|" << this->GetPlatformName() << ".Build.0 = " << *i << "|"
277            << (platformMapping ? platformMapping : this->GetPlatformName())
278            << "\n";
279       }
280     }
281 }
282
283 //----------------------------------------------------------------------------
284 bool cmGlobalVisualStudio8Generator::ComputeTargetDepends()
285 {
286   // Skip over the cmGlobalVisualStudioGenerator implementation!
287   // We do not need the support that VS <= 7.1 needs.
288   return this->cmGlobalGenerator::ComputeTargetDepends();
289 }
290
291 //----------------------------------------------------------------------------
292 void cmGlobalVisualStudio8Generator::WriteProjectDepends(
293   std::ostream& fout, const char*, const char*, cmTarget& t)
294 {
295   TargetDependSet const& unordered = this->GetTargetDirectDepends(t);
296   OrderedTargetDependSet depends(unordered);
297   for(OrderedTargetDependSet::const_iterator i = depends.begin();
298       i != depends.end(); ++i)
299     {
300     std::string guid = this->GetGUID((*i)->GetName());
301     fout << "\t\t{" << guid << "} = {" << guid << "}\n";
302     }
303 }
304
305 //----------------------------------------------------------------------------
306 bool cmGlobalVisualStudio8Generator::NeedLinkLibraryDependencies(
307   cmTarget& target)
308 {
309   // Look for utility dependencies that magically link.
310   for(std::set<cmStdString>::const_iterator ui =
311         target.GetUtilities().begin();
312       ui != target.GetUtilities().end(); ++ui)
313     {
314     if(cmTarget* depTarget = this->FindTarget(0, ui->c_str()))
315       {
316       if(depTarget->GetProperty("EXTERNAL_MSPROJECT"))
317         {
318         // This utility dependency names an external .vcproj target.
319         // We use LinkLibraryDependencies="true" to link to it without
320         // predicting the .lib file location or name.
321         return true;
322         }
323       }
324     }
325   return false;
326 }
327
328 //----------------------------------------------------------------------------
329 static cmVS7FlagTable cmVS8ExtraFlagTable[] =
330 {
331   {"CallingConvention", "Gd", "cdecl", "0", 0 },
332   {"CallingConvention", "Gr", "fastcall", "1", 0 },
333   {"CallingConvention", "Gz", "stdcall", "2", 0 },
334
335   {"Detect64BitPortabilityProblems", "Wp64",
336    "Detect 64Bit Portability Problems", "true", 0 },
337   {"ErrorReporting", "errorReport:prompt", "Report immediately", "1", 0 },
338   {"ErrorReporting", "errorReport:queue", "Queue for next login", "2", 0 },
339   // Precompiled header and related options.  Note that the
340   // UsePrecompiledHeader entries are marked as "Continue" so that the
341   // corresponding PrecompiledHeaderThrough entry can be found.
342   {"UsePrecompiledHeader", "Yu", "Use Precompiled Header", "2",
343    cmVS7FlagTable::UserValueIgnored | cmVS7FlagTable::Continue},
344   {"PrecompiledHeaderThrough", "Yu", "Precompiled Header Name", "",
345    cmVS7FlagTable::UserValueRequired},
346   // There is no YX option in the VS8 IDE.
347
348   // Exception handling mode.  If no entries match, it will be FALSE.
349   {"ExceptionHandling", "GX", "enable c++ exceptions", "1", 0},
350   {"ExceptionHandling", "EHsc", "enable c++ exceptions", "1", 0},
351   {"ExceptionHandling", "EHa", "enable SEH exceptions", "2", 0},
352
353   {"EnablePREfast", "analyze", "", "true", 0},
354   {"EnablePREfast", "analyze-", "", "false", 0},
355
356   // Language options
357   {"TreatWChar_tAsBuiltInType", "Zc:wchar_t",
358    "wchar_t is a built-in type", "true", 0},
359   {"TreatWChar_tAsBuiltInType", "Zc:wchar_t-",
360    "wchar_t is not a built-in type", "false", 0},
361
362   {0,0,0,0,0}
363 };
364 cmIDEFlagTable const* cmGlobalVisualStudio8Generator::GetExtraFlagTableVS8()
365 {
366   return cmVS8ExtraFlagTable;
367 }