cmExportCommand.cxx 10 KB

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