Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prepare for publishing to crates.io #2

Merged
merged 6 commits into from
Oct 25, 2023
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
4 changes: 2 additions & 2 deletions .github/workflows/cargo_build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ name: Rust

on:
push:
branches: [ "main" ]
branches: [ "*" ]
pull_request:
branches: [ "main" ]
branches: [ "*" ]

env:
CARGO_TERM_COLOR: always
Expand Down
10 changes: 10 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion crates/llama_cpp_sys/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@ edition = "2021"
authors = ["Dakota Thompson <me@scriptis.net>"]
repository = "https://github.com/binedge/llama_cpp-rs"
license = "MIT OR APACHE-2.0"
include = ["src/**/*"]
readme = "../../README.md"
publish = true

[dependencies]
link-cplusplus = "1.0.9"

[build-dependencies]
bindgen = "0.68.1"
cmake = "0.1.50"
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
2 changes: 2 additions & 0 deletions crates/llama_cpp_sys/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,6 @@
#![allow(non_camel_case_types)]
#![allow(non_snake_case)]

extern crate link_cplusplus;

include!(concat!(env!("OUT_DIR"), "/bindings.rs"));