-
-
Notifications
You must be signed in to change notification settings - Fork 329
Expand file tree
/
Copy pathbuild.rs
More file actions
108 lines (93 loc) · 3.71 KB
/
build.rs
File metadata and controls
108 lines (93 loc) · 3.71 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
105
106
107
108
// Custom build script to use pre-built llama.cpp libraries
use std::env;
use std::path::PathBuf;
/// Validates version consistency to prevent Issue #63 version mismatch problems
fn validate_version() {
// Get version from Cargo.toml
let version = env!("CARGO_PKG_VERSION");
// Validate version is not the default placeholder
if version == "0.1.0" {
panic!(
"ERROR: Version is set to default 0.1.0\n\
This suggests the package was not properly configured.\n\
Please ensure Cargo.toml has the correct version number.\n\
This prevents the version mismatch issue reported in Issue #63."
);
}
// Validate version is not empty
if version.is_empty() {
panic!("ERROR: CARGO_PKG_VERSION is empty. Check your build environment.");
}
// Validate semantic versioning format
let parts: Vec<&str> = version.split('.').collect();
if parts.len() < 3 {
panic!(
"ERROR: Version '{}' does not follow semantic versioning (major.minor.patch)\n\
Please use a valid version format like '1.4.2'",
version
);
}
// Validate each version component is numeric
for (i, part) in parts.iter().take(3).enumerate() {
if part.parse::<u32>().is_err() {
panic!(
"ERROR: Version component '{}' at position {} is not a valid number\n\
Version: {}",
part, i, version
);
}
}
// Set build-time version for verification
println!("cargo:rustc-env=SHIMMY_BUILD_VERSION={}", version);
// Rebuild if version-related files change
println!("cargo:rerun-if-changed=Cargo.toml");
println!("cargo:warning=Building shimmy version {}", version);
}
fn main() {
// Version validation - prevents Issue #63 version mismatch problems
validate_version();
println!("cargo:rerun-if-changed=libs/");
// Check if we should use pre-built libraries
if env::var("SHIMMY_USE_PREBUILT_LLAMA").is_ok() {
println!("cargo:warning=Using pre-built llama.cpp libraries");
let target = env::var("TARGET").unwrap();
let lib_dir = match target.as_str() {
"x86_64-pc-windows-msvc" => "libs/windows-x86_64",
"x86_64-apple-darwin" => "libs/macos-intel",
"aarch64-apple-darwin" => "libs/macos-arm64",
"x86_64-unknown-linux-gnu" => "libs/linux-x86_64",
"aarch64-unknown-linux-gnu" => "libs/linux-arm64",
_ => {
println!(
"cargo:warning=No pre-built library for target {}, falling back to compilation",
target
);
return;
}
};
// Check if the library exists
let lib_path = PathBuf::from(lib_dir).join(if target.contains("windows") {
"llama.lib"
} else {
"libllama.a"
});
if lib_path.exists() {
println!(
"cargo:warning=Found pre-built library: {}",
lib_path.display()
);
// Tell Cargo where to find the library
println!("cargo:rustc-link-search=native={}", lib_dir);
println!("cargo:rustc-link-lib=static=llama");
// Set environment variables to tell llama-cpp-sys-2 to skip building
println!("cargo:rustc-env=LLAMA_CPP_PREBUILT=1");
println!("cargo:rustc-env=LLAMA_CPP_LIB_DIR={}", lib_dir);
println!("cargo:rustc-env=LLAMA_CPP_SKIP_BUILD=1");
} else {
println!(
"cargo:warning=Pre-built library not found: {}, falling back to compilation",
lib_path.display()
);
}
}
}