cmExportCommand.cxx 11 KB

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