llama-cpp-bindings-build 0.17.0

Build system for llama-cpp-bindings-sys FFI bindings
Documentation
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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
use std::env;
use std::path::{Path, PathBuf};

use thiserror::Error;

const DEFAULT_ANDROID_API_LEVEL: &str = "28";

#[derive(Debug, Error)]
pub enum AndroidNdkDetectionError {
    #[error("Android NDK not found for target {target_triple}. Set ANDROID_NDK_HOME.")]
    NdkRootNotConfigured {
        target_triple: String,
        #[source]
        source: env::VarError,
    },
    #[error("Android NDK path does not exist: {path}")]
    NdkRootMissing { path: PathBuf },
    #[error("Android NDK toolchain file not found: {path}")]
    NdkToolchainFileMissing { path: PathBuf },
    #[error("Android NDK toolchain not found at: {path}")]
    NdkToolchainDirectoryMissing { path: PathBuf },
    #[error("Unsupported host platform for Android NDK")]
    UnsupportedHostPlatform,
    #[error("Unsupported Android target architecture: {cargo_cfg_target_arch}")]
    UnsupportedAndroidTarget { cargo_cfg_target_arch: String },
    #[error("ANDROID_PLATFORM is set but could not be read: {source}")]
    AndroidPlatformUnreadable {
        #[source]
        source: env::VarError,
    },
    #[error("Android NDK Clang directory could not be read at {path}: {source}")]
    ClangDirectoryUnreadable {
        path: PathBuf,
        #[source]
        source: std::io::Error,
    },
    #[error("Android NDK Clang version directory was not found at {path}")]
    ClangVersionDirectoryMissing { path: PathBuf },
    #[error("Android NDK Clang directory entry could not be read at {path}: {source}")]
    ClangDirectoryEntryUnreadable {
        path: PathBuf,
        #[source]
        source: std::io::Error,
    },
    #[error("Android NDK Clang built-in include directory was not found at {path}")]
    ClangBuiltinIncludesMissing { path: PathBuf },
}

#[derive(Debug)]
pub struct AndroidNdk {
    pub ndk_path: String,
    pub api_level: String,
    pub abi: &'static str,
    pub host_tag: &'static str,
    pub toolchain_path: String,
    pub sysroot: String,
    pub target_prefix: &'static str,
    pub clang_builtin_includes: String,
}

impl AndroidNdk {
    /// # Errors
    ///
    /// Returns [`AndroidNdkDetectionError`] when the NDK installation cannot be
    /// located, an environment variable is missing or malformed, the target
    /// architecture is unsupported, or the host platform is not supported by the NDK.
    pub fn detect(
        target_triple: &str,
        cargo_cfg_target_arch: &str,
    ) -> Result<Self, AndroidNdkDetectionError> {
        let ndk_path = detect_ndk_path(target_triple)?;

        validate_ndk_installation(&ndk_path)?;

        let architecture = AndroidArchitecture::from_cargo_cfg(cargo_cfg_target_arch)?;
        let api_level = detect_api_level()?;
        let abi = architecture.abi();
        let host_tag = detect_host_tag()?;
        let target_prefix = architecture.ndk_prefix();
        let toolchain_path = format!("{ndk_path}/toolchains/llvm/prebuilt/{host_tag}");

        if !Path::new(&toolchain_path).exists() {
            return Err(AndroidNdkDetectionError::NdkToolchainDirectoryMissing {
                path: PathBuf::from(toolchain_path),
            });
        }

        let sysroot = format!("{toolchain_path}/sysroot");
        let clang_builtin_includes = find_clang_builtin_includes(&toolchain_path)?;

        Ok(Self {
            ndk_path,
            api_level,
            abi,
            host_tag,
            toolchain_path,
            sysroot,
            target_prefix,
            clang_builtin_includes,
        })
    }

    pub fn android_platform(&self) -> String {
        format!("android-{}", self.api_level)
    }

