|
| 1 | +/** |
| 2 | + * |
| 3 | + * @file ArtifactCache.hpp |
| 4 | + * @author Gaspard Kirira |
| 5 | + * |
| 6 | + * Copyright 2026, Gaspard Kirira. All rights reserved. |
| 7 | + * https://github.com/vixcpp/vix |
| 8 | + * Use of this source code is governed by a MIT license |
| 9 | + * that can be found in the License file. |
| 10 | + * |
| 11 | + * Vix.cpp |
| 12 | + * |
| 13 | + * Global compiled artifact cache |
| 14 | + * |
| 15 | + * This module provides a reusable cache layer for compiled packages. |
| 16 | + * Inspired by Zig/Mojo/Nix strategies, it allows: |
| 17 | + * |
| 18 | + * - Reuse of compiled dependencies across projects |
| 19 | + * - Avoid recompilation of unchanged packages |
| 20 | + * - Deterministic build reuse based on fingerprint |
| 21 | + * |
| 22 | + * Cache layout: |
| 23 | + * |
| 24 | + * ~/.vix/cache/build/ |
| 25 | + * <target>/ |
| 26 | + * <compiler>/ |
| 27 | + * <build-type>/ |
| 28 | + * <package>@<version>/ |
| 29 | + * <fingerprint>/ |
| 30 | + * include/ |
| 31 | + * lib/ |
| 32 | + * share/ |
| 33 | + * manifest.json |
| 34 | + * |
| 35 | + */ |
| 36 | + |
| 37 | +#ifndef VIX_CLI_ARTIFACT_CACHE_HPP |
| 38 | +#define VIX_CLI_ARTIFACT_CACHE_HPP |
| 39 | + |
| 40 | +#include <filesystem> |
| 41 | +#include <optional> |
| 42 | +#include <string> |
| 43 | + |
| 44 | +namespace vix::cli::cache |
| 45 | +{ |
| 46 | + namespace fs = std::filesystem; |
| 47 | + |
| 48 | + /** |
| 49 | + * @brief Description of a compiled artifact |
| 50 | + * |
| 51 | + * This structure identifies one reusable compiled artifact stored |
| 52 | + * in the global Vix cache. |
| 53 | + */ |
| 54 | + struct Artifact |
| 55 | + { |
| 56 | + std::string package; ///< Package name (ex: softadastra-core) |
| 57 | + std::string version; ///< Version (ex: 1.3.0) |
| 58 | + std::string target; ///< Target triple (ex: x86_64-linux-gnu) |
| 59 | + std::string compiler; ///< Compiler identity/version |
| 60 | + std::string buildType; ///< Build type (Debug / Release) |
| 61 | + std::string fingerprint; ///< Unique fingerprint of the build configuration |
| 62 | + |
| 63 | + fs::path root; ///< Artifact root directory |
| 64 | + fs::path include; ///< Include directory |
| 65 | + fs::path lib; ///< Library directory |
| 66 | + }; |
| 67 | + |
| 68 | + /** |
| 69 | + * @brief Global artifact cache manager |
| 70 | + * |
| 71 | + * This class is responsible for: |
| 72 | + * - locating the global cache root |
| 73 | + * - computing deterministic artifact paths |
| 74 | + * - checking cache existence |
| 75 | + * - preparing artifact layout |
| 76 | + * - writing and reading manifest metadata |
| 77 | + */ |
| 78 | + class ArtifactCache |
| 79 | + { |
| 80 | + public: |
| 81 | + /** |
| 82 | + * @brief Return the global artifact cache root directory |
| 83 | + * |
| 84 | + * Usually: |
| 85 | + * ~/.vix/cache/build |
| 86 | + * |
| 87 | + * @return Cache root path |
| 88 | + */ |
| 89 | + static fs::path cache_root(); |
| 90 | + |
| 91 | + /** |
| 92 | + * @brief Compute the on-disk path of an artifact |
| 93 | + * |
| 94 | + * The path is derived from: |
| 95 | + * target / compiler / build-type / package@version / fingerprint |
| 96 | + * |
| 97 | + * @param a Artifact descriptor |
| 98 | + * @return Artifact root path |
| 99 | + */ |
| 100 | + static fs::path artifact_path(const Artifact &a); |
| 101 | + |
| 102 | + /** |
| 103 | + * @brief Check whether an artifact exists in the cache |
| 104 | + * |
| 105 | + * A valid artifact is expected to contain: |
| 106 | + * - include/ |
| 107 | + * - lib/ |
| 108 | + * - manifest.json |
| 109 | + * |
| 110 | + * @param a Artifact descriptor |
| 111 | + * @return true if the artifact is available |
| 112 | + */ |
| 113 | + static bool exists(const Artifact &a); |
| 114 | + |
| 115 | + /** |
| 116 | + * @brief Resolve an artifact from cache |
| 117 | + * |
| 118 | + * If the artifact exists, this returns a copy with resolved |
| 119 | + * root/include/lib paths filled in. |
| 120 | + * |
| 121 | + * @param a Artifact descriptor |
| 122 | + * @return Resolved artifact, or std::nullopt if missing |
| 123 | + */ |
| 124 | + static std::optional<Artifact> resolve(const Artifact &a); |
| 125 | + |
| 126 | + /** |
| 127 | + * @brief Ensure the directory layout for an artifact exists |
| 128 | + * |
| 129 | + * The layout includes: |
| 130 | + * - root/ |
| 131 | + * - include/ |
| 132 | + * - lib/ |
| 133 | + * - share/ |
| 134 | + * |
| 135 | + * @param a Artifact descriptor |
| 136 | + * @return true on success |
| 137 | + */ |
| 138 | + static bool ensure_layout(const Artifact &a); |
| 139 | + |
| 140 | + /** |
| 141 | + * @brief Write the manifest metadata for an artifact |
| 142 | + * |
| 143 | + * This also ensures the layout exists before writing. |
| 144 | + * |
| 145 | + * @param a Artifact descriptor |
| 146 | + * @return true on success |
| 147 | + */ |
| 148 | + static bool write_manifest(const Artifact &a); |
| 149 | + |
| 150 | + /** |
| 151 | + * @brief Read an artifact manifest from disk |
| 152 | + * |
| 153 | + * @param artifactRoot Root directory of the artifact |
| 154 | + * @return Parsed artifact metadata, or std::nullopt if invalid |
| 155 | + */ |
| 156 | + static std::optional<Artifact> read_manifest(const fs::path &artifactRoot); |
| 157 | + }; |
| 158 | + |
| 159 | +} // namespace vix::cli::cache |
| 160 | + |
| 161 | +#endif |
0 commit comments