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

Improving JavaScript Bindings #282

Merged
merged 9 commits into from
Oct 2, 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
146 changes: 84 additions & 62 deletions .github/workflows/prerelease.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,60 +16,102 @@ permissions:
contents: read

jobs:

test_c:
name: Test C
test_cpp_c_ubuntu_gcc:
name: Test C++ Ubuntu (GCC)
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3
with:
ref: main-dev
- run: git submodule update --init --recursive
- name: Prepare Environment
run: |
sudo apt update &&
sudo apt install -y cmake build-essential libjemalloc-dev

- name: Install Dependencies
run: sudo apt update && sudo apt install -y cmake build-essential libjemalloc-dev libomp-dev g++-12

- name: Build
run: |
cmake -B ./build_release \
-DCMAKE_BUILD_TYPE=Release \
-DUSEARCH_USE_OPENMP=1 \
-DUSEARCH_USE_SIMSIMD=1 \
-DUSEARCH_USE_JEMALLOC=1 \
-DUSEARCH_USE_NATIVE_F16=0 \
-DUSEARCH_BUILD_CLIB=1 \
-DUSEARCH_BUILD_CTEST=1 \
-DUSEARCH_BUILD_BENCHMARK=0 &&
make -C ./build_release -j
- name: Run tests
run: ./build_release/test_c
export CC=gcc-12
export CXX=g++-12
cmake -B build -DCMAKE_BUILD_TYPE=Debug -DUSEARCH_BUILD_TEST_CPP=1 -DUSEARCH_BUILD_TEST_C=1 -DUSEARCH_USE_OPENMP=1 -DUSEARCH_USE_SIMSIMD=1 -DUSEARCH_USE_JEMALLOC=1
cmake --build build --config Debug

- name: Run C++ tests
run: ./build/test_cpp
- name: Run C tests
run: ./build/test_c


test_cpp:
name: Test C++
test_cpp_c_ubuntu_clang:
name: Test C++ Ubuntu (Clang)
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3
with:
ref: main-dev
- run: git submodule update --init --recursive
- name: Prepare Environment

- name: Install Dependencies
run: sudo apt update && sudo apt install -y cmake build-essential libjemalloc-dev clang-15

- name: Build
run: |
sudo apt update &&
sudo apt install -y cmake build-essential libjemalloc-dev
export CC=clang-15
export CXX=clang++-15
cmake -B build -DCMAKE_BUILD_TYPE=Debug -DUSEARCH_BUILD_TEST_CPP=1 -DUSEARCH_BUILD_TEST_C=1 -DUSEARCH_USE_SIMSIMD=1 -DUSEARCH_USE_JEMALLOC=1
cmake --build build --config Debug

- name: Run C++ tests
run: ./build/test_cpp
- name: Run C tests
run: ./build/test_c


test_cpp_c_macos:
name: Test C++ MacOS
runs-on: macos-latest
steps:
- uses: actions/checkout@v3
with:
ref: main-dev
- run: git submodule update --init --recursive

- name: Install Dependencies
run: |
brew update
brew install cmake

- name: Build
run: |
cmake -B ./build_release \
-DCMAKE_BUILD_TYPE=Release \
-DUSEARCH_USE_OPENMP=1 \
-DUSEARCH_USE_SIMSIMD=1 \
-DUSEARCH_USE_JEMALLOC=1 \
-DUSEARCH_USE_NATIVE_F16=0 \
-DUSEARCH_BUILD_TEST=1 \
-DUSEARCH_BUILD_BENCHMARK=0 &&
make -C ./build_release -j
- name: Run tests
run: ./build_release/test_cpp

cmake -B build -DCMAKE_BUILD_TYPE=Debug -DUSEARCH_BUILD_TEST_CPP=1 -DUSEARCH_BUILD_TEST_C=1
cmake --build build --config Debug
- name: Run C++ tests
run: ./build/test_cpp
- name: Run C tests
run: ./build/test_c

test_cpp_c_windows:
name: Test C++ Windows
runs-on: windows-latest
steps:
- uses: actions/checkout@v3
with:
ref: main-dev
- run: git submodule update --init --recursive

- name: Install Dependencies
run: choco install cmake

- name: Build
run: |
cmake -B build -DCMAKE_BUILD_TYPE=Debug -DUSEARCH_BUILD_TEST_CPP=1 -DUSEARCH_BUILD_TEST_C=1
cmake --build build --config Debug
- name: Run C++ tests
run: .\build\test_cpp
- name: Run C tests
run: .\build\test_c

test_python_311:
name: Test Python
Expand All @@ -92,15 +134,7 @@ jobs:
python -m pip install --upgrade pip
pip install pytest numpy
- name: Build locally on Ubuntu
run: |
export CC=/usr/bin/gcc-12
export CXX=/usr/bin/g++-12
export LD_LIBRARY_PATH=/usr/lib/gcc/x86_64-linux-gnu/12/:$LD_LIBRARY_PATH
python -m pip install .
if: ${{ matrix.os == 'ubuntu-22.04' }}
- name: Build locally
run: python -m pip install .
if: ${{ matrix.os != 'ubuntu-22.04' }}
- name: Test with PyTest
run: pytest python/scripts/ -s -x

