Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 1 addition & 13 deletions .github/actions/install-build-dependencies/action.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: install-build-dependencies
description: Install OS-specific system packages needed to build llama-cpp-bindings (CMake, libclang, GNU make).
description: Install the Linux system packages needed to build llama-cpp-bindings (CMake, libclang).

runs:
using: composite
Expand All @@ -8,15 +8,3 @@ runs:
if: runner.os == 'Linux'
shell: bash
run: sudo apt-get update && sudo apt-get install -y cmake libclang-dev

- name: install windows build dependencies
if: runner.os == 'Windows'
shell: bash
run: choco install -y make

- name: set windows libclang path
if: runner.os == 'Windows'
shell: bash
env:
WINDOWS_LIBCLANG_PATH: 'C:\Program Files\LLVM\bin'
run: echo "LIBCLANG_PATH=$WINDOWS_LIBCLANG_PATH" >> "$GITHUB_ENV"
2 changes: 1 addition & 1 deletion .github/workflows/unit-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ jobs:
strategy:
fail-fast: false
matrix:
os: [ubuntu-latest, windows-latest, macos-latest]
os: [ubuntu-latest, macos-latest]
defaults:
run:
shell: bash
Expand Down
2 changes: 0 additions & 2 deletions llama-cpp-bindings-build/src/android_ndk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,8 +152,6 @@ const fn detect_host_tag() -> Result<&'static str, AndroidNdkDetectionError> {
Ok("darwin-x86_64")
} else if cfg!(target_os = "linux") {
Ok("linux-x86_64")
} else if cfg!(target_os = "windows") {
Ok("windows-x86_64")
} else {
Err(AndroidNdkDetectionError::UnsupportedHostPlatform)
}
Expand Down
44 changes: 1 addition & 43 deletions llama-cpp-bindings-build/src/bindgen_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,10 +154,6 @@ pub fn generate_bindings(
builder = configure_android_bindgen(builder, ndk, target_triple);
}

if target_os.is_msvc() {
builder = configure_msvc_bindgen(builder, target_triple)?;
}

let bindings = builder.generate().map_err(BuildError::Bindgen)?;

callbacks.verify_every_privatized_field_was_found()?;
Expand All @@ -180,6 +176,7 @@ fn create_base_builder(llama_src: &Path, callbacks: BindingCallbacks) -> bindgen
.derive_partialeq(true)
.allowlist_function("ggml_.*")
.allowlist_type("ggml_.*")
.allowlist_var("GGML_MAX_DIMS")
.allowlist_function("gguf_.*")
.allowlist_type("gguf_.*")
.allowlist_function("llama_.*")
Expand All @@ -192,7 +189,6 @@ fn create_base_builder(llama_src: &Path, callbacks: BindingCallbacks) -> bindgen
.blocklist_function("llama_model_load_from_file_ptr")
.blocklist_type("FILE")
.blocklist_type("_IO_.*")
.blocklist_type("_iobuf")
.prepend_enum_name(false);

for function in DEPRECATED_FUNCTIONS {
Expand Down Expand Up @@ -228,41 +224,3 @@ fn configure_android_bindgen(

builder.clang_arg(format!("--target={target_triple}"))
}

fn configure_msvc_bindgen(
mut builder: bindgen::Builder,
target_triple: &str,
) -> Result<bindgen::Builder, BuildError> {
let compiler = cc::Build::new()
.try_get_compiler()
.map_err(BuildError::NativeCompiler)?;

let msvc_include_paths = compiler
.env()
.iter()
.find(|(key, _)| key.eq_ignore_ascii_case("INCLUDE"))
.map(|(_, value)| value.clone());

if let Some(include_paths) = msvc_include_paths {
for include_path in include_paths
.to_string_lossy()
.split(';')
.filter(|path| !path.is_empty())
{
builder = builder.clang_arg("-isystem").clang_arg(include_path);
debug_log!("Added MSVC include path: {}", include_path);
}
}

builder = builder
.clang_arg(format!("--target={target_triple}"))
.clang_arg("-fms-compatibility")
.clang_arg("-fms-extensions");

debug_log!(
"Configured bindgen with MSVC toolchain for target: {}",
target_triple
);

Ok(builder)
}
118 changes: 2 additions & 116 deletions llama-cpp-bindings-build/src/cmake_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,6 @@ pub fn configure_and_build(context: &BuildContext) -> Result<PathBuf, BuildError
configure_system_ggml(&mut config)?;
let backends_dir = configure_dynamic_backends(&mut config, &context.cmake_dir)?;

config.static_crt(context.static_crt);
configure_msvc_exception_handling(&mut config, context.target_os);
configure_msvc_config_flags(&mut config, context.target_os, &context.profile);
config
.out_dir(&context.cmake_dir)
.profile(&context.profile)
Expand Down Expand Up @@ -145,46 +142,6 @@ fn map_cpu_feature_to_ggml(feature: &str) -> Option<&'static str> {
}
}

