cmPackageInfoReader.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. file LICENSE.rst or https://cmake.org/licensing for details. */
  3. #pragma once
  4. #include "cmConfigure.h" // IWYU pragma: keep
  5. #include <map>
  6. #include <memory>
  7. #include <string>
  8. #include <vector>
  9. #include <cm/optional>
  10. #include <cm/string_view>
  11. #include <cm3p/json/value.h>
  12. #include "cmStateTypes.h"
  13. class cmExecutionStatus;
  14. class cmMakefile;
  15. class cmTarget;
  16. struct cmPackageRequirement
  17. {
  18. std::string Name;
  19. std::string Version;
  20. std::vector<std::string> Components;
  21. std::vector<std::string> Hints;
  22. };
  23. /** \class cmPackageInfoReader
  24. * \brief Read and parse CPS files.
  25. *
  26. * This class encapsulates the functionality to read package configuration
  27. * files which use the Common Package Specification, and provides utilities to
  28. * translate the declarations therein into imported targets.
  29. */
  30. class cmPackageInfoReader
  31. {
  32. public:
  33. static std::unique_ptr<cmPackageInfoReader> Read(
  34. std::string const& path, cmPackageInfoReader const* parent = nullptr);
  35. std::string GetName() const;
  36. cm::optional<std::string> GetVersion() const;
  37. cm::optional<std::string> GetCompatVersion() const;
  38. // NOTE: The eventual intent is for CPS to support multiple version schemas,
  39. // and in particular, we expect to want to support "simple", "custom", "rpm",
  40. // "dpkg" and "pep440". Additionally, we desire to be able to parse each of
  41. // these to the maximum extent possible; in particular, we want to be able
  42. // to decompose "simple" and "pep440" versions into components represented
  43. // as numeric types rather than strings, which is not possible with the "rpm"
  44. // and "dpkg" schemas. Therefore, we require different data structures to
  45. // represent different version schemas.
  46. struct Pep440Version
  47. {
  48. // NOTE: This structure is currently incomplete as we only support the
  49. // "simple" schema at this time.
  50. bool Simple; // "simple" can be represented as a subset of "pep440"
  51. std::vector<unsigned> ReleaseComponents;
  52. cm::optional<std::string> LocalLabel;
  53. };
  54. // FIXME: Return a sum type (e.g. {cm,std}::variant) of possible versions
  55. // when we support more than just the "simple" (and possibly "pep440")
  56. // schema(s).
  57. /// If the package uses the 'simple' version scheme, parse the provided
  58. /// version string as a numeric tuple and optional trailing string. Returns
  59. /// a disengaged optional for other schemes or if no version is specified.
  60. cm::optional<Pep440Version> ParseVersion(
  61. cm::optional<std::string> const& version) const;
  62. std::vector<cmPackageRequirement> GetRequirements() const;
  63. std::vector<std::string> GetComponentNames() const;
  64. /// Create targets for components specified in the CPS file.
  65. bool ImportTargets(cmMakefile* makefile, cmExecutionStatus& status);
  66. /// Add configuration-specific properties for targets.
  67. bool ImportTargetConfigurations(cmMakefile* makefile,
  68. cmExecutionStatus& status) const;
  69. private:
  70. cmPackageInfoReader() = default;
  71. cmTarget* AddLibraryComponent(cmMakefile* makefile,
  72. cmStateEnums::TargetType type,
  73. std::string const& name,
  74. Json::Value const& data,
  75. std::string const& package) const;
  76. void AddTargetConfiguration(cmTarget* target,
  77. cm::string_view configuration) const;
  78. void SetTargetProperties(cmMakefile* makefile, cmTarget* target,
  79. Json::Value const& data, std::string const& package,
  80. cm::string_view configuration) const;
  81. void SetImportProperty(cmTarget* target, cm::string_view property,
  82. cm::string_view configuration,
  83. Json::Value const& value) const;
  84. void SetMetaProperty(cmTarget* target, cm::string_view property,
  85. Json::Value const& value,
  86. std::string const& defaultValue = {}) const;
  87. std::string ResolvePath(std::string path) const;
  88. std::string Path;
  89. Json::Value Data;
  90. std::string Prefix;
  91. std::map<std::string, cmTarget*> ComponentTargets;
  92. std::vector<std::string> DefaultConfigurations;
  93. std::string DefaultLicense;
  94. };