Imported Upstream version 2.8.9
[platform/upstream/cmake.git] / Source / cmGlobalVisualStudio10Generator.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 "cmGlobalVisualStudio10Generator.h"
14 #include "cmLocalVisualStudio10Generator.h"
15 #include "cmMakefile.h"
16 #include "cmSourceFile.h"
17 #include "cmake.h"
18
19
20 cmGlobalVisualStudio10Generator::cmGlobalVisualStudio10Generator()
21 {
22   this->FindMakeProgramFile = "CMakeVS10FindMake.cmake";
23   std::string vc10Express;
24   this->ExpressEdition = cmSystemTools::ReadRegistryValue(
25     "HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\VCExpress\\10.0\\Setup\\VC;"
26     "ProductDir", vc10Express, cmSystemTools::KeyWOW64_32);
27 }
28
29 //----------------------------------------------------------------------------
30 void cmGlobalVisualStudio10Generator::AddPlatformDefinitions(cmMakefile* mf)
31 {
32   mf->AddDefinition("MSVC10", "1");
33   mf->AddDefinition("MSVC_C_ARCHITECTURE_ID", "X86");
34   mf->AddDefinition("MSVC_CXX_ARCHITECTURE_ID", "X86");
35 }
36
37 //----------------------------------------------------------------------------
38 void cmGlobalVisualStudio10Generator::WriteSLNHeader(std::ostream& fout)
39 {
40   fout << "Microsoft Visual Studio Solution File, Format Version 11.00\n";
41   fout << "# Visual Studio 2010\n";
42 }
43
44 ///! Create a local generator appropriate to this Global Generator
45 cmLocalGenerator *cmGlobalVisualStudio10Generator::CreateLocalGenerator()
46 {
47   cmLocalVisualStudio10Generator* lg =
48     new cmLocalVisualStudio10Generator(cmLocalVisualStudioGenerator::VS10);
49   lg->SetPlatformName(this->GetPlatformName());
50   lg->SetGlobalGenerator(this);
51   return lg;
52 }
53
54 //----------------------------------------------------------------------------
55 void cmGlobalVisualStudio10Generator::Generate()
56 {
57   this->LongestSource = LongestSourcePath();
58   this->cmGlobalVisualStudio8Generator::Generate();
59   if(this->LongestSource.Length > 0)
60     {
61     cmMakefile* mf = this->LongestSource.Target->GetMakefile();
62     cmOStringStream e;
63     e <<
64       "The binary and/or source directory paths may be too long to generate "
65       "Visual Studio 10 files for this project.  "
66       "Consider choosing shorter directory names to build this project with "
67       "Visual Studio 10.  "
68       "A more detailed explanation follows."
69       "\n"
70       "There is a bug in the VS 10 IDE that renders property dialog fields "
71       "blank for files referenced by full path in the project file.  "
72       "However, CMake must reference at least one file by full path:\n"
73       "  " << this->LongestSource.SourceFile->GetFullPath() << "\n"
74       "This is because some Visual Studio tools would append the relative "
75       "path to the end of the referencing directory path, as in:\n"
76       "  " << mf->GetCurrentOutputDirectory() << "/"
77       << this->LongestSource.SourceRel << "\n"
78       "and then incorrectly complain that the file does not exist because "
79       "the path length is too long for some internal buffer or API.  "
80       "To avoid this problem CMake must use a full path for this file "
81       "which then triggers the VS 10 property dialog bug.";
82     mf->IssueMessage(cmake::WARNING, e.str().c_str());
83     }
84 }
85
86 //----------------------------------------------------------------------------
87 void cmGlobalVisualStudio10Generator
88 ::GetDocumentation(cmDocumentationEntry& entry) const
89 {
90   entry.Name = this->GetName();
91   entry.Brief = "Generates Visual Studio 10 project files.";
92   entry.Full = "";
93 }
94
95 //----------------------------------------------------------------------------
96 void cmGlobalVisualStudio10Generator
97 ::EnableLanguage(std::vector<std::string>const &  lang, 
98                  cmMakefile *mf, bool optional)
99 {
100   cmGlobalVisualStudio8Generator::EnableLanguage(lang, mf, optional);
101 }
102
103 //----------------------------------------------------------------------------
104 const char* cmGlobalVisualStudio10Generator::GetPlatformToolset()
105 {
106   if(!this->PlatformToolset.empty())
107     {
108     return this->PlatformToolset.c_str();
109     }
110   return 0;
111 }
112
113 //----------------------------------------------------------------------------
114 std::string cmGlobalVisualStudio10Generator::GetUserMacrosDirectory()
115 {
116   std::string base;
117   std::string path;
118
119   // base begins with the VisualStudioProjectsLocation reg value...
120   if (cmSystemTools::ReadRegistryValue(
121     "HKEY_CURRENT_USER\\Software\\Microsoft\\VisualStudio\\10.0;"
122     "VisualStudioProjectsLocation",
123     base))
124     {
125     cmSystemTools::ConvertToUnixSlashes(base);
126
127     // 9.0 macros folder:
128     path = base + "/VSMacros80";
129       // *NOT* a typo; right now in Visual Studio 2008 beta the macros
130       // folder is VSMacros80... They may change it to 90 before final
131       // release of 2008 or they may not... we'll have to keep our eyes
132       // on it
133     }
134
135   // path is (correctly) still empty if we did not read the base value from
136   // the Registry value
137   return path;
138 }
139
140 //----------------------------------------------------------------------------
141 std::string cmGlobalVisualStudio10Generator::GetUserMacrosRegKeyBase()
142 {
143   return "Software\\Microsoft\\VisualStudio\\10.0\\vsmacros";
144 }
145
146
147 std::string cmGlobalVisualStudio10Generator
148 ::GenerateBuildCommand(const char* makeProgram,
149                        const char *projectName, 
150                        const char* additionalOptions, const char *targetName,
151                        const char* config, bool ignoreErrors, bool fast)
152 {
153   // now build the test
154   std::string makeCommand 
155     = cmSystemTools::ConvertToOutputPath(makeProgram);
156   std::string lowerCaseCommand = makeCommand;
157   cmSystemTools::LowerCase(lowerCaseCommand);
158
159   // If makeProgram is devenv, parent class knows how to generate command:
160   if (lowerCaseCommand.find("devenv") != std::string::npos ||
161       lowerCaseCommand.find("VCExpress") != std::string::npos)
162     {
163     return cmGlobalVisualStudio7Generator::GenerateBuildCommand(makeProgram,
164       projectName, additionalOptions, targetName, config, ignoreErrors, fast);
165     }
166
167   // Otherwise, assume MSBuild command line, and construct accordingly.
168
169   // if there are spaces in the makeCommand, assume a full path
170   // and convert it to a path with no spaces in it as the
171   // RunSingleCommand does not like spaces
172   if(makeCommand.find(' ') != std::string::npos)
173     {
174     cmSystemTools::GetShortPath(makeCommand.c_str(), makeCommand);
175     }
176   // msbuild.exe CxxOnly.sln /t:Build /p:Configuration=Debug /target:ALL_BUILD
177   if(!targetName || strlen(targetName) == 0)
178     {
179     targetName = "ALL_BUILD";
180     }    
181   bool clean = false;
182   if ( targetName && strcmp(targetName, "clean") == 0 )
183     {
184     clean = true;
185     makeCommand += " ";
186     makeCommand += projectName;
187     makeCommand += ".sln ";
188     makeCommand += "/t:Clean ";
189     }
190   else
191     {
192     makeCommand += " ";
193     makeCommand += targetName;
194     makeCommand += ".vcxproj ";
195     }
196   makeCommand += "/p:Configuration=";
197   if(config && strlen(config))
198     {
199     makeCommand += config;
200     }
201   else
202     {
203     makeCommand += "Debug";
204     }
205   if ( additionalOptions )
206     {
207     makeCommand += " ";
208     makeCommand += additionalOptions;
209     }
210   return makeCommand;
211 }
212
213 //----------------------------------------------------------------------------
214 bool cmGlobalVisualStudio10Generator::Find64BitTools(cmMakefile* mf)
215 {
216   if(!this->PlatformToolset.empty())
217     {
218     return true;
219     }
220   // This edition does not come with 64-bit tools.  Look for them.
221   //
222   // TODO: Detect available tools?  x64\v100 exists but does not work?
223   // KHLM\\SOFTWARE\\Microsoft\\MSBuild\\ToolsVersions\\4.0;VCTargetsPath
224   // c:/Program Files (x86)/MSBuild/Microsoft.Cpp/v4.0/Platforms/
225   //   {Itanium,Win32,x64}/PlatformToolsets/{v100,v90,Windows7.1SDK}
226   std::string winSDK_7_1;
227   if(cmSystemTools::ReadRegistryValue(
228        "HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Microsoft SDKs\\"
229        "Windows\\v7.1;InstallationFolder", winSDK_7_1))
230     {
231     cmOStringStream m;
232     m << "Found Windows SDK v7.1: " << winSDK_7_1;
233     mf->DisplayStatus(m.str().c_str(), -1);
234     this->PlatformToolset = "Windows7.1SDK";
235     return true;
236     }
237   else
238     {
239     cmOStringStream e;
240     e << "Cannot enable 64-bit tools with Visual Studio 2010 Express.\n"
241       << "Install the Microsoft Windows SDK v7.1 to get 64-bit tools:\n"
242       << "  http://msdn.microsoft.com/en-us/windows/bb980924.aspx";
243     mf->IssueMessage(cmake::FATAL_ERROR, e.str().c_str());
244     cmSystemTools::SetFatalErrorOccured();
245     return false;
246     }
247 }
248
249 //----------------------------------------------------------------------------
250 std::string
251 cmGlobalVisualStudio10Generator
252 ::GenerateRuleFile(std::string const& output) const
253 {
254   // The VS 10 generator needs to create the .rule files on disk.
255   // Hide them away under the CMakeFiles directory.
256   std::string ruleDir = this->GetCMakeInstance()->GetHomeOutputDirectory();
257   ruleDir += cmake::GetCMakeFilesDirectory();
258   ruleDir += "/";
259   ruleDir += cmSystemTools::ComputeStringMD5(
260     cmSystemTools::GetFilenamePath(output).c_str());
261   std::string ruleFile = ruleDir + "/";
262   ruleFile += cmSystemTools::GetFilenameName(output);
263   ruleFile += ".rule";
264   return ruleFile;
265 }
266
267 //----------------------------------------------------------------------------
268 void cmGlobalVisualStudio10Generator::PathTooLong(
269   cmTarget* target, cmSourceFile* sf, std::string const& sfRel)
270 {
271   size_t len = (strlen(target->GetMakefile()->GetCurrentOutputDirectory()) +
272                 1 + sfRel.length());
273   if(len > this->LongestSource.Length)
274     {
275     this->LongestSource.Length = len;
276     this->LongestSource.Target = target;
277     this->LongestSource.SourceFile = sf;
278     this->LongestSource.SourceRel = sfRel;
279     }
280 }
281
282 //----------------------------------------------------------------------------
283 bool cmGlobalVisualStudio10Generator::UseFolderProperty()
284 {
285   return IsExpressEdition() ? false : cmGlobalGenerator::UseFolderProperty();
286 }