puffin_build/library/build/
builder.rs1use std::path::{Path, PathBuf};
2
3use serde::{Deserialize, Serialize};
4
5use super::Options;
6use crate::error::{Error, Result};
7use crate::utils::cmake;
8
9#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
10#[serde(tag = "type")]
11#[serde(rename_all = "lowercase")]
12pub enum Builder {
13 BuiltIn { name: String },
14 CMake { path: PathBuf },
15}
16
17impl Builder {
18 pub fn build(
19 &self,
20 sources: impl AsRef<Path>,
21 out_dir: impl AsRef<Path>,
22 version: impl AsRef<str>,
23 options: impl AsRef<Options>,
24 cput: bool,
25 ) -> Result<()> {
26 match self {
27 Self::BuiltIn { .. } | Self::CMake { .. } => {
28 let mut cmake_conf = match self {
29 Self::BuiltIn { .. } => cmake::command("builder", out_dir),
30 Self::CMake { path, .. } => cmake::Config {
31 src_dir: path.clone(),
32 out_dir: out_dir.as_ref().to_path_buf(),
33 bld_dir: out_dir.as_ref().join("build"),
34 cfg_args: vec![],
35 },
36 };
37
38 if let Self::BuiltIn { name, .. } = self {
39 cmake_conf.cfg_args.push(format!("-DBUILDER={}", name));
40 }
41
42 cmake_conf
43 .cfg_args
44 .push(format!("-DSOURCES={}", sources.as_ref().display()));
45 cmake_conf
46 .cfg_args
47 .push(format!("-DVENDOR_VERSION={}", version.as_ref()));
48
49 cmake_conf
50 .cfg_args
51 .push(format!("-DCPUT={}", if cput { "1" } else { "0" }));
52
53 for (name, value) in options.as_ref().into_iter() {
54 cmake_conf
55 .cfg_args
56 .push(format!("-D{}={}", name, value.to_cmake_value()));
57 }
58
59 cmake_conf.build()
60 }
61 }
62 .map_err(|e| Error::LibraryBuilderFailed {
63 builder: self.clone(),
64 sources: sources.as_ref().to_path_buf(),
65 options: options.as_ref().clone(),
66 reason: e,
67 })
68 }
69}