Expand Down Expand Up @@ -129,13 +163,7 @@ jobs:
pip install pytest numpy

- name: Build locally
run: |
export CC=/usr/bin/gcc-12
export CXX=/usr/bin/g++-12
export LD_LIBRARY_PATH=/usr/lib/gcc/x86_64-linux-gnu/12/:$LD_LIBRARY_PATH
python -m pip install .
if: ${{ matrix.os == 'ubuntu-22.04' }}

run: python -m pip install .
- name: Test with PyTest
run: pytest python/scripts/ -s -x

Expand All @@ -149,10 +177,7 @@ jobs:
- uses: actions/setup-node@v3
with:
node-version: 18
- run: |
export CC=/usr/bin/gcc-12
export CXX=/usr/bin/g++-12
npm install
- run: npm install
- run: npm ci
- run: npm test

Expand Down Expand Up @@ -180,10 +205,7 @@ jobs:
- name: Setup Gradle
uses: gradle/[email protected]
- name: Execute Gradle build
run:
export CC=/usr/bin/gcc-12
export CXX=/usr/bin/g++-12
gradle clean build
run: gradle clean build

test_swift:
name: Test ObjC & Swift
Expand Down Expand Up @@ -254,14 +276,14 @@ jobs:
- name: Prepare environment
run: |
sudo apt update &&
sudo apt install -y cmake build-essential libjemalloc-dev
sudo apt install -y build-essential

- name: Build library
run: |
cd golang/
make USEARCH_USE_OPENMP=0 USEARCH_USE_NATIVE_F16=0 USEARCH_USE_SIMSIMD=1 -C ../c libusearch_c.so
sudo mv ../c/libusearch_c.so /usr/local/lib/libusearch_c.a
sudo mv ../c/usearch.h /usr/local/include/usearch.h
sudo cp ../c/libusearch_c.so /usr/local/lib/libusearch_c.a
sudo cp ../c/usearch.h /usr/local/include/usearch.h

- name: Run test
run: |
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
datasets
bin
*.usearch
.cache

