From 96f16ae38b7fe15f74c775005d319bdc8226f0ec Mon Sep 17 00:00:00 2001 From: sky <3374614481@qq.com> Date: Wed, 26 Apr 2023 13:21:37 +0800 Subject: [PATCH 01/10] static link support --- .gitmodules | 3 +++ faiss-sys/Cargo.toml | 3 +++ faiss-sys/build.rs | 53 +++++++++++++++++++++++++++++++++++++++++++- faiss-sys/faiss | 1 + faiss-sys/src/lib.rs | 1 + src/index/id_map.rs | 8 +++++-- 6 files changed, 66 insertions(+), 3 deletions(-) create mode 100644 .gitmodules create mode 160000 faiss-sys/faiss diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 0000000..72ea07b --- /dev/null +++ b/.gitmodules @@ -0,0 +1,3 @@ +[submodule "faiss-sys/faiss"] + path = faiss-sys/faiss + url = https://github.com/Enet4/faiss.git diff --git a/faiss-sys/Cargo.toml b/faiss-sys/Cargo.toml index 80ba0bc..4b3ef46 100644 --- a/faiss-sys/Cargo.toml +++ b/faiss-sys/Cargo.toml @@ -12,6 +12,9 @@ keywords = ["cbir", "clustering", "index", "similarity", "bindings"] [features] gpu = [] +[build-dependencies] +cmake = "0.1.50" + [badges.maintenance] status = "passively-maintained" diff --git a/faiss-sys/build.rs b/faiss-sys/build.rs index 0e1c94c..a8b4d5f 100644 --- a/faiss-sys/build.rs +++ b/faiss-sys/build.rs @@ -1,3 +1,54 @@ fn main() { - println!("cargo:rustc-link-lib=faiss_c"); + build_faiss(); + link_cxx(); + println!("cargo:rustc-link-lib=static=faiss_c"); + println!("cargo:rustc-link-lib=static=faiss"); + println!("cargo:rustc-link-lib=gomp"); + println!("cargo:rustc-link-lib=blas"); + println!("cargo:rustc-link-lib=lapack"); +} + +fn build_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() + ); +} + +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); + } } diff --git a/faiss-sys/faiss b/faiss-sys/faiss new file mode 160000 index 0000000..c08cbff --- /dev/null +++ b/faiss-sys/faiss @@ -0,0 +1 @@ +Subproject commit c08cbff1a4d6c9afb6b8f69004c5530aaf80237a diff --git a/faiss-sys/src/lib.rs b/faiss-sys/src/lib.rs index c0505fd..2ae4bc2 100644 --- a/faiss-sys/src/lib.rs +++ b/faiss-sys/src/lib.rs @@ -1,6 +1,7 @@ #![allow(non_upper_case_globals)] #![allow(non_camel_case_types)] #![allow(non_snake_case)] +// extern crate openmp_sys; #[cfg(feature = "gpu")] mod bindings_gpu; diff --git a/src/index/id_map.rs b/src/index/id_map.rs index a55fe76..5575cf1 100644 --- a/src/index/id_map.rs +++ b/src/index/id_map.rs @@ -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> { unsafe { @@ -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, + }) } } } From d705cf4acfacdb40f3145a0fc46585c08c235471 Mon Sep 17 00:00:00 2001 From: sky <3374614481@qq.com> Date: Wed, 26 Apr 2023 13:25:24 +0800 Subject: [PATCH 02/10] delete useless modification --- faiss-sys/faiss | 1 - faiss-sys/src/lib.rs | 1 - 2 files changed, 2 deletions(-) delete mode 160000 faiss-sys/faiss diff --git a/faiss-sys/faiss b/faiss-sys/faiss deleted file mode 160000 index c08cbff..0000000 --- a/faiss-sys/faiss +++ /dev/null @@ -1 +0,0 @@ -Subproject commit c08cbff1a4d6c9afb6b8f69004c5530aaf80237a diff --git a/faiss-sys/src/lib.rs b/faiss-sys/src/lib.rs index 2ae4bc2..c0505fd 100644 --- a/faiss-sys/src/lib.rs +++ b/faiss-sys/src/lib.rs @@ -1,7 +1,6 @@ #![allow(non_upper_case_globals)] #![allow(non_camel_case_types)] #![allow(non_snake_case)] -// extern crate openmp_sys; #[cfg(feature = "gpu")] mod bindings_gpu; From d506c0c7f2ddbd0b3c956fba47ed6c10c61e0da0 Mon Sep 17 00:00:00 2001 From: sky <3374614481@qq.com> Date: Wed, 26 Apr 2023 13:54:31 +0800 Subject: [PATCH 03/10] static as a feature --- .gitmodules | 2 +- Cargo.toml | 1 + faiss-sys/Cargo.toml | 4 +++- faiss-sys/build.rs | 20 ++++++++++++-------- faiss-sys/faiss | 1 + 5 files changed, 18 insertions(+), 10 deletions(-) create mode 160000 faiss-sys/faiss diff --git a/.gitmodules b/.gitmodules index 72ea07b..e149c63 100644 --- a/.gitmodules +++ b/.gitmodules @@ -1,3 +1,3 @@ [submodule "faiss-sys/faiss"] path = faiss-sys/faiss - url = https://github.com/Enet4/faiss.git + url = git@github.com:Enet4/faiss-rs.git diff --git a/Cargo.toml b/Cargo.toml index 2f038aa..f26f811 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -12,6 +12,7 @@ edition = "2018" [features] gpu = ["faiss-sys/gpu"] +static = ["faiss-sys/static"] [badges.maintenance] status = "passively-maintained" diff --git a/faiss-sys/Cargo.toml b/faiss-sys/Cargo.toml index 4b3ef46..1a84204 100644 --- a/faiss-sys/Cargo.toml +++ b/faiss-sys/Cargo.toml @@ -11,6 +11,8 @@ keywords = ["cbir", "clustering", "index", "similarity", "bindings"] [features] gpu = [] +static = [] + [build-dependencies] cmake = "0.1.50" @@ -19,4 +21,4 @@ cmake = "0.1.50" status = "passively-maintained" [package.metadata.docs.rs] -features = ["gpu"] +features = ["gpu"] \ No newline at end of file diff --git a/faiss-sys/build.rs b/faiss-sys/build.rs index a8b4d5f..8934c79 100644 --- a/faiss-sys/build.rs +++ b/faiss-sys/build.rs @@ -1,14 +1,12 @@ fn main() { - build_faiss(); - link_cxx(); - println!("cargo:rustc-link-lib=static=faiss_c"); - println!("cargo:rustc-link-lib=static=faiss"); - println!("cargo:rustc-link-lib=gomp"); - println!("cargo:rustc-link-lib=blas"); - println!("cargo:rustc-link-lib=lapack"); + #[cfg(feature = "static")] + static_link_faiss(); + #[cfg(not(feature = "static"))] + println!("cargo:rustc-link-lib=faiss_c"); } -fn build_faiss() { +#[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") @@ -28,6 +26,12 @@ fn build_faiss() { "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"); } fn link_cxx() { diff --git a/faiss-sys/faiss b/faiss-sys/faiss new file mode 160000 index 0000000..c08cbff --- /dev/null +++ b/faiss-sys/faiss @@ -0,0 +1 @@ +Subproject commit c08cbff1a4d6c9afb6b8f69004c5530aaf80237a From e721459486b7caee7ce1fe265d8c4e16a06b5a5f Mon Sep 17 00:00:00 2001 From: sky <3374614481@qq.com> Date: Wed, 26 Apr 2023 14:10:29 +0800 Subject: [PATCH 04/10] change submodule to proto to https --- .gitmodules | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.gitmodules b/.gitmodules index e149c63..72ea07b 100644 --- a/.gitmodules +++ b/.gitmodules @@ -1,3 +1,3 @@ [submodule "faiss-sys/faiss"] path = faiss-sys/faiss - url = git@github.com:Enet4/faiss-rs.git + url = https://github.com/Enet4/faiss.git From 4038c6e930701918cf6a32036f5310fc3de63b32 Mon Sep 17 00:00:00 2001 From: sky <3374614481@qq.com> Date: Fri, 28 Apr 2023 16:11:13 +0800 Subject: [PATCH 05/10] fix deps --- .vscode/settings.json | 3 +++ faiss-sys/.vscode/settings.json | 3 +++ faiss-sys/Cargo.toml | 4 ++-- faiss-sys/faiss | 2 +- 4 files changed, 9 insertions(+), 3 deletions(-) create mode 100644 .vscode/settings.json create mode 100644 faiss-sys/.vscode/settings.json diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..8ef7d52 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,3 @@ +{ + "cmake.sourceDirectory": "/home/wu/skyfan/faiss-rs/faiss-sys/faiss/c_api" +} \ No newline at end of file diff --git a/faiss-sys/.vscode/settings.json b/faiss-sys/.vscode/settings.json new file mode 100644 index 0000000..8ef7d52 --- /dev/null +++ b/faiss-sys/.vscode/settings.json @@ -0,0 +1,3 @@ +{ + "cmake.sourceDirectory": "/home/wu/skyfan/faiss-rs/faiss-sys/faiss/c_api" +} \ No newline at end of file diff --git a/faiss-sys/Cargo.toml b/faiss-sys/Cargo.toml index 1a84204..66f3749 100644 --- a/faiss-sys/Cargo.toml +++ b/faiss-sys/Cargo.toml @@ -11,11 +11,11 @@ keywords = ["cbir", "clustering", "index", "similarity", "bindings"] [features] gpu = [] -static = [] +static = ["cmake"] [build-dependencies] -cmake = "0.1.50" +cmake = {version = "0.1.50",optional = true} [badges.maintenance] status = "passively-maintained" diff --git a/faiss-sys/faiss b/faiss-sys/faiss index c08cbff..2bfbead 160000 --- a/faiss-sys/faiss +++ b/faiss-sys/faiss @@ -1 +1 @@ -Subproject commit c08cbff1a4d6c9afb6b8f69004c5530aaf80237a +Subproject commit 2bfbead8f1f29030c11797d161b0b9dec6c2d8a3 From 44d02d6f598e239ce304d807161d60035ca8c103 Mon Sep 17 00:00:00 2001 From: sky <3374614481@qq.com> Date: Fri, 28 Apr 2023 16:11:58 +0800 Subject: [PATCH 06/10] delete .vscode --- .gitignore | 1 + .vscode/settings.json | 3 --- 2 files changed, 1 insertion(+), 3 deletions(-) delete mode 100644 .vscode/settings.json diff --git a/.gitignore b/.gitignore index 6aa1064..0f0b6f1 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ /target/ **/*.rs.bk Cargo.lock +.vscode/ \ No newline at end of file diff --git a/.vscode/settings.json b/.vscode/settings.json deleted file mode 100644 index 8ef7d52..0000000 --- a/.vscode/settings.json +++ /dev/null @@ -1,3 +0,0 @@ -{ - "cmake.sourceDirectory": "/home/wu/skyfan/faiss-rs/faiss-sys/faiss/c_api" -} \ No newline at end of file From f8bf91d6919538311412ced930420e41106a9334 Mon Sep 17 00:00:00 2001 From: sky <3374614481@qq.com> Date: Fri, 28 Apr 2023 16:13:08 +0800 Subject: [PATCH 07/10] delete .vscode --- faiss-sys/.vscode/settings.json | 3 --- 1 file changed, 3 deletions(-) delete mode 100644 faiss-sys/.vscode/settings.json diff --git a/faiss-sys/.vscode/settings.json b/faiss-sys/.vscode/settings.json deleted file mode 100644 index 8ef7d52..0000000 --- a/faiss-sys/.vscode/settings.json +++ /dev/null @@ -1,3 +0,0 @@ -{ - "cmake.sourceDirectory": "/home/wu/skyfan/faiss-rs/faiss-sys/faiss/c_api" -} \ No newline at end of file From 47f31f4192a982db7e1d04fa734a4e35c065a8b8 Mon Sep 17 00:00:00 2001 From: Sky Fan <3374614481@qq.com> Date: Sun, 30 Apr 2023 19:00:55 +0800 Subject: [PATCH 08/10] Update .gitmodules Co-authored-by: Eduardo Pinho --- .gitmodules | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitmodules b/.gitmodules index 72ea07b..3652c81 100644 --- a/.gitmodules +++ b/.gitmodules @@ -1,3 +1,4 @@ [submodule "faiss-sys/faiss"] path = faiss-sys/faiss url = https://github.com/Enet4/faiss.git + branch = c_api_head From 043993090033febaa699992228a228ac5751062e Mon Sep 17 00:00:00 2001 From: Sky Fan <3374614481@qq.com> Date: Sun, 30 Apr 2023 19:01:22 +0800 Subject: [PATCH 09/10] Update faiss-sys/build.rs Co-authored-by: Eduardo Pinho --- faiss-sys/build.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/faiss-sys/build.rs b/faiss-sys/build.rs index 8934c79..9b272c7 100644 --- a/faiss-sys/build.rs +++ b/faiss-sys/build.rs @@ -34,6 +34,7 @@ fn static_link_faiss() { 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, From 4de6e5075388ec1d6f1ab97ff044996aad32eeaf Mon Sep 17 00:00:00 2001 From: Sky <3374614481@qq.com> Date: Sun, 30 Apr 2023 19:11:55 +0800 Subject: [PATCH 10/10] fix gen_binding.sh --- faiss-sys/gen_bindings.sh | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/faiss-sys/gen_bindings.sh b/faiss-sys/gen_bindings.sh index 828abaf..68e635c 100755 --- a/faiss-sys/gen_bindings.sh +++ b/faiss-sys/gen_bindings.sh @@ -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'