Imported Upstream version 2.8.9
[platform/upstream/cmake.git] / Source / cmExportCommand.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 "cmExportCommand.h"
13 #include "cmGlobalGenerator.h"
14 #include "cmLocalGenerator.h"
15 #include "cmGeneratedFileStream.h"
16 #include "cmake.h"
17
18 #include <cmsys/RegularExpression.hxx>
19
20 #include "cmExportBuildFileGenerator.h"
21
22 #if defined(__HAIKU__)
23 #include <StorageKit.h>
24 #endif
25
26 cmExportCommand::cmExportCommand()
27 :cmCommand()
28 ,ArgumentGroup()
29 ,Targets(&Helper, "TARGETS")
30 ,Append(&Helper, "APPEND", &ArgumentGroup)
31 ,Namespace(&Helper, "NAMESPACE", &ArgumentGroup)
32 ,Filename(&Helper, "FILE", &ArgumentGroup)
33 {
34   // at first TARGETS
35   this->Targets.Follows(0);
36   // and after that the other options in any order
37   this->ArgumentGroup.Follows(&this->Targets);
38 }
39
40
41 // cmExportCommand
42 bool cmExportCommand
43 ::InitialPass(std::vector<std::string> const& args, cmExecutionStatus &)
44 {
45   if(args.size() < 2 )
46     {
47     this->SetError("called with too few arguments");
48     return false;
49     }
50
51   if(args[0] == "PACKAGE")
52     {
53     return this->HandlePackage(args);
54     }
55
56   std::vector<std::string> unknownArgs;
57   this->Helper.Parse(&args, &unknownArgs);
58
59   if (!unknownArgs.empty())
60     {
61     this->SetError("Unknown arguments.");
62     return false;
63     }
64
65   if (this->Targets.WasFound() == false)
66     {
67     this->SetError("TARGETS option missing.");
68     return false;
69     }
70
71   if(!this->Filename.WasFound())
72     {
73     this->SetError("FILE <filename> option missing.");
74     return false;
75     }
76
77   // Make sure the file has a .cmake extension.
78   if(cmSystemTools::GetFilenameLastExtension(this->Filename.GetCString())
79      != ".cmake")
80     {
81     cmOStringStream e;
82     e << "FILE option given filename \"" << this->Filename.GetString()
83       << "\" which does not have an extension of \".cmake\".\n";
84     this->SetError(e.str().c_str());
85     return false;
86     }
87
88   // Get the file to write.
89   std::string fname = this->Filename.GetString();
90   if(cmSystemTools::FileIsFullPath(fname.c_str()))
91     {
92     if(!this->Makefile->CanIWriteThisFile(fname.c_str()))
93       {
94       cmOStringStream e;
95       e << "FILE option given filename \"" << fname
96         << "\" which is in the source tree.\n";
97       this->SetError(e.str().c_str());
98       return false;
99       }
100     }
101   else
102     {
103     // Interpret relative paths with respect to the current build dir.
104     fname = this->Makefile->GetCurrentOutputDirectory();
105     fname += "/";
106     fname += this->Filename.GetString();
107     }
108
109   // Collect the targets to be exported.
110   std::vector<cmTarget*> targets;
111   for(std::vector<std::string>::const_iterator
112       currentTarget = this->Targets.GetVector().begin();
113       currentTarget != this->Targets.GetVector().end();
114       ++currentTarget)
115     {
116     if(cmTarget* target =
117        this->Makefile->GetLocalGenerator()->
118        GetGlobalGenerator()->FindTarget(0, currentTarget->c_str()))
119       {
120       if((target->GetType() == cmTarget::EXECUTABLE) ||
121          (target->GetType() == cmTarget::STATIC_LIBRARY) ||
122          (target->GetType() == cmTarget::SHARED_LIBRARY) ||
123          (target->GetType() == cmTarget::MODULE_LIBRARY))
124         {
125         targets.push_back(target);
126         }
127       else if(target->GetType() == cmTarget::OBJECT_LIBRARY)
128         {
129         cmOStringStream e;
130         e << "given OBJECT library \"" << *currentTarget
131           << "\" which may not be exported.";
132         this->SetError(e.str().c_str());
133         return false;
134         }
135       else
136         {
137         cmOStringStream e;
138         e << "given target \"" << *currentTarget
139           << "\" which is not an executable or library.";
140         this->SetError(e.str().c_str());
141         return false;
142         }
143       }
144     else
145       {
146       cmOStringStream e;
147       e << "given target \"" << *currentTarget
148         << "\" which is not built by this project.";
149       this->SetError(e.str().c_str());
150       return false;
151       }
152     }
153
154   // Setup export file generation.
155   cmExportBuildFileGenerator ebfg;
156   ebfg.SetExportFile(fname.c_str());
157   ebfg.SetNamespace(this->Namespace.GetCString());
158   ebfg.SetAppendMode(this->Append.IsEnabled());
159   ebfg.SetExports(&targets);
160   ebfg.SetCommand(this);
161
162   // Compute the set of configurations exported.
163   std::vector<std::string> configurationTypes;
164   this->Makefile->GetConfigurations(configurationTypes);
165   if(!configurationTypes.empty())
166     {
167     for(std::vector<std::string>::const_iterator
168           ci = configurationTypes.begin();
169         ci != configurationTypes.end(); ++ci)
170       {
171       ebfg.AddConfiguration(ci->c_str());
172       }
173     }
174   else
175     {
176     ebfg.AddConfiguration("");
177     }
178
179   // Generate the import file.
180   if(!ebfg.GenerateImportFile() && this->ErrorMessage.empty())
181     {
182     this->SetError("could not write export file.");
183     return false;
184     }
185
186   // Report generated error message if any.
187   if(!this->ErrorMessage.empty())
188     {
189     this->SetError(this->ErrorMessage.c_str());
190     return false;
191     }
192
193   return true;
194 }
195
196 //----------------------------------------------------------------------------
197 bool cmExportCommand::HandlePackage(std::vector<std::string> const& args)
198 {
199   // Parse PACKAGE mode arguments.
200   enum Doing { DoingNone, DoingPackage };
201   Doing doing = DoingPackage;
202   std::string package;
203   for(unsigned int i=1; i < args.size(); ++i)
204     {
205     if(doing == DoingPackage)
206       {
207       package = args[i];
208       doing = DoingNone;
209       }
210     else
211       {
212       cmOStringStream e;
213       e << "PACKAGE given unknown argumsnt: " << args[i];
214       this->SetError(e.str().c_str());
215       return false;
216       }
217     }
218
219   // Verify the package name.
220   if(package.empty())
221     {
222     this->SetError("PACKAGE must be given a package name.");
223     return false;
224     }
225   const char* packageExpr = "^[A-Za-z0-9_.-]+$";
226   cmsys::RegularExpression packageRegex(packageExpr);
227   if(!packageRegex.find(package.c_str()))
228     {
229     cmOStringStream e;
230     e << "PACKAGE given invalid package name \"" << package << "\".  "
231       << "Package names must match \"" << packageExpr << "\".";
232     this->SetError(e.str().c_str());
233     return false;
234     }
235
236   // We store the current build directory in the registry as a value
237   // named by a hash of its own content.  This is deterministic and is
238   // unique with high probability.
239   const char* outDir = this->Makefile->GetCurrentOutputDirectory();
240   std::string hash = cmSystemTools::ComputeStringMD5(outDir);
241 #if defined(_WIN32) && !defined(__CYGWIN__)
242   this->StorePackageRegistryWin(package, outDir, hash.c_str());
243 #else
244   this->StorePackageRegistryDir(package, outDir, hash.c_str());
245 #endif
246
247   return true;
248 }
249
250 #if defined(_WIN32) && !defined(__CYGWIN__)
251 # include <windows.h>
252 # undef GetCurrentDirectory
253 //----------------------------------------------------------------------------
254 void cmExportCommand::ReportRegistryError(std::string const& msg,
255                                           std::string const& key,
256                                           long err)
257 {
258   cmOStringStream e;
259   e << msg << "\n"
260     << "  HKEY_CURRENT_USER\\" << key << "\n";
261   char winmsg[1024];
262   if(FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM |
263                    FORMAT_MESSAGE_IGNORE_INSERTS, 0, err,
264                    MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
265                    winmsg, 1024, 0) > 0)
266     {
267     e << "Windows reported:\n"
268       << "  " << winmsg;
269     }
270   this->Makefile->IssueMessage(cmake::WARNING, e.str());
271 }
272
273 //----------------------------------------------------------------------------
274 void cmExportCommand::StorePackageRegistryWin(std::string const& package,
275                                               const char* content,
276                                               const char* hash)
277 {
278   std::string key = "Software\\Kitware\\CMake\\Packages\\";
279   key += package;
280   HKEY hKey;
281   LONG err = RegCreateKeyEx(HKEY_CURRENT_USER,
282                             key.c_str(), 0, 0, REG_OPTION_NON_VOLATILE,
283                             KEY_SET_VALUE, 0, &hKey, 0);
284   if(err != ERROR_SUCCESS)
285     {
286     this->ReportRegistryError(
287       "Cannot create/open registry key", key, err);
288     return;
289     }
290   err = RegSetValueEx(hKey, hash, 0, REG_SZ, (BYTE const*)content,
291                       static_cast<DWORD>(strlen(content)+1));
292   RegCloseKey(hKey);
293   if(err != ERROR_SUCCESS)
294     {
295     cmOStringStream msg;
296     msg << "Cannot set registry value \"" << hash << "\" under key";
297     this->ReportRegistryError(msg.str(), key, err);
298     return;
299     }
300 }
301 #else
302 //----------------------------------------------------------------------------
303 void cmExportCommand::StorePackageRegistryDir(std::string const& package,
304                                               const char* content,
305                                               const char* hash)
306 {
307 #if defined(__HAIKU__)
308   BPath dir;
309   if (find_directory(B_USER_SETTINGS_DIRECTORY, &dir) != B_OK)
310     {
311     return;
312     }
313   dir.Append("cmake/packages");
314   dir.Append(package.c_str());
315   std::string fname = dir.Path();
316 #else
317   const char* home = cmSystemTools::GetEnv("HOME");
318   if(!home)
319     {
320     return;
321     }
322   std::string fname = home;
323   cmSystemTools::ConvertToUnixSlashes(fname);
324   fname += "/.cmake/packages/";
325   fname += package;
326 #endif
327   cmSystemTools::MakeDirectory(fname.c_str());
328   fname += "/";
329   fname += hash;
330   if(!cmSystemTools::FileExists(fname.c_str()))
331     {
332     cmGeneratedFileStream entry(fname.c_str(), true);
333     if(entry)
334       {
335       entry << content << "\n";
336       }
337     else
338       {
339       cmOStringStream e;
340       e << "Cannot create package registry file:\n"
341         << "  " << fname << "\n"
342         << cmSystemTools::GetLastSystemError() << "\n";
343       this->Makefile->IssueMessage(cmake::WARNING, e.str());
344       }
345     }
346 }
347 #endif