    pub fn cmake_toolchain_file(&self) -> String {
        format!("{}/build/cmake/android.toolchain.cmake", self.ndk_path)
    }
}

fn detect_ndk_path(target_triple: &str) -> Result<String, AndroidNdkDetectionError> {
    env::var("ANDROID_NDK_HOME").map_err(|source| AndroidNdkDetectionError::NdkRootNotConfigured {
        target_triple: target_triple.to_owned(),
        source,
    })
}

fn validate_ndk_installation(ndk_path: &str) -> Result<(), AndroidNdkDetectionError> {
    let ndk_path = Path::new(ndk_path);

    if !ndk_path.exists() {
        return Err(AndroidNdkDetectionError::NdkRootMissing {
            path: ndk_path.to_path_buf(),
        });
    }

    let toolchain_file = ndk_path.join("build/cmake/android.toolchain.cmake");

    if !toolchain_file.exists() {
        return Err(AndroidNdkDetectionError::NdkToolchainFileMissing {
            path: toolchain_file,
        });
    }

    Ok(())
}

fn detect_api_level() -> Result<String, AndroidNdkDetectionError> {
    match env::var("ANDROID_PLATFORM") {
        Ok(platform) => Ok(platform
            .strip_prefix("android-")
            .unwrap_or(&platform)
            .to_owned()),
        Err(env::VarError::NotPresent) => Ok(DEFAULT_ANDROID_API_LEVEL.to_string()),
        Err(source) => Err(AndroidNdkDetectionError::AndroidPlatformUnreadable { source }),
    }
}

const fn detect_host_tag() -> Result<&'static str, AndroidNdkDetectionError> {
    if cfg!(target_os = "macos") {
        Ok("darwin-x86_64")
    } else if cfg!(target_os = "linux") {
        Ok("linux-x86_64")
    } else {
        Err(AndroidNdkDetectionError::UnsupportedHostPlatform)
    }
}

#[derive(Debug, Clone, Copy, Eq, PartialEq)]
enum AndroidArchitecture {
    Aarch64,
    Armv7,
    X86_64,
    X86,
}

impl AndroidArchitecture {
    fn from_cargo_cfg(cargo_cfg_target_arch: &str) -> Result<Self, AndroidNdkDetectionError> {
        match cargo_cfg_target_arch {
            "aarch64" => Ok(Self::Aarch64),
            "arm" => Ok(Self::Armv7),
            "x86_64" => Ok(Self::X86_64),
            "x86" => Ok(Self::X86),
            unsupported => Err(AndroidNdkDetectionError::UnsupportedAndroidTarget {
                cargo_cfg_target_arch: unsupported.to_owned(),
            }),
        }
    }

    const fn abi(self) -> &'static str {
        match self {
            Self::Aarch64 => "arm64-v8a",
            Self::Armv7 => "armeabi-v7a",
            Self::X86_64 => "x86_64",
            Self::X86 => "x86",
        }
    }

    const fn ndk_prefix(self) -> &'static str {
        match self {
            Self::Aarch64 => "aarch64-linux-android",
            Self::Armv7 => "arm-linux-androideabi",
            Self::X86_64 => "x86_64-linux-android",
            Self::X86 => "i686-linux-android",
        }
    }
}

