|
| 1 | +// mcpp.build.version_floor — "this needs at least that much of something". |
| 2 | +// |
| 3 | +// WHY THIS EXISTS |
| 4 | +// |
| 5 | +// Some facts about a machine bound what may be built for it, and the failure |
| 6 | +// when they are ignored arrives late. The case this was written for: a device |
| 7 | +// runtime must not be newer than the driver it runs against, and when it is, |
| 8 | +// the build compiles and links cleanly and then fails at the first allocation |
| 9 | +// with a message that names neither the toolkit nor the driver. |
| 10 | +// |
| 11 | +// Both numbers are knowable before anything is compiled. What mcpp must not do |
| 12 | +// is go and ask a vendor's tool for them — `tests/unit/test_runtime_contract` |
| 13 | +// refuses provider-specific probes in `src/`, and rightly: an engine that |
| 14 | +// learns to run one vendor's probe learns to run four. So the numbers arrive as |
| 15 | +// DECLARATIONS and this module compares them. |
| 16 | +// |
| 17 | +// WHAT THE DECLARATIONS LOOK LIKE |
| 18 | +// |
| 19 | +// A package that needs something states a floor, in `runtime.requirements`: |
| 20 | +// |
| 21 | +// [[runtime.requirements]] |
| 22 | +// kind = "version-floor" |
| 23 | +// value = "cuda.driver >= 12.0" |
| 24 | +// |
| 25 | +// A package that KNOWS a fact about this machine states it, in |
| 26 | +// `runtime.provides`, having established it at install time: |
| 27 | +// |
| 28 | +// provides = ["cuda.driver=12.4"] |
| 29 | +// |
| 30 | +// ⚠️ NO VENDOR NAME APPEARS IN THIS FILE, and that is the point rather than a |
| 31 | +// coincidence. `cuda.driver` is data flowing through: this module reads a name, |
| 32 | +// a relation and a version, and knows nothing about what any of them mean. A |
| 33 | +// second backend needs no change here. |
| 34 | +// |
| 35 | +// WHAT A VERSION IS |
| 36 | +// |
| 37 | +// A dot-separated sequence of integers, compared component by component, with a |
| 38 | +// missing component reading as zero — so `12` and `12.0` are the same version |
| 39 | +// and `12.4` is above both. Anything that is not that shape yields no version, |
| 40 | +// and an absent version on either side yields no claim: a check that cannot |
| 41 | +// reach an answer must not manufacture a refusal. |
| 42 | + |
| 43 | +export module mcpp.build.version_floor; |
| 44 | + |
| 45 | +import std; |
| 46 | + |
| 47 | +export namespace mcpp::build { |
| 48 | + |
| 49 | +// A parsed `<name> >= <version>` requirement. |
| 50 | +struct VersionFloor { |
| 51 | + std::string name; |
| 52 | + std::string version; |
| 53 | + bool valid() const { return !name.empty() && !version.empty(); } |
| 54 | +}; |
| 55 | + |
| 56 | +// A parsed `<name>=<version>` fact. |
| 57 | +struct VersionFact { |
| 58 | + std::string name; |
| 59 | + std::string version; |
| 60 | + bool valid() const { return !name.empty() && !version.empty(); } |
| 61 | +}; |
| 62 | + |
| 63 | +// Read `<name> >= <version>`. Whitespace around each part is ignored; any other |
| 64 | +// shape yields an invalid result rather than a guess. |
| 65 | +VersionFloor parse_version_floor(std::string_view text); |
| 66 | + |
| 67 | +// Read `<name>=<version>`. The separator is a bare `=` so the spelling matches |
| 68 | +// the capability strings a descriptor already writes. |
| 69 | +VersionFact parse_version_fact(std::string_view text); |
| 70 | + |
| 71 | +// Is `have` at or above `want`? Both are dot-separated integers; a missing |
| 72 | +// component reads as zero. |
| 73 | +// |
| 74 | +// Returns std::nullopt when either side is not a version, which callers report |
| 75 | +// as "no claim" rather than as a failure. |
| 76 | +std::optional<bool> version_at_least(std::string_view have, std::string_view want); |
| 77 | + |
| 78 | +} // namespace mcpp::build |
| 79 | + |
| 80 | +namespace mcpp::build { |
| 81 | + |
| 82 | +namespace { |
| 83 | + |
| 84 | +std::string_view trim(std::string_view s) { |
| 85 | + while (!s.empty() && (s.front() == ' ' || s.front() == '\t')) s.remove_prefix(1); |
| 86 | + while (!s.empty() && (s.back() == ' ' || s.back() == '\t')) s.remove_suffix(1); |
| 87 | + return s; |
| 88 | +} |
| 89 | + |
| 90 | +// The components of a dotted version, or an empty vector when the text is not |
| 91 | +// one. A trailing or leading dot makes it not one: `12.` is a typo, not `12`. |
| 92 | +std::optional<std::vector<long long>> components(std::string_view v) { |
| 93 | + v = trim(v); |
| 94 | + if (v.empty()) return std::nullopt; |
| 95 | + std::vector<long long> out; |
| 96 | + long long acc = 0; |
| 97 | + bool digits = false; |
| 98 | + for (char c : v) { |
| 99 | + if (c == '.') { |
| 100 | + if (!digits) return std::nullopt; |
| 101 | + out.push_back(acc); |
| 102 | + acc = 0; |
| 103 | + digits = false; |
| 104 | + continue; |
| 105 | + } |
| 106 | + if (!std::isdigit(static_cast<unsigned char>(c))) return std::nullopt; |
| 107 | + acc = acc * 10 + (c - '0'); |
| 108 | + digits = true; |
| 109 | + } |
| 110 | + if (!digits) return std::nullopt; |
| 111 | + out.push_back(acc); |
| 112 | + return out; |
| 113 | +} |
| 114 | + |
| 115 | +} // namespace |
| 116 | + |
| 117 | +VersionFloor parse_version_floor(std::string_view text) { |
| 118 | + VersionFloor f; |
| 119 | + auto at = text.find(">="); |
| 120 | + if (at == std::string_view::npos) return f; |
| 121 | + auto name = trim(text.substr(0, at)); |
| 122 | + auto ver = trim(text.substr(at + 2)); |
| 123 | + if (name.empty() || ver.empty()) return f; |
| 124 | + if (!components(ver)) return f; |
| 125 | + f.name = std::string(name); |
| 126 | + f.version = std::string(ver); |
| 127 | + return f; |
| 128 | +} |
| 129 | + |
| 130 | +VersionFact parse_version_fact(std::string_view text) { |
| 131 | + VersionFact f; |
| 132 | + auto at = text.find('='); |
| 133 | + if (at == std::string_view::npos) return f; |
| 134 | + // `>=` is a floor, not a fact; refusing it here keeps one spelling from |
| 135 | + // being read as the other when both live in string lists. |
| 136 | + if (at > 0 && text[at - 1] == '>') return f; |
| 137 | + auto name = trim(text.substr(0, at)); |
| 138 | + auto ver = trim(text.substr(at + 1)); |
| 139 | + if (name.empty() || ver.empty()) return f; |
| 140 | + if (!components(ver)) return f; |
| 141 | + f.name = std::string(name); |
| 142 | + f.version = std::string(ver); |
| 143 | + return f; |
| 144 | +} |
| 145 | + |
| 146 | +std::optional<bool> version_at_least(std::string_view have, std::string_view want) { |
| 147 | + auto h = components(have); |
| 148 | + auto w = components(want); |
| 149 | + if (!h || !w) return std::nullopt; |
| 150 | + const auto n = std::max(h->size(), w->size()); |
| 151 | + for (std::size_t i = 0; i < n; ++i) { |
| 152 | + const long long a = i < h->size() ? (*h)[i] : 0; |
| 153 | + const long long b = i < w->size() ? (*w)[i] : 0; |
| 154 | + if (a != b) return a > b; |
| 155 | + } |
| 156 | + return true; |
| 157 | +} |
| 158 | + |
| 159 | +} // namespace mcpp::build |
0 commit comments