const fn msvc_exception_handling_flag(target_os: TargetOs) -> Option<&'static str> {
if target_os.is_msvc() {
Some("/EHsc")
} else {
None
}
}

fn configure_msvc_exception_handling(config: &mut Config, target_os: TargetOs) {
let Some(flag) = msvc_exception_handling_flag(target_os) else {
return;
};

config.cxxflag(flag);
}

fn msvc_config_flags(target_os: TargetOs, profile: &str) -> Option<&'static str> {
if !target_os.is_msvc() {
return None;
}

match profile {
"Debug" => Some("/Ob0 /Od /RTC1"),
"MinSizeRel" => Some("/O1 /Ob1 /DNDEBUG"),
"Release" => Some("/O2 /Ob2 /DNDEBUG"),
"RelWithDebInfo" => Some("/O2 /Ob1 /DNDEBUG"),
_ => None,
}
}

fn configure_msvc_config_flags(config: &mut Config, target_os: TargetOs, profile: &str) {
let Some(flags) = msvc_config_flags(target_os, profile) else {
return;
};
let config_suffix = profile.to_uppercase();

config.define(format!("CMAKE_C_FLAGS_{config_suffix}"), flags);
config.define(format!("CMAKE_CXX_FLAGS_{config_suffix}"), flags);
}

fn configure_shared_libs(config: &mut Config, build_shared_libs: bool) {
config.define(
"BUILD_SHARED_LIBS",
Expand All @@ -207,7 +164,7 @@ fn configure_platform_specific(
configure_android_cmake(config, ndk, target_triple);
}
}
_ => {}
TargetOs::Linux => {}
}
}

Expand Down Expand Up @@ -261,16 +218,6 @@ fn configure_gpu_backends(config: &mut Config, target_os: TargetOs) -> Result<()

