cmExportCommand.cxx 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  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::UTILITY) {
  133. this->SetError("given custom target \"" + currentTarget +
  134. "\" which may not be exported.");
  135. return false;
  136. }
  137. } else {
  138. std::ostringstream e;
  139. e << "given target \"" << currentTarget
  140. << "\" which is not built by this project.";
  141. this->SetError(e.str());
  142. return false;
  143. }
  144. targets.push_back(currentTarget);
  145. }
  146. if (this->Append.IsEnabled()) {
  147. if (cmExportBuildFileGenerator* ebfg =
  148. gg->GetExportedTargetsFile(fname)) {
  149. ebfg->AppendTargets(targets);
  150. return true;
  151. }
  152. }
  153. } else {
  154. this->SetError("EXPORT or TARGETS specifier missing.");
  155. return false;
  156. }
  157. // Setup export file generation.
  158. cmExportBuildFileGenerator* ebfg = nullptr;
  159. if (android) {
  160. ebfg = new cmExportBuildAndroidMKGenerator;
  161. } else {
  162. ebfg = new cmExportBuildFileGenerator;
  163. }
  164. ebfg->SetExportFile(fname.c_str());
  165. ebfg->SetNamespace(this->Namespace.GetCString());
  166. ebfg->SetAppendMode(this->Append.IsEnabled());
  167. if (this->ExportSet) {
  168. ebfg->SetExportSet(this->ExportSet);
  169. } else {
  170. ebfg->SetTargets(targets);
  171. }
  172. this->Makefile->AddExportBuildFileGenerator(ebfg);
  173. ebfg->SetExportOld(this->ExportOld.IsEnabled());
  174. // Compute the set of configurations exported.
  175. std::vector<std::string> configurationTypes;
  176. this->Makefile->GetConfigurations(configurationTypes);
  177. if (configurationTypes.empty()) {
  178. configurationTypes.emplace_back();
  179. }
  180. for (std::string const& ct : configurationTypes) {
  181. ebfg->AddConfiguration(ct);
  182. }
  183. if (this->ExportSet) {
  184. gg->AddBuildExportExportSet(ebfg);
  185. } else {
  186. gg->AddBuildExportSet(ebfg);
  187. }
  188. return true;
  189. }
  190. bool cmExportCommand::HandlePackage(std::vector<std::string> const& args)
  191. {
  192. // Parse PACKAGE mode arguments.
  193. enum Doing
  194. {
  195. DoingNone,
  196. DoingPackage
  197. };
  198. Doing doing = DoingPackage;
  199. std::string package;
  200. for (unsigned int i = 1; i < args.size(); ++i) {
  201. if (doing == DoingPackage) {
  202. package = args[i];
  203. doing = DoingNone;
  204. } else {
  205. std::ostringstream e;
  206. e << "PACKAGE given unknown argument: " << args[i];
  207. this->SetError(e.str());
  208. return false;
  209. }
  210. }
  211. // Verify the package name.
  212. if (package.empty()) {
  213. this->SetError("PACKAGE must be given a package name.");
  214. return false;
  215. }
  216. const char* packageExpr = "^[A-Za-z0-9_.-]+$";
  217. cmsys::RegularExpression packageRegex(packageExpr);
  218. if (!packageRegex.find(package.c_str())) {
  219. std::ostringstream e;
  220. e << "PACKAGE given invalid package name \"" << package << "\". "
  221. << "Package names must match \"" << packageExpr << "\".";
  222. this->SetError(e.str());
  223. return false;
  224. }
  225. // If the CMAKE_EXPORT_NO_PACKAGE_REGISTRY variable is set the command
  226. // export(PACKAGE) does nothing.
  227. if (this->Makefile->IsOn("CMAKE_EXPORT_NO_PACKAGE_REGISTRY")) {
  228. return true;
  229. }
  230. // We store the current build directory in the registry as a value
  231. // named by a hash of its own content. This is deterministic and is
  232. // unique with high probability.
  233. const char* outDir = this->Makefile->GetCurrentBinaryDirectory();
  234. std::string hash = cmSystemTools::ComputeStringMD5(outDir);
  235. #if defined(_WIN32) && !defined(__CYGWIN__)
  236. this->StorePackageRegistryWin(package, outDir, hash.c_str());
  237. #else
  238. this->StorePackageRegistryDir(package, outDir, hash.c_str());
  239. #endif
  240. return true;
  241. }
  242. #if defined(_WIN32) && !defined(__CYGWIN__)
  243. #include <windows.h>
  244. void cmExportCommand::ReportRegistryError(std::string const& msg,
  245. std::string const& key, long err)
  246. {
  247. std::ostringstream e;
  248. e << msg << "\n"
  249. << " HKEY_CURRENT_USER\\" << key << "\n";
  250. wchar_t winmsg[1024];
  251. if (FormatMessageW(
  252. FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, 0, err,
  253. MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), winmsg, 1024, 0) > 0) {
  254. e << "Windows reported:\n"
  255. << " " << cmsys::Encoding::ToNarrow(winmsg);
  256. }
  257. this->Makefile->IssueMessage(cmake::WARNING, e.str());
  258. }
  259. void cmExportCommand::StorePackageRegistryWin(std::string const& package,
  260. const char* content,
  261. const char* hash)
  262. {
  263. std::string key = "Software\\Kitware\\CMake\\Packages\\";
  264. key += package;
  265. HKEY hKey;
  266. LONG err =
  267. RegCreateKeyExW(HKEY_CURRENT_USER, cmsys::Encoding::ToWide(key).c_str(), 0,
  268. 0, REG_OPTION_NON_VOLATILE, KEY_SET_VALUE, 0, &hKey, 0);
  269. if (err != ERROR_SUCCESS) {
  270. this->ReportRegistryError("Cannot create/open registry key", key, err);
  271. return;
  272. }
  273. std::wstring wcontent = cmsys::Encoding::ToWide(content);
  274. err =
  275. RegSetValueExW(hKey, cmsys::Encoding::ToWide(hash).c_str(), 0, REG_SZ,
  276. (BYTE const*)wcontent.c_str(),
  277. static_cast<DWORD>(wcontent.size() + 1) * sizeof(wchar_t));
  278. RegCloseKey(hKey);
  279. if (err != ERROR_SUCCESS) {
  280. std::ostringstream msg;
  281. msg << "Cannot set registry value \"" << hash << "\" under key";
  282. this->ReportRegistryError(msg.str(), key, err);
  283. return;
  284. }
  285. }
  286. #else
  287. void cmExportCommand::StorePackageRegistryDir(std::string const& package,
  288. const char* content,
  289. const char* hash)
  290. {
  291. #if defined(__HAIKU__)
  292. char dir[B_PATH_NAME_LENGTH];
  293. if (find_directory(B_USER_SETTINGS_DIRECTORY, -1, false, dir, sizeof(dir)) !=
  294. B_OK) {
  295. return;
  296. }
  297. std::string fname = dir;
  298. fname += "/cmake/packages/";
  299. fname += package;
  300. #else
  301. std::string fname;
  302. if (!cmSystemTools::GetEnv("HOME", fname)) {
  303. return;
  304. }
  305. cmSystemTools::ConvertToUnixSlashes(fname);
  306. fname += "/.cmake/packages/";
  307. fname += package;
  308. #endif
  309. cmSystemTools::MakeDirectory(fname);
  310. fname += "/";
  311. fname += hash;
  312. if (!cmSystemTools::FileExists(fname)) {
  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