Coverage Report

Created: 2026-07-14 07:09

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/CMake/Source/cmPackageInfoReader.h
Line
Count
Source
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
5
#include "cmConfigure.h" // IWYU pragma: keep
6
7
#include <map>
8
#include <memory>
9
#include <string>
10
#include <vector>
11
12
#include <cm/optional>
13
#include <cm/string_view>
14
15
#include <cm3p/json/value.h>
16
17
class cmExecutionStatus;
18
class cmMakefile;
19
class cmTarget;
20
21
namespace cm {
22
enum class ImportedTargetScope;
23
enum class TargetType;
24
} // namespace cm
25
26
struct cmPackageRequirement
27
{
28
  std::string Name;
29
  std::string Version;
30
  std::vector<std::string> Components;
31
  std::vector<std::string> Hints;
32
};
33
34
/** \class cmPackageInfoReader
35
 * \brief Read and parse CPS files.
36
 *
37
 * This class encapsulates the functionality to read package configuration
38
 * files which use the Common Package Specification, and provides utilities to
39
 * translate the declarations therein into imported targets.
40
 */
41
class cmPackageInfoReader
42
{
43
public:
44
  static std::unique_ptr<cmPackageInfoReader> Read(
45
    cmMakefile* makefile, std::string const& path,
46
    cmPackageInfoReader const* parent = nullptr);
47
48
  std::string GetName() const;
49
  cm::optional<std::string> GetVersion() const;
50
  cm::optional<std::string> GetCompatVersion() const;
51
52
  // NOTE: The eventual intent is for CPS to support multiple version schemas,
53
  // and in particular, we expect to want to support "simple", "custom", "rpm",
54
  // "dpkg" and "pep440". Additionally, we desire to be able to parse each of
55
  // these to the maximum extent possible; in particular, we want to be able
56
  // to decompose "simple" and "pep440" versions into components represented
57
  // as numeric types rather than strings, which is not possible with the "rpm"
58
  // and "dpkg" schemas. Therefore, we require different data structures to
59
  // represent different version schemas.
60
61
  struct Pep440Version
62
  {
63
    // NOTE: This structure is currently incomplete as we only support the
64
    // "simple" schema at this time.
65
    bool Simple; // "simple" can be represented as a subset of "pep440"
66
    std::vector<unsigned> ReleaseComponents;
67
    cm::optional<std::string> LocalLabel;
68
  };
69
70
  // FIXME: Return a sum type (e.g. {cm,std}::variant) of possible versions
71
  // when we support more than just the "simple" (and possibly "pep440")
72
  // schema(s).
73
  /// If the package uses the 'simple' version scheme, parse the provided
74
  /// version string as a numeric tuple and optional trailing string.  Returns
75
  /// a disengaged optional for other schemes or if no version is specified.
76
  cm::optional<Pep440Version> ParseVersion(
77
    cm::optional<std::string> const& version) const;
78
79
  std::vector<cmPackageRequirement> GetRequirements() const;
80
  std::vector<std::string> GetComponentNames() const;
81
82
  /// Create targets for components specified in the CPS file.
83
  bool ImportTargets(cmMakefile* makefile, cmExecutionStatus& status,
84
                     cm::ImportedTargetScope scope);
85
86
  /// Add configuration-specific properties for targets.
87
  bool ImportTargetConfigurations(cmMakefile* makefile,
88
                                  cmExecutionStatus& status) const;
89
90
private:
91
0
  cmPackageInfoReader() = default;
92
93
  cmTarget* AddLibraryComponent(cmMakefile* makefile, cm::TargetType type,
94
                                std::string const& name,
95
                                Json::Value const& data,
96
                                std::string const& package,
97
                                cm::ImportedTargetScope scope) const;
98
99
  void AddTargetConfiguration(cmTarget* target,
100
                              cm::string_view configuration) const;
101
102
  void SetTargetProperties(cmMakefile* makefile, cmTarget* target,
103
                           Json::Value const& data, std::string const& package,
104
                           cm::string_view configuration) const;
105
  void SetImportProperty(cmMakefile* makefile, cmTarget* target,
106
                         cm::string_view property,
107
                         cm::string_view configuration,
108
                         Json::Value const& object,
109
                         std::string const& attribute) const;
110
  void SetMetaProperty(cmMakefile* makefile, cmTarget* target,
111
                       std::string const& property, Json::Value const& object,
112
                       std::string const& attribute,
113
                       std::string const& defaultValue = {}) const;
114
  void ReadCxxModulesMetadata(cmMakefile* makefile, cmTarget* target,
115
                              cm::string_view configuration,
116
                              Json::Value const& object) const;
117
118
  std::string ResolvePath(std::string path) const;
119
120
  std::string Path;
121
  Json::Value Data;
122
  std::string Prefix;
123
124
  std::map<std::string, cmTarget*> ComponentTargets;
125
  std::vector<std::string> DefaultConfigurations;
126
  std::string DefaultLicense;
127
};