diff --git a/examples/gguf-hash/gguf-hash.cpp b/examples/gguf-hash/gguf-hash.cpp index 331de301ff..43de6300d9 100644 --- a/examples/gguf-hash/gguf-hash.cpp +++ b/examples/gguf-hash/gguf-hash.cpp @@ -18,13 +18,16 @@ extern "C" { #endif #include "xxhash/xxhash.h" -#include "sha1/sha1.h" #include "sha256/sha256.h" #ifdef __cplusplus } #endif +// sha1 is compiled as C++ and lives in a namespace, see scripts/sync_vendor.py +#include "sha1/sha1.h" +using namespace vendor_hash; + // uuid.uuid5(uuid.NAMESPACE_URL, 'en.wikipedia.org/wiki/Llama.cpp') #define UUID_NAMESPACE_LLAMA_CPP "ef001206-dadc-5f6d-a15f-3359e577d4e5" diff --git a/scripts/sync_vendor.py b/scripts/sync_vendor.py index 3ab62f34c1..18a94e1c69 100755 --- a/scripts/sync_vendor.py +++ b/scripts/sync_vendor.py @@ -56,6 +56,44 @@ patches = { ' && (defined(_MSC_VER) && (_MSC_VER >= 1000) || !defined(_MSC_VER)) /* >= C11 */\n' )], + # sha1 exports a bare "SHA1" symbol, which clashes with the boringssl one at link time. + # we compile it as C++ (see vendor/hash/CMakeLists.txt) and put it in a namespace. + "vendor/hash/sha1/sha1.h": [ + ( + '#if defined(__cplusplus)\n' + 'extern "C" {\n' + '#endif\n', + + 'namespace vendor_hash {\n' + ), + ( + '#if defined(__cplusplus)\n' + '}\n' + '#endif\n', + + '} // namespace vendor_hash\n' + ), + ], + + "vendor/hash/sha1/sha1.c": [ + ( + '#include "sha1.h"\n', + + '#include "sha1.h"\n' + '\n' + 'namespace vendor_hash {\n' + ), + ( + ' SHA1Final((unsigned char *)hash_out, &ctx);\n' + '}\n', + + ' SHA1Final((unsigned char *)hash_out, &ctx);\n' + '}\n' + '\n' + '} // namespace vendor_hash\n' + ), + ], + # silence a maybe-uninitialized warning "vendor/hash/sha256/sha256.c": [( " uint32_t W[16];\n", diff --git a/vendor/hash/CMakeLists.txt b/vendor/hash/CMakeLists.txt index 6788c12839..efdf58e63f 100644 --- a/vendor/hash/CMakeLists.txt +++ b/vendor/hash/CMakeLists.txt @@ -26,5 +26,8 @@ else() endif() set_source_files_properties(${VENDOR_SRCS} PROPERTIES COMPILE_OPTIONS ${NO_WARN_FLAG}) +# sha1 lives in a namespace to avoid a clash with boringssl, see scripts/sync_vendor.py +set_source_files_properties(sha1/sha1.c PROPERTIES LANGUAGE CXX) + # sha256.c includes "rotate-bits/rotate-bits.h", so consumers get this dir too target_include_directories(${TARGET} PUBLIC .) diff --git a/vendor/hash/sha1/sha1.c b/vendor/hash/sha1/sha1.c index 76cd6ca338..4d84340d43 100644 --- a/vendor/hash/sha1/sha1.c +++ b/vendor/hash/sha1/sha1.c @@ -25,6 +25,8 @@ A million repetitions of "a" #include "sha1.h" +namespace vendor_hash { + #define rol(value, bits) (((value) << (bits)) | ((value) >> (32 - (bits)))) @@ -293,3 +295,5 @@ void SHA1( SHA1Final((unsigned char *)hash_out, &ctx); } +} // namespace vendor_hash + diff --git a/vendor/hash/sha1/sha1.h b/vendor/hash/sha1/sha1.h index f492009c97..4ec5df0f4f 100644 --- a/vendor/hash/sha1/sha1.h +++ b/vendor/hash/sha1/sha1.h @@ -9,9 +9,7 @@ #include "stdint.h" -#if defined(__cplusplus) -extern "C" { -#endif +namespace vendor_hash { typedef struct { @@ -45,8 +43,6 @@ void SHA1( const char *str, uint32_t len); -#if defined(__cplusplus) -} -#endif +} // namespace vendor_hash #endif /* SHA1_H */