cmPackageInfoArguments.cxx 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. file LICENSE.rst or https://cmake.org/licensing for details. */
  3. #include "cmPackageInfoArguments.h"
  4. #include <utility>
  5. #include <cm/string_view>
  6. #include "cmExecutionStatus.h"
  7. #include "cmGeneratorExpression.h"
  8. #include "cmMakefile.h"
  9. #include "cmStateSnapshot.h"
  10. #include "cmStringAlgorithms.h"
  11. #include "cmSystemTools.h"
  12. #include "cmValue.h"
  13. template void cmPackageInfoArguments::Bind<void>(cmArgumentParser<void>&,
  14. cmPackageInfoArguments*);
  15. namespace {
  16. bool ArgWasSpecified(bool value)
  17. {
  18. return value;
  19. }
  20. bool ArgWasSpecified(std::string const& value)
  21. {
  22. return !value.empty();
  23. }
  24. bool ArgWasSpecified(std::vector<std::string> const& value)
  25. {
  26. return !value.empty();
  27. }
  28. } // anonymous namespace
  29. #define ENFORCE_REQUIRES(req, value, arg) \
  30. do { \
  31. if (ArgWasSpecified(value)) { \
  32. status.SetError(arg " requires " req "."); \
  33. return false; \
  34. } \
  35. } while (false)
  36. #define ENFORCE_EXCLUSIVE(arg1, value, arg2) \
  37. do { \
  38. if (ArgWasSpecified(value)) { \
  39. status.SetError(arg1 " and " arg2 " are mutually exclusive."); \
  40. return false; \
  41. } \
  42. } while (false)
  43. bool cmPackageInfoArguments::Check(cmExecutionStatus& status,
  44. bool enable) const
  45. {
  46. if (!enable) {
  47. // Check if any options were given.
  48. ENFORCE_REQUIRES("PACKAGE_INFO", this->LowerCase, "LOWER_CASE_FILE");
  49. ENFORCE_REQUIRES("PACKAGE_INFO", this->Appendix, "APPENDIX");
  50. ENFORCE_REQUIRES("PACKAGE_INFO", this->Version, "VERSION");
  51. ENFORCE_REQUIRES("PACKAGE_INFO", this->License, "LICENSE");
  52. ENFORCE_REQUIRES("PACKAGE_INFO", this->DefaultLicense, "DEFAULT_LICENSE");
  53. ENFORCE_REQUIRES("PACKAGE_INFO", this->Description, "DESCRIPTION");
  54. ENFORCE_REQUIRES("PACKAGE_INFO", this->Website, "HOMEPAGE_URL");
  55. ENFORCE_REQUIRES("PACKAGE_INFO", this->DefaultTargets, "DEFAULT_TARGETS");
  56. ENFORCE_REQUIRES("PACKAGE_INFO", this->DefaultConfigs,
  57. "DEFAULT_CONFIGURATIONS");
  58. ENFORCE_REQUIRES("PACKAGE_INFO", this->ProjectName, "PROJECT");
  59. ENFORCE_REQUIRES("PACKAGE_INFO", this->NoProjectDefaults,
  60. "NO_PROJECT_METADATA");
  61. }
  62. // Check for incompatible options.
  63. if (!this->Appendix.empty()) {
  64. ENFORCE_EXCLUSIVE("APPENDIX", this->Version, "VERSION");
  65. ENFORCE_EXCLUSIVE("APPENDIX", this->License, "LICENSE");
  66. ENFORCE_EXCLUSIVE("APPENDIX", this->Description, "DESCRIPTION");
  67. ENFORCE_EXCLUSIVE("APPENDIX", this->Website, "HOMEPAGE_URL");
  68. ENFORCE_EXCLUSIVE("APPENDIX", this->DefaultTargets, "DEFAULT_TARGETS");
  69. ENFORCE_EXCLUSIVE("APPENDIX", this->DefaultConfigs,
  70. "DEFAULT_CONFIGURATIONS");
  71. ENFORCE_EXCLUSIVE("APPENDIX", this->ProjectName, "PROJECT");
  72. }
  73. if (this->NoProjectDefaults) {
  74. ENFORCE_EXCLUSIVE("PROJECT", this->ProjectName, "NO_PROJECT_METADATA");
  75. }
  76. // Check for options that require other options.
  77. if (this->Version.empty()) {
  78. ENFORCE_REQUIRES("VERSION", this->VersionCompat, "COMPAT_VERSION");
  79. ENFORCE_REQUIRES("VERSION", this->VersionSchema, "VERSION_SCHEMA");
  80. }
  81. // Validate the package name.
  82. if (!this->PackageName.empty()) {
  83. if (!cmGeneratorExpression::IsValidTargetName(this->PackageName) ||
  84. this->PackageName.find(':') != std::string::npos) {
  85. status.SetError(
  86. cmStrCat(R"(PACKAGE_INFO given invalid package name ")"_s,
  87. this->PackageName, R"(".)"_s));
  88. return false;
  89. }
  90. }
  91. return true;
  92. }
  93. #undef ENFORCE_REQUIRES
  94. #undef ENFORCE_EXCLUSIVE
  95. bool cmPackageInfoArguments::SetMetadataFromProject(cmExecutionStatus& status)
  96. {
  97. // Determine what project to use for inherited metadata.
  98. if (!this->SetEffectiveProject(status)) {
  99. return false;
  100. }
  101. if (this->ProjectName.empty()) {
  102. // We are not inheriting from a project.
  103. return true;
  104. }
  105. cmMakefile& mf = status.GetMakefile();
  106. auto mapProjectValue = [&](std::string& arg, cm::string_view suffix) {
  107. cmValue const& projectValue =
  108. mf.GetDefinition(cmStrCat(this->ProjectName, '_', suffix));
  109. if (projectValue) {
  110. arg = *projectValue;
  111. return true;
  112. }
  113. return false;
  114. };
  115. if (this->Version.empty()) {
  116. if (mapProjectValue(this->Version, "VERSION"_s)) {
  117. mapProjectValue(this->VersionCompat, "COMPAT_VERSION"_s);
  118. }
  119. }
  120. if (this->License.empty()) {
  121. mapProjectValue(this->License, "SPDX_LICENSE"_s);
  122. }
  123. if (this->Description.empty()) {
  124. mapProjectValue(this->Description, "DESCRIPTION"_s);
  125. }
  126. if (this->Website.empty()) {
  127. mapProjectValue(this->Website, "HOMEPAGE_URL"_s);
  128. }
  129. return true;
  130. }
  131. bool cmPackageInfoArguments::SetEffectiveProject(cmExecutionStatus& status)
  132. {
  133. if (!this->Appendix.empty()) {
  134. // Appendices are not allowed to specify package metadata.
  135. return true;
  136. }
  137. if (this->NoProjectDefaults) {
  138. // User requested that metadata not be inherited.
  139. return true;
  140. }
  141. cmMakefile& mf = status.GetMakefile();
  142. if (!this->ProjectName.empty()) {
  143. // User specified a project; make sure it exists.
  144. if (!mf.GetStateSnapshot().CheckProjectName(this->ProjectName)) {
  145. status.SetError(cmStrCat(R"(PROJECT given unknown project name ")"_s,
  146. this->ProjectName, R"(".)"_s));
  147. return false;
  148. }
  149. } else {
  150. // No project was specified; check if the package name is also a project.
  151. std::string project = mf.GetStateSnapshot().GetProjectName();
  152. if (this->PackageName == project) {
  153. this->ProjectName = std::move(project);
  154. }
  155. }
  156. return true;
  157. }
  158. std::string cmPackageInfoArguments::GetNamespace() const
  159. {
  160. return cmStrCat(this->PackageName, "::"_s);
  161. }
  162. std::string cmPackageInfoArguments::GetPackageDirName() const
  163. {
  164. if (this->LowerCase) {
  165. return cmSystemTools::LowerCase(this->PackageName);
  166. }
  167. return this->PackageName;
  168. }
  169. std::string cmPackageInfoArguments::GetPackageFileName() const
  170. {
  171. std::string const pkgNameOnDisk = this->GetPackageDirName();
  172. if (!this->Appendix.empty()) {
  173. return cmStrCat(pkgNameOnDisk, '-', this->Appendix, ".cps"_s);
  174. }
  175. return cmStrCat(pkgNameOnDisk, ".cps"_s);
  176. }