-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathbuild.rs
More file actions
174 lines (152 loc) · 6.1 KB
/
build.rs
File metadata and controls
174 lines (152 loc) · 6.1 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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
use std::env;
use std::fs;
use std::io;
use std::path::PathBuf;
fn main() {
println!("cargo:rerun-if-changed=versions.env");
// Load versions from versions.env
let versions_env = fs::read_to_string("versions.env").expect("Failed to read versions.env");
let mut pg_version = String::new();
let mut pgvector_version = String::new();
let mut pgvector_tag = String::new();
let mut pgvector_repo = String::new();
for line in versions_env.lines() {
let line = line.trim();
if line.is_empty() || line.starts_with('#') {
continue;
}
if let Some((key, value)) = line.split_once('=') {
match key.trim() {
"PG_VERSION" => pg_version = value.trim().to_string(),
"PGVECTOR_VERSION" => pgvector_version = value.trim().to_string(),
"PGVECTOR_COMPILED_TAG" => pgvector_tag = value.trim().to_string(),
"PGVECTOR_COMPILED_REPO" => pgvector_repo = value.trim().to_string(),
_ => {}
}
}
}
println!("cargo:rustc-env=PG_VERSION={}", pg_version);
println!("cargo:rustc-env=PGVECTOR_VERSION={}", pgvector_version);
println!("cargo:rustc-env=PGVECTOR_COMPILED_TAG={}", pgvector_tag);
println!("cargo:rustc-env=PGVECTOR_COMPILED_REPO={}", pgvector_repo);
let out_dir = PathBuf::from(env::var("OUT_DIR").unwrap());
// Bundle PostgreSQL and pgvector
bundle_postgresql(&pg_version, &out_dir);
bundle_pgvector(&pg_version, &pgvector_tag, &pgvector_repo, &out_dir);
}
fn bundle_postgresql(pg_version: &str, out_dir: &PathBuf) {
let target = env::var("TARGET").unwrap();
// Map Rust target to theseus-rs binary name
let pg_target = match target.as_str() {
"aarch64-apple-darwin" => "aarch64-apple-darwin",
"x86_64-apple-darwin" => "x86_64-apple-darwin",
"x86_64-unknown-linux-gnu" => "x86_64-unknown-linux-gnu",
"x86_64-unknown-linux-musl" => "x86_64-unknown-linux-musl",
"aarch64-unknown-linux-gnu" => "aarch64-unknown-linux-gnu",
"aarch64-unknown-linux-musl" => "aarch64-unknown-linux-musl",
"x86_64-pc-windows-msvc" => "x86_64-pc-windows-msvc",
_ => {
eprintln!(
"Warning: Unknown target {}, PostgreSQL will not be bundled",
target
);
let marker = out_dir.join("postgresql_bundle.tar.gz");
fs::write(&marker, b"").expect("Failed to create empty bundle marker");
println!(
"cargo:rustc-env=POSTGRESQL_BUNDLE_PATH={}",
marker.display()
);
println!("cargo:rustc-env=POSTGRESQL_BUNDLED=false");
return;
}
};
let ext = if target.contains("windows") {
"zip"
} else {
"tar.gz"
};
let filename = format!("postgresql-{}-{}.{}", pg_version, pg_target, ext);
let url = format!(
"https://github.com/theseus-rs/postgresql-binaries/releases/download/{}/{}",
pg_version, filename
);
let bundle_path = out_dir.join(&filename);
// Download if not already cached
if !bundle_path.exists() {
eprintln!(
"Downloading PostgreSQL {} for {}...",
pg_version, pg_target
);
download_file(&url, &bundle_path).expect("Failed to download PostgreSQL bundle");
eprintln!("Downloaded to {}", bundle_path.display());
} else {
eprintln!("Using cached PostgreSQL bundle: {}", bundle_path.display());
}
println!(
"cargo:rustc-env=POSTGRESQL_BUNDLE_PATH={}",
bundle_path.display()
);
}
fn bundle_pgvector(pg_version: &str, pgvector_tag: &str, pgvector_repo: &str, out_dir: &PathBuf) {
let target = env::var("TARGET").unwrap();
// Map Rust target to pgvector platform name
let pgvector_platform = match target.as_str() {
"aarch64-apple-darwin" => "aarch64-apple-darwin",
"x86_64-apple-darwin" => "x86_64-apple-darwin",
"x86_64-unknown-linux-gnu" => "x86_64-unknown-linux-gnu",
"x86_64-unknown-linux-musl" => "x86_64-unknown-linux-gnu", // musl uses gnu pgvector
"aarch64-unknown-linux-gnu" => "aarch64-unknown-linux-gnu",
"aarch64-unknown-linux-musl" => "aarch64-unknown-linux-gnu", // musl uses gnu pgvector
"x86_64-pc-windows-msvc" => "x86_64-pc-windows-msvc",
_ => {
eprintln!(
"Warning: Unknown target {}, pgvector will not be bundled",
target
);
let marker = out_dir.join("pgvector_bundle.tar.gz");
fs::write(&marker, b"").expect("Failed to create empty pgvector marker");
println!(
"cargo:rustc-env=PGVECTOR_BUNDLE_PATH={}",
marker.display()
);
return;
}
};
// Get PG major version (e.g., "18" from "18.1.0")
let pg_major = pg_version.split('.').next().unwrap_or("18");
let filename = format!("pgvector-{}-pg{}.tar.gz", pgvector_platform, pg_major);
let url = format!(
"https://github.com/{}/releases/download/{}/{}",
pgvector_repo, pgvector_tag, filename
);
let bundle_path = out_dir.join(&filename);
// Download if not already cached
if !bundle_path.exists() {
eprintln!(
"Downloading pgvector for {} (PG {})...",
pgvector_platform, pg_major
);
download_file(&url, &bundle_path).expect("Failed to download pgvector bundle");
eprintln!("Downloaded to {}", bundle_path.display());
} else {
eprintln!("Using cached pgvector bundle: {}", bundle_path.display());
}
println!(
"cargo:rustc-env=PGVECTOR_BUNDLE_PATH={}",
bundle_path.display()
);
}
fn download_file(url: &str, dest: &PathBuf) -> io::Result<()> {
// Use curl for downloading (available on all CI platforms)
let status = std::process::Command::new("curl")
.args(["-fsSL", url, "-o"])
.arg(dest)
.status()?;
if !status.success() {
return Err(io::Error::new(
io::ErrorKind::Other,
format!("Failed to download {}", url),
));
}
Ok(())
}