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