cmExportCommand.cxx 9.8 KB

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