cmExportCommand.cxx 9.6 KB

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