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";
26 //----------------------------------------------------------------------------
27 ///! Create a local generator appropriate to this Global Generator
28 cmLocalGenerator *cmGlobalVisualStudio8Generator::CreateLocalGenerator()
30 cmLocalVisualStudio7Generator *lg =
31 new cmLocalVisualStudio7Generator(cmLocalVisualStudioGenerator::VS8);
32 lg->SetPlatformName(this->GetPlatformName());
33 lg->SetExtraFlagTable(this->GetExtraFlagTableVS8());
34 lg->SetGlobalGenerator(this);
38 //----------------------------------------------------------------------------
39 // ouput standard header for dsw file
40 void cmGlobalVisualStudio8Generator::WriteSLNHeader(std::ostream& fout)
42 fout << "Microsoft Visual Studio Solution File, Format Version 9.00\n";
43 fout << "# Visual Studio 2005\n";
46 //----------------------------------------------------------------------------
47 void cmGlobalVisualStudio8Generator
48 ::GetDocumentation(cmDocumentationEntry& entry) const
50 entry.Name = this->GetName();
51 entry.Brief = "Generates Visual Studio 8 2005 project files.";
55 //----------------------------------------------------------------------------
56 void cmGlobalVisualStudio8Generator::Configure()
58 this->cmGlobalVisualStudio7Generator::Configure();
59 this->CreateGUID(CMAKE_CHECK_BUILD_SYSTEM_TARGET);
62 //----------------------------------------------------------------------------
63 std::string cmGlobalVisualStudio8Generator::GetUserMacrosDirectory()
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;";
74 if (!cmSystemTools::ReadRegistryValue(vc8sp1Registry, vc8sp1) &&
75 !cmSystemTools::ReadRegistryValue(vc8exSP1Registry, vc8sp1))
83 // base begins with the VisualStudioProjectsLocation reg value...
84 if (cmSystemTools::ReadRegistryValue(
85 "HKEY_CURRENT_USER\\Software\\Microsoft\\VisualStudio\\8.0;"
86 "VisualStudioProjectsLocation",
89 cmSystemTools::ConvertToUnixSlashes(base);
92 path = base + "/VSMacros80";
95 // path is (correctly) still empty if we did not read the base value from
100 //----------------------------------------------------------------------------
101 std::string cmGlobalVisualStudio8Generator::GetUserMacrosRegKeyBase()
103 return "Software\\Microsoft\\VisualStudio\\8.0\\vsmacros";
106 //----------------------------------------------------------------------------
107 void cmGlobalVisualStudio8Generator::AddCheckTarget()
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();
118 // Skip the target if no regeneration is to be done.
119 if(mf->IsOn("CMAKE_SUPPRESS_REGENERATION"))
124 std::string cmake_command = mf->GetRequiredDefinition("CMAKE_COMMAND");
125 cmCustomCommandLines noCommandLines;
127 mf->AddUtilityCommand(CMAKE_CHECK_BUILD_SYSTEM_TARGET, false,
128 no_working_directory, no_depends,
131 // Organize in the "predefined targets" folder:
133 if (this->UseFolderProperty())
135 tgt->SetProperty("FOLDER", this->GetPredefinedTargetsFolder());
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";
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)
152 stampFile = (*gi)->GetMakefile()->GetCurrentOutputDirectory();
154 stampFile += cmake::GetCMakeFilesDirectoryPostSlash();
155 stampFile += "generate.stamp";
156 fout << stampFile << "\n";
157 stamps.push_back(stampFile);
161 // Add a custom rule to re-run CMake if any input files changed.
163 // Collect the input files used to generate all targets in this
165 std::vector<std::string> listFiles;
166 for(unsigned int j = 0; j < generators.size(); ++j)
168 cmMakefile* lmf = generators[j]->GetMakefile();
169 listFiles.insert(listFiles.end(), lmf->GetListFiles().begin(),
170 lmf->GetListFiles().end());
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());
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);
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(
211 no_main_dependency, commandLines, "Checking Build System",
212 no_working_directory, true))
214 tgt->AddSourceFile(file);
218 cmSystemTools::Error("Error adding rule for ", stamps[0].c_str());
223 //----------------------------------------------------------------------------
224 void cmGlobalVisualStudio8Generator::Generate()
226 this->AddCheckTarget();
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)
233 if(ti->first != CMAKE_CHECK_BUILD_SYSTEM_TARGET)
235 ti->second->AddUtility(CMAKE_CHECK_BUILD_SYSTEM_TARGET);
239 // Now perform the main generation.
240 this->cmGlobalVisualStudio7Generator::Generate();
243 //----------------------------------------------------------------------------
245 cmGlobalVisualStudio8Generator
246 ::WriteSolutionConfigurations(std::ostream& fout)
248 fout << "\tGlobalSection(SolutionConfigurationPlatforms) = preSolution\n";
249 for(std::vector<std::string>::iterator i = this->Configurations.begin();
250 i != this->Configurations.end(); ++i)
252 fout << "\t\t" << *i << "|" << this->GetPlatformName()
253 << " = " << *i << "|" << this->GetPlatformName() << "\n";
255 fout << "\tEndGlobalSection\n";
258 //----------------------------------------------------------------------------
260 cmGlobalVisualStudio8Generator
261 ::WriteProjectConfigurations(std::ostream& fout, const char* name,
262 bool partOfDefaultBuild,
263 const char* platformMapping)
265 std::string guid = this->GetGUID(name);
266 for(std::vector<std::string>::iterator i = this->Configurations.begin();
267 i != this->Configurations.end(); ++i)
269 fout << "\t\t{" << guid << "}." << *i
270 << "|" << this->GetPlatformName() << ".ActiveCfg = " << *i << "|"
271 << (platformMapping ? platformMapping : this->GetPlatformName())
273 if(partOfDefaultBuild)
275 fout << "\t\t{" << guid << "}." << *i
276 << "|" << this->GetPlatformName() << ".Build.0 = " << *i << "|"
277 << (platformMapping ? platformMapping : this->GetPlatformName())
283 //----------------------------------------------------------------------------
284 bool cmGlobalVisualStudio8Generator::ComputeTargetDepends()
286 // Skip over the cmGlobalVisualStudioGenerator implementation!
287 // We do not need the support that VS <= 7.1 needs.
288 return this->cmGlobalGenerator::ComputeTargetDepends();
291 //----------------------------------------------------------------------------
292 void cmGlobalVisualStudio8Generator::WriteProjectDepends(
293 std::ostream& fout, const char*, const char*, cmTarget& t)
295 TargetDependSet const& unordered = this->GetTargetDirectDepends(t);
296 OrderedTargetDependSet depends(unordered);
297 for(OrderedTargetDependSet::const_iterator i = depends.begin();
298 i != depends.end(); ++i)
300 std::string guid = this->GetGUID((*i)->GetName());
301 fout << "\t\t{" << guid << "} = {" << guid << "}\n";
305 //----------------------------------------------------------------------------
306 bool cmGlobalVisualStudio8Generator::NeedLinkLibraryDependencies(
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)
314 if(cmTarget* depTarget = this->FindTarget(0, ui->c_str()))
316 if(depTarget->GetProperty("EXTERNAL_MSPROJECT"))
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.
328 //----------------------------------------------------------------------------
329 static cmVS7FlagTable cmVS8ExtraFlagTable[] =
331 {"CallingConvention", "Gd", "cdecl", "0", 0 },
332 {"CallingConvention", "Gr", "fastcall", "1", 0 },
333 {"CallingConvention", "Gz", "stdcall", "2", 0 },
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.
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},
353 {"EnablePREfast", "analyze", "", "true", 0},
354 {"EnablePREfast", "analyze-", "", "false", 0},
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},
364 cmIDEFlagTable const* cmGlobalVisualStudio8Generator::GetExtraFlagTableVS8()
366 return cmVS8ExtraFlagTable;