Coverage Report

Created: 2026-02-09 06:05

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