fn configure_vulkan_linking(target_os: TargetOs) -> Result<(), BuildError> {
match target_os {
TargetOs::Windows(_) => {
let vulkan_path = env::var("VULKAN_SDK").map_err(|source| BuildError::Environment {
name: "VULKAN_SDK",
source,
})?;
let vulkan_lib_path = Path::new(&vulkan_path).join("Lib");

println!("cargo:rustc-link-search={}", vulkan_lib_path.display());
println!("cargo:rustc-link-lib=vulkan-1");
}
TargetOs::Linux => {
match env::var("VULKAN_SDK") {
Ok(vulkan_path) => {
Expand All @@ -289,7 +236,7 @@ fn configure_vulkan_linking(target_os: TargetOs) -> Result<(), BuildError> {

println!("cargo:rustc-link-lib=vulkan");
}
_ => (),
TargetOs::Apple(_) | TargetOs::Android => {}
}

Ok(())
Expand All @@ -315,67 +262,6 @@ fn configure_system_ggml(config: &mut Config) -> Result<(), BuildError> {
Ok(())
}

#[cfg(test)]
mod msvc_exception_handling_tests {
use crate::target_os::TargetOs;
use crate::windows_variant::WindowsVariant;

use super::msvc_exception_handling_flag;

#[test]
fn msvc_targets_compile_llama_cpp_with_unwind_semantics() {
assert_eq!(
msvc_exception_handling_flag(TargetOs::Windows(WindowsVariant::Msvc)),
Some("/EHsc")
);
}

#[test]
fn targets_without_msvc_keep_their_toolchain_default_exception_handling() {
assert_eq!(msvc_exception_handling_flag(TargetOs::Linux), None);
assert_eq!(
msvc_exception_handling_flag(TargetOs::Windows(WindowsVariant::Other)),
None
);
}
}

#[cfg(test)]
mod msvc_config_flag_tests {
use crate::target_os::TargetOs;
use crate::windows_variant::WindowsVariant;

use super::msvc_config_flags;

#[test]
fn every_msvc_configuration_keeps_llama_cpp_assertions_compiled_out() {
let msvc = TargetOs::Windows(WindowsVariant::Msvc);

assert_eq!(msvc_config_flags(msvc, "Debug"), Some("/Ob0 /Od /RTC1"));
assert_eq!(
msvc_config_flags(msvc, "MinSizeRel"),
Some("/O1 /Ob1 /DNDEBUG")
);
assert_eq!(
msvc_config_flags(msvc, "Release"),
Some("/O2 /Ob2 /DNDEBUG")
);
assert_eq!(
msvc_config_flags(msvc, "RelWithDebInfo"),
Some("/O2 /Ob1 /DNDEBUG")
);
}

#[test]
fn targets_and_profiles_without_msvc_defaults_keep_the_flags_cmake_chose() {
assert_eq!(msvc_config_flags(TargetOs::Linux, "Release"), None);
assert_eq!(
msvc_config_flags(TargetOs::Windows(WindowsVariant::Msvc), "Fastest"),
None
);
}
}

#[cfg(test)]
mod cpu_feature_mapping_tests {
use super::map_cpu_feature_to_ggml;
Expand Down
5 changes: 0 additions & 5 deletions llama-cpp-bindings-build/src/cpp_wrapper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,6 @@ pub fn compile_cpp_wrappers(target_os: TargetOs) -> Result<(), BuildError> {
build.file(source);
}

if target_os.is_msvc() {
build.flag(format!("/std:{CPP_STANDARD}"));
build.flag("/EHsc");
}

if target_os.is_android() && cfg!(feature = "static-stdcxx") {
build.cpp_link_stdlib(None);
}
Expand Down
16 changes: 4 additions & 12 deletions llama-cpp-bindings-build/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ mod library_linking;
mod native_library;
mod rebuild_tracking;
mod target_os;
mod windows_variant;

use std::env;
use std::path::{Path, PathBuf};
Expand Down Expand Up @@ -42,8 +41,6 @@ pub enum BuildError {
},
#[error("generated bindings could not be written: {0}")]
BindingsWrite(#[source] std::io::Error),
#[error("native compiler setup failed: {0}")]
NativeCompiler(#[source] cc::Error),
#[error("native wrapper compilation failed: {0}")]
NativeWrapper(#[source] cc::Error),
#[error("filesystem operation failed for {path}: {source}")]
Expand Down Expand Up @@ -88,7 +85,6 @@ pub struct BuildContext {
pub cargo_cfg_target_env: String,
pub build_shared_libs: bool,
pub profile: String,
pub static_crt: bool,
pub android_ndk: Option<AndroidNdk>,
}

Expand All @@ -97,20 +93,17 @@ impl BuildContext {
let target_triple = required_env("TARGET")?;
let cargo_cfg_target_os = required_env("CARGO_CFG_TARGET_OS")?;
let cargo_cfg_target_env = optional_env("CARGO_CFG_TARGET_ENV")?.unwrap_or_default();
let target_os = TargetOs::from_cargo_cfg(&cargo_cfg_target_os, &cargo_cfg_target_env)
.ok_or_else(|| BuildError::UnsupportedTargetOs {
let target_os = TargetOs::from_cargo_cfg(&cargo_cfg_target_os).ok_or_else(|| {
BuildError::UnsupportedTargetOs {
cargo_cfg_target_os: cargo_cfg_target_os.clone(),
})?;
}
})?;
let out_dir = PathBuf::from(required_env("OUT_DIR")?);
let manifest_dir = required_env("CARGO_MANIFEST_DIR")?;
let llama_src = Path::new(&manifest_dir).join("llama.cpp");

let build_shared_libs = cfg!(feature = "dynamic-link");
let profile = native_profile(&required_env("PROFILE")?);
let static_crt = optional_env("CARGO_CFG_TARGET_FEATURE")?
.unwrap_or_default()
.split(',')
.any(|feature| feature == "crt-static");

let cargo_cfg_target_arch = required_env("CARGO_CFG_TARGET_ARCH")?;
let android_ndk = if target_os.is_android() {
Expand All @@ -137,7 +130,6 @@ impl BuildContext {
cargo_cfg_target_env,
build_shared_libs,
profile,
static_crt,
android_ndk,
})
}
Expand Down
Loading
Loading