cmExportCommand.cxx 11 KB

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