Skip to content

Commit 2829b4a

Browse files
make xxhash dependency optional
1 parent ca88692 commit 2829b4a

File tree

5 files changed

+58
-7
lines changed

5 files changed

+58
-7
lines changed

CMakeLists.txt

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,7 @@ else()
242242
option(ENABLE_TESTS "Build tests" OFF)
243243
option(ENABLE_VALGRIND_TESTS "Build tests with valgrind" OFF)
244244
endif()
245+
option(USE_XXHASH "Use xxhash if available" ON)
245246
option(ENABLE_PERF_TESTS "Build performance tests" OFF)
246247
option(ENABLE_COVERAGE "Build code coverage report from tests" OFF)
247248
option(ENABLE_FUZZ_TARGETS "Build target programs suitable for fuzzing with AFL" OFF)
@@ -412,9 +413,15 @@ include_directories(${PCRE2_INCLUDE_DIRS})
412413
target_link_libraries(yang ${PCRE2_LIBRARIES})
413414

414415
# XXHash include and library
415-
find_package(XXHash REQUIRED)
416-
include_directories(${XXHASH_INCLUDE_DIR})
417-
target_link_libraries(yang ${XXHASH_LIBRARY})
416+
find_package(XXHash)
417+
if (XXHASH_FOUND AND USE_XXHASH)
418+
add_definitions(-DUSE_XXHASH)
419+
include_directories(${XXHASH_INCLUDE_DIR})
420+
target_link_libraries(yang ${XXHASH_LIBRARY})
421+
else()
422+
set (USE_XXHASH OFF)
423+
message(STATUS "xxhash not found, USE_XXHASH turned OFF")
424+
endif()
418425

419426
# generated header list
420427
foreach(h IN LISTS gen_headers)

src/hash_table.c

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,15 @@
2020
#include <stdint.h>
2121
#include <stdlib.h>
2222
#include <string.h>
23-
#include <xxhash.h>
2423

2524
#include "compat.h"
2625
#include "dict.h"
2726
#include "log.h"
2827
#include "ly_common.h"
2928

29+
#ifdef USE_XXHASH
30+
#include <xxhash.h>
31+
3032
LIBYANG_API_DEF uint32_t
3133
lyht_hash_multi(uint32_t hash, const char *key_part, size_t len)
3234
{
@@ -43,6 +45,39 @@ lyht_hash(const char *key, size_t len)
4345
return XXH3_64bits(key, len);
4446
}
4547

48+
#else
49+
50+
LIBYANG_API_DEF uint32_t
51+
lyht_hash_multi(uint32_t hash, const char *key_part, size_t len)
52+
{
53+
uint32_t i;
54+
55+
if (key_part && len) {
56+
for (i = 0; i < len; ++i) {
57+
hash += key_part[i];
58+
hash += (hash << 10);
59+
hash ^= (hash >> 6);
60+
}
61+
} else {
62+
hash += (hash << 3);
63+
hash ^= (hash >> 11);
64+
hash += (hash << 15);
65+
}
66+
67+
return hash;
68+
}
69+
70+
LIBYANG_API_DEF uint32_t
71+
lyht_hash(const char *key, size_t len)
72+
{
73+
uint32_t hash;
74+
75+
hash = lyht_hash_multi(0, key, len);
76+
return lyht_hash_multi(hash, NULL, len);
77+
}
78+
79+
#endif
80+
4681
static LY_ERR
4782
lyht_init_hlists_and_records(struct ly_ht *ht)
4883
{

tests/CMakeLists.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,9 @@ function(ly_add_utest)
3030
endif()
3131
endif()
3232

33-
target_link_libraries(${TEST_NAME} ${XXHASH_LIBRARY})
33+
if (USE_XXHASH)
34+
target_link_libraries(${TEST_NAME} ${XXHASH_LIBRARY})
35+
endif()
3436
add_test(NAME ${TEST_NAME} COMMAND ${TEST_NAME})
3537
set_property(TEST ${TEST_NAME} APPEND PROPERTY ENVIRONMENT "MALLOC_CHECK_=3")
3638

tests/perf/CMakeLists.txt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,10 @@ set(format_sources
66
add_executable(ly_perf ${CMAKE_CURRENT_SOURCE_DIR}/perf.c $<TARGET_OBJECTS:yangobj>)
77
set_target_properties(ly_perf PROPERTIES RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/tests")
88
target_link_libraries(ly_perf ${CMAKE_THREAD_LIBS_INIT} ${PCRE2_LIBRARIES} ${CMAKE_DL_LIBS})
9-
target_link_libraries(ly_perf ${XXHASH_LIBRARY})
9+
if (USE_XXHASH)
10+
target_link_libraries(ly_perf ${XXHASH_LIBRARY})
11+
endif()
12+
1013
if (NOT WIN32)
1114
target_link_libraries(ly_perf m)
1215
endif()

tests/style/CMakeLists.txt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,9 @@ endif()
88
# just compile
99
add_executable(cpp_compat cpp_compat.c $<TARGET_OBJECTS:yangobj>)
1010
target_include_directories(cpp_compat BEFORE PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})
11-
target_link_libraries(cpp_compat ${CMAKE_THREAD_LIBS_INIT} ${PCRE2_LIBRARIES} ${CMAKE_DL_LIBS} m ${XXHASH_LIBRARY})
11+
target_link_libraries(cpp_compat ${CMAKE_THREAD_LIBS_INIT} ${PCRE2_LIBRARIES} ${CMAKE_DL_LIBS} m)
12+
if (USE_XXHASH)
13+
target_link_libraries(cpp_compat ${XXHASH_LIBRARY})
14+
endif()
15+
1216
target_compile_options(cpp_compat PUBLIC "-Werror=c++-compat")

0 commit comments

Comments
 (0)