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

CMake support #33

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,8 @@ resolved_deps.py
compile_commands.json
.devcontainer/devcontainer.json
.devcontainer/._devcontainer.json
.codelite/
.cache/
build-*
*.workspace

50 changes: 50 additions & 0 deletions BUILD_cmake.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# Building with `CMake`

If you wish to work with `Bazel` instead of `CMake`, follow this [guide instead][1]

Note: the below was tested on `Ubuntu Linux`, it might not work on other Linux distros


## Install basic tools

```bash
sudo apt update
sudo apt install -y clangd \
build-essential \
g++ \
cmake \
libgtest-dev \
libssl-dev
```


## Build the module

For your convenience, we provide a `build.sh` script. First, clone the code:

```bash
git clone https://github.com/valkey-io/valkey-search.git
cd valkey-search
```

Next, build the module for the `release` configuration by typing this into your terminal:

```bash
./build.sh --run-tests=all
```

Once the build the completed, all build generated output can be found in `.build-release/` folder.

Tip: use `./build.sh --help` to see more build options

## Loading the module

After a successful build, the module is placed under `.build-release/valkeysearch.so`.
to load it into `valkey-server`, use the following command:

```bash
valkey-server --loadmodule .build-release/valkeysearch.so
```


[1]: https://github.com/valkey-io/valkey-search/blob/main/DEVELOPER.md
33 changes: 33 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
cmake_minimum_required(VERSION 3.16)
project(VSS)

# Options
option(BUILD_TESTS "Build valkey-search tests" ON)

set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmake/Modules/")

set(CMAKE_CXX_STANDARD 20 REQUIRED)
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)

include_directories(${CMAKE_SOURCE_DIR})
include_directories(${CMAKE_BINARY_DIR}/.deps/install/include)

add_subdirectory(submodules)
include(valkey_search)

add_subdirectory(vmsdk)
add_subdirectory(third_party)
add_subdirectory(src)

if(BUILD_TESTS)
message(STATUS "Building tests")
add_subdirectory(testing)
endif()

# Create a symbolic link to the root directory for compile_commands.json
execute_process(
COMMAND
${CMAKE_COMMAND} -E create_symlink
${CMAKE_BINARY_DIR}/compile_commands.json
${CMAKE_SOURCE_DIR}/compile_commands.json)
125 changes: 125 additions & 0 deletions build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
#!/bin/bash -e

BUILD_CONFIG=release
RUN_CMAKE="no"
ROOT_DIR=$(readlink -f $(dirname $0))
VERBOSE_ARGS=""
CMAKE_TARGET=""
RUN_TEST=""

echo "Root directory: ${ROOT_DIR}"

function print_usage() {
cat<<EOF
Usage: build.sh [options...]

--help | -h Print this help message and exit
--configure Run cmake stage (aka configure stage)
--verbose | -v Run verbose build
--debug Build for debug version
--clean Clean the current build configuration (debug or release)
--run-tests Run all tests. Optionally, pass a test name to run: "--run-tests=<test-name>"

Example usage:

# Build the release configuration, run cmake if needed
build.sh

# Force run cmake and build the debug configuration
build.sh --configure --debug

EOF
}

function configure() {
mkdir -p ${ROOT_DIR}/.build-${BUILD_CONFIG}
cd $_
local BUILD_TYPE=$(echo ${BUILD_CONFIG^})
cmake .. -DCMAKE_BUILD_TYPE=${BUILD_TYPE} -DBUILD_TESTS=ON -Wno-dev
cd ${ROOT_DIR}
}

function build() {
# If the build folder does not exist, run cmake
if [ ! -d "${ROOT_DIR}/.build-${BUILD_CONFIG}" ]; then
configure
fi
cd ${ROOT_DIR}/.build-${BUILD_CONFIG}
make -j$(nproc) ${VERBOSE_ARGS} ${CMAKE_TARGET}
cd ${ROOT_DIR}
}

## Parse command line arguments
while [ $# -gt 0 ]
do
arg=$1
case $arg in
--clean)
shift || true
CMAKE_TARGET="clean"
echo "Will run 'make clean'"
;;
--debug)
shift || true
BUILD_CONFIG="debug"
echo "Building in Debug mode"
;;
--configure)
shift || true
RUN_CMAKE="yes"
echo "Running cmake: true"
;;
--run-tests)
RUN_TEST="all"
shift || true
echo "Running all tests"
;;
--run-tests=*)
RUN_TEST=${1#*=}
shift || true
echo "Running test ${RUN_TEST}"
;;
--verbose|-v)
shift || true
VERBOSE_ARGS="VERBOSE=1"
echo "Verbose build: true"
;;
--help|-h)
print_usage
exit 0
;;
*)
print_usage
exit 1
;;
esac
done

function PRINT_TEST_NAME() {
echo -e "\e[35;1m-- Running: $1 \e[0m"
}

START_TIME=`date +%s`
if [[ "${RUN_CMAKE}" == "yes" ]]; then
configure
fi
build
END_TIME=`date +%s`
BUILD_RUNTIME=$((END_TIME - START_TIME))

START_TIME=`date +%s`
TESTS_DIR=${ROOT_DIR}/.build-${BUILD_CONFIG}/tests
if [[ "${RUN_TEST}" == "all" ]]; then
TESTS=$(ls ${TESTS_DIR}/*_test)
for test in $TESTS; do
PRINT_TEST_NAME "${test}"
${test} --gtest_color=yes
done
elif [ ! -z "${RUN_TEST}" ]; then
PRINT_TEST_NAME "${TESTS_DIR}/${RUN_TEST}"
${TESTS_DIR}/${RUN_TEST} --gtest_color=yes
fi
END_TIME=`date +%s`
TEST_RUNTIME=$((END_TIME - START_TIME))

echo -e "\e[38:5:243;1m== Build time: ${BUILD_RUNTIME} seconds, Tests time: ${TEST_RUNTIME} seconds ==\e[0m "
Loading
Loading