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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
|
use crate::cfg::config::Config;
use std::{fs::create_dir_all, path::Path, process::Command};
use num_cpus;
use super::archive::ArchiveOperations;
use super::types::{Build, BuildSystems, Package};
pub trait BuildOperations {
fn execute_build(&self, build_meta: &Build) -> Result<(), std::io::Error>;
fn build(&mut self) -> Result<bool, std::io::Error>;
}
#[allow(dead_code)]
impl BuildOperations for Package {
/// Execute the build script for the package.
///
/// This function takes the `Build` meta information as an argument and
/// executes the build script accordingly. It also handles the different
/// build systems supported (Make, CMake, Meson, Cargo).
///
/// # Errors
///
/// Returns an error if the build command fails.
fn execute_build(&self, build_meta: &Build) -> Result<(), std::io::Error> {
let config = Config::parse().map_err(|e| std::io::Error::other(e.to_string()))?;
let build_dir =
Path::new(&config.paths.cache_dir).join(format!("{}-{}", self.name, self.version));
if !build_dir.exists() {
return Err(std::io::Error::other(format!(
"Build directory not found: {}",
build_dir.display()
)));
}
let mut cmd_envs: Vec<(String, String)> = Vec::new();
if let Some(ref env_vars) = build_meta.env {
for env_line in env_vars.lines() {
if let Some((key, value)) = env_line.split_once('=') {
cmd_envs.push((key.trim().to_string(), value.trim().to_string()));
}
}
}
if let Some(ref script) = build_meta.script {
log::info!("Executing custom build script: {}", script);
Self::validate_custom_script(script)?;
let mut cmd = if script.starts_with("./") || script.starts_with('/') {
let script_path = build_dir.join(script);
if !script_path.exists() {
return Err(std::io::Error::other(format!(
"Custom script file not found: {}",
script_path.display()
)));
}
let mut inner_cmd = Command::new("/bin/sh");
inner_cmd.arg("-c");
inner_cmd.arg(script_path.to_str().unwrap());
inner_cmd
} else {
let mut inner_cmd = Command::new("/bin/sh");
inner_cmd.arg("-c");
inner_cmd.arg(script);
inner_cmd
};
cmd.current_dir(&build_dir);
for (key, value) in &cmd_envs {
cmd.env(key, value);
}
let output = cmd
.output()
.map_err(|e| std::io::Error::other(format!("Custom build script failed: {}", e)))?;
if !output.status.success() {
let stderr = String::from_utf8_lossy(&output.stderr);
let stdout = String::from_utf8_lossy(&output.stdout);
return Err(std::io::Error::other(format!(
"Custom build script failed:\nStdout: {}\nStderr: {}",
stdout, stderr
)));
}
log::info!(
"Custom build script completed successfully for package: {}",
self.name
);
return Ok(());
}
// No custom script, proceed with build system
match build_meta.build_system {
BuildSystems::Make => {
// Check for Makefile
let makefile_path = build_dir.join("Makefile");
if !makefile_path.exists() {
return Err(std::io::Error::other(format!(
"Makefile not found: {}",
makefile_path.display()
)));
}
let mut cmd = Command::new("make");
cmd.current_dir(&build_dir);
cmd.arg("all");
for (key, value) in &cmd_envs {
cmd.env(key, value);
}
log::info!("Running Make build: {:?}", cmd);
let output = cmd
.output()
.map_err(|e| std::io::Error::other(format!("Make build failed: {}", e)))?;
if !output.status.success() {
let stderr = String::from_utf8_lossy(&output.stderr);
let stdout = String::from_utf8_lossy(&output.stdout);
return Err(std::io::Error::other(format!(
"Make build failed:\nStdout: {}\nStderr: {}",
stdout, stderr
)));
}
}
BuildSystems::CMake => {
let cmake_file = build_dir.join("CMakeLists.txt");
if !cmake_file.exists() {
return Err(std::io::Error::other(format!(
"CMakeLists.txt not found: {}",
cmake_file.display()
)));
}
let build_dir_build = build_dir.join("build");
create_dir_all(&build_dir_build)?;
let mut config_cmd = Command::new("cmake");
config_cmd
.arg("-S")
.arg(&build_dir)
.arg("-B")
.arg(&build_dir_build)
.current_dir(&build_dir);
for (key, value) in &cmd_envs {
config_cmd.env(key, value);
}
log::info!("Running CMake configuration: {:?}", config_cmd);
let config_output = config_cmd
.output()
.map_err(|e| std::io::Error::other(format!("CMake config failed: {}", e)))?;
if !config_output.status.success() {
let stderr = String::from_utf8_lossy(&config_output.stderr);
return Err(std::io::Error::other(format!(
"CMake config failed:\n{}",
stderr
)));
}
let mut build_cmd = Command::new("make");
build_cmd.current_dir(&build_dir_build);
build_cmd.arg("-j").arg(num_cpus::get().to_string()); // Parallel build
for (key, value) in &cmd_envs {
build_cmd.env(key, value);
}
log::info!("Running CMake build: {:?}", build_cmd);
let build_output = build_cmd
.output()
.map_err(|e| std::io::Error::other(format!("CMake build failed: {}", e)))?;
if !build_output.status.success() {
let stderr = String::from_utf8_lossy(&build_output.stderr);
let stdout = String::from_utf8_lossy(&build_output.stdout);
return Err(std::io::Error::other(format!(
"CMake build failed:\nStdout: {}\nStderr: {}",
stdout, stderr
)));
}
}
BuildSystems::Meson => {
let meson_file = build_dir.join("meson.build");
if !meson_file.exists() {
return Err(std::io::Error::other(format!(
"meson.build not found: {}",
meson_file.display()
)));
}
let build_dir_build = build_dir.join("build");
create_dir_all(&build_dir_build)?;
let mut config_cmd = Command::new("meson");
config_cmd
.arg("setup")
.arg(&build_dir_build)
.current_dir(&build_dir);
for (key, value) in &cmd_envs {
config_cmd.env(key, value);
}
log::info!("Running Meson configuration: {:?}", config_cmd);
let config_output = config_cmd
.output()
.map_err(|e| std::io::Error::other(format!("Meson config failed: {}", e)))?;
if !config_output.status.success() {
let stderr = String::from_utf8_lossy(&config_output.stderr);
return Err(std::io::Error::other(format!(
"Meson config failed:\n{}",
stderr
)));
}
// Now build
let mut build_cmd = Command::new("ninja");
build_cmd.current_dir(&build_dir_build);
build_cmd.arg("-j").arg(num_cpus::get().to_string()); // Parallel build
for (key, value) in &cmd_envs {
build_cmd.env(key, value);
}
log::info!("Running Meson build: {:?}", build_cmd);
let build_output = build_cmd
.output()
.map_err(|e| std::io::Error::other(format!("Meson build failed: {}", e)))?;
if !build_output.status.success() {
let stderr = String::from_utf8_lossy(&build_output.stderr);
let stdout = String::from_utf8_lossy(&build_output.stdout);
return Err(std::io::Error::other(format!(
"Meson build failed:\nStdout: {}\nStderr: {}",
stdout, stderr
)));
}
}
BuildSystems::Cargo => {
// Check for Cargo.toml
let cargo_file = build_dir.join("Cargo.toml");
if !cargo_file.exists() {
return Err(std::io::Error::other(format!(
"Cargo.toml not found: {}",
cargo_file.display()
)));
}
let mut cmd = Command::new("cargo");
cmd.arg("build").arg("--release").current_dir(&build_dir);
for (key, value) in &cmd_envs {
cmd.env(key, value);
}
log::info!("Running Cargo build: {:?}", cmd);
let output = cmd
.output()
.map_err(|e| std::io::Error::other(format!("Cargo build failed: {}", e)))?;
if !output.status.success() {
let stderr = String::from_utf8_lossy(&output.stderr);
let stdout = String::from_utf8_lossy(&output.stdout);
return Err(std::io::Error::other(format!(
"Cargo build failed:\nStdout: {}\nStderr: {}",
stdout, stderr
)));
}
}
}
log::info!("Build completed successfully for package: {}", self.name);
Ok(())
}
/// Builds the package according to the BUILD file in the archive.
///
/// There are two strategies for building the package. If the BUILD file is empty, the package is assumed to be a binary package and the default install hook is skipped. If the BUILD file is not empty, the package is assumed to be a source package and the default build hook is skipped.
///
/// If the BUILD file is empty and the INSTALL file contains a custom script, the custom script is run instead of the default install hook.
/// If the BUILD file is not empty and the INSTALL file contains a custom script, the custom script is ignored.
///
///
/// # Errors
///
/// Returns an error if the BUILD file is invalid or if the build or install hook fails.
fn build(&mut self) -> Result<bool, std::io::Error> {
let meta = Self::loadmeta(self)?;
let install_meta = meta.0;
// let setts_meta = meta.1;
let build_meta = meta.2;
// BUILD NOT EMPTY. SOURCE: -> BUILD -> INSTALL -> SETTS
// BUILD EMPTY. BIN: -> INSTALL -> SETTS
enum Strategies {
Bin,
Source,
}
let strategy; //default
if build_meta.is_none() {
log::info!("BUILD file is empty. Skipping build, preparing to install");
strategy = Strategies::Bin;
} else {
strategy = Strategies::Source;
log::info!("BUILD file is not empty. Skipping install, preparing to build");
}
match strategy {
Strategies::Bin => {
if install_meta.install.custom_script.is_none() {
log::info!("Strategy: BIN; No custom script. Running default install hook.");
} else {
log::info!("Strategy: BIN; Running custom script.");
let script = install_meta.install.custom_script.as_ref().unwrap();
// Validate script before execution
Self::validate_custom_script(script)?;
if !script.starts_with("./") {
let output = std::process::Command::new(script).output().map_err(|e| {
std::io::Error::other(format!("Failed to execute custom script: {}", e))
})?;
if !output.status.success() {
let stderr = String::from_utf8_lossy(&output.stderr);
return Err(std::io::Error::other(format!(
"Custom script failed:\n{}",
stderr
)));
}
} else {
let output = std::process::Command::new("/bin/sh")
.arg("-c")
.arg(script)
.output()
.map_err(|e| {
std::io::Error::other(format!(
"Failed to execute custom script: {}",
e
))
})?;
if !output.status.success() {
let stderr = String::from_utf8_lossy(&output.stderr);
return Err(std::io::Error::other(format!(
"Custom script failed:\n{}",
stderr
)));
}
}
}
}
Strategies::Source => {
log::info!("Strategy: SOURCE; Running default build hook.");
if let Err(e) = self.execute_build(&build_meta.unwrap()) {
return Err(std::io::Error::other(format!("Build failed: {}", e)));
}
}
}
Ok(true)
}
}
|