cmPackageInfoArguments.cxx 5.4 KB

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