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