cmExportCommand.cxx 10 KB

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