Skip to main content

llama_cpp_bindings_build/
lib.rs

1#![cfg_attr(
2    not(test),
3    deny(clippy::unwrap_used, clippy::expect_used, clippy::panic)
4)]
5
6mod android_architecture;
7mod android_ndk;
8mod binding_callbacks;
9mod bindings_generation;
10mod build_context;
11pub mod build_environment;
12pub mod build_error;
13pub mod build_feature;
14mod cargo_directive;
15mod cmake_build;
16mod cmake_cache;
17mod cmake_cache_entry;
18mod cmake_configuration;
19mod cmake_define;
20mod cmake_file_api;
21mod cmake_replies;
22mod cmake_reply_file;
23mod cmake_reply_index;
24mod cpu_tuning;
25mod emit_directives;
26mod enabled_build_features;
27mod host_os;
28mod link_directives;
29mod link_kind;
30mod native_build;
31mod native_library;
32mod native_profile;
33mod openmp_linking;
34mod openmp_runtime_source;
35mod privatized_field;
36mod rebuild_triggers;
37mod target_os;
38mod wrapper_compilation;
39mod x86_ggml_switch;
40
41use std::io::Write;
42
43use crate::build_context::BuildContext;
44use crate::build_environment::BuildEnvironment;
45use crate::build_error::BuildError;
46use crate::host_os::HostOs;
47use crate::native_build::NativeBuild;
48
49/// Generates the bindings, compiles the C++ wrappers, and builds llama.cpp for the build script
50/// run `environment` describes, then writes the cargo directives that link them to `output`.
51///
52/// # Errors
53///
54/// Returns the [`BuildError`] of the first decision or build step that fails; nothing is written
55/// to `output` then.
56pub fn build(environment: &BuildEnvironment, output: &mut impl Write) -> Result<(), BuildError> {
57    let context = BuildContext::from_environment(environment, HostOs::current())?;
58
59    NativeBuild::of(&context, environment)?.run(output)
60}
61
62#[cfg(test)]
63mod tests {
64    use std::ffi::OsStr;
65
66    use enumflags2::BitFlags;
67
68    use super::build;
69    use crate::build_context::tests::cargo_environment;
70    use crate::build_environment::BuildEnvironment;
71    use crate::build_environment::tests::variable;
72    use crate::build_feature::BuildFeature;
73
74    const FIXTURE_CMAKE_PROJECT: &str =
75        concat!(env!("CARGO_MANIFEST_DIR"), "/fixtures/cmake_project");
76
77    fn failure(environment: &BuildEnvironment) -> [Option<String>; 2] {
78        let mut output = Vec::new();
79
80        [
81            build(environment, &mut output)
82                .err()
83                .map(|error| error.to_string()),
84            String::from_utf8(output).ok(),
85        ]
86    }
87
88    #[test]
89    fn a_failure_at_any_stage_is_reported_and_nothing_is_emitted() {
90        let mut without_target = cargo_environment(BitFlags::empty(), &[]);
91
92        without_target.variables.remove(OsStr::new("TARGET"));
93
94        assert_eq!(
95            [
96                failure(&without_target),
97                failure(&cargo_environment(
98                    BitFlags::from(BuildFeature::SystemGgml),
99                    &[]
100                )),
101                failure(&cargo_environment(BitFlags::from(BuildFeature::Rocm), &[])),
102                failure(&cargo_environment(
103                    BitFlags::empty(),
104                    &[variable("CARGO_MANIFEST_DIR", FIXTURE_CMAKE_PROJECT)]
105                )),
106            ],
107            [
108                [
109                    Some(
110                        "environment variable TARGET could not be read: environment variable not \
111                         found"
112                            .to_owned()
113                    ),
114                    Some(String::new())
115                ],
116                [
117                    Some(
118                        "environment variable GGML_DIR could not be read: environment variable \
119                         not found"
120                            .to_owned()
121                    ),
122                    Some(String::new())
123                ],
124                [
125                    Some(
126                        "environment variable ROCM_PATH could not be read: environment variable \
127                         not found"
128                            .to_owned()
129                    ),
130                    Some(String::new())
131                ],
132                [
133                    Some(format!(
134                        "bindgen failed: header '{FIXTURE_CMAKE_PROJECT}/wrapper.h' does not exist."
135                    )),
136                    Some(String::new())
137                ],
138            ]
139        );
140    }
141}