1 /*============================================================================
2 CMake - Cross Platform Makefile Generator
3 Copyright 2000-2009 Kitware, Inc., Insight Software Consortium
5 Distributed under the OSI-approved BSD License (the "License");
6 see accompanying file Copyright.txt for details.
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"
17 #include "cmGeneratedFileStream.h"
19 //----------------------------------------------------------------------------
20 cmGlobalVisualStudio8Generator::cmGlobalVisualStudio8Generator()
22 this->FindMakeProgramFile = "CMakeVS8FindMake.cmake";
23 this->ProjectConfigurationSectionName = "ProjectConfigurationPlatforms";
24 this->ArchitectureId = "X86";
27 //----------------------------------------------------------------------------
28 ///! Create a local generator appropriate to this Global Generator
29 cmLocalGenerator *cmGlobalVisualStudio8Generator::CreateLocalGenerator()
31 cmLocalVisualStudio7Generator *lg =
32 new cmLocalVisualStudio7Generator(cmLocalVisualStudioGenerator::VS8);
33 lg->SetPlatformName(this->GetPlatformName());
34 lg->SetExtraFlagTable(this->GetExtraFlagTableVS8());
35 lg->SetGlobalGenerator(this);
39 //----------------------------------------------------------------------------
40 // ouput standard header for dsw file
41 void cmGlobalVisualStudio8Generator::WriteSLNHeader(std::ostream& fout)
43 fout << "Microsoft Visual Studio Solution File, Format Version 9.00\n";
44 fout << "# Visual Studio 2005\n";
47 //----------------------------------------------------------------------------
48 void cmGlobalVisualStudio8Generator
49 ::GetDocumentation(cmDocumentationEntry& entry) const
51 entry.Name = this->GetName();
52 entry.Brief = "Generates Visual Studio .NET 2005 project files.";
56 //----------------------------------------------------------------------------
57 void cmGlobalVisualStudio8Generator::AddPlatformDefinitions(cmMakefile* mf)
59 mf->AddDefinition("MSVC_C_ARCHITECTURE_ID", this->ArchitectureId);
60 mf->AddDefinition("MSVC_CXX_ARCHITECTURE_ID", this->ArchitectureId);
61 mf->AddDefinition("MSVC80", "1");
64 //----------------------------------------------------------------------------
65 void cmGlobalVisualStudio8Generator::Configure()
67 this->cmGlobalVisualStudio7Generator::Configure();
68 this->CreateGUID(CMAKE_CHECK_BUILD_SYSTEM_TARGET);
71 //----------------------------------------------------------------------------
72 std::string cmGlobalVisualStudio8Generator::GetUserMacrosDirectory()
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;";
83 if (!cmSystemTools::ReadRegistryValue(vc8sp1Registry, vc8sp1) &&
84 !cmSystemTools::ReadRegistryValue(vc8exSP1Registry, vc8sp1))
92 // base begins with the VisualStudioProjectsLocation reg value...
93 if (cmSystemTools::ReadRegistryValue(
94 "HKEY_CURRENT_USER\\Software\\Microsoft\\VisualStudio\\8.0;"
95 "VisualStudioProjectsLocation",
98 cmSystemTools::ConvertToUnixSlashes(base);
100 // 8.0 macros folder:
101 path = base + "/VSMacros80";
104 // path is (correctly) still empty if we did not read the base value from
105 // the Registry value
109 //----------------------------------------------------------------------------
110 std::string cmGlobalVisualStudio8Generator::GetUserMacrosRegKeyBase()
112 return "Software\\Microsoft\\VisualStudio\\8.0\\vsmacros";
115 //----------------------------------------------------------------------------
116 void cmGlobalVisualStudio8Generator::AddCheckTarget()
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();
127 // Skip the target if no regeneration is to be done.
128 if(mf->IsOn("CMAKE_SUPPRESS_REGENERATION"))
133 std::string cmake_command = mf->GetRequiredDefinition("CMAKE_COMMAND");
134 cmCustomCommandLines noCommandLines;
136 mf->AddUtilityCommand(CMAKE_CHECK_BUILD_SYSTEM_TARGET, false,
137 no_working_directory, no_depends,
140 // Organize in the "predefined targets" folder:
142 if (this->UseFolderProperty())
144 tgt->SetProperty("FOLDER", this->GetPredefinedTargetsFolder());
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";
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)
161 stampFile = (*gi)->GetMakefile()->GetCurrentOutputDirectory();
163 stampFile += cmake::GetCMakeFilesDirectoryPostSlash();
164 stampFile += "generate.stamp";
165 fout << stampFile << "\n";
166 stamps.push_back(stampFile);
170 // Add a custom rule to re-run CMake if any input files changed.
172 // Collect the input files used to generate all targets in this
174 std::vector<std::string> listFiles;
175 for(unsigned int j = 0; j < generators.size(); ++j)
177 cmMakefile* lmf = generators[j]->GetMakefile();
178 listFiles.insert(listFiles.end(), lmf->GetListFiles().begin(),
179 lmf->GetListFiles().end());
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());
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);
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(
220 no_main_dependency, commandLines, "Checking Build System",
221 no_working_directory, true))
223 tgt->AddSourceFile(file);
227 cmSystemTools::Error("Error adding rule for ", stamps[0].c_str());
232 //----------------------------------------------------------------------------
233 void cmGlobalVisualStudio8Generator::Generate()
235 this->AddCheckTarget();
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)
242 if(ti->first != CMAKE_CHECK_BUILD_SYSTEM_TARGET)
244 ti->second->AddUtility(CMAKE_CHECK_BUILD_SYSTEM_TARGET);
248 // Now perform the main generation.
249 this->cmGlobalVisualStudio7Generator::Generate();
252 //----------------------------------------------------------------------------
254 cmGlobalVisualStudio8Generator
255 ::WriteSolutionConfigurations(std::ostream& fout)
257 fout << "\tGlobalSection(SolutionConfigurationPlatforms) = preSolution\n";
258 for(std::vector<std::string>::iterator i = this->Configurations.begin();
259 i != this->Configurations.end(); ++i)
261 fout << "\t\t" << *i << "|" << this->GetPlatformName()
262 << " = " << *i << "|" << this->GetPlatformName() << "\n";
264 fout << "\tEndGlobalSection\n";
267 //----------------------------------------------------------------------------
269 cmGlobalVisualStudio8Generator
270 ::WriteProjectConfigurations(std::ostream& fout, const char* name,
271 bool partOfDefaultBuild,
272 const char* platformMapping)
274 std::string guid = this->GetGUID(name);
275 for(std::vector<std::string>::iterator i = this->Configurations.begin();
276 i != this->Configurations.end(); ++i)
278 fout << "\t\t{" << guid << "}." << *i
279 << "|" << this->GetPlatformName() << ".ActiveCfg = " << *i << "|"
280 << (platformMapping ? platformMapping : this->GetPlatformName())
282 if(partOfDefaultBuild)
284 fout << "\t\t{" << guid << "}." << *i
285 << "|" << this->GetPlatformName() << ".Build.0 = " << *i << "|"
286 << (platformMapping ? platformMapping : this->GetPlatformName())
292 //----------------------------------------------------------------------------
293 bool cmGlobalVisualStudio8Generator::ComputeTargetDepends()
295 // Skip over the cmGlobalVisualStudioGenerator implementation!
296 // We do not need the support that VS <= 7.1 needs.
297 return this->cmGlobalGenerator::ComputeTargetDepends();
300 //----------------------------------------------------------------------------
301 void cmGlobalVisualStudio8Generator::WriteProjectDepends(
302 std::ostream& fout, const char*, const char*, cmTarget& t)
304 TargetDependSet const& unordered = this->GetTargetDirectDepends(t);
305 OrderedTargetDependSet depends(unordered);
306 for(OrderedTargetDependSet::const_iterator i = depends.begin();
307 i != depends.end(); ++i)
309 std::string guid = this->GetGUID((*i)->GetName());
310 fout << "\t\t{" << guid << "} = {" << guid << "}\n";
314 //----------------------------------------------------------------------------
315 bool cmGlobalVisualStudio8Generator::NeedLinkLibraryDependencies(
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)
323 if(cmTarget* depTarget = this->FindTarget(0, ui->c_str()))
325 if(depTarget->GetProperty("EXTERNAL_MSPROJECT"))
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.
337 //----------------------------------------------------------------------------
338 static cmVS7FlagTable cmVS8ExtraFlagTable[] =
340 {"CallingConvention", "Gd", "cdecl", "0", 0 },
341 {"CallingConvention", "Gr", "fastcall", "1", 0 },
342 {"CallingConvention", "Gz", "stdcall", "2", 0 },
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.
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},
362 {"EnablePREfast", "analyze", "", "true", 0},
363 {"EnablePREfast", "analyze-", "", "false", 0},
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},
373 cmIDEFlagTable const* cmGlobalVisualStudio8Generator::GetExtraFlagTableVS8()
375 return cmVS8ExtraFlagTable;