llama-cpp-bindings-build 0.32.0

Build scripts of llama-cpp-native, which builds llama.cpp with CMake, and llama-cpp-bindings-sys, which generates the bindings and compiles the C++ wrapper
use std::fs;
use std::path::PathBuf;

use cmake::Config;

use crate::build_error::BuildError;
use crate::cmake_cache::CmakeCache;
use crate::cmake_define::CmakeDefine;
use crate::cmake_file_api::CmakeFileApi;
use crate::constants::CACHE_QUERY_SCRIPT;
use crate::constants::NATIVE_PROFILE;

/// Configures the `CMake` project in `source_dir` and builds and installs it into `out_dir`,
/// reading back through `file_api` the cache `CMake` configured it with.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct CmakeBuild {
    pub source_dir: PathBuf,
    pub out_dir: PathBuf,
    pub file_api: CmakeFileApi,
    pub target_triple: String,
    pub host_triple: String,
    pub defines: Vec<CmakeDefine>,
    pub required_directories: Vec<PathBuf>,
}

impl CmakeBuild {
    /// # Errors
    ///
    /// Returns [`BuildError::Filesystem`] when a directory the project installs into cannot be
    /// created, or the [`BuildError`] of [`CmakeFileApi::cache`].
    ///
    /// # Panics
    ///
    /// Panics when `CMake` fails, the way the `cmake` crate reports a failed build.
    pub fn build(&self) -> Result<CmakeCache, BuildError> {
        self.required_directories.iter().try_for_each(|directory| {
            fs::create_dir_all(directory).map_err(|source| BuildError::Filesystem {
                path: directory.clone(),
                source,
            })
        })?;

        let mut config = Config::new(&self.source_dir);

        for CmakeDefine { name, value } in &self.defines {
            config.define(name, value);
        }

        config
            .configure_arg("-C")
            .configure_arg(CACHE_QUERY_SCRIPT)
            .target(&self.target_triple)
            .host(&self.host_triple)
            .out_dir(&self.out_dir)
            .profile(NATIVE_PROFILE)
            .build();

        self.file_api.cache()
    }
}