Skip to content

Commit

Permalink
Merge pull request #67 from SkyFan2002/master
Browse files Browse the repository at this point in the history
Support static link as a feature
  • Loading branch information
Enet4 authored Aug 23, 2023
2 parents ce9345f + 4de6e50 commit c2838b8
Show file tree
Hide file tree
Showing 8 changed files with 78 additions and 4 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/target/
**/*.rs.bk
Cargo.lock
.vscode/
4 changes: 4 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[submodule "faiss-sys/faiss"]
path = faiss-sys/faiss
url = https://github.com/Enet4/faiss.git
branch = c_api_head
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ edition = "2018"

[features]
gpu = ["faiss-sys/gpu"]
static = ["faiss-sys/static"]

[badges.maintenance]
status = "passively-maintained"
Expand Down
7 changes: 6 additions & 1 deletion faiss-sys/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,14 @@ keywords = ["cbir", "clustering", "index", "similarity", "bindings"]

[features]
gpu = []
static = ["cmake"]


[build-dependencies]
cmake = {version = "0.1.50",optional = true}

[badges.maintenance]
status = "passively-maintained"

[package.metadata.docs.rs]
features = ["gpu"]
features = ["gpu"]
56 changes: 56 additions & 0 deletions faiss-sys/build.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,59 @@
fn main() {
#[cfg(feature = "static")]
static_link_faiss();
#[cfg(not(feature = "static"))]
println!("cargo:rustc-link-lib=faiss_c");
}

#[cfg(feature = "static")]
fn static_link_faiss() {
let mut cfg = cmake::Config::new("faiss");
cfg.define("FAISS_ENABLE_C_API", "ON")
.define("BUILD_SHARED_LIBS", "OFF")
.define("CMAKE_BUILD_TYPE", "Release")
.define("FAISS_ENABLE_GPU", "OFF")
.define("FAISS_ENABLE_PYTHON", "OFF")
.define("BUILD_TESTING", "OFF")
.very_verbose(true);
let dst = cfg.build();
let faiss_location = dst.join("lib");
let faiss_c_location = dst.join("build/c_api");
println!(
"cargo:rustc-link-search=native={}",
faiss_location.display()
);
println!(
"cargo:rustc-link-search=native={}",
faiss_c_location.display()
);
println!("cargo:rustc-link-lib=static=faiss_c");
println!("cargo:rustc-link-lib=static=faiss");
link_cxx();
println!("cargo:rustc-link-lib=gomp");
println!("cargo:rustc-link-lib=blas");
println!("cargo:rustc-link-lib=lapack");
}

#[cfg(feature = "static")]
fn link_cxx() {
let cxx = match std::env::var("CXXSTDLIB") {
Ok(s) if s.is_empty() => None,
Ok(s) => Some(s),
Err(_) => {
let target = std::env::var("TARGET").unwrap();
if target.contains("msvc") {
None
} else if target.contains("apple")
| target.contains("freebsd")
| target.contains("openbsd")
{
Some("c++".to_string())
} else {
Some("stdc++".to_string())
}
}
};
if let Some(cxx) = cxx {
println!("cargo:rustc-link-lib={}", cxx);
}
}
1 change: 1 addition & 0 deletions faiss-sys/faiss
Submodule faiss added at 2bfbea
4 changes: 3 additions & 1 deletion faiss-sys/gen_bindings.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ repo_url=https://github.com/facebookresearch/faiss
repo_rev=v1.7.2
cuda_root=/opt/cuda

git clone "$repo_url" faiss --branch "$repo_rev" --depth 1
if [ ! -d faiss ]; then
git clone "$repo_url" faiss --branch "$repo_rev" --depth 1
fi

bindgen_opt='--size_t-is-usize --whitelist-function faiss_.* --whitelist-type idx_t|Faiss.* --opaque-type FILE'

Expand Down
8 changes: 6 additions & 2 deletions src/index/id_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -368,7 +368,7 @@ where

impl IndexImpl {
/// Attempt a dynamic cast of the index to one that is [ID-mapped][1].
///
///
/// [1]: crate::IdMap
pub fn into_id_map(self) -> Result<IdMap<IndexImpl>> {
unsafe {
Expand All @@ -378,7 +378,11 @@ impl IndexImpl {
} else {
mem::forget(self);
let index_inner = faiss_IndexIDMap_sub_index(new_inner);
Ok(IdMap { inner: new_inner, index_inner, phantom: PhantomData })
Ok(IdMap {
inner: new_inner,
index_inner,
phantom: PhantomData,
})
}
}
}
Expand Down

0 comments on commit c2838b8

Please sign in to comment.