cmExportCommand.cxx 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  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 if(target->GetType() == cmTarget::OBJECT_LIBRARY)
  112. {
  113. cmOStringStream e;
  114. e << "given OBJECT library \"" << *currentTarget
  115. << "\" which may not be exported.";
  116. this->SetError(e.str().c_str());
  117. return false;
  118. }
  119. else
  120. {
  121. cmOStringStream e;
  122. e << "given target \"" << *currentTarget
  123. << "\" which is not an executable or library.";
  124. this->SetError(e.str().c_str());
  125. return false;
  126. }
  127. }
  128. else
  129. {
  130. cmOStringStream e;
  131. e << "given target \"" << *currentTarget
  132. << "\" which is not built by this project.";
  133. this->SetError(e.str().c_str());
  134. return false;
  135. }
  136. }
  137. // Setup export file generation.
  138. cmExportBuildFileGenerator ebfg;
  139. ebfg.SetExportFile(fname.c_str());
  140. ebfg.SetNamespace(this->Namespace.GetCString());
  141. ebfg.SetAppendMode(this->Append.IsEnabled());
  142. ebfg.SetExports(&targets);
  143. ebfg.SetCommand(this);
  144. // Compute the set of configurations exported.
  145. std::vector<std::string> configurationTypes;
  146. this->Makefile->GetConfigurations(configurationTypes);
  147. if(!configurationTypes.empty())
  148. {
  149. for(std::vector<std::string>::const_iterator
  150. ci = configurationTypes.begin();
  151. ci != configurationTypes.end(); ++ci)
  152. {
  153. ebfg.AddConfiguration(ci->c_str());
  154. }
  155. }
  156. else
  157. {
  158. ebfg.AddConfiguration("");
  159. }
  160. // Generate the import file.
  161. if(!ebfg.GenerateImportFile() && this->ErrorMessage.empty())
  162. {
  163. this->SetError("could not write export file.");
  164. return false;
  165. }
  166. // Report generated error message if any.
  167. if(!this->ErrorMessage.empty())
  168. {
  169. this->SetError(this->ErrorMessage.c_str());
  170. return false;
  171. }
  172. return true;
  173. }
  174. //----------------------------------------------------------------------------
  175. bool cmExportCommand::HandlePackage(std::vector<std::string> const& args)
  176. {
  177. // Parse PACKAGE mode arguments.
  178. enum Doing { DoingNone, DoingPackage };
  179. Doing doing = DoingPackage;
  180. std::string package;
  181. for(unsigned int i=1; i < args.size(); ++i)
  182. {
  183. if(doing == DoingPackage)
  184. {
  185. package = args[i];
  186. doing = DoingNone;
  187. }
  188. else
  189. {
  190. cmOStringStream e;
  191. e << "PACKAGE given unknown argumsnt: " << args[i];
  192. this->SetError(e.str().c_str());
  193. return false;
  194. }
  195. }
  196. // Verify the package name.
  197. if(package.empty())
  198. {
  199. this->SetError("PACKAGE must be given a package name.");
  200. return false;
  201. }
  202. const char* packageExpr = "^[A-Za-z0-9_.-]+$";
  203. cmsys::RegularExpression packageRegex(packageExpr);
  204. if(!packageRegex.find(package.c_str()))
  205. {
  206. cmOStringStream e;
  207. e << "PACKAGE given invalid package name \"" << package << "\". "
  208. << "Package names must match \"" << packageExpr << "\".";
  209. this->SetError(e.str().c_str());
  210. return false;
  211. }
  212. // We store the current build directory in the registry as a value
  213. // named by a hash of its own content. This is deterministic and is
  214. // unique with high probability.
  215. const char* outDir = this->Makefile->GetCurrentOutputDirectory();
  216. std::string hash = cmSystemTools::ComputeStringMD5(outDir);
  217. #if defined(_WIN32) && !defined(__CYGWIN__)
  218. this->StorePackageRegistryWin(package, outDir, hash.c_str());
  219. #else
  220. this->StorePackageRegistryDir(package, outDir, hash.c_str());
  221. #endif
  222. return true;
  223. }
  224. #if defined(_WIN32) && !defined(__CYGWIN__)
  225. # include <windows.h>
  226. # undef GetCurrentDirectory
  227. //----------------------------------------------------------------------------
  228. void cmExportCommand::ReportRegistryError(std::string const& msg,
  229. std::string const& key,
  230. long err)
  231. {
  232. cmOStringStream e;
  233. e << msg << "\n"
  234. << " HKEY_CURRENT_USER\\" << key << "\n";
  235. char winmsg[1024];
  236. if(FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM |
  237. FORMAT_MESSAGE_IGNORE_INSERTS, 0, err,
  238. MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
  239. winmsg, 1024, 0) > 0)
  240. {
  241. e << "Windows reported:\n"
  242. << " " << winmsg;
  243. }
  244. this->Makefile->IssueMessage(cmake::WARNING, e.str());
  245. }
  246. //----------------------------------------------------------------------------
  247. void cmExportCommand::StorePackageRegistryWin(std::string const& package,
  248. const char* content,
  249. const char* hash)
  250. {
  251. std::string key = "Software\\Kitware\\CMake\\Packages\\";
  252. key += package;
  253. HKEY hKey;
  254. LONG err = RegCreateKeyEx(HKEY_CURRENT_USER,
  255. key.c_str(), 0, 0, REG_OPTION_NON_VOLATILE,
  256. KEY_SET_VALUE, 0, &hKey, 0);
  257. if(err != ERROR_SUCCESS)
  258. {
  259. this->ReportRegistryError(
  260. "Cannot create/open registry key", key, err);
  261. return;
  262. }
  263. err = RegSetValueEx(hKey, hash, 0, REG_SZ, (BYTE const*)content,
  264. static_cast<DWORD>(strlen(content)+1));
  265. RegCloseKey(hKey);
  266. if(err != ERROR_SUCCESS)
  267. {
  268. cmOStringStream msg;
  269. msg << "Cannot set registry value \"" << hash << "\" under key";
  270. this->ReportRegistryError(msg.str(), key, err);
  271. return;
  272. }
  273. }
  274. #else
  275. //----------------------------------------------------------------------------
  276. void cmExportCommand::StorePackageRegistryDir(std::string const& package,
  277. const char* content,
  278. const char* hash)
  279. {
  280. #if defined(__HAIKU__)
  281. BPath dir;
  282. if (find_directory(B_USER_SETTINGS_DIRECTORY, &dir) != B_OK)
  283. {
  284. return;
  285. }
  286. dir.Append("cmake/packages");
  287. dir.Append(package.c_str());
  288. std::string fname = dir.Path();
  289. #else
  290. const char* home = cmSystemTools::GetEnv("HOME");
  291. if(!home)
  292. {
  293. return;
  294. }
  295. std::string fname = home;
  296. cmSystemTools::ConvertToUnixSlashes(fname);
  297. fname += "/.cmake/packages/";
  298. fname += package;
  299. #endif
  300. cmSystemTools::MakeDirectory(fname.c_str());
  301. fname += "/";
  302. fname += hash;
  303. if(!cmSystemTools::FileExists(fname.c_str()))
  304. {
  305. cmGeneratedFileStream entry(fname.c_str(), true);
  306. if(entry)
  307. {
  308. entry << content << "\n";
  309. }
  310. else
  311. {
  312. cmOStringStream e;
  313. e << "Cannot create package registry file:\n"
  314. << " " << fname << "\n"
  315. << cmSystemTools::GetLastSystemError() << "\n";
  316. this->Makefile->IssueMessage(cmake::WARNING, e.str());
  317. }
  318. }
  319. }
  320. #endif