# C++ builds
build
Expand Down
4 changes: 4 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -113,10 +113,13 @@
"BLAS",
"Cdecl",
"cflags",
"cibuildwheel",
"citerator",
"downcasted",
"Downcasting",
"dtype",
"DUSEARCH",
"emcmake",
"equi",
"equidimensional",
"FAISS",
Expand Down Expand Up @@ -153,6 +156,7 @@
"usearch",
"usecases",
"Vardanian",
"vectorize",
"Xunit"
],
"autoDocstring.docstringFormat": "sphinx",
Expand Down
8 changes: 4 additions & 4 deletions .vscode/tasks.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"tasks": [
{
"label": "Linux Build C++ Debug",
"command": "cmake -DCMAKE_CXX_COMPILER=gcc-12 -DCMAKE_CXX_COMPILER=g++-12 -DCMAKE_BUILD_TYPE=Debug -B ./build_debug && make -C ./build_debug",
"command": "cmake -DCMAKE_BUILD_TYPE=Debug -B ./build_debug && make -C ./build_debug",
"args": [],
"type": "shell",
"problemMatcher": [
Expand All @@ -12,7 +12,7 @@
},
{
"label": "Linux Build C++ Release",
"command": "cmake -DCMAKE_CXX_COMPILER=gcc-12 -DCMAKE_CXX_COMPILER=g++-12 -DCMAKE_BUILD_TYPE=RelWithDebInfo -B ./build_release && make -C ./build_release",
"command": "cmake -DCMAKE_BUILD_TYPE=RelWithDebInfo -B ./build_release && make -C ./build_release",
"args": [],
"type": "shell",
"problemMatcher": [
Expand All @@ -21,13 +21,13 @@
},
{
"label": "MacOS Build C++ Debug",
"command": "cmake -DCMAKE_C_COMPILER=/opt/homebrew/opt/llvm/bin/clang -DCMAKE_CXX_COMPILER=/opt/homebrew/opt/llvm/bin/clang++ -DCMAKE_BUILD_TYPE=Debug -B ./build_debug && make -C ./build_debug",
"command": "cmake -DCMAKE_BUILD_TYPE=Debug -B ./build_debug && make -C ./build_debug",
"args": [],
"type": "shell",
},
{
"label": "MacOS Build C++ Release",
"command": "cmake -DCMAKE_C_COMPILER=/opt/homebrew/opt/llvm/bin/clang -DCMAKE_CXX_COMPILER=/opt/homebrew/opt/llvm/bin/clang++ -DCMAKE_BUILD_TYPE=RelWithDebInfo -B ./build_release && make -C ./build_release",
"command": "cmake -DCMAKE_BUILD_TYPE=RelWithDebInfo -B ./build_release && make -C ./build_release",
"args": [],
"type": "shell"
}
Expand Down
25 changes: 17 additions & 8 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ option(USEARCH_USE_SIMSIMD "Use SimSIMD hardware-accelerated metrics" OFF)
option(USEARCH_USE_JEMALLOC "Use JeMalloc for faster memory allocations" OFF)
option(USEARCH_USE_NATIVE_F16 "Use native half-precision types" OFF)

option(USEARCH_BUILD_TEST "Compile a native unit test in C++" ${USEARCH_IS_MAIN_PROJECT})
option(USEARCH_BUILD_BENCHMARK "Compile a native benchmark in C++" ${USEARCH_IS_MAIN_PROJECT})
option(USEARCH_BUILD_CLIB "Compile a native library for the C 99 interface" OFF)
option(USEARCH_BUILD_CTEST "Compile a test for the C 99 interface" OFF)
option(USEARCH_BUILD_TEST_CPP "Compile a native unit test in C++" ${USEARCH_IS_MAIN_PROJECT})
option(USEARCH_BUILD_BENCH_CPP "Compile a native benchmark in C++" ${USEARCH_IS_MAIN_PROJECT})
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)

# Includes
Expand Down Expand Up @@ -198,7 +198,6 @@ function (setup_target TARGET_NAME)
-g>
$<$<CONFIG:DEBUG>:-g
-fsanitize=address
-fsanitize=leak
-fsanitize=alignment
-fsanitize=undefined
>
Expand All @@ -214,13 +213,21 @@ function (setup_target TARGET_NAME)
PRIVATE
$<$<CONFIG:DEBUG>:-g
-fsanitize=address
-fsanitize=leak
-fsanitize=alignment
-fsanitize=undefined
>
-fPIC
)

# Check if the compiler is AppleClang, and if not, add the leak sanitizer
if (CMAKE_HOST_SYSTEM_NAME MATCHES "Darwin")
# It's likely AppleClang Adjust options as needed for AppleClang
else ()
# It's likely LLVM Clang
target_compile_options(${TARGET_NAME} PRIVATE $<$<CONFIG:DEBUG>:-fsanitize=leak>)
target_link_options(${TARGET_NAME} PRIVATE $<$<CONFIG:DEBUG>:-fsanitize=leak>)
endif ()

if (USEARCH_USE_OPENMP)
target_link_libraries(${TARGET_NAME} PRIVATE OpenMP::OpenMP_CXX)
endif ()
Expand Down Expand Up @@ -251,6 +258,8 @@ function (setup_target TARGET_NAME)
endif ()

target_include_directories(${TARGET_NAME} PRIVATE ${USEARCH_HEADER_INCLUDES})
set_target_properties(${TARGET_NAME} PROPERTIES RUNTIME_OUTPUT_DIRECTORY_DEBUG ${CMAKE_BINARY_DIR})
set_target_properties(${TARGET_NAME} PROPERTIES RUNTIME_OUTPUT_DIRECTORY_RELEASE ${CMAKE_BINARY_DIR})
set_target_properties(${TARGET_NAME} PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR})
set_target_properties(${TARGET_NAME} PROPERTIES ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR})
set_target_properties(${TARGET_NAME} PROPERTIES LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR})
Expand All @@ -268,7 +277,7 @@ function (setup_target TARGET_NAME)

endfunction ()

if (${USEARCH_BUILD_TEST} OR ${USEARCH_BUILD_BENCHMARK})
if (${USEARCH_BUILD_TEST_CPP} OR ${USEARCH_BUILD_BENCH_CPP})
add_subdirectory(cpp)
endif ()

Expand All @@ -280,6 +289,6 @@ if (${USEARCH_BUILD_WASM})
add_subdirectory(wasm)
endif ()

if (${USEARCH_BUILD_CLIB} OR ${USEARCH_BUILD_CTEST})
if (${USEARCH_BUILD_LIB_C} OR ${USEARCH_BUILD_TEST_C})
add_subdirectory(c)
endif ()
5 changes: 3 additions & 2 deletions binding.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"simsimd/include",
],
"dependencies": ["<!(node -p \"require('node-addon-api').gyp\")"],
"defines": ["USEARCH_USE_SIMSIMD=1"],
"cflags": [
"-fexceptions",
"-Wno-unknown-pragmas",
Expand All @@ -19,7 +20,7 @@
"-fexceptions",
"-Wno-unknown-pragmas",
"-Wno-maybe-uninitialized",
"-std=c++11",
"-std=c++17",
],
"xcode_settings": {
"GCC_ENABLE_CPP_EXCEPTIONS": "YES",
Expand All @@ -29,7 +30,7 @@
"msvs_settings": {
"VCCLCompilerTool": {
"ExceptionHandling": 1,
"AdditionalOptions": ["-std:c++11"],
"AdditionalOptions": ["-std:c++17"],
}
},
}
Expand Down
4 changes: 2 additions & 2 deletions c/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
if (USEARCH_BUILD_CLIB)
if (USEARCH_BUILD_TEST_C)
add_executable(test_c test.c lib.cpp)
setup_target(test_c)
include(CTest)
Expand All @@ -8,7 +8,7 @@ endif ()

# 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_CTEST)
if (USEARCH_BUILD_LIB_C)
add_library(usearch_c SHARED lib.cpp)
add_library(usearch_static_c STATIC lib.cpp)

Expand Down
Loading