cmInstallCommandArguments.cxx 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  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 "cmInstallCommandArguments.h"
  4. #include <algorithm>
  5. #include <functional>
  6. #include <utility>
  7. #include <cm/string_view>
  8. #include <cmext/string_view>
  9. #include "cmCMakePath.h"
  10. #include "cmGeneratorExpression.h"
  11. #include "cmMakefile.h"
  12. #include "cmMessageType.h"
  13. #include "cmPolicies.h"
  14. #include "cmRange.h"
  15. #include "cmStringAlgorithms.h"
  16. #include "cmSystemTools.h"
  17. // Table of valid permissions.
  18. const char* cmInstallCommandArguments::PermissionsTable[] = {
  19. "OWNER_READ", "OWNER_WRITE", "OWNER_EXECUTE", "GROUP_READ",
  20. "GROUP_WRITE", "GROUP_EXECUTE", "WORLD_READ", "WORLD_WRITE",
  21. "WORLD_EXECUTE", "SETUID", "SETGID", nullptr
  22. };
  23. const std::string cmInstallCommandArguments::EmptyString;
  24. cmInstallCommandArguments::cmInstallCommandArguments(
  25. std::string defaultComponent, cmMakefile& makefile)
  26. : DefaultComponentName(std::move(defaultComponent))
  27. {
  28. std::function<ArgumentParser::Continue(cm::string_view)> normalizeDest;
  29. switch (makefile.GetPolicyStatus(cmPolicies::CMP0177)) {
  30. case cmPolicies::OLD:
  31. normalizeDest = [this](cm::string_view arg) -> ArgumentParser::Continue {
  32. this->Destination = std::string(arg.begin(), arg.end());
  33. return ArgumentParser::Continue::Yes;
  34. };
  35. break;
  36. case cmPolicies::WARN:
  37. normalizeDest =
  38. [this, &makefile](cm::string_view arg) -> ArgumentParser::Continue {
  39. this->Destination = std::string(arg.begin(), arg.end());
  40. // We can't be certain if a warning is appropriate if there are any
  41. // generator expressions
  42. if (cmGeneratorExpression::Find(arg) == cm::string_view::npos &&
  43. arg != cmCMakePath(arg).Normal().String()) {
  44. makefile.IssueMessage(
  45. MessageType::AUTHOR_WARNING,
  46. cmPolicies::GetPolicyWarning(cmPolicies::CMP0177));
  47. }
  48. return ArgumentParser::Continue::Yes;
  49. };
  50. break;
  51. case cmPolicies::NEW:
  52. normalizeDest = [this](cm::string_view arg) -> ArgumentParser::Continue {
  53. if (cmGeneratorExpression::Find(arg) == cm::string_view::npos) {
  54. this->Destination = cmCMakePath(arg).Normal().String();
  55. } else {
  56. this->Destination =
  57. cmStrCat("$<PATH:CMAKE_PATH,NORMALIZE,", arg, '>');
  58. }
  59. return ArgumentParser::Continue::Yes;
  60. };
  61. break;
  62. case cmPolicies::REQUIRED_ALWAYS:
  63. case cmPolicies::REQUIRED_IF_USED:
  64. // We should never get here, only OLD, WARN, and NEW are used
  65. makefile.IssueMessage(
  66. MessageType::FATAL_ERROR,
  67. cmPolicies::GetRequiredPolicyError(cmPolicies::CMP0177));
  68. }
  69. this->Bind("DESTINATION"_s, normalizeDest);
  70. this->Bind("COMPONENT"_s, this->Component);
  71. this->Bind("NAMELINK_COMPONENT"_s, this->NamelinkComponent);
  72. this->Bind("EXCLUDE_FROM_ALL"_s, this->ExcludeFromAll);
  73. this->Bind("RENAME"_s, this->Rename);
  74. this->Bind("PERMISSIONS"_s, this->Permissions);
  75. this->Bind("CONFIGURATIONS"_s, this->Configurations);
  76. this->Bind("OPTIONAL"_s, this->Optional);
  77. this->Bind("NAMELINK_ONLY"_s, this->NamelinkOnly);
  78. this->Bind("NAMELINK_SKIP"_s, this->NamelinkSkip);
  79. this->Bind("TYPE"_s, this->Type);
  80. }
  81. const std::string& cmInstallCommandArguments::GetDestination() const
  82. {
  83. if (!this->DestinationString.empty()) {
  84. return this->DestinationString;
  85. }
  86. if (this->GenericArguments) {
  87. return this->GenericArguments->GetDestination();
  88. }
  89. return EmptyString;
  90. }
  91. const std::string& cmInstallCommandArguments::GetComponent() const
  92. {
  93. if (!this->Component.empty()) {
  94. return this->Component;
  95. }
  96. if (this->GenericArguments) {
  97. return this->GenericArguments->GetComponent();
  98. }
  99. if (!this->DefaultComponentName.empty()) {
  100. return this->DefaultComponentName;
  101. }
  102. static std::string unspecifiedComponent = "Unspecified";
  103. return unspecifiedComponent;
  104. }
  105. const std::string& cmInstallCommandArguments::GetNamelinkComponent() const
  106. {
  107. if (!this->NamelinkComponent.empty()) {
  108. return this->NamelinkComponent;
  109. }
  110. return this->GetComponent();
  111. }
  112. const std::string& cmInstallCommandArguments::GetRename() const
  113. {
  114. if (!this->Rename.empty()) {
  115. return this->Rename;
  116. }
  117. if (this->GenericArguments) {
  118. return this->GenericArguments->GetRename();
  119. }
  120. return EmptyString;
  121. }
  122. const std::string& cmInstallCommandArguments::GetPermissions() const
  123. {
  124. if (!this->PermissionsString.empty()) {
  125. return this->PermissionsString;
  126. }
  127. if (this->GenericArguments) {
  128. return this->GenericArguments->GetPermissions();
  129. }
  130. return EmptyString;
  131. }
  132. bool cmInstallCommandArguments::GetOptional() const
  133. {
  134. if (this->Optional) {
  135. return true;
  136. }
  137. if (this->GenericArguments) {
  138. return this->GenericArguments->GetOptional();
  139. }
  140. return false;
  141. }
  142. bool cmInstallCommandArguments::GetExcludeFromAll() const
  143. {
  144. if (this->ExcludeFromAll) {
  145. return true;
  146. }
  147. if (this->GenericArguments) {
  148. return this->GenericArguments->GetExcludeFromAll();
  149. }
  150. return false;
  151. }
  152. bool cmInstallCommandArguments::GetNamelinkOnly() const
  153. {
  154. if (this->NamelinkOnly) {
  155. return true;
  156. }
  157. if (this->GenericArguments) {
  158. return this->GenericArguments->GetNamelinkOnly();
  159. }
  160. return false;
  161. }
  162. bool cmInstallCommandArguments::GetNamelinkSkip() const
  163. {
  164. if (this->NamelinkSkip) {
  165. return true;
  166. }
  167. if (this->GenericArguments) {
  168. return this->GenericArguments->GetNamelinkSkip();
  169. }
  170. return false;
  171. }
  172. bool cmInstallCommandArguments::HasNamelinkComponent() const
  173. {
  174. if (!this->NamelinkComponent.empty()) {
  175. return true;
  176. }
  177. if (this->GenericArguments) {
  178. return this->GenericArguments->HasNamelinkComponent();
  179. }
  180. return false;
  181. }
  182. const std::string& cmInstallCommandArguments::GetType() const
  183. {
  184. return this->Type;
  185. }
  186. const std::string& cmInstallCommandArguments::GetDefaultComponent() const
  187. {
  188. return this->DefaultComponentName;
  189. }
  190. const std::vector<std::string>& cmInstallCommandArguments::GetConfigurations()
  191. const
  192. {
  193. if (!this->Configurations.empty()) {
  194. return this->Configurations;
  195. }
  196. if (this->GenericArguments) {
  197. return this->GenericArguments->GetConfigurations();
  198. }
  199. return this->Configurations;
  200. }
  201. bool cmInstallCommandArguments::Finalize()
  202. {
  203. if (!this->CheckPermissions()) {
  204. return false;
  205. }
  206. this->DestinationString = this->Destination;
  207. cmSystemTools::ConvertToUnixSlashes(this->DestinationString);
  208. return true;
  209. }
  210. bool cmInstallCommandArguments::CheckPermissions()
  211. {
  212. this->PermissionsString.clear();
  213. return std::all_of(this->Permissions.begin(), this->Permissions.end(),
  214. [this](std::string const& perm) -> bool {
  215. return cmInstallCommandArguments::CheckPermissions(
  216. perm, this->PermissionsString);
  217. });
  218. }
  219. bool cmInstallCommandArguments::CheckPermissions(
  220. const std::string& onePermission, std::string& permissions)
  221. {
  222. // Check the permission against the table.
  223. for (const char** valid = cmInstallCommandArguments::PermissionsTable;
  224. *valid; ++valid) {
  225. if (onePermission == *valid) {
  226. // This is a valid permission.
  227. permissions += " ";
  228. permissions += onePermission;
  229. return true;
  230. }
  231. }
  232. // This is not a valid permission.
  233. return false;
  234. }
  235. cmInstallCommandIncludesArgument::cmInstallCommandIncludesArgument() = default;
  236. const std::vector<std::string>&
  237. cmInstallCommandIncludesArgument::GetIncludeDirs() const
  238. {
  239. return this->IncludeDirs;
  240. }
  241. void cmInstallCommandIncludesArgument::Parse(
  242. const std::vector<std::string>* args, std::vector<std::string>*)
  243. {
  244. if (args->empty()) {
  245. return;
  246. }
  247. for (std::string dir : cmMakeRange(*args).advance(1)) {
  248. cmSystemTools::ConvertToUnixSlashes(dir);
  249. this->IncludeDirs.push_back(std::move(dir));
  250. }
  251. }
  252. cmInstallCommandFileSetArguments::cmInstallCommandFileSetArguments(
  253. std::string defaultComponent, cmMakefile& makefile)
  254. : cmInstallCommandArguments(std::move(defaultComponent), makefile)
  255. {
  256. this->Bind("FILE_SET"_s, this->FileSet);
  257. }
  258. void cmInstallCommandFileSetArguments::Parse(
  259. std::vector<std::string> args, std::vector<std::string>* unconsumedArgs)
  260. {
  261. args.insert(args.begin(), "FILE_SET");
  262. this->cmInstallCommandArguments::Parse(args, unconsumedArgs);
  263. }