Skip to content

Commit

Permalink
Add: Standalone SQLite binary
Browse files Browse the repository at this point in the history
Closes #358
  • Loading branch information
ashvardanian committed Mar 20, 2024
1 parent fd94aa8 commit 227083b
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 4 deletions.
14 changes: 10 additions & 4 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ project(

# Determine if USearch is built as a subproject (using `add_subdirectory`) or if it is the main project
set(USEARCH_IS_MAIN_PROJECT OFF)

if (CMAKE_CURRENT_SOURCE_DIR STREQUAL CMAKE_SOURCE_DIR)
set(USEARCH_IS_MAIN_PROJECT ON)
endif ()
Expand All @@ -31,6 +32,7 @@ option(USEARCH_BUILD_BENCH_CPP "Compile a native benchmark in C++" ${USEARCH_IS_
option(USEARCH_BUILD_LIB_C "Compile a native library for the C 99 interface" OFF)
option(USEARCH_BUILD_TEST_C "Compile a test for the C 99 interface" OFF)
option(USEARCH_BUILD_WOLFRAM "Compile Wolfram Language bindings" OFF)
option(USEARCH_BUILD_SQLITE "Compile separate SQLite extension" OFF)

# Includes
set(CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/cmake ${CMAKE_MODULE_PATH})
Expand All @@ -56,6 +58,7 @@ set(USEARCH_PKGCONFIG_INSTALL_DIR "${CMAKE_INSTALL_DATADIR}/pkgconfig")
add_library(${USEARCH_TARGET_NAME} INTERFACE)
add_library(${PROJECT_NAME}::${USEARCH_TARGET_NAME} ALIAS ${USEARCH_TARGET_NAME})
set(USEARCH_INCLUDE_BUILD_DIR "${PROJECT_SOURCE_DIR}/include/")

if (${CMAKE_VERSION} VERSION_LESS "3.8.0")
target_compile_features(${USEARCH_TARGET_NAME} INTERFACE cxx_range_for)
else ()
Expand Down Expand Up @@ -122,6 +125,7 @@ if (CMAKE_HOST_SYSTEM_NAME MATCHES "Darwin")
set(CMAKE_USE_PTHREADS_INIT 1)
set(THREADS_PREFER_PTHREAD_FLAG ON)
endif ()

find_package(Threads REQUIRED)

if (USEARCH_USE_OPENMP)
Expand Down Expand Up @@ -291,6 +295,7 @@ function (setup_target TARGET_NAME)
# On Linux and MacOS our primary shared libraries for C are called `libusearch_c.so` and `libusearch_c.dylib`, while
# on Windows the "lib" prefix might be missing by default, so let's ensure it's always there
get_target_property(TARGET_TYPE ${TARGET_NAME} TYPE)

if (TARGET_TYPE STREQUAL "SHARED_LIBRARY" OR TARGET_TYPE STREQUAL "STATIC_LIBRARY")
set_target_properties(${TARGET_NAME} PROPERTIES PREFIX "lib")
endif ()
Expand All @@ -299,12 +304,9 @@ function (setup_target TARGET_NAME)
target_compile_definitions(${TARGET_NAME} PRIVATE $<$<NOT:$<BOOL:${USEARCH_USE_OPENMP}>>:USEARCH_USE_OPENMP=0>)

# Supplementary compilation settings affecting "index_plugins.hpp"
target_compile_definitions(
${TARGET_NAME} PRIVATE $<$<NOT:$<BOOL:${USEARCH_USE_FP16LIB}>>:USEARCH_USE_FP16LIB=1>
)
target_compile_definitions(${TARGET_NAME} PRIVATE $<$<NOT:$<BOOL:${USEARCH_USE_FP16LIB}>>:USEARCH_USE_FP16LIB=1>)

target_compile_definitions(${TARGET_NAME} PRIVATE $<$<NOT:$<BOOL:${USEARCH_USE_SIMSIMD}>>:USEARCH_USE_SIMSIMD=0>)

endfunction ()

if (${USEARCH_BUILD_TEST_CPP} OR ${USEARCH_BUILD_BENCH_CPP})
Expand All @@ -322,3 +324,7 @@ endif ()
if (${USEARCH_BUILD_LIB_C} OR ${USEARCH_BUILD_TEST_C})
add_subdirectory(c)
endif ()

if (${USEARCH_BUILD_SQLITE})
add_subdirectory(sqlite)
endif ()
2 changes: 2 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ The CMakeLists.txt file has a number of options you can pass:
- `USEARCH_BUILD_BENCH_CPP` - build the C++ benchmark suite
- `USEARCH_BUILD_LIB_C` - build the C library
- `USEARCH_BUILD_TEST_C` - build the C test suite
- `USEARCH_BUILD_SQLITE` - build the SQLite extension
- Which dependencies to use:
- `USEARCH_USE_OPENMP` - use OpenMP for parallelism
- `USEARCH_USE_SIMSIMD` - use SimSIMD for vectorization
Expand All @@ -66,6 +67,7 @@ cmake \
-DUSEARCH_BUILD_BENCH_CPP=1 \
-DUSEARCH_BUILD_LIB_C=1 \
-DUSEARCH_BUILD_TEST_C=1 \
-DUSEARCH_BUILD_SQLITE=1 \
-B ./build_release

cmake --build ./build_release --config Release
Expand Down
23 changes: 23 additions & 0 deletions sqlite/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# This article discusses a better way to allow building either static or shared libraries:
# https://alexreinking.com/blog/building-a-dual-shared-and-static-library-with-cmake.html
if (USEARCH_BUILD_SQLITE)
find_package(SQLite3 REQUIRED)

add_library(usearch_sqlite STATIC lib.cpp)

# Set the compile options for the static library
if (CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
target_compile_options(usearch_sqlite PRIVATE -static-libstdc++)
elseif (CMAKE_CXX_COMPILER_ID MATCHES "Clang")
target_compile_options(usearch_sqlite PRIVATE -static)
endif ()

target_link_libraries(usearch_sqlite PRIVATE SQLite::SQLite3)
target_include_directories(usearch_sqlite PRIVATE ${SQLite3_INCLUDE_DIRS})

target_include_directories(usearch_sqlite PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/../stringzilla/include)
target_compile_options(usearch_sqlite PRIVATE -fPIC)
target_compile_options(usearch_sqlite PRIVATE -Wno-vla -Wno-unused-function -Wno-cast-function-type)
setup_target(usearch_sqlite)

endif ()
1 change: 1 addition & 0 deletions sqlite/lib.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,7 @@ extern "C" SZ_DYNAMIC int sqlite3_compiled_init( //

int flags = SQLITE_UTF8 | SQLITE_DETERMINISTIC | SQLITE_INNOCUOUS;
int num_params = -1; // Any number will be accepted
(void)error_message; // Unused

// String similarity metrics
sqlite3_create_function(db, "distance_levenshtein_bytes", num_params, flags, NULL,
Expand Down

0 comments on commit 227083b

Please sign in to comment.