cmExportCommand.cxx 11 KB

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