Skip to content

Commit

Permalink
Prepare for publishing to crates.io (#2)
Browse files Browse the repository at this point in the history
* ci: disable static linking of llama.o

* ci: build+test on all branches/prs

* ci: use `link-cplusplus`

* publish: remove `include`

* publish: do not include `build-info.h`

* publish: work around llama.cpp polluting the CWD
  • Loading branch information
scriptis authored Oct 25, 2023
1 parent 2d14d8d commit f35e282
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 8 deletions.
1 change: 0 additions & 1 deletion crates/llama_cpp_sys/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ edition = "2021"
authors = ["Dakota Thompson <[email protected]>"]
repository = "https://github.com/binedge/llama_cpp-rs"
license = "MIT OR APACHE-2.0"
include = ["src/**/*"]
readme = "../../README.md"
publish = true

Expand Down
79 changes: 72 additions & 7 deletions crates/llama_cpp_sys/build.rs
Original file line number Diff line number Diff line change
@@ -1,27 +1,92 @@
use std::path::PathBuf;
use std::path::{Path, PathBuf};
use std::process::exit;
use std::{env, fs};
use std::{env, fs, io};

const SUBMODULE_DIR: &str = concat!(env!("CARGO_MANIFEST_DIR"), "/thirdparty/llama.cpp");
const HEADER_DIR: &str = concat!(env!("CARGO_MANIFEST_DIR"), "/thirdparty/llama.cpp/llama.h");

fn copy_recursively(src: &Path, dst: &Path) -> io::Result<()> {
if !dst.exists() {
fs::create_dir_all(dst)?;
}

for entry in fs::read_dir(src)? {
let entry = entry?;
let file_type = entry.file_type()?;

if file_type.is_dir() {
copy_recursively(&entry.path(), &dst.join(entry.file_name()))?;
} else {
fs::copy(entry.path(), dst.join(entry.file_name()))?;
}
}

Ok(())
}

fn main() {
if fs::read_dir(SUBMODULE_DIR).is_err() {
let submodule_dir = &PathBuf::from(SUBMODULE_DIR);

let out_dir = PathBuf::from(env::var_os("OUT_DIR").unwrap());

let build_dir = out_dir.join("build");
let header_path = out_dir.join("build/llama.h");
let git_path = build_dir.join(".git");
let git_ignored_path = build_dir.join(".git-ignored");
let build_info_path = build_dir.join("build-info.h");

if fs::read_dir(submodule_dir).is_err() {
eprintln!("Could not find {SUBMODULE_DIR}. Did you forget to initialize submodules?");

exit(1);
}

let dst = cmake::Config::new(SUBMODULE_DIR)
.configure_arg("DLLAMA_STATIC=On")
if let Err(err) = fs::create_dir_all(&build_dir) {
eprintln!("Could not create {build_dir:#?}: {err}");

exit(1);
}

if let Err(err) = copy_recursively(submodule_dir, &build_dir) {
eprintln!("Could not copy {submodule_dir:#?} into {build_dir:#?}: {err}");

exit(1);
}

// TODO(scriptis): This is a gross hack. `llama.cpp` tries to read `.git` and create/update
// `build-info.h` in the root of itself, which breaks Cargo's "don't modify the source tree
// in `build.rs` rule. This is a workaround for that: get rid of `.git` and manually create
// `build-info.h`.
if git_path.exists() {
fs::rename(&git_path, &git_ignored_path).unwrap();
}

if !build_info_path.exists() {
fs::write(build_info_path, "\
#ifndef BUILD_INFO_H
#define BUILD_INFO_H
#define BUILD_NUMBER 1
#define BUILD_COMMIT \"ffffffff\"
#define BUILD_COMPILER \"rustc\"
#define BUILD_TARGET \"rustc\"
#endif // BUILD_INFO_H
").unwrap();
}
let dst = cmake::Config::new(&build_dir)
.configure_arg("-DLLAMA_STATIC=On")
.configure_arg("-DLLAMA_BUILD_EXAMPLES=Off")
.configure_arg("-DLLAMA_BUILD_SERVER=Off")
.configure_arg("-DLLAMA_BUILD_TESTS=Off")
.build();

println!("cargo:rustc-link-search=native={}/lib", dst.display());
println!("cargo:rustc-link-search=native={}/lib64", dst.display());
println!("cargo:rustc-link-lib=static=llama");

let bindings = bindgen::Builder::default()
.header(HEADER_DIR)
.header(header_path.to_string_lossy())
.parse_callbacks(Box::new(bindgen::CargoCallbacks))
.generate_comments(false)
.allowlist_function("llama_.*")
Expand Down

0 comments on commit f35e282

Please sign in to comment.