-
Notifications
You must be signed in to change notification settings - Fork 415
Expand file tree
/
Copy pathbackend_utils.h
More file actions
104 lines (80 loc) · 5.12 KB
/
Copy pathbackend_utils.h
File metadata and controls
104 lines (80 loc) · 5.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#pragma once
#include <string>
#include <functional>
#include <filesystem>
namespace fs = std::filesystem;
// Forward declare DownloadProgressCallback to avoid heavy model_manager.h include
namespace lemon {
struct DownloadProgress;
using DownloadProgressCallback = std::function<bool(const DownloadProgress&)>;
}
namespace lemon::backends {
struct InstallParams {
std::string repo; // GitHub "org/repo"
std::string filename; // Release asset filename
};
struct BackendSpec {
const std::string recipe;
const std::string binary;
using InstallParamsFn = InstallParams(*)(const std::string& backend, const std::string& version);
InstallParamsFn install_params_fn; // nullptr for FLM (special installer)
BackendSpec(std::string r, std::string b, InstallParamsFn fn = nullptr)
: recipe(std::move(r)), binary(std::move(b)), install_params_fn(fn) {}
std::string log_name() const { return recipe + " Server"; };
};
// Return the backend spec for recipes that use the standard BackendSpec flow.
// Returns nullptr for recipes that require custom handling (e.g., flm) or unknown recipes.
const BackendSpec* try_get_spec_for_recipe(const std::string& recipe);
/**
* Utility functions for backend management
*/
class BackendUtils {
public:
/**
* Extract ZIP files (Windows/Linux built-in tools)
* @param zip_path Path to the ZIP file
* @param dest_dir Destination directory to extract to
* @return true if extraction was successful, false otherwise
*/
static bool extract_zip(const std::string& zip_path, const std::string& dest_dir, const std::string& backend_name);
/**
* Extract tar.gz files (Linux/macOS/Windows)
* @param tarball_path Path to the tar.gz file
* @param dest_dir Destination directory to extract to
* @return true if extraction was successful, false otherwise
*/
static bool extract_tarball(const std::string& tarball_path, const std::string& dest_dir, const std::string& backend_name);
/**
* Detect if archive is tar or zip
* @param tarball_path Path to the archive file
* @param dest_dir Destination directory to extract to
* @return true if extraction was successful, false otherwise
*/
static bool extract_archive(const std::string& archive_path, const std::string& dest_dir, const std::string& backend_name);
/** Download and install the specified version of the backend from github.
* If progress_cb is provided, it receives download progress events instead of console output. */
static void install_from_github(const BackendSpec& spec, const std::string& expected_version, const std::string& repo, const std::string& filename, const std::string& backend, DownloadProgressCallback progress_cb = nullptr);
/** Get the latest version number for the given recipe/backend */
static std::string get_backend_version(const std::string& recipe, const std::string& backend);
/** Check if ROCm libraries are installed system-wide (Linux only) */
static bool is_rocm_installed_system_wide();
/** Get TheRock installation directory for a specific architecture and version */
static std::string get_therock_install_dir(const std::string& arch, const std::string& version);
/** Download and install TheRock ROCm tarball for the specified architecture (Linux only) */
static void install_therock(const std::string& arch, const std::string& version);
/** Clean up old TheRock versions, keeping only the specified version */
static void cleanup_old_therock_versions(const std::string& current_version);
/** Get TheRock lib directory path if available, or empty string if not needed */
static std::string get_therock_lib_path(const std::string& rocm_arch);
/** Get the path to the backend's binary. Gives precedence to the path set through environment variables, if set. Throws if not found. */
static std::string get_backend_binary_path(const BackendSpec& spec, const std::string& backend);
/** Get the path where the version indicator is installed. Does not check existence. */
static std::string get_installed_version_file(const BackendSpec& spec, const std::string& backend);
/** Get the install directory for the backend. Generally only used internally by BackendUtils */
static std::string get_install_directory(const std::string& dir_name, const std::string& backend);
/** Find the executable in the installation directory. Generally only used internally by BackendUtils */
static std::string find_executable_in_install_dir(const std::string& install_dir, const std::string& binary_name);
/** Checks the environment for a variable following the scheme LEMONADE_BACKEND_VARIANT_BIN and return its value, if available. Generally only used internally by BackendUtils */
static std::string find_external_backend_binary(const std::string& recipe, const std::string& backend);
};
} // namespace lemon::backends