cmExportCommand.cxx 11 KB

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