|
| 1 | +// Copyright (C) 2018-2025 Intel Corporation |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | +// |
| 4 | +#pragma once |
| 5 | + |
| 6 | +#include <string> |
| 7 | +#include <regex> |
| 8 | +#include <sstream> |
| 9 | +#include <stdexcept> |
| 10 | + |
| 11 | +namespace ov::util { |
| 12 | +struct OVVersion { |
| 13 | + int major = 0; |
| 14 | + int minor = 0; |
| 15 | + int patch = 0; |
| 16 | + int tweak = 0; |
| 17 | + int build = 0; |
| 18 | + |
| 19 | + OVVersion() = default; |
| 20 | + |
| 21 | + OVVersion(const std::string& version_str) { |
| 22 | + // Pattern: MAJOR.MINOR.PATCH[.TWEAK]-BUILD-... |
| 23 | + std::regex full_pattern(R"(^([0-9]+)\.([0-9]+)\.([0-9]+)(\.([0-9]+))?\-([0-9]+)\-.*)"); |
| 24 | + std::regex build_only_pattern(R"(^[0-9]+$)"); |
| 25 | + std::smatch match; |
| 26 | + |
| 27 | + if (std::regex_match(version_str, match, full_pattern)) { |
| 28 | + major = std::stoi(match[1].str()); |
| 29 | + minor = std::stoi(match[2].str()); |
| 30 | + patch = std::stoi(match[3].str()); |
| 31 | + |
| 32 | + // match[4] is the entire optional group (\.TWEAK), match[5] is TWEAK |
| 33 | + // the 4th version number is not used in OpenVINO, but used in OpenVINO extensions |
| 34 | + if (match[5].matched) { |
| 35 | + tweak = std::stoi(match[5].str()); |
| 36 | + } |
| 37 | + |
| 38 | + build = std::stoi(match[6].str()); |
| 39 | + } |
| 40 | + else if (std::regex_match(version_str, build_only_pattern)) { |
| 41 | + // Parse as just a build number |
| 42 | + build = std::stoi(version_str); |
| 43 | + } |
| 44 | + else { |
| 45 | + throw std::invalid_argument("Failed to parse version: " + version_str); |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + OVVersion(const char* version_str) : OVVersion(std::string(version_str)) {}; |
| 50 | + |
| 51 | + // Comparison operators |
| 52 | + bool operator==(const OVVersion& other) const { |
| 53 | + return major == other.major && |
| 54 | + minor == other.minor && |
| 55 | + patch == other.patch && |
| 56 | + tweak == other.tweak && |
| 57 | + build == other.build; |
| 58 | + } |
| 59 | + |
| 60 | + bool operator!=(const OVVersion& other) const { |
| 61 | + return !(*this == other); |
| 62 | + } |
| 63 | + |
| 64 | + bool operator<(const OVVersion& other) const { |
| 65 | + if (major != other.major) return major < other.major; |
| 66 | + if (minor != other.minor) return minor < other.minor; |
| 67 | + if (patch != other.patch) return patch < other.patch; |
| 68 | + if (tweak != other.tweak) return tweak < other.tweak; |
| 69 | + return build < other.build; |
| 70 | + } |
| 71 | + |
| 72 | + bool operator>(const OVVersion& other) const { |
| 73 | + return other < *this; |
| 74 | + } |
| 75 | + |
| 76 | + bool operator<=(const OVVersion& other) const { |
| 77 | + return !(other < *this); |
| 78 | + } |
| 79 | + |
| 80 | + bool operator>=(const OVVersion& other) const { |
| 81 | + return !(*this < other); |
| 82 | + } |
| 83 | +}; |
| 84 | + |
| 85 | +bool is_compiled_blob_compatible(const OVVersion& compiled_blob_version, const OVVersion& runtime_version) { |
| 86 | + if (compiled_blob_version > runtime_version) { |
| 87 | + return false; |
| 88 | + } |
| 89 | + |
| 90 | + // Major version must be equal |
| 91 | + if (compiled_blob_version.major != runtime_version.major) { |
| 92 | + return false; |
| 93 | + } |
| 94 | + |
| 95 | + // compiled blob minor version can be lower than runtime minor version up to 5 |
| 96 | + if (runtime_version.minor - compiled_blob_version.minor >= 5) { |
| 97 | + return false; |
| 98 | + } |
| 99 | + |
| 100 | + return true; |
| 101 | +} |
| 102 | +} // namespace ov::util |
0 commit comments