fn find_clang_builtin_includes(toolchain_path: &str) -> Result<String, AndroidNdkDetectionError> {
    let clang_lib_path = PathBuf::from(toolchain_path).join("lib/clang");
    let entries = std::fs::read_dir(&clang_lib_path).map_err(|source| {
        AndroidNdkDetectionError::ClangDirectoryUnreadable {
            path: clang_lib_path.clone(),
            source,
        }
    })?;
    let mut version_dir = None;
    for entry in entries {
        let entry =
            entry.map_err(
                |source| AndroidNdkDetectionError::ClangDirectoryEntryUnreadable {
                    path: clang_lib_path.clone(),
                    source,
                },
            )?;
        let file_type = entry.file_type().map_err(|source| {
            AndroidNdkDetectionError::ClangDirectoryEntryUnreadable {
                path: entry.path(),
                source,
            }
        })?;
        if file_type.is_dir()
            && entry
                .file_name()
                .to_str()
                .is_some_and(|name| name.starts_with(|character: char| character.is_ascii_digit()))
        {
            version_dir = Some(entry);
            break;
        }
    }
    let version_dir =
        version_dir.ok_or_else(|| AndroidNdkDetectionError::ClangVersionDirectoryMissing {
            path: clang_lib_path.clone(),
        })?;

    let include_path = clang_lib_path.join(version_dir.file_name()).join("include");

    if !include_path.is_dir() {
        return Err(AndroidNdkDetectionError::ClangBuiltinIncludesMissing { path: include_path });
    }

    Ok(include_path.to_string_lossy().into_owned())
}

#[cfg(test)]
mod android_ndk_resolution_tests {
    use std::path::PathBuf;
    use std::sync::atomic::{AtomicUsize, Ordering};

    use super::AndroidArchitecture;
    use super::AndroidNdk;
    use super::AndroidNdkDetectionError;
    use super::find_clang_builtin_includes;
    use super::validate_ndk_installation;

    static NEXT_DIRECTORY_ID: AtomicUsize = AtomicUsize::new(0);

    fn temporary_directory(name: &str) -> PathBuf {
        let id = NEXT_DIRECTORY_ID.fetch_add(1, Ordering::Relaxed);
        std::env::temp_dir().join(format!(
            "llama-cpp-bindings-{name}-{}-{id}",
            std::process::id()
        ))
    }

    #[test]
    fn every_supported_android_architecture_maps_to_its_abi_and_ndk_prefix() {
        let architectures = [
            ("aarch64", "arm64-v8a", "aarch64-linux-android"),
            ("arm", "armeabi-v7a", "arm-linux-androideabi"),
            ("x86_64", "x86_64", "x86_64-linux-android"),
            ("x86", "x86", "i686-linux-android"),
        ];

        for (cargo_cfg_target_arch, abi, prefix) in architectures {
            let architecture = AndroidArchitecture::from_cargo_cfg(cargo_cfg_target_arch)
                .expect("the architecture is supported");

            assert_eq!(architecture.abi(), abi);
            assert_eq!(architecture.ndk_prefix(), prefix);
        }
    }

    #[test]
    fn an_unsupported_android_architecture_preserves_what_cargo_reported() {
        let error = AndroidArchitecture::from_cargo_cfg("riscv64")
            .expect_err("riscv64 has no NDK toolchain in this build");

        assert!(matches!(
            error,
            AndroidNdkDetectionError::UnsupportedAndroidTarget { cargo_cfg_target_arch }
                if cargo_cfg_target_arch == "riscv64"
        ));
    }

    #[test]
    fn android_ndk_paths_derive_from_the_resolved_root_and_api_level() {
        let ndk = AndroidNdk {
            ndk_path: "/opt/android-ndk".to_owned(),
            api_level: "35".to_owned(),
            abi: "arm64-v8a",
            host_tag: "linux-x86_64",
            toolchain_path: "/opt/android-ndk/toolchains/llvm/prebuilt/linux-x86_64".to_owned(),
            sysroot: "/opt/android-ndk/toolchains/llvm/prebuilt/linux-x86_64/sysroot".to_owned(),
            target_prefix: "aarch64-linux-android",
            clang_builtin_includes: "/opt/android-ndk/lib/clang/20/include".to_owned(),
        };

        assert_eq!(ndk.android_platform(), "android-35");
        assert_eq!(
            ndk.cmake_toolchain_file(),
            "/opt/android-ndk/build/cmake/android.toolchain.cmake"
        );
    }

