cmExportCommand.cxx 10 KB

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