cmExportCommand.cxx 12 KB

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