    #[test]
    fn ndk_validation_rejects_a_missing_root_directory() {
        let root = temporary_directory("missing-ndk-root");

        let error = validate_ndk_installation(root.to_str().expect("UTF-8 temporary path"))
            .expect_err("missing NDK root must fail");

        assert!(matches!(
            error,
            AndroidNdkDetectionError::NdkRootMissing { path } if path == root
        ));
    }

    #[test]
    fn ndk_validation_rejects_a_root_without_a_cmake_toolchain() {
        let root = temporary_directory("ndk-without-toolchain");
        std::fs::create_dir_all(&root).expect("temporary NDK root must be created");

        let error = validate_ndk_installation(root.to_str().expect("UTF-8 temporary path"))
            .expect_err("missing toolchain file must fail");

        assert!(matches!(
            error,
            AndroidNdkDetectionError::NdkToolchainFileMissing { path }
                if path == root.join("build/cmake/android.toolchain.cmake")
        ));
        std::fs::remove_dir_all(root).expect("temporary NDK root must be removed");
    }

    #[test]
    fn ndk_validation_accepts_a_root_with_a_cmake_toolchain() {
        let root = temporary_directory("valid-ndk-root");
        let toolchain = root.join("build/cmake/android.toolchain.cmake");
        std::fs::create_dir_all(toolchain.parent().expect("toolchain parent"))
            .expect("toolchain directory must be created");
        std::fs::write(&toolchain, "").expect("toolchain file must be created");

        validate_ndk_installation(root.to_str().expect("UTF-8 temporary path"))
            .expect("valid NDK root must pass");

        std::fs::remove_dir_all(root).expect("temporary NDK root must be removed");
    }

    #[test]
    fn clang_builtin_include_resolution_rejects_a_missing_clang_directory() {
        let toolchain = temporary_directory("missing-clang-directory");

        let error = find_clang_builtin_includes(toolchain.to_str().expect("UTF-8 temporary path"))
            .expect_err("missing Clang directory must fail");

        assert!(matches!(
            error,
            AndroidNdkDetectionError::ClangDirectoryUnreadable { path, .. }
                if path == toolchain.join("lib/clang")
        ));
    }

    #[test]
    fn clang_builtin_include_resolution_requires_a_version_directory() {
        let toolchain = temporary_directory("clang-without-version");
        std::fs::create_dir_all(toolchain.join("lib/clang/not-a-version"))
            .expect("Clang directory must be created");

        let error = find_clang_builtin_includes(toolchain.to_str().expect("UTF-8 temporary path"))
            .expect_err("missing version directory must fail");

        assert!(matches!(
            error,
            AndroidNdkDetectionError::ClangVersionDirectoryMissing { path }
                if path == toolchain.join("lib/clang")
        ));
        std::fs::remove_dir_all(toolchain).expect("temporary toolchain must be removed");
    }

    #[test]
    fn clang_builtin_include_resolution_requires_an_include_directory() {
        let toolchain = temporary_directory("clang-without-includes");
        std::fs::create_dir_all(toolchain.join("lib/clang/20"))
            .expect("Clang version directory must be created");

        let error = find_clang_builtin_includes(toolchain.to_str().expect("UTF-8 temporary path"))
            .expect_err("missing built-in includes must fail");

        assert!(matches!(
            error,
            AndroidNdkDetectionError::ClangBuiltinIncludesMissing { path }
                if path == toolchain.join("lib/clang/20/include")
        ));
        std::fs::remove_dir_all(toolchain).expect("temporary toolchain must be removed");
    }

    #[test]
    fn clang_builtin_include_resolution_returns_the_version_include_directory() {
        let toolchain = temporary_directory("clang-with-includes");
        let include = toolchain.join("lib/clang/20/include");
        std::fs::create_dir_all(&include).expect("Clang include directory must be created");

        let resolved =
            find_clang_builtin_includes(toolchain.to_str().expect("UTF-8 temporary path"))
                .expect("built-in includes must resolve");

        assert_eq!(PathBuf::from(resolved), include);
        std::fs::remove_dir_all(toolchain).expect("temporary toolchain must be removed");
    }
}