cmExportCommand.cxx